Skip to content

Commit

Permalink
fix(manifests): add missing generateName to embedded ObjectMetadata
Browse files Browse the repository at this point in the history
Signed-off-by: Mason Malone <651224+MasonM@users.noreply.github.com>
  • Loading branch information
MasonM committed Jan 5, 2025
1 parent 57ba14a commit 5ef2933
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 11 deletions.
43 changes: 34 additions & 9 deletions hack/manifests/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,53 @@ func cleanCRD(filename string) {
schema := crd.OpenAPIV3Schema()
switch crd.Name() {
case "cronworkflows.argoproj.io":
patchMetadata(&schema, "spec", "properties", "workflowMetadata", "properties")
patchWorkflowSpecTemplateFields(&schema, "spec", "properties", "workflowSpec", "properties")
case "clusterworkflowtemplates.argoproj.io", "workflows.argoproj.io", "workflowtemplates.argoproj.io":
case "clusterworkflowtemplates.argoproj.io", "workflowtemplates.argoproj.io":
patchWorkflowSpecTemplateFields(&schema, "spec", "properties")
case "workflows.argoproj.io":
patchWorkflowSpecTemplateFields(&schema, "spec", "properties")
}
if crd.Name() == "workflows.argoproj.io" {
patchTemplateFields(&schema, "status", "properties", "storedTemplates", "additionalProperties")
patchWorkflowSpecTemplateFields(&schema, "status", "properties", "storedWorkflowTemplateSpec", "properties")
patchTemplateFields(&schema, "status", "properties", "storedTemplates", "additionalProperties", "properties")
patchEphemeralVolumeFields(&schema, "status", "properties", "persistentVolumeClaims", "items", "properties")
case "workfloweventbindings.argoproj.io":
patchMetadata(&schema, "spec", "properties", "submit", "properties", "metadata", "properties")
case "workflowtasksets.argoproj.io":
patchVolumeFields(&schema, "spec", "properties", "tasks", "additionalProperties", "properties")

}
crd.WriteYaml(filename)
}

func patchWorkflowSpecTemplateFields(schema *obj, baseFields ...string) {
patchTemplateFields(schema, append(baseFields, "templateDefaults")...)
patchTemplateFields(schema, append(baseFields, "templates", "items")...)
patchTemplateFields(schema, append(baseFields, "templateDefaults", "properties")...)
patchTemplateFields(schema, append(baseFields, "templates", "items", "properties")...)
patchVolumeFields(schema, baseFields...)
patchMetadata(schema, append(baseFields, "volumeClaimTemplates", "items", "properties", "metadata", "properties")...)
}

// Workaround for missing generateName (see https://github.com/kubernetes-sigs/controller-tools/pull/640)
func patchMetadata(schema *obj, baseFields ...string) {
schema.SetNestedField(map[string]string{
"type": "string",
}, append(baseFields, "generateName")...)
}

func patchEphemeralVolumeFields(schema *obj, baseFields ...string) {
patchMetadata(schema, append(baseFields, "ephemeral", "properties", "volumeClaimTemplate", "properties", "metadata", "properties")...)
}

func patchVolumeFields(schema *obj, baseFields ...string) {
patchEphemeralVolumeFields(schema, append(baseFields, "volumes", "items", "properties")...)
}

func patchTemplateFields(schema *obj, baseFields ...string) {
patchVolumeFields(schema, baseFields...)
// "container" and "script" templates embed the k8s.io/api/core/v1/Container
// struct, which requires the "name" field to be specified, but it's not actually required,
// and there's no easy way to override validating rules for embedded structs in kubebuilder.
schema.RemoveNestedField(append(baseFields, "properties", "container", "required")...)
schema.RemoveNestedField(append(baseFields, "properties", "script", "required")...)
schema.RemoveNestedField(append(baseFields, "container", "required")...)
schema.RemoveNestedField(append(baseFields, "script", "required")...)

// Hack to transform "steps" from an array of objects, e.g.
// steps:
Expand All @@ -48,7 +73,7 @@ func patchTemplateFields(schema *obj, baseFields ...string) {
// The reason it's represented as an array of objects in "workflow_types.go"
// is because that's the only way to get kubebuilder to generate the spec
// without breaking API compatibility.
stepFields := append(baseFields, "properties", "steps", "items")
stepFields := append(baseFields, "steps", "items")
schema.CopyNestedField(append(stepFields, "properties", "steps"), stepFields)
}

Expand Down
8 changes: 6 additions & 2 deletions hack/manifests/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ func (o *obj) RemoveNestedField(fields ...string) {
unstructured.RemoveNestedField(*o, fields...)
}

func (o *obj) SetNestedField(value interface{}, fields ...string) {
parentField := nestedFieldNoCopy[map[string]interface{}](o, fields[:len(fields)-1]...)
parentField[fields[len(fields)-1]] = value
}

func (o *obj) CopyNestedField(sourceFields []string, targetFields []string) {
value := nestedFieldNoCopy[any](o, sourceFields...)
parentField := nestedFieldNoCopy[map[string]interface{}](o, targetFields[:len(targetFields)-1]...)
parentField[targetFields[len(targetFields)-1]] = value
o.SetNestedField(value, targetFields...)
}

func (o *obj) Name() string {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions manifests/base/crds/full/argoproj.io_cronworkflows.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions manifests/base/crds/full/argoproj.io_workflows.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions manifests/base/crds/full/argoproj.io_workflowtasksets.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions manifests/base/crds/full/argoproj.io_workflowtemplates.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions manifests/quick-start-minimal.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions manifests/quick-start-mysql.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions manifests/quick-start-postgres.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5ef2933

Please sign in to comment.