From 4405f71dac759e55ceda60df20e7a55002378c1c Mon Sep 17 00:00:00 2001 From: "liheng.zms" Date: Fri, 30 Aug 2024 16:53:33 +0800 Subject: [PATCH] remove normal initcontainer in sidecarset's sidecar list Signed-off-by: liheng.zms --- pkg/control/sidecarcontrol/util.go | 23 +++++++++++++++-------- pkg/webhook/pod/mutating/sidecarset.go | 5 ++++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkg/control/sidecarcontrol/util.go b/pkg/control/sidecarcontrol/util.go index 7b70d03d4b..b590f45848 100644 --- a/pkg/control/sidecarcontrol/util.go +++ b/pkg/control/sidecarcontrol/util.go @@ -242,14 +242,7 @@ func UpdatePodSidecarSetHash(pod *corev1.Pod, sidecarSet *appsv1alpha1.SidecarSe // compatible done } - sidecarList := sets.NewString() - for _, sidecar := range sidecarSet.Spec.Containers { - sidecarList.Insert(sidecar.Name) - } - for _, sidecar := range sidecarSet.Spec.InitContainers { - sidecarList.Insert(sidecar.Name) - } - + sidecarList := listSidecarNameInSidecarSet(sidecarSet) sidecarSetHash[sidecarSet.Name] = SidecarSetUpgradeSpec{ UpdateTimestamp: metav1.Now(), SidecarSetHash: GetSidecarSetRevision(sidecarSet), @@ -575,3 +568,17 @@ func IsSidecarContainer(container corev1.Container) bool { } return false } + +// listSidecarNameInSidecarSet list always init containers and sidecar containers +func listSidecarNameInSidecarSet(sidecarSet *appsv1alpha1.SidecarSet) sets.String { + sidecarList := sets.NewString() + for _, sidecar := range sidecarSet.Spec.InitContainers { + if IsSidecarContainer(sidecar.Container) { + sidecarList.Insert(sidecar.Name) + } + } + for _, sidecar := range sidecarSet.Spec.Containers { + sidecarList.Insert(sidecar.Name) + } + return sidecarList +} diff --git a/pkg/webhook/pod/mutating/sidecarset.go b/pkg/webhook/pod/mutating/sidecarset.go index cfadb2f7de..2ed9a2e3fb 100644 --- a/pkg/webhook/pod/mutating/sidecarset.go +++ b/pkg/webhook/pod/mutating/sidecarset.go @@ -373,7 +373,10 @@ func buildSidecars(isUpdated bool, pod *corev1.Pod, oldPod *corev1.Pod, matchedS if !isUpdated { for i := range sidecarSet.Spec.InitContainers { initContainer := &sidecarSet.Spec.InitContainers[i] - sidecarList.Insert(initContainer.Name) + // only insert k8s native sidecar container for in-place update + if sidecarcontrol.IsSidecarContainer(initContainer.Container) { + sidecarList.Insert(initContainer.Name) + } // volumeMounts that injected into sidecar container // when volumeMounts SubPathExpr contains expansions, then need copy container EnvVars(injectEnvs) injectedMounts, injectedEnvs := sidecarcontrol.GetInjectedVolumeMountsAndEnvs(control, initContainer, pod)