This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
generated from cruise-automation/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from alpe/alex/size_limit_mutation_policy_36
Add EmptyDir size limit policy
- Loading branch information
Showing
12 changed files
with
465 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package policies | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
apiresource "k8s.io/apimachinery/pkg/api/resource" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func TestMutateEmptyDirSizeLimit(t *testing.T) { | ||
specs := map[string]struct { | ||
src string | ||
exp *MutateEmptyDirSizeLimit | ||
expErr bool | ||
}{ | ||
|
||
"all good": { | ||
src: ` | ||
mutate_empty_dir_size_limit: | ||
maximum_size_limit: "1Gi" | ||
default_size_limit: "512Mi" | ||
`, | ||
exp: &MutateEmptyDirSizeLimit{ | ||
MaximumSizeLimit: apiresource.MustParse("1Gi"), | ||
DefaultSizeLimit: apiresource.MustParse("512Mi"), | ||
}, | ||
}, | ||
"default > max": { | ||
src: ` | ||
mutate_empty_dir_size_limit: | ||
maximum_size_limit: "1Gi" | ||
default_size_limit: "2Gi" | ||
`, | ||
expErr: true, | ||
}, | ||
"default not set": { | ||
src: ` | ||
mutate_empty_dir_size_limit: | ||
maximum_size_limit: "1Gi" | ||
`, | ||
expErr: true, | ||
}, | ||
"max not set": { | ||
src: ` | ||
mutate_empty_dir_size_limit: | ||
default_size_limit: "2Gi" | ||
`, | ||
expErr: true, | ||
}, | ||
"unsupported type": { | ||
src: ` | ||
mutate_empty_dir_size_limit: | ||
default_size_limit: "2ALX" | ||
maximum_size_limit: "2ALX" | ||
`, | ||
expErr: true, | ||
}, | ||
} | ||
for msg, spec := range specs { | ||
t.Run(msg, func(t *testing.T) { | ||
var cfg Config | ||
switch err := yaml.Unmarshal([]byte(spec.src), &cfg); { | ||
case spec.expErr && err != nil: | ||
return | ||
case spec.expErr: | ||
t.Fatal("expected error") | ||
case !spec.expErr && err != nil: | ||
t.Fatalf("unexpected error: %+v", err) | ||
} | ||
if exp, got := *spec.exp, cfg.MutateEmptyDirSizeLimit; !reflect.DeepEqual(exp, got) { | ||
t.Errorf("expected %v but got %v", exp, got) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2019 Cruise LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License.package ingress | ||
|
||
package pod | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cruise-automation/k-rail/policies" | ||
"github.com/cruise-automation/k-rail/resource" | ||
admissionv1beta1 "k8s.io/api/admission/v1beta1" | ||
) | ||
|
||
type PolicyEmptyDirSizeLimit struct { | ||
} | ||
|
||
func (p PolicyEmptyDirSizeLimit) Name() string { | ||
return "pod_empty_dir_size_limit" | ||
} | ||
|
||
const violationText = "Empty dir size limit: size limit exceeds the max value" | ||
|
||
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, ctx) | ||
if podResource == nil { | ||
return resourceViolations, nil | ||
} | ||
|
||
cfg := config.MutateEmptyDirSizeLimit | ||
var patches []policies.PatchOperation | ||
|
||
for i, volume := range podResource.PodSpec.Volumes { | ||
if volume.EmptyDir == nil { | ||
continue | ||
} | ||
if volume.EmptyDir.SizeLimit == nil || volume.EmptyDir.SizeLimit.IsZero() { | ||
patches = append(patches, policies.PatchOperation{ | ||
Op: "replace", | ||
Path: fmt.Sprintf(volumePatchPath(podResource.ResourceKind)+"/%d/emptyDir/sizeLimit", i), | ||
Value: cfg.DefaultSizeLimit.String(), | ||
}) | ||
continue | ||
} | ||
|
||
if volume.EmptyDir.SizeLimit.Cmp(cfg.MaximumSizeLimit) > 0 { | ||
resourceViolations = append(resourceViolations, policies.ResourceViolation{ | ||
Namespace: ar.Namespace, | ||
ResourceName: podResource.ResourceName, | ||
ResourceKind: podResource.ResourceKind, | ||
Violation: violationText, | ||
Policy: p.Name(), | ||
}) | ||
} | ||
} | ||
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 | ||
} |
Oops, something went wrong.