From 541193c17da5f615597c0116f59b2df1309241c6 Mon Sep 17 00:00:00 2001 From: Shachar Sharon Date: Thu, 29 Feb 2024 10:58:09 +0200 Subject: [PATCH] resources: fix case of nil PodAntiAffinity Protect from case where user provided 'affinity' entry in podSettings of smbcommonconfig but without 'podAntiAffinity' sub-entry. This in turn would cause samba-operator a nil pointer dereference. Fixes: https://github.com/samba-in-kubernetes/samba-operator/issues/330 Signed-off-by: Shachar Sharon --- internal/resources/statefulsets.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/resources/statefulsets.go b/internal/resources/statefulsets.go index 34f0da61..6e7c5800 100644 --- a/internal/resources/statefulsets.go +++ b/internal/resources/statefulsets.go @@ -81,9 +81,10 @@ func buildOneSmbdPerNodeAffinity( affinity := affinityForSmbPod(planner) if affinity == nil { - affinity = &corev1.Affinity{ - PodAntiAffinity: &corev1.PodAntiAffinity{}, - } + affinity = &corev1.Affinity{} + } + if affinity.PodAntiAffinity == nil { + affinity.PodAntiAffinity = &corev1.PodAntiAffinity{} } affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution = append( affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution,