Skip to content

Commit

Permalink
Remove container resource OpenAPI validation from all types
Browse files Browse the repository at this point in the history
  • Loading branch information
jessesuen committed Oct 10, 2019
1 parent 8668f4e commit 9e57065
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 146 deletions.
110 changes: 64 additions & 46 deletions hack/gen-crd-spec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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...)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

}
27 changes: 0 additions & 27 deletions manifests/crds/analysis-run-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 0 additions & 27 deletions manifests/crds/analysis-template-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 0 additions & 27 deletions manifests/crds/experiment-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 0 additions & 19 deletions manifests/crds/rollout-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,6 @@ spec:
type: integer
type: object
resources:
properties: {}
type: object
securityContext:
properties:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 9e57065

Please sign in to comment.