Skip to content

Commit

Permalink
clean up (#9)
Browse files Browse the repository at this point in the history
* clean up

* add more logging

* fix test
  • Loading branch information
bennsimon authored Oct 31, 2023
1 parent 90636db commit 0d68a31
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module bennsimon.github.io/workload-scheduler-operator
go 1.20

require (
github.com/alistanis/cartesian v0.0.0-20220409094110-a224e60a7f74
github.com/go-co-op/gocron v1.31.0
github.com/onsi/ginkgo/v2 v2.9.5
github.com/onsi/gomega v1.27.7
github.com/stretchr/testify v1.8.2
k8s.io/api v0.27.2
k8s.io/apimachinery v0.27.2
k8s.io/client-go v0.27.2
k8s.io/utils v0.0.0-20230209194617-a36077c30491
sigs.k8s.io/controller-runtime v0.15.0
)

Expand Down Expand Up @@ -72,7 +72,6 @@ require (
k8s.io/component-base v0.27.2 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alistanis/cartesian v0.0.0-20220409094110-a224e60a7f74 h1:BTwPbXVJ3nBrGnk7rVmG+l8UKnbk/xtyAVjHyuZW1l0=
github.com/alistanis/cartesian v0.0.0-20220409094110-a224e60a7f74/go.mod h1:VWlG+7/XaAhmeBJ/aS8ezA7thtXu9yN78ogu3DomQII=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
24 changes: 19 additions & 5 deletions handler/workloadScheduleHandler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bennsimon.github.io/workload-scheduler-operator/util/config"
"context"
"fmt"
"github.com/alistanis/cartesian"
apps "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/util/validation"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -111,9 +110,10 @@ func (w *WorkloadScheduleHandler) BuildSpecMap(_workloadSchedule workloadschedul
namespaces = []string{util.ALL}
}

if len(kinds) != 0 {
keysArr[1] = '1'
} else {
//set to 1, when kind is not specified it defaults to include both deployment & statefulset which are valid workloads.
keysArr[1] = '1'

if len(kinds) == 0 {
kinds = []string{util.DEPLOYMENT, util.STATEFULSET}
}

Expand All @@ -129,7 +129,15 @@ func (w *WorkloadScheduleHandler) BuildSpecMap(_workloadSchedule workloadschedul

keyStr := string(keysArr)
//
var extractedKeys = cartesian.Product(namespaces, kinds, names)
var extractedKeys [][]string

for _, namespace := range namespaces {
for _, kind := range kinds {
for _, name := range names {
extractedKeys = append(extractedKeys, []string{namespace, kind, name})
}
}
}

for _, _keyComb := range extractedKeys {
keyComb := strings.Join(_keyComb, config.MapKeySeparator)
Expand Down Expand Up @@ -366,6 +374,9 @@ func (w *WorkloadScheduleHandler) executeActionOnStatefulSet(r client.Client, ct
if err != nil {
log.Log.Error(err, fmt.Sprintf("error occurred when fetching %s", util.STATEFULSET))
} else {
if len(statefulSetWorkloads.Items) == 0 && w.Config.LookUpBooleanEnv(config.Debug) {
log.Log.Info(fmt.Sprintf("%s: %s not found. NS: %s, WS: %s", util.STATEFULSET, _workloadSchedule.Name, _workloadSchedule.Namespace, _workloadSchedule.WorkloadScheduler))
}
for _, statefulSet := range statefulSetWorkloads.Items {
w.AdjustReplicas(_workloadSchedule, r, ctx, processedWorkloads, NewStatefulSetHandler(&statefulSet))
}
Expand All @@ -378,6 +389,9 @@ func (w *WorkloadScheduleHandler) executeActionOnDeployment(r client.Client, ctx
if err != nil {
log.Log.Error(err, fmt.Sprintf("error occurred when fetching %s", util.DEPLOYMENT))
} else {
if len(deploymentWorkloads.Items) == 0 && w.Config.LookUpBooleanEnv(config.Debug) {
log.Log.Info(fmt.Sprintf("%s: %s not found. NS: %s, WS: %s", util.DEPLOYMENT, _workloadSchedule.Name, _workloadSchedule.Namespace, _workloadSchedule.WorkloadScheduler))
}
for _, deployment := range deploymentWorkloads.Items {
w.AdjustReplicas(_workloadSchedule, r, ctx, processedWorkloads, NewDeploymentHandler(&deployment))
}
Expand Down
4 changes: 2 additions & 2 deletions handler/workloadScheduleHandler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestWorkloadScheduleHandler_BuildSpecMap(t *testing.T) {
},
schedule: v1.Schedule{ObjectMeta: metav1.ObjectMeta{Name: "test-schedule"}},
}, want: map[string]map[string][]v1.WorkloadScheduleData{
"0010": {
"0110": {
"*/deployment/test-deploy": []v1.WorkloadScheduleData{{Name: "test-deploy", Namespace: "*", Kind: "deployment", Desired: 0, WorkloadScheduler: "test-workload-scheduler"}},
"*/statefulset/test-deploy": []v1.WorkloadScheduleData{{Name: "test-deploy", Namespace: "*", Kind: "statefulset", Desired: 0, WorkloadScheduler: "test-workload-scheduler"}},
},
Expand All @@ -224,7 +224,7 @@ func TestWorkloadScheduleHandler_BuildSpecMap(t *testing.T) {
},
schedule: v1.Schedule{ObjectMeta: metav1.ObjectMeta{Name: "test-schedule"}},
}, want: map[string]map[string][]v1.WorkloadScheduleData{
"0000": {
"0100": {
"*/deployment/*": []v1.WorkloadScheduleData{{Name: "*", Namespace: "*", Kind: "deployment", Desired: 0, WorkloadScheduler: "test-workload-scheduler"}},
"*/statefulset/*": []v1.WorkloadScheduleData{{Name: "*", Namespace: "*", Kind: "statefulset", Desired: 0, WorkloadScheduler: "test-workload-scheduler"}},
},
Expand Down

0 comments on commit 0d68a31

Please sign in to comment.