diff --git a/go.mod b/go.mod index 89b5c98..95ba0f6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ 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 @@ -11,6 +10,7 @@ require ( 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 ) @@ -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 diff --git a/go.sum b/go.sum index e707799..a03f13b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/handler/workloadScheduleHandler/handler.go b/handler/workloadScheduleHandler/handler.go index b0e3464..0e072ec 100644 --- a/handler/workloadScheduleHandler/handler.go +++ b/handler/workloadScheduleHandler/handler.go @@ -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" @@ -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} } @@ -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) @@ -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)) } @@ -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)) } diff --git a/handler/workloadScheduleHandler/handler_test.go b/handler/workloadScheduleHandler/handler_test.go index e27afb7..b04b55a 100644 --- a/handler/workloadScheduleHandler/handler_test.go +++ b/handler/workloadScheduleHandler/handler_test.go @@ -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"}}, }, @@ -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"}}, },