From ea6c092d21cbb04c3305df24cd6fb5d83f2930d5 Mon Sep 17 00:00:00 2001 From: adrianc Date: Wed, 7 Feb 2024 15:32:11 +0200 Subject: [PATCH 1/2] Remove bool pointer from SriovOperatorConfig We dont really need these fields to be pointers, change to concrete type to simplify code. Signed-off-by: adrianc --- controllers/sriovoperatorconfig_controller.go | 8 ++++---- .../sriovoperatorconfig_controller_test.go | 16 ++++++++-------- main.go | 4 ++-- pkg/webhook/validate_test.go | 4 ++-- test/conformance/tests/test_sriov_operator.go | 12 ++++++------ test/validation/tests/test_validation.go | 4 ++-- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/controllers/sriovoperatorconfig_controller.go b/controllers/sriovoperatorconfig_controller.go index 41eae2a3b..e21229e69 100644 --- a/controllers/sriovoperatorconfig_controller.go +++ b/controllers/sriovoperatorconfig_controller.go @@ -89,8 +89,8 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. defaultConfig.SetNamespace(vars.Namespace) defaultConfig.SetName(consts.DefaultConfigName) defaultConfig.Spec = sriovnetworkv1.SriovOperatorConfigSpec{ - EnableInjector: func() *bool { b := vars.EnableAdmissionController; return &b }(), - EnableOperatorWebhook: func() *bool { b := vars.EnableAdmissionController; return &b }(), + EnableInjector: vars.EnableAdmissionController, + EnableOperatorWebhook: vars.EnableAdmissionController, ConfigDaemonNodeSelector: map[string]string{}, LogLevel: 2, DisableDrain: singleNode, @@ -266,7 +266,7 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(ctx context.Context, dc } // Delete injector webhook - if !*dc.Spec.EnableInjector && path == consts.InjectorWebHookPath { + if !dc.Spec.EnableInjector && path == consts.InjectorWebHookPath { for _, obj := range objs { err = r.deleteWebhookObject(ctx, obj) if err != nil { @@ -279,7 +279,7 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(ctx context.Context, dc continue } // Delete operator webhook - if !*dc.Spec.EnableOperatorWebhook && path == consts.OperatorWebHookPath { + if !dc.Spec.EnableOperatorWebhook && path == consts.OperatorWebHookPath { for _, obj := range objs { err = r.deleteWebhookObject(ctx, obj) if err != nil { diff --git a/controllers/sriovoperatorconfig_controller_test.go b/controllers/sriovoperatorconfig_controller_test.go index f6e034970..dd5d72c99 100644 --- a/controllers/sriovoperatorconfig_controller_test.go +++ b/controllers/sriovoperatorconfig_controller_test.go @@ -30,8 +30,8 @@ var _ = Describe("SriovOperatorConfig controller", Ordered, func() { config.SetNamespace(testNamespace) config.SetName(constants.DefaultConfigName) config.Spec = sriovnetworkv1.SriovOperatorConfigSpec{ - EnableInjector: func() *bool { b := true; return &b }(), - EnableOperatorWebhook: func() *bool { b := true; return &b }(), + EnableInjector: true, + EnableOperatorWebhook: true, ConfigDaemonNodeSelector: map[string]string{}, LogLevel: 2, } @@ -100,8 +100,8 @@ var _ = Describe("SriovOperatorConfig controller", Ordered, func() { err := util.WaitForNamespacedObject(config, k8sClient, testNamespace, "default", util.RetryInterval, util.APITimeout) Expect(err).NotTo(HaveOccurred()) config.Spec = sriovnetworkv1.SriovOperatorConfigSpec{ - EnableInjector: func() *bool { b := true; return &b }(), - EnableOperatorWebhook: func() *bool { b := true; return &b }(), + EnableInjector: true, + EnableOperatorWebhook: true, // ConfigDaemonNodeSelector: map[string]string{}, LogLevel: 2, } @@ -137,7 +137,7 @@ var _ = Describe("SriovOperatorConfig controller", Ordered, func() { err := util.WaitForNamespacedObject(config, k8sClient, testNamespace, "default", util.RetryInterval, util.APITimeout) Expect(err).NotTo(HaveOccurred()) - *config.Spec.EnableInjector = false + config.Spec.EnableInjector = false err = k8sClient.Update(ctx, config) Expect(err).NotTo(HaveOccurred()) @@ -153,7 +153,7 @@ var _ = Describe("SriovOperatorConfig controller", Ordered, func() { err = util.WaitForNamespacedObject(config, k8sClient, testNamespace, "default", util.RetryInterval, util.APITimeout) Expect(err).NotTo(HaveOccurred()) - *config.Spec.EnableInjector = true + config.Spec.EnableInjector = true err = k8sClient.Update(ctx, config) Expect(err).NotTo(HaveOccurred()) @@ -173,7 +173,7 @@ var _ = Describe("SriovOperatorConfig controller", Ordered, func() { err := util.WaitForNamespacedObject(config, k8sClient, testNamespace, "default", util.RetryInterval, util.APITimeout) Expect(err).NotTo(HaveOccurred()) - *config.Spec.EnableOperatorWebhook = false + config.Spec.EnableOperatorWebhook = false err = k8sClient.Update(ctx, config) Expect(err).NotTo(HaveOccurred()) @@ -193,7 +193,7 @@ var _ = Describe("SriovOperatorConfig controller", Ordered, func() { err = util.WaitForNamespacedObject(config, k8sClient, testNamespace, "default", util.RetryInterval, util.APITimeout) Expect(err).NotTo(HaveOccurred()) - *config.Spec.EnableOperatorWebhook = true + config.Spec.EnableOperatorWebhook = true err = k8sClient.Update(ctx, config) Expect(err).NotTo(HaveOccurred()) diff --git a/main.go b/main.go index 07f3f70be..da23db301 100644 --- a/main.go +++ b/main.go @@ -271,8 +271,8 @@ func createDefaultOperatorConfig(c client.Client) error { enableAdmissionController := os.Getenv("ADMISSION_CONTROLLERS_ENABLED") == "true" config := &sriovnetworkv1.SriovOperatorConfig{ Spec: sriovnetworkv1.SriovOperatorConfigSpec{ - EnableInjector: func() *bool { b := enableAdmissionController; return &b }(), - EnableOperatorWebhook: func() *bool { b := enableAdmissionController; return &b }(), + EnableInjector: enableAdmissionController, + EnableOperatorWebhook: enableAdmissionController, ConfigDaemonNodeSelector: map[string]string{}, LogLevel: 2, DisableDrain: singleNode, diff --git a/pkg/webhook/validate_test.go b/pkg/webhook/validate_test.go index d67e1a011..f4705d519 100644 --- a/pkg/webhook/validate_test.go +++ b/pkg/webhook/validate_test.go @@ -141,8 +141,8 @@ func newDefaultOperatorConfig() *SriovOperatorConfig { Spec: SriovOperatorConfigSpec{ ConfigDaemonNodeSelector: map[string]string{}, DisableDrain: true, - EnableInjector: func() *bool { b := true; return &b }(), - EnableOperatorWebhook: func() *bool { b := true; return &b }(), + EnableInjector: true, + EnableOperatorWebhook: true, LogLevel: 2, }, } diff --git a/test/conformance/tests/test_sriov_operator.go b/test/conformance/tests/test_sriov_operator.go index 4abc5e0f5..4210f7817 100644 --- a/test/conformance/tests/test_sriov_operator.go +++ b/test/conformance/tests/test_sriov_operator.go @@ -2560,18 +2560,18 @@ func setSriovOperatorSpecFlag(flagName string, flagValue bool) { }, &cfg) Expect(err).ToNot(HaveOccurred()) - if flagName == operatorNetworkInjectorFlag && *cfg.Spec.EnableInjector != flagValue { - cfg.Spec.EnableInjector = &flagValue + if flagName == operatorNetworkInjectorFlag && cfg.Spec.EnableInjector != flagValue { + cfg.Spec.EnableInjector = flagValue err = clients.Update(context.TODO(), &cfg) Expect(err).ToNot(HaveOccurred()) - Expect(*cfg.Spec.EnableInjector).To(Equal(flagValue)) + Expect(cfg.Spec.EnableInjector).To(Equal(flagValue)) } - if flagName == operatorWebhookFlag && *cfg.Spec.EnableOperatorWebhook != flagValue { - cfg.Spec.EnableOperatorWebhook = &flagValue + if flagName == operatorWebhookFlag && cfg.Spec.EnableOperatorWebhook != flagValue { + cfg.Spec.EnableOperatorWebhook = flagValue clients.Update(context.TODO(), &cfg) Expect(err).ToNot(HaveOccurred()) - Expect(*cfg.Spec.EnableOperatorWebhook).To(Equal(flagValue)) + Expect(cfg.Spec.EnableOperatorWebhook).To(Equal(flagValue)) } if flagValue { diff --git a/test/validation/tests/test_validation.go b/test/validation/tests/test_validation.go index 4c7980389..3c2e0379b 100644 --- a/test/validation/tests/test_validation.go +++ b/test/validation/tests/test_validation.go @@ -87,7 +87,7 @@ var _ = Describe("validation", func() { err := clients.Client.Get(context.TODO(), goclient.ObjectKey{Name: "default", Namespace: operatorNamespace}, operatorConfig) Expect(err).ToNot(HaveOccurred()) - if *operatorConfig.Spec.EnableInjector { + if operatorConfig.Spec.EnableInjector { daemonset, err := clients.DaemonSets(operatorNamespace).Get(context.Background(), "network-resources-injector", metav1.GetOptions{}) Expect(err).ToNot(HaveOccurred()) Expect(daemonset.Status.DesiredNumberScheduled).To(Equal(daemonset.Status.NumberReady)) @@ -103,7 +103,7 @@ var _ = Describe("validation", func() { err := clients.Get(context.TODO(), goclient.ObjectKey{Name: "default", Namespace: operatorNamespace}, operatorConfig) Expect(err).ToNot(HaveOccurred()) - if *operatorConfig.Spec.EnableOperatorWebhook { + if operatorConfig.Spec.EnableOperatorWebhook { daemonset, err := clients.DaemonSets(operatorNamespace).Get(context.Background(), "operator-webhook", metav1.GetOptions{}) Expect(err).ToNot(HaveOccurred()) Expect(daemonset.Status.DesiredNumberScheduled).To(Equal(daemonset.Status.NumberReady)) From 9d4492eb3c19ccf179327c0c2e721df15b2761b9 Mon Sep 17 00:00:00 2001 From: adrianc Date: Wed, 7 Feb 2024 15:34:17 +0200 Subject: [PATCH 2/2] Run make generate run `make generate` to update deepcopy functions Signed-off-by: adrianc --- api/v1/sriovoperatorconfig_types.go | 4 ++-- api/v1/zz_generated.deepcopy.go | 10 ---------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/api/v1/sriovoperatorconfig_types.go b/api/v1/sriovoperatorconfig_types.go index 27417ce11..3aab64688 100644 --- a/api/v1/sriovoperatorconfig_types.go +++ b/api/v1/sriovoperatorconfig_types.go @@ -44,9 +44,9 @@ type SriovOperatorConfigSpec struct { // NodeSelector selects the nodes to be configured ConfigDaemonNodeSelector map[string]string `json:"configDaemonNodeSelector,omitempty"` // Flag to control whether the network resource injector webhook shall be deployed - EnableInjector *bool `json:"enableInjector,omitempty"` + EnableInjector bool `json:"enableInjector,omitempty"` // Flag to control whether the operator admission controller webhook shall be deployed - EnableOperatorWebhook *bool `json:"enableOperatorWebhook,omitempty"` + EnableOperatorWebhook bool `json:"enableOperatorWebhook,omitempty"` // Flag to control the log verbose level of the operator. Set to '0' to show only the basic logs. And set to '2' to show all the available logs. // +kubebuilder:validation:Minimum=0 // +kubebuilder:validation:Maximum=2 diff --git a/api/v1/zz_generated.deepcopy.go b/api/v1/zz_generated.deepcopy.go index 38b027179..e5f2afb23 100644 --- a/api/v1/zz_generated.deepcopy.go +++ b/api/v1/zz_generated.deepcopy.go @@ -734,16 +734,6 @@ func (in *SriovOperatorConfigSpec) DeepCopyInto(out *SriovOperatorConfigSpec) { (*out)[key] = val } } - if in.EnableInjector != nil { - in, out := &in.EnableInjector, &out.EnableInjector - *out = new(bool) - **out = **in - } - if in.EnableOperatorWebhook != nil { - in, out := &in.EnableOperatorWebhook, &out.EnableOperatorWebhook - *out = new(bool) - **out = **in - } if in.DisablePlugins != nil { in, out := &in.DisablePlugins, &out.DisablePlugins *out = make(PluginNameSlice, len(*in))