-
Notifications
You must be signed in to change notification settings - Fork 272
NO-JIRA: Guard nil dereference in CachedConfigMapGetter and CachedSecretGetter #2378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -36,9 +36,13 @@ type combinedConfigMapInterface struct { | |||||
| } | ||||||
|
|
||||||
| func (g combinedConfigMapGetter) ConfigMaps(namespace string) corev1client.ConfigMapInterface { | ||||||
| informer := g.listers.InformersFor(namespace) | ||||||
| if informer == nil { | ||||||
| panic(fmt.Sprintf("namespace %q is missing", namespace)) | ||||||
| } | ||||||
| return combinedConfigMapInterface{ | ||||||
| ConfigMapInterface: g.client.ConfigMaps(namespace), | ||||||
| lister: g.listers.InformersFor(namespace).Core().V1().ConfigMaps().Lister().ConfigMaps(namespace), | ||||||
| lister: informer.Core().V1().ConfigMaps().Lister().ConfigMaps(namespace), | ||||||
| namespace: namespace, | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -90,9 +94,13 @@ type combinedSecretInterface struct { | |||||
| } | ||||||
|
|
||||||
| func (g combinedSecretGetter) Secrets(namespace string) corev1client.SecretInterface { | ||||||
| informer := g.listers.InformersFor(namespace) | ||||||
| if informer == nil { | ||||||
| panic(fmt.Sprintf("namespace %q is missing", namespace)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| return combinedSecretInterface{ | ||||||
| SecretInterface: g.client.Secrets(namespace), | ||||||
| lister: g.listers.InformersFor(namespace).Core().V1().Secrets().Lister().Secrets(namespace), | ||||||
| lister: informer.Core().V1().Secrets().Lister().Secrets(namespace), | ||||||
| namespace: namespace, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package v1helpers | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "k8s.io/client-go/informers" | ||
| fakekube "k8s.io/client-go/kubernetes/fake" | ||
| ) | ||
|
|
||
| func TestCachedConfigMapGetterPanicsForMissingNamespace(t *testing.T) { | ||
| kubeClient := fakekube.NewSimpleClientset() | ||
| kubeInformers := NewKubeInformersForNamespaces(kubeClient, "openshift-config") | ||
|
|
||
| getter := CachedConfigMapGetter(kubeClient.CoreV1(), kubeInformers) | ||
|
|
||
| assertPanicsWith(t, fmt.Sprintf("namespace %q is missing", "unregistered"), func() { | ||
| getter.ConfigMaps("unregistered") | ||
| }) | ||
| } | ||
|
|
||
| func TestCachedSecretGetterPanicsForMissingNamespace(t *testing.T) { | ||
| kubeClient := fakekube.NewSimpleClientset() | ||
| kubeInformers := NewKubeInformersForNamespaces(kubeClient, "openshift-config") | ||
|
|
||
| getter := CachedSecretGetter(kubeClient.CoreV1(), kubeInformers) | ||
|
|
||
| assertPanicsWith(t, fmt.Sprintf("namespace %q is missing", "unregistered"), func() { | ||
| getter.Secrets("unregistered") | ||
| }) | ||
| } | ||
|
|
||
| func TestCachedConfigMapGetterSucceedsForRegisteredNamespace(t *testing.T) { | ||
| kubeClient := fakekube.NewSimpleClientset() | ||
| kubeInformers := NewKubeInformersForNamespaces(kubeClient, "openshift-config") | ||
|
|
||
| getter := CachedConfigMapGetter(kubeClient.CoreV1(), kubeInformers) | ||
|
|
||
| // Should not panic for a registered namespace | ||
| _ = getter.ConfigMaps("openshift-config") | ||
| } | ||
|
|
||
| func TestCachedSecretGetterSucceedsForRegisteredNamespace(t *testing.T) { | ||
| kubeClient := fakekube.NewSimpleClientset() | ||
| kubeInformers := NewKubeInformersForNamespaces(kubeClient, "openshift-config") | ||
|
|
||
| getter := CachedSecretGetter(kubeClient.CoreV1(), kubeInformers) | ||
|
|
||
| // Should not panic for a registered namespace | ||
| _ = getter.Secrets("openshift-config") | ||
| } | ||
|
|
||
| func TestCachedGetterWithFakeInformers(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to me this test is basically duplicating the other tests added in this PR, so I would go with one of the approaches, but the other part can be removed. |
||
| kubeClient := fakekube.NewSimpleClientset() | ||
| fakeInformers := NewFakeKubeInformersForNamespaces(map[string]informers.SharedInformerFactory{ | ||
| "test-ns": informers.NewSharedInformerFactory(kubeClient, 0), | ||
| }) | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| namespace string | ||
| panics bool | ||
| }{ | ||
| { | ||
| name: "registered namespace succeeds", | ||
| namespace: "test-ns", | ||
| panics: false, | ||
| }, | ||
| { | ||
| name: "unregistered namespace panics", | ||
| namespace: "missing-ns", | ||
| panics: true, | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| configMapGetter := CachedConfigMapGetter(kubeClient.CoreV1(), fakeInformers) | ||
| secretGetter := CachedSecretGetter(kubeClient.CoreV1(), fakeInformers) | ||
|
|
||
| if tc.panics { | ||
| expectedMsg := fmt.Sprintf("namespace %q is missing", tc.namespace) | ||
| assertPanicsWith(t, expectedMsg, func() { | ||
| configMapGetter.ConfigMaps(tc.namespace) | ||
| }) | ||
| assertPanicsWith(t, expectedMsg, func() { | ||
| secretGetter.Secrets(tc.namespace) | ||
| }) | ||
| } else { | ||
| _ = configMapGetter.ConfigMaps(tc.namespace) | ||
| _ = secretGetter.Secrets(tc.namespace) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func assertPanicsWith(t *testing.T, expectedMessage string, f func()) { | ||
| t.Helper() | ||
| defer func() { | ||
| r := recover() | ||
| if r == nil { | ||
| t.Fatal("expected panic, but none occurred") | ||
| } | ||
| got := fmt.Sprintf("%v", r) | ||
| if got != expectedMessage { | ||
| t.Fatalf("expected panic message %q, got %q", expectedMessage, got) | ||
| } | ||
| }() | ||
| f() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.