From d5a6c49a512b10125e227abd468637bb88d6b5b0 Mon Sep 17 00:00:00 2001 From: Tomas Aschan Date: Fri, 6 May 2022 10:59:51 +0200 Subject: [PATCH 1/2] Avoid nilref when copying leader election options from custom config --- pkg/manager/manager.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index f6c4d6f144..c768db5ac6 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -494,6 +494,11 @@ func (o Options) AndFromOrDie(loader config.ControllerManagerConfiguration) Opti } func (o Options) setLeaderElectionConfig(obj v1alpha1.ControllerManagerConfigurationSpec) Options { + if obj.LeaderElection == nil { + // The source does not have any configuration; noop + return o + } + if !o.LeaderElection && obj.LeaderElection.LeaderElect != nil { o.LeaderElection = *obj.LeaderElection.LeaderElect } From 598027b8ea36e5914aeb0f3923a13817772d3648 Mon Sep 17 00:00:00 2001 From: Tomas Aschan Date: Tue, 10 May 2022 14:59:28 +0200 Subject: [PATCH 2/2] Add regression test --- pkg/manager/manager_options_test.go | 54 +++++++++++++++++++++++++ pkg/manager/testdata/custom-config.yaml | 3 ++ 2 files changed, 57 insertions(+) create mode 100644 pkg/manager/manager_options_test.go create mode 100644 pkg/manager/testdata/custom-config.yaml diff --git a/pkg/manager/manager_options_test.go b/pkg/manager/manager_options_test.go new file mode 100644 index 0000000000..048441e56f --- /dev/null +++ b/pkg/manager/manager_options_test.go @@ -0,0 +1,54 @@ +package manager + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "sigs.k8s.io/controller-runtime/pkg/config" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + configv1alpha1 "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1" +) + +var _ = Describe("manager.Options", func() { + Describe("AndFrom", func() { + Describe("reading custom type using OfKind", func() { + var ( + o Options + c customConfig + err error + ) + + JustBeforeEach(func() { + s := runtime.NewScheme() + o = Options{Scheme: s} + c = customConfig{} + + _, err = o.AndFrom(config.File().AtPath("./testdata/custom-config.yaml").OfKind(&c)) + }) + + It("should not panic or fail", func() { + Expect(err).To(Succeed()) + }) + It("should set custom properties", func() { + Expect(c.CustomValue).To(Equal("foo")) + }) + }) + }) +}) + +type customConfig struct { + metav1.TypeMeta `json:",inline"` + configv1alpha1.ControllerManagerConfigurationSpec `json:",inline"` + CustomValue string `json:"customValue"` +} + +func (in *customConfig) DeepCopyObject() runtime.Object { + out := &customConfig{} + *out = *in + + in.ControllerManagerConfigurationSpec.DeepCopyInto(&out.ControllerManagerConfigurationSpec) + + return out +} diff --git a/pkg/manager/testdata/custom-config.yaml b/pkg/manager/testdata/custom-config.yaml new file mode 100644 index 0000000000..a15c9f8e5c --- /dev/null +++ b/pkg/manager/testdata/custom-config.yaml @@ -0,0 +1,3 @@ +apiVersion: controller-runtime.sigs.k8s.io/v1alpha1 +kind: CustomControllerManagerConfiguration +customValue: foo