Skip to content

Commit

Permalink
Merge pull request #2 from m3dev/feature-add-basic-features
Browse files Browse the repository at this point in the history
Add basic features
  • Loading branch information
kitagry authored Apr 2, 2024
2 parents 8a70e3a + 394ba96 commit 78b1fad
Show file tree
Hide file tree
Showing 15 changed files with 681 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN go mod download
# Copy the go source
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/controller/ internal/controller/
COPY internal/ internal/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand Down
64 changes: 62 additions & 2 deletions api/v1alpha1/broom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,79 @@ limitations under the License.
package v1alpha1

import (
"fmt"
"strconv"

"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

type BroomTarget struct {
Name string `json:"name,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Namespace string `json:"namespace,omitempty"`
}

type BroomAdjustmentType string

const (
AddAdjustment BroomAdjustmentType = "Add"
MulAdjustment BroomAdjustmentType = "Mul"
)

type BroomAdjustment struct {
Type BroomAdjustmentType `json:"type"`
Value string `json:"value"`
}

func (adj BroomAdjustment) IncreaseMemory(m *resource.Quantity) error {
switch adj.Type {
case AddAdjustment:
y, err := resource.ParseQuantity(adj.Value)
if err != nil {
return fmt.Errorf("unable to parse value to resource.Quantity: %w", err)
}
m.Add(y)
case MulAdjustment:
y, err := strconv.Atoi(adj.Value)
if err != nil {
return fmt.Errorf("unable to parse value to int: %w", err)
}
m.Mul(int64(y))
}
return nil
}

type BroomRestartPolicy string

const (
RestartOnOOMPolicy BroomRestartPolicy = "OnOOM"
RestartNeverPolicy BroomRestartPolicy = "Never"
)

type BroomSlackWebhookSecret struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
Key string `json:"key"`
}

type BroomSlackWebhook struct {
Secret BroomSlackWebhookSecret `json:"secret"`
Channel string `json:"channel,omitempty"`
}

// BroomSpec defines the desired state of Broom
type BroomSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Broom. Edit broom_types.go to remove/update
Foo string `json:"foo,omitempty"`
Target BroomTarget `json:"target,omitempty"`
Adjustment BroomAdjustment `json:"adjustment"`
RestartPolicy BroomRestartPolicy `json:"restartPolicy"`
SlackWebhook BroomSlackWebhook `json:"slackWebhook"`
}

// BroomStatus defines the observed state of Broom
Expand Down
73 changes: 72 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ func main() {
os.Exit(1)
}

if err = (&controller.BroomReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
if err = controller.New(mgr).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Broom")
os.Exit(1)
}
Expand Down
49 changes: 46 additions & 3 deletions config/crd/bases/ai.m3.com_brooms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,53 @@ spec:
spec:
description: BroomSpec defines the desired state of Broom
properties:
foo:
description: Foo is an example field of Broom. Edit broom_types.go
to remove/update
adjustment:
properties:
type:
type: string
value:
type: string
required:
- type
- value
type: object
restartPolicy:
type: string
slackWebhook:
properties:
channel:
type: string
secret:
properties:
key:
type: string
name:
type: string
namespace:
type: string
required:
- key
- name
- namespace
type: object
required:
- secret
type: object
target:
properties:
labels:
additionalProperties:
type: string
type: object
name:
type: string
namespace:
type: string
type: object
required:
- adjustment
- restartPolicy
- slackWebhook
type: object
status:
description: BroomStatus defines the observed state of Broom
Expand Down
6 changes: 6 additions & 0 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
resources:
- manager.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: asia-northeast1-docker.pkg.dev/m3ai-ullman-dev/arbok/broom
newTag: latest
52 changes: 52 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ kind: ClusterRole
metadata:
name: manager-role
rules:
- apiGroups:
- ""
resources:
- events
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- pods
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- list
- watch
- apiGroups:
- ai.m3.com
resources:
Expand All @@ -30,3 +58,27 @@ rules:
- get
- patch
- update
- apiGroups:
- batch
resources:
- cronjobs
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- batch
resources:
- jobs
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
16 changes: 15 additions & 1 deletion config/samples/ai_v1alpha1_broom.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,18 @@ metadata:
app.kubernetes.io/created-by: broom
name: broom-sample
spec:
# TODO(user): Add fields here
target:
# name: oom-sample
labels:
m3.com/use-broom: "true"
# namespace: broom
adjustment:
type: Mul
value: "2"
restartPolicy: "OnOOM"
slackWebhook:
secret:
namespace: default
name: broom
key: SLACK_WEBHOOK_URL
# channel: "#alert"
34 changes: 34 additions & 0 deletions config/samples/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: oom-sample
labels:
m3.com/use-broom: "true"
spec:
schedule: "*/2 * * * *"
jobTemplate:
spec:
activeDeadlineSeconds: 60
backoffLimit: 1
template:
spec:
containers:
- name: oom-container
image: ubuntu:latest
command:
- /bin/bash
- -c
args:
- |
echo PID=$$
for i in {0..9}
do
eval a$i'=$(head --bytes 5000000 /dev/zero | cat -v)'
echo $((i++));
done
resources:
limits:
memory: "100Mi"
requests:
memory: "50Mi"
restartPolicy: Never
7 changes: 7 additions & 0 deletions config/samples/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: broom
type: Opaque
data:
SLACK_WEBHOOK_URL: TO_BE_SPECIFIED
Loading

0 comments on commit 78b1fad

Please sign in to comment.