-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
How to reflect CR definition to its spec in the code #6346
Comments
@cornBuddy I created a very quick test project with the API spec which you have provided and tested it locally: https://github.com/varshaprasad96/wireguard-operator. It works as expected. When I change the
Could you verify if your CR looks like this. You could also check the operator (and even api server) logs in your cluster to check if there is anything suspicious happening. |
Thank for your response @varshaprasad96! ---
apiVersion: vpn.ahova.com/v1alpha1
kind: Wireguard
metadata:
name: wireguard-default
spec:
replicas: 1
---
apiVersion: vpn.ahova.com/v1alpha1
kind: Wireguard
metadata:
name: wireguard-internal-dns
spec:
replicas: 1
externalDns:
enabled: false It seems like I fugured things out by myself :) It looks very similar to the kubernetes-sigs/controller-tools#622 issue. I managed to workaround it by providing defaults to the nested structure at the top level // WireguardSpec defines the desired state of Wireguard
type WireguardSpec struct {
// ...
// Provides configuration of the dns sidecar
// +kubebuilder:default={enabled: true, image: "docker.io/klutchell/unbound:v1.17.1"}
ExternalDNS ExternalDNS `json:"externalDns,omitempty"`
// ...
}
type ExternalDNS struct {
// no defaults here
Enabled bool `json:"enabled,omitempty"`
// no defaults here
Image string `json:"image,omitempty"`
} Yet it still feels like a bug because I expect defaults of nested structure to be populated in the spec after |
Thank you for sharing your workaround. Defaults in boolean types can be elusive since there are only two options ( However, if the issue recurs, I would consider changing the In your specific scenario, since you have a default value, at the end of the day you should still be working with either true or false. |
Seem like setting the default to an empty object manually (like As a workaround set it to the string EDIT: This only works for all optional fields in the child objects. Otherwise you get |
Issues go stale after 90d of inactivity. Mark the issue as fresh by commenting If this issue is safe to close now please do so with /lifecycle stale |
Stale issues rot after 30d of inactivity. Mark the issue as fresh by commenting If this issue is safe to close now please do so with /lifecycle rotten |
Rotten issues close after 30d of inactivity. Reopen the issue by commenting /close |
@openshift-bot: Closing this issue. 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. |
Type of question
How to implement a specific feature
Question
What did you do?
I followed the go operator tutorial for my custom resource, and ended up with this API so far
I added some logic which relies on the spec values in my
Reconcile
function and it seems like the.Spec
field of the Wireguard resource always has default values, egin the
Reconcile
functionWhat did you expect to see?
I expect that
.Spec
of the CR is up to date with resource definitionWhat did you see instead? Under which circumstances?
.Spec
always has default values of the CREnvironment
Operator type:
/language go
Kubernetes cluster type:
minikube
$ operator-sdk version
operator-sdk version: "v1.27.0", commit: "5cbdad9209332043b7c730856b6302edc8996faf", kubernetes version: "v1.25.0", go version: "go1.19.5", GOOS: "darwin", GOARCH: "amd64"
$ go version
(if language is Go)go version go1.19.6 darwin/amd64
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"26", GitVersion:"v1.26.1", GitCommit:"8f94681cd294aa8cfd3407b8191f6c70214973a4", GitTreeState:"clean", BuildDate:"2023-01-18T15:51:24Z", GoVersion:"go1.19.5", Compiler:"gc", Platform:"darwin/amd64"} Kustomize Version: v4.5.7 Server Version: version.Info{Major:"1", Minor:"26", GitVersion:"v1.26.1", GitCommit:"8f94681cd294aa8cfd3407b8191f6c70214973a4", GitTreeState:"clean", BuildDate:"2023-01-18T15:51:25Z", GoVersion:"go1.19.5", Compiler:"gc", Platform:"linux/amd64"}
The text was updated successfully, but these errors were encountered: