operator-sdk
operator-sdk copied to clipboard
Go Operator : ObjectMeta struct in CRD spec leads to incorrect spec YAML generation
Bug Report
// WorkloadSpec defines the desired state of Workload
type FooSpec struct {
FooTemplate appsv1.DeploymentSpec `json:"fooTemplate,omitempty"`
}
type Foo struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec FooSpec `json:"spec,omitempty"`
Status FooStatus `json:"status,omitempty"`
}
Here FooTemplate is of type DeploymentSpec which contains a ObjectMeta in its PodTemplateSpec.
In the generated CRD yaml, PodTemplateSpec.ObjectMeta is turned-out as type object
When the CR is applied of type Foo, with foo.Spec.fooTemplate.template.metadata.labels` set as follows
following error is seen
error when creating "foo-cr.yaml": Foo in version "v1alpha1" cannot be handled as a Foo: strict decoding error: unknown field "spec.fooTemplate.template.metadata.labels"
What did you do?
Performed as described above
What did you expect to see?
CR of type Foo with '"spec.fooTemplate.template.metadata.labels"applied to cluster without an issue
What did you see instead? Under which circumstances?
Environment
Operator type:
/language go
Kubernetes cluster type:
minikube
$ operator-sdk version
operator-sdk version: "v1.22.2", commit: "da3346113a8a75e11225f586482934000504a60f", kubernetes version: "1.24.1", go version: "go1.18.4", GOOS: "linux", GOARCH: "amd64"
$ go version (if language is Go)
go version go1.20.6 linux/amd64
$ kubectl version
Client Version: v1.28.1 Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3 Server Version: v1.27.4
Possible Solution
Additional context
Afaik by default controller gen does not generate any embedded metadata of the object. Could you try by passing generateEmbeddedObjectMeta to true in the generator? In your makefile, along with the crd gen command you can pass, something like: controller-gen crd:maxDescLen=0,generateEmbeddedObjectMeta=true ... and check if that works. There is some documentation regarding this in kubebuilder regarding the format of the command: https://book.kubebuilder.io/reference/controller-gen.html?highlight=generateEmbeddedObjectMeta#generators. Hopefully this works!
I tried what @varshaprasad96 suggested and changed the Makefile from:
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
to
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:generateEmbeddedObjectMeta=true webhook paths="./..." output:crd:artifacts:config=config/crd/bases
And you can see the diff in the results of make manifest:
This is what you expected @varshaprasad96? Does this solve your problem @suneelkumarch?
Issues go stale after 90d of inactivity.
Mark the issue as fresh by commenting /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.
Exclude this issue from closing by commenting /lifecycle frozen.
If this issue is safe to close now please do so with /close.
/lifecycle stale
Stale issues rot after 30d of inactivity.
Mark the issue as fresh by commenting /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.
Exclude this issue from closing by commenting /lifecycle frozen.
If this issue is safe to close now please do so with /close.
/lifecycle rotten /remove-lifecycle stale
Rotten issues close after 30d of inactivity.
Reopen the issue by commenting /reopen.
Mark the issue as fresh by commenting /remove-lifecycle rotten.
Exclude this issue from closing again by commenting /lifecycle frozen.
/close
@openshift-bot: Closing this issue.
In response to this:
Rotten issues close after 30d of inactivity.
Reopen the issue by commenting
/reopen. Mark the issue as fresh by commenting/remove-lifecycle rotten. Exclude this issue from closing again by commenting/lifecycle frozen./close
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.