Skip to content

Commit

Permalink
Override required dataSource for PersistentVolumeClaimSpec
Browse files Browse the repository at this point in the history
Remove required attribute from openAPI and remove empty dataSource
fields in marshaller

See kubevirt#1882 (comment) for
more details.
  • Loading branch information
gonzolino authored and davidvossel committed Jan 21, 2019
1 parent 5914d69 commit 0d229fc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/util/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ func LoadOpenAPISpec(webServices []*restful.WebService) *spec.Swagger {
prop.Type = spec.StringOrArray{"string", "number"}
s.Properties["port"] = prop
}
if k == "v1.PersistentVolumeClaimSpec" {
for i, r := range s.Required {
if r == "dataSource" {
s.Required = append(s.Required[:i], s.Required[i+1:]...)
openapispec.Definitions[k] = s
break
}
}
}
}

return openapispec
Expand Down
27 changes: 27 additions & 0 deletions tools/util/marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,33 @@ func MarshallObject(obj interface{}, writer io.Writer) error {
unstructured.RemoveNestedField(r.Object, "spec", "template", "metadata", "creationTimestamp")
unstructured.RemoveNestedField(r.Object, "status")

// remove dataSource from PVCs if empty
templates, exists, err := unstructured.NestedSlice(r.Object, "spec", "dataVolumeTemplates")
if exists {
for _, tmpl := range templates {
template := tmpl.(map[string]interface{})
_, exists, err = unstructured.NestedString(template, "spec", "pvc", "dataSource")
if !exists {
unstructured.RemoveNestedField(template, "spec", "pvc", "dataSource")
}
}
unstructured.SetNestedSlice(r.Object, templates, "spec", "dataVolumeTemplates")
}
objects, exists, err := unstructured.NestedSlice(r.Object, "objects")
if exists {
for _, obj := range objects {
object := obj.(map[string]interface{})
kind, exists, _ := unstructured.NestedString(object, "kind")
if exists && kind == "PersistentVolumeClaim" {
_, exists, err = unstructured.NestedString(object, "spec", "dataSource")
if !exists {
unstructured.RemoveNestedField(object, "spec", "dataSource")
}
}
}
unstructured.SetNestedSlice(r.Object, objects, "objects")
}

// remove "managed by operator" label...
labels, exists, err := unstructured.NestedMap(r.Object, "metadata", "labels")
if exists {
Expand Down

0 comments on commit 0d229fc

Please sign in to comment.