Skip to content

Commit d975066

Browse files
authored
fix: nil pointer dereference (#216)
Signed-off-by: Skye Gill <[email protected]> Signed-off-by: Skye Gill <[email protected]>
1 parent cb3b06d commit d975066

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

webhooks/pod_webhook.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ type PodMutator struct {
4545

4646
// Handle injects the flagd sidecar (if the prerequisites are all met)
4747
func (m *PodMutator) Handle(ctx context.Context, req admission.Request) admission.Response {
48+
defer func() {
49+
if err := recover(); err != nil {
50+
admission.Errored(http.StatusInternalServerError, fmt.Errorf("%v", err))
51+
}
52+
}()
53+
4854
pod := &corev1.Pod{}
4955
err := m.decoder.Decode(req, pod)
5056
if err != nil {
@@ -249,12 +255,11 @@ func (m *PodMutator) injectSidecar(pod *corev1.Pod, configMap string, featureFla
249255
FlagDTag = os.Getenv("FLAGD_VERSION")
250256
}
251257

252-
if featureFlag.Spec.FlagDSpec.MetricsPort != 0 {
253-
flagdMetricsPort = featureFlag.Spec.FlagDSpec.MetricsPort
254-
}
255-
256258
var envs []corev1.EnvVar
257259
if featureFlag.Spec.FlagDSpec != nil {
260+
if featureFlag.Spec.FlagDSpec.MetricsPort != 0 {
261+
flagdMetricsPort = featureFlag.Spec.FlagDSpec.MetricsPort
262+
}
258263
envs = featureFlag.Spec.FlagDSpec.Envs
259264
}
260265

webhooks/pod_webhook_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,23 @@ var _ = Describe("pod mutation webhook", func() {
236236
err = k8sClient.Update(testCtx, ffConfig)
237237
Expect(err).ShouldNot(HaveOccurred())
238238
})
239+
240+
It("should not panic if flagDSpec isn't provided", func() {
241+
ffConfigName := "feature-flag-configuration-panic-test"
242+
ffConfig := &corev1alpha1.FeatureFlagConfiguration{}
243+
ffConfig.Namespace = mutatePodNamespace
244+
ffConfig.Name = ffConfigName
245+
ffConfig.Spec.FeatureFlagSpec = featureFlagSpec
246+
err := k8sClient.Create(testCtx, ffConfig)
247+
Expect(err).ShouldNot(HaveOccurred())
248+
249+
pod := testPod()
250+
pod.Annotations["openfeature.dev/featureflagconfiguration"] = ffConfigName
251+
err = k8sClient.Create(testCtx, pod)
252+
Expect(err).ShouldNot(HaveOccurred())
253+
254+
podMutationWebhookCleanup()
255+
err = k8sClient.Delete(testCtx, ffConfig, client.GracePeriodSeconds(0))
256+
Expect(err).ShouldNot(HaveOccurred())
257+
})
239258
})

0 commit comments

Comments
 (0)