Skip to content

Commit 2f77235

Browse files
authored
🐛 Avoid nilref when copying leader election options from custom config (#1889)
* Avoid nilref when copying leader election options from custom config * Add regression test
1 parent 26c95ad commit 2f77235

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

pkg/manager/manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ func (o Options) AndFromOrDie(loader config.ControllerManagerConfiguration) Opti
494494
}
495495

496496
func (o Options) setLeaderElectionConfig(obj v1alpha1.ControllerManagerConfigurationSpec) Options {
497+
if obj.LeaderElection == nil {
498+
// The source does not have any configuration; noop
499+
return o
500+
}
501+
497502
if !o.LeaderElection && obj.LeaderElection.LeaderElect != nil {
498503
o.LeaderElection = *obj.LeaderElection.LeaderElect
499504
}

pkg/manager/manager_options_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package manager
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
7+
"sigs.k8s.io/controller-runtime/pkg/config"
8+
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/runtime"
11+
configv1alpha1 "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"
12+
)
13+
14+
var _ = Describe("manager.Options", func() {
15+
Describe("AndFrom", func() {
16+
Describe("reading custom type using OfKind", func() {
17+
var (
18+
o Options
19+
c customConfig
20+
err error
21+
)
22+
23+
JustBeforeEach(func() {
24+
s := runtime.NewScheme()
25+
o = Options{Scheme: s}
26+
c = customConfig{}
27+
28+
_, err = o.AndFrom(config.File().AtPath("./testdata/custom-config.yaml").OfKind(&c))
29+
})
30+
31+
It("should not panic or fail", func() {
32+
Expect(err).To(Succeed())
33+
})
34+
It("should set custom properties", func() {
35+
Expect(c.CustomValue).To(Equal("foo"))
36+
})
37+
})
38+
})
39+
})
40+
41+
type customConfig struct {
42+
metav1.TypeMeta `json:",inline"`
43+
configv1alpha1.ControllerManagerConfigurationSpec `json:",inline"`
44+
CustomValue string `json:"customValue"`
45+
}
46+
47+
func (in *customConfig) DeepCopyObject() runtime.Object {
48+
out := &customConfig{}
49+
*out = *in
50+
51+
in.ControllerManagerConfigurationSpec.DeepCopyInto(&out.ControllerManagerConfigurationSpec)
52+
53+
return out
54+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
apiVersion: controller-runtime.sigs.k8s.io/v1alpha1
2+
kind: CustomControllerManagerConfiguration
3+
customValue: foo

0 commit comments

Comments
 (0)