Skip to content
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

fix: nil pointer dereference #216

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
})
})