Skip to content

Commit

Permalink
fix: nil pointer dereference (#216)
Browse files Browse the repository at this point in the history
Signed-off-by: Skye Gill <gill.skye95@gmail.com>

Signed-off-by: Skye Gill <gill.skye95@gmail.com>
  • Loading branch information
skyerus authored Nov 10, 2022
1 parent cb3b06d commit d975066
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 9 additions & 4 deletions webhooks/pod_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ type PodMutator struct {

// Handle injects the flagd sidecar (if the prerequisites are all met)
func (m *PodMutator) Handle(ctx context.Context, req admission.Request) admission.Response {
defer func() {
if err := recover(); err != nil {
admission.Errored(http.StatusInternalServerError, fmt.Errorf("%v", err))
}
}()

pod := &corev1.Pod{}
err := m.decoder.Decode(req, pod)
if err != nil {
Expand Down Expand Up @@ -249,12 +255,11 @@ func (m *PodMutator) injectSidecar(pod *corev1.Pod, configMap string, featureFla
FlagDTag = os.Getenv("FLAGD_VERSION")
}

if featureFlag.Spec.FlagDSpec.MetricsPort != 0 {
flagdMetricsPort = featureFlag.Spec.FlagDSpec.MetricsPort
}

var envs []corev1.EnvVar
if featureFlag.Spec.FlagDSpec != nil {
if featureFlag.Spec.FlagDSpec.MetricsPort != 0 {
flagdMetricsPort = featureFlag.Spec.FlagDSpec.MetricsPort
}
envs = featureFlag.Spec.FlagDSpec.Envs
}

Expand Down
19 changes: 19 additions & 0 deletions webhooks/pod_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,23 @@ var _ = Describe("pod mutation webhook", func() {
err = k8sClient.Update(testCtx, ffConfig)
Expect(err).ShouldNot(HaveOccurred())
})

It("should not panic if flagDSpec isn't provided", func() {
ffConfigName := "feature-flag-configuration-panic-test"
ffConfig := &corev1alpha1.FeatureFlagConfiguration{}
ffConfig.Namespace = mutatePodNamespace
ffConfig.Name = ffConfigName
ffConfig.Spec.FeatureFlagSpec = featureFlagSpec
err := k8sClient.Create(testCtx, ffConfig)
Expect(err).ShouldNot(HaveOccurred())

pod := testPod()
pod.Annotations["openfeature.dev/featureflagconfiguration"] = ffConfigName
err = k8sClient.Create(testCtx, pod)
Expect(err).ShouldNot(HaveOccurred())

podMutationWebhookCleanup()
err = k8sClient.Delete(testCtx, ffConfig, client.GracePeriodSeconds(0))
Expect(err).ShouldNot(HaveOccurred())
})
})

0 comments on commit d975066

Please sign in to comment.