From 33b024e87e0cd62cfcd3046959ae3a8279d9374d Mon Sep 17 00:00:00 2001 From: Jesse Suen Date: Wed, 9 Oct 2019 18:26:47 -0700 Subject: [PATCH] Remove container resource OpenAPI validation from all types --- hack/gen-crd-spec/main.go | 110 +++++++++++++--------- manifests/crds/analysis-run-crd.yaml | 27 ------ manifests/crds/analysis-template-crd.yaml | 27 ------ manifests/crds/experiment-crd.yaml | 27 ------ manifests/crds/rollout-crd.yaml | 19 ---- 5 files changed, 64 insertions(+), 146 deletions(-) diff --git a/hack/gen-crd-spec/main.go b/hack/gen-crd-spec/main.go index 326967533f..7704231442 100644 --- a/hack/gen-crd-spec/main.go +++ b/hack/gen-crd-spec/main.go @@ -14,12 +14,12 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) -const ( - rolloutCrdPath = "manifests/crds/rollout-crd.yaml" - experimentCrdPath = "manifests/crds/experiment-crd.yaml" - analysisRunCrdPath = "manifests/crds/analysis-run-crd.yaml" - analysisTemplateCrdPath = "manifests/crds/analysis-template-crd.yaml" -) +var crdPaths = map[string]string{ + "Rollout": "manifests/crds/rollout-crd.yaml", + "Experiment": "manifests/crds/experiment-crd.yaml", + "AnalysisTemplate": "manifests/crds/analysis-template-crd.yaml", + "AnalysisRun": "manifests/crds/analysis-run-crd.yaml", +} func NewCustomResourceDefinition() []*extensionsobj.CustomResourceDefinition { crdYamlBytes, err := exec.Command( @@ -43,15 +43,9 @@ func NewCustomResourceDefinition() []*extensionsobj.CustomResourceDefinition { for i := range objs { obj := objs[i] - //We need to completely remove validation of problematic fields such as creationTimestamp, - //which get marshalled to `null`, but are typed as as a `string` during Open API validation - removeValidataion(obj, "metadata.creationTimestamp") - removeValidataion(obj, "spec.template.metadata.creationTimestamp") removeNestedItems(obj) removeDescriptions(obj) - if obj.GetKind() == "Rollout" { - removeRolloutResourceValidation(obj) - } + removeResourceValidation(obj) crd := toCRD(obj) crd.Spec.Scope = "Namespaced" crds = append(crds, crd) @@ -60,6 +54,15 @@ func NewCustomResourceDefinition() []*extensionsobj.CustomResourceDefinition { return crds } +func crdKind(crd *unstructured.Unstructured) string { + kind, found, err := unstructured.NestedFieldNoCopy(crd.Object, "spec", "names", "kind") + checkErr(err) + if !found { + panic("kind not found") + } + return kind.(string) +} + var diffSeparator = regexp.MustCompile(`\n---`) // splitYAML splits a YAML file into unstructured objects. Returns list of all unstructured objects @@ -90,10 +93,20 @@ func deleteFile(path string) { checkErr(os.Remove(path)) } -func removeValidataion(un *unstructured.Unstructured, path string) { +func removeValidation(un *unstructured.Unstructured, path string) { schemaPath := []string{"spec", "validation", "openAPIV3Schema"} for _, part := range strings.Split(path, ".") { - schemaPath = append(schemaPath, "properties", part) + if strings.HasSuffix(part, "[]") { + part = strings.TrimSuffix(part, "[]") + schemaPath = append(schemaPath, "properties", part, "items") + } else { + schemaPath = append(schemaPath, "properties", part) + } + } + _, ok, err := unstructured.NestedFieldNoCopy(un.Object, schemaPath...) + checkErr(err) + if !ok { + panic(fmt.Sprintf("%s not found for kind %s", schemaPath, crdKind(un))) } unstructured.RemoveNestedField(un.Object, schemaPath...) } @@ -141,26 +154,40 @@ func removeNestedItemsHelper(obj map[string]interface{}) { } } -var resourcesSchemaPath = []string{ - "spec", - "validation", - "openAPIV3Schema", "properties", - "spec", "properties", - "template", "properties", - "spec", "properties", - "containers", "items", "properties", - "resources", "properties", -} - -func removeRolloutResourceValidation(un *unstructured.Unstructured) { - containersFieldIf, ok, err := unstructured.NestedFieldNoCopy(un.Object, resourcesSchemaPath...) - checkErr(err) - if !ok { - panic(fmt.Sprintf("%s not found", resourcesSchemaPath)) +func removeResourceValidation(un *unstructured.Unstructured) { + kind := crdKind(un) + switch kind { + case "Rollout": + removeValidation(un, "spec.template.spec.containers[].resources.limits") + removeValidation(un, "spec.template.spec.containers[].resources.requests") + removeValidation(un, "spec.template.spec.initContainers[].resources.limits") + removeValidation(un, "spec.template.spec.initContainers[].resources.requests") + removeValidation(un, "spec.template.spec.ephemeralContainers[].resources.limits") + removeValidation(un, "spec.template.spec.ephemeralContainers[].resources.requests") + case "Experiment": + removeValidation(un, "spec.templates[].template.spec.containers[].resources.limits") + removeValidation(un, "spec.templates[].template.spec.containers[].resources.requests") + removeValidation(un, "spec.templates[].template.spec.initContainers[].resources.limits") + removeValidation(un, "spec.templates[].template.spec.initContainers[].resources.requests") + removeValidation(un, "spec.templates[].template.spec.ephemeralContainers[].resources.limits") + removeValidation(un, "spec.templates[].template.spec.ephemeralContainers[].resources.requests") + case "AnalysisTemplate": + removeValidation(un, "spec.metrics[].provider.job.spec.template.spec.containers[].resources.limits") + removeValidation(un, "spec.metrics[].provider.job.spec.template.spec.containers[].resources.requests") + removeValidation(un, "spec.metrics[].provider.job.spec.template.spec.initContainers[].resources.limits") + removeValidation(un, "spec.metrics[].provider.job.spec.template.spec.initContainers[].resources.requests") + removeValidation(un, "spec.metrics[].provider.job.spec.template.spec.ephemeralContainers[].resources.limits") + removeValidation(un, "spec.metrics[].provider.job.spec.template.spec.ephemeralContainers[].resources.requests") + case "AnalysisRun": + removeValidation(un, "spec.analysisSpec.metrics[].provider.job.spec.template.spec.containers[].resources.limits") + removeValidation(un, "spec.analysisSpec.metrics[].provider.job.spec.template.spec.containers[].resources.requests") + removeValidation(un, "spec.analysisSpec.metrics[].provider.job.spec.template.spec.initContainers[].resources.limits") + removeValidation(un, "spec.analysisSpec.metrics[].provider.job.spec.template.spec.initContainers[].resources.requests") + removeValidation(un, "spec.analysisSpec.metrics[].provider.job.spec.template.spec.ephemeralContainers[].resources.limits") + removeValidation(un, "spec.analysisSpec.metrics[].provider.job.spec.template.spec.ephemeralContainers[].resources.requests") + default: + panic(fmt.Sprintf("unknown kind: %s", kind)) } - containers := containersFieldIf.(map[string]interface{}) - unstructured.RemoveNestedField(containers, "limits") - unstructured.RemoveNestedField(containers, "requests") } func toCRD(un *unstructured.Unstructured) *extensionsobj.CustomResourceDefinition { @@ -196,26 +223,17 @@ func main() { // clean up crd yaml before marshalling unstructured.RemoveNestedField(r.Object, "status") unstructured.RemoveNestedField(r.Object, "metadata", "creationTimestamp") - if crdKind == "Rollout" { - removeRolloutResourceValidation(&r) - } jsonBytes, err = json.MarshalIndent(r.Object, "", " ") checkErr(err) yamlBytes, err := yaml.JSONToYAML(jsonBytes) checkErr(err) - path := rolloutCrdPath - switch crdKind { - case "Experiment": - path = experimentCrdPath - case "AnalysisTemplate": - path = analysisTemplateCrdPath - case "AnalysisRun": - path = analysisRunCrdPath + path := crdPaths[crdKind] + if path == "" { + panic(fmt.Sprintf("unknown kind: %s", crdKind)) } err = ioutil.WriteFile(path, yamlBytes, 0644) checkErr(err) } - } diff --git a/manifests/crds/analysis-run-crd.yaml b/manifests/crds/analysis-run-crd.yaml index 46c47cb26a..a4d9575ed8 100644 --- a/manifests/crds/analysis-run-crd.yaml +++ b/manifests/crds/analysis-run-crd.yaml @@ -704,15 +704,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: @@ -1238,15 +1229,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: @@ -1776,15 +1758,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: diff --git a/manifests/crds/analysis-template-crd.yaml b/manifests/crds/analysis-template-crd.yaml index d57cea161e..d2f8da1735 100644 --- a/manifests/crds/analysis-template-crd.yaml +++ b/manifests/crds/analysis-template-crd.yaml @@ -702,15 +702,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: @@ -1236,15 +1227,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: @@ -1774,15 +1756,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: diff --git a/manifests/crds/experiment-crd.yaml b/manifests/crds/experiment-crd.yaml index c3ea3ce3bb..2db170f2c2 100644 --- a/manifests/crds/experiment-crd.yaml +++ b/manifests/crds/experiment-crd.yaml @@ -673,15 +673,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: @@ -1207,15 +1198,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: @@ -1745,15 +1727,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: diff --git a/manifests/crds/rollout-crd.yaml b/manifests/crds/rollout-crd.yaml index 62bd8fd23a..628caed99c 100644 --- a/manifests/crds/rollout-crd.yaml +++ b/manifests/crds/rollout-crd.yaml @@ -829,7 +829,6 @@ spec: type: integer type: object resources: - properties: {} type: object securityContext: properties: @@ -1355,15 +1354,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: @@ -1893,15 +1883,6 @@ spec: type: integer type: object resources: - properties: - limits: - additionalProperties: - type: string - type: object - requests: - additionalProperties: - type: string - type: object type: object securityContext: properties: