From e40af2c52f858ee74bffc8ab5ac3f3c638b849e9 Mon Sep 17 00:00:00 2001 From: stoneshi-yunify Date: Wed, 4 Sep 2024 17:26:43 +0800 Subject: [PATCH] fix bugs Signed-off-by: stoneshi-yunify --- deploy/webhook-deployment.yaml | 2 +- pkg/webhook/pod.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/deploy/webhook-deployment.yaml b/deploy/webhook-deployment.yaml index 9449f58..bd25410 100644 --- a/deploy/webhook-deployment.yaml +++ b/deploy/webhook-deployment.yaml @@ -43,7 +43,7 @@ rules: - apiGroups: [""] resources: ["persistentvolumeclaims"] verbs: ["get", "list", "watch"] - - apiGroups: [""] + - apiGroups: ["storage.k8s.io"] resources: ["storageclasses"] verbs: ["get", "list", "watch"] - apiGroups: ["tenant.kubesphere.io"] diff --git a/pkg/webhook/pod.go b/pkg/webhook/pod.go index d6d51f2..5b1e469 100644 --- a/pkg/webhook/pod.go +++ b/pkg/webhook/pod.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "path" + "slices" "github.com/kubesphere/volume-initializer/pkg/apis/storage/v1alpha1" admissionv1 "k8s.io/api/admission/v1" @@ -111,6 +112,11 @@ func (a *Admitter) Decide(ctx context.Context, reqInfo *ReqInfo) *admissionv1.Ad return toV1AdmissionResponseWithPatch(nil) } + var containerNames []string + for _, c := range reqInfo.Pod.Spec.Containers { + containerNames = append(containerNames, c.Name) + } + initializerList := &v1alpha1.InitializerList{} err = a.client.List(ctx, initializerList) if err != nil { @@ -143,6 +149,13 @@ func (a *Admitter) Decide(ctx context.Context, reqInfo *ReqInfo) *admissionv1.Ad pvcInitContainer.MountPathRoot = "/" } container := pvcInitContainer.Container + container.Name = fmt.Sprintf("%s-vol-%s", container.Name, volume.Name) + + // check if the container already exists + if slices.Contains(containerNames, container.Name) { + continue + } + mountPath := path.Join(pvcInitContainer.MountPathRoot, volume.Name) volumeMount := corev1.VolumeMount{ Name: volume.Name,