Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
Support volume patch path for different resource kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jan 6, 2020
1 parent 9fba29f commit e262eab
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions policies/pod/empty_dir_size_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const violationText = "Empty dir size limit: size limit is required for Pods tha
func (p PolicyEmptyDirSizeLimit) Validate(ctx context.Context, config policies.Config, ar *admissionv1beta1.AdmissionRequest) ([]policies.ResourceViolation, []policies.PatchOperation) {
var resourceViolations []policies.ResourceViolation

podResource := resource.GetPodResource(ar)
podResource := resource.GetPodResource(ar, ctx)
if podResource == nil {
return resourceViolations, nil
}
Expand All @@ -48,7 +48,7 @@ func (p PolicyEmptyDirSizeLimit) Validate(ctx context.Context, config policies.C
if volume.EmptyDir.SizeLimit == nil || volume.EmptyDir.SizeLimit.IsZero() {
patches = append(patches, policies.PatchOperation{
Op: "replace",
Path: fmt.Sprintf("/spec/volumes/%d/emptyDir/sizeLimit", i),
Path: fmt.Sprintf(volumePatchPath(podResource.ResourceKind)+"/%d/emptyDir/sizeLimit", i),
Value: cfg.DefaultSizeLimit.String(),
})
continue
Expand All @@ -66,3 +66,16 @@ func (p PolicyEmptyDirSizeLimit) Validate(ctx context.Context, config policies.C
}
return resourceViolations, patches
}

const templateVolumePath = "/spec/template/spec/volumes"

func volumePatchPath(podKind string) string {
nonTemplateKinds := map[string]string{
"Pod": "/spec/volumes",
"CronJob": "/spec/jobTemplate/spec/template/spec/volumes",
}
if pathPath, ok := nonTemplateKinds[podKind]; ok {
return pathPath
}
return templateVolumePath
}

0 comments on commit e262eab

Please sign in to comment.