Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
Signed-off-by: stoneshi-yunify <[email protected]>
  • Loading branch information
stoneshi-yunify committed Sep 5, 2024
1 parent e40af2c commit bd36aa8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
34 changes: 29 additions & 5 deletions config/samples/storage.kubesphere.io_v1alpha1_initializer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ metadata:
spec:
enabled: true
initContainers:
- name: busybox
- name: busybox-chmod
image: busybox:latest
command:
- sh
Expand All @@ -24,15 +24,30 @@ spec:
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
- name: busybox-chown
image: busybox:latest
command:
- sh
- '-c'
- chown -R 1001:0 $PVC_1_MOUNT_PATH
resources:
limits:
cpu: 500m
memory: 100Mi
requests:
cpu: 100m
memory: 100Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
pvcMatchers:
- name: local
- name: local-1
storageClass:
fieldSelector:
- key: name
operator: In
values:
- local-path
- local-path2
namespace:
labelSelector:
- key: "kubernetes.io/metadata.name"
Expand All @@ -46,8 +61,17 @@ spec:
operator: NotIn
values:
- ws1
- name: local-2
storageClass:
fieldSelector:
- key: name
operator: In
values:
- local-path2
pvcInitializers:
- pvcMatcherName: local
initContainerName: busybox
- pvcMatcherName: local-1
initContainerName: busybox-chmod
- pvcMatcherName: local-2
initContainerName: busybox-chown
mountPathRoot: "/pvc"
status: {}
2 changes: 1 addition & 1 deletion deploy/webhook-deployment-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
8 changes: 8 additions & 0 deletions pkg/webhook/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (a *Admitter) Decide(ctx context.Context, reqInfo *ReqInfo) *admissionv1.Ad
return toV1AdmissionResponse(err)
}
if pvcInitContainer == nil {
klog.Infof("no initContainer found for pvc %s", pvc.Name)
continue
}
if pvcInitContainer.MountPathRoot == "" {
Expand All @@ -153,8 +154,10 @@ func (a *Admitter) Decide(ctx context.Context, reqInfo *ReqInfo) *admissionv1.Ad

// check if the container already exists
if slices.Contains(containerNames, container.Name) {
klog.Warningf("initContainer %s already exists in pod or patch", container.Name)
continue
}
containerNames = append(containerNames, container.Name)

mountPath := path.Join(pvcInitContainer.MountPathRoot, volume.Name)
volumeMount := corev1.VolumeMount{
Expand Down Expand Up @@ -204,6 +207,9 @@ type PVCInitContainer struct {
MountPathRoot string
}

// getPVCInitContainer returns a PVInitContainer that matches the pvc.
// If pvc does not match any pvcMatcher, nil will be returned.
// If pvc matches multiple pvcMatchers, the first one will be used and the corresponding initContainer will be returned.
func (a *Admitter) getPVCInitContainer(ctx context.Context, pvc *corev1.PersistentVolumeClaim, initializerList *v1alpha1.InitializerList) (*PVCInitContainer, error) {
getPvcMatcherByName := func(matcherName string, pvcMatchers []v1alpha1.PVCMatcher) *v1alpha1.PVCMatcher {
for _, m := range pvcMatchers {
Expand All @@ -225,6 +231,7 @@ func (a *Admitter) getPVCInitContainer(ctx context.Context, pvc *corev1.Persiste

for _, initializer := range initializerList.Items {
if !initializer.Spec.Enabled {
klog.Infof("initializer %s not enabled", initializer.Name)
continue
}
for _, pvcInitializer := range initializer.Spec.PVCInitializers {
Expand All @@ -236,6 +243,7 @@ func (a *Admitter) getPVCInitContainer(ctx context.Context, pvc *corev1.Persiste
if match {
container := getContainerByName(pvcInitializer.InitContainerName, initializer.Spec.InitContainers)
if container == nil {
klog.Warningf("initContainer %s not found in initializer %s", pvcInitializer.InitContainerName, initializer.Name)
continue
}
pvcInitContainer := &PVCInitContainer{
Expand Down

0 comments on commit bd36aa8

Please sign in to comment.