-
Notifications
You must be signed in to change notification settings - Fork 401
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
Allow map[string]interface{} CRD fields #636
Comments
The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs. This bot triages issues and PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
/remove-lifecycle stale |
/remove-lifecycle stale |
The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs. This bot triages issues and PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
@armsnyder You can do it like this to define a schemaless deployment spec: type DeploymentTemplate struct {
ObjectMeta ObjectMetaTemplate `json:"metadata,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Schemaless
Spec interface{} `json:"spec"`
} Or if you must define a map with its value schemaless, this will be fine: type DeploymentTemplate struct {
ObjectMeta ObjectMetaTemplate `json:"metadata,omitempty"`
Spec map[string]AnyType `json:"spec"`
}
type AnyType struct {
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Schemaless
Field interface{} `json:",inline"`
} And you can simply use type DeploymentTemplate struct {
ObjectMeta ObjectMetaTemplate `json:"metadata,omitempty"`
Spec map[string]runtime.RawExtension `json:"spec"`
} |
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. This bot triages issues and PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
/remove-lifecycle rotten |
Is there a solution for this? I'm still hitting these issues - cannot use an interface in my CRD. |
I'm curious about the use case here. Kubernetes objects over the wire are encoded as JSON, they are often presented as YAML, reading this, it suggests to me that you want to put some other data encoding, embedded within the spec of your object? Or have I misread that? When your client serializes the data to send to the API server, it will convert everything to JSON, I don't think if you put some random data in, that it would understand you've changed data type and that it shouldn't convert that too (unless it's in a multi-line string?). There have been many folks who've wanted something schemaless, or, to be able to put varying data in and work out the content at runtime that have used |
If it helps, I wrote a short thread about my use case (and the “so what” is at the bottom). https://twitter.com/vsoch/status/1610855797713670145?s=46&t=6DRoASXZ9cLoZLvjoPrJ7A |
Your example of limits as a use case for this, mimics existing Kube APIs. Except the Kube APIs use I think your use case would have been covered by |
Oh this is fantastic to know - I’ll try it out! I think beyond this example, the only other use cases I might imagine would allow a third type. I can’t comment on the original poster’s need, but I think the solution I figured out might be helpful. Thanks for the help! |
It seems to me that this issues can be closed? |
Yes! Sorry forgot to do so. For future readers, it was actually really important to use |
The Kubernetes project currently lacks enough contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
I think it's still relevant. The solution that worked for me was to use the
It would be more convenient to use a map. |
Given Kubernetes guidelines discourage the use of maps, have you considered using a list type with a discriminator key instead of a map here? I suspect this isn't something we really want to support in controller-tools since it goes against the kube conventions in a few ways |
Thanks for sharing this convention. Good reference. My case is similar to the flux helm-controller that has a field to represent the Any suggestion? See the helm-controller example: https://github.com/fluxcd/helm-controller/blob/main/api/v2beta1/helmrelease_types.go#L181 I have used helm-controller as an example because Helm is a common tool in the Kubernetes ecosystem. |
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. This bot triages issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /close not-planned |
@k8s-triage-robot: Closing this issue, marking it as "Not Planned". In response to this:
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. |
Overview
I would love it if controller-gen allowed you to use
map[string]interface{}
types in CRD fields, specifically those marked with:// +kubebuilder:pruning:PreserveUnknownFields
.The use case is when you have a CRD that has a "template" field, similar to Deployment or Job's
.spec.template
field. It is desirable to have unstructured input, so that the controller can use server-side-apply to create the child object without implicitly owning zero-valued fields of the embedded type.I have read past discussion in #294, #301, and #577. I see the recommendation at the time was to use either
runtime.RawExtension
orjson.RawMessage
. Both require manually marshalling and unmarshalling json, and I would rather use my CRD types in a consistent way without making assumptions about how the data is encoded. The former additionally does not make sense for unstructured data that is not a Kubernetes object. The latter is currently bugged (see #637). I believe that themap[string]interface{}
type has a legitimate use case that controller-gen should not discourage.Example
Versions
controller-gen
v0.6.2
k8s.io/apimachinery
v0.22.2
k8s.io/client-go
v0.22.2
sigs.k8s.io/controller-runtime
v0.10.2
The text was updated successfully, but these errors were encountered: