Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround K8s inability to properly validate 'items' subfields #114

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions hack/gen-crd-spec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func NewRolloutCustomResourceDefinition() *extensionsobj.CustomResourceDefinitio
removeValidataion(&un, "metadata.creationTimestamp")
removeValidataion(&un, "spec.template.metadata.creationTimestamp")
removeResourceValidation(&un)
removeNestedItems(&un)
removeDescriptions(&un)

crd := toCRD(&un)
crd.Spec.Scope = "Namespaced"
Expand All @@ -63,6 +65,49 @@ func removeValidataion(un *unstructured.Unstructured, path string) {
unstructured.RemoveNestedField(un.Object, schemaPath...)
}

// removeDescriptions removes all descriptions which bloats the API spec
func removeDescriptions(un *unstructured.Unstructured) {
validation, _, _ := unstructured.NestedMap(un.Object, "spec", "validation", "openAPIV3Schema")
removeDescriptionsHelper(validation)
unstructured.SetNestedMap(un.Object, validation, "spec", "validation", "openAPIV3Schema")
}

func removeDescriptionsHelper(obj map[string]interface{}) {
for k, v := range obj {
if k == "description" {
delete(obj, k)
continue
}
if vObj, ok := v.(map[string]interface{}); ok {
removeDescriptionsHelper(vObj)
}
}
}

// removeNestedItems completely removes validation for a field whenever 'items' is used as a sub field name.
// This is due to Kubernetes' inability to properly validate objects with fields with the name 'items'
// (e.g. spec.template.spec.volumes.configMap)
func removeNestedItems(un *unstructured.Unstructured) {
validation, _, _ := unstructured.NestedMap(un.Object, "spec", "validation", "openAPIV3Schema")
removeNestedItemsHelper(validation)
unstructured.SetNestedMap(un.Object, validation, "spec", "validation", "openAPIV3Schema")
}

func removeNestedItemsHelper(obj map[string]interface{}) {
for k, v := range obj {
vObj, ok := v.(map[string]interface{})
if !ok {
continue
}
_, ok, _ = unstructured.NestedMap(vObj, "properties", "items", "items")
if ok {
delete(obj, k)
} else {
removeNestedItemsHelper(vObj)
}
}
}

var resourcesSchemaPath = []string{
"spec",
"validation",
Expand All @@ -74,6 +119,7 @@ var resourcesSchemaPath = []string{
"resources", "properties",
}

// removeResourceValidation needs to be removed since open api cannot accept both numbers and strings
func removeResourceValidation(un *unstructured.Unstructured) {
containersFieldIf, ok, err := unstructured.NestedFieldNoCopy(un.Object, resourcesSchemaPath...)
checkErr(err)
Expand Down
Loading