Skip to content

Instantly share code, notes, and snippets.

@gibizer
Created November 2, 2022 16:59
Show Gist options
  • Save gibizer/3c8a7ca7fb8f0e8967c771d51ea2ac08 to your computer and use it in GitHub Desktop.
Save gibizer/3c8a7ca7fb8f0e8967c771d51ea2ac08 to your computer and use it in GitHub Desktop.
// How to default a struct field?
// Example code: https://github.com/gibizer/simple-operator-example/commit/08fb63dda8aae4c20272b8e87d680228948e8ff6
// terminal #1
[fedora@gibi-f35 simple-operator-example]$ make run
test -s /home/fedora/op-build/simple-operator-example/bin/controller-gen || GOBIN=/home/fedora/op-build/simple-operator-example/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.9.2
/home/fedora/op-build/simple-operator-example/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
/home/fedora/op-build/simple-operator-example/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
go fmt ./...
go vet ./...
go run ./main.go
I1102 16:50:47.048115 547877 request.go:601] Waited for 1.039133514s due to client-side throttling, not priority and fairness, request: GET:https://api.crc.testing:6443/apis/monitoring.coreos.com/v1?timeout=32s
1.6674078485037808e+09 INFO controller-runtime.metrics Metrics server is starting to listen {"addr": ":8080"}
1.66740784850418e+09 INFO setup starting manager
1.6674078485048904e+09 INFO Starting server {"kind": "health probe", "addr": "[::]:8081"}
1.6674078485049362e+09 INFO Starting EventSource {"controller": "simple", "controllerGroup": "test.test.org", "controllerKind": "Simple", "source": "kind source: *v1beta1.Simple"}
1.6674078485045125e+09 INFO Starting server {"path": "/metrics", "kind": "metrics", "addr": "[::]:8080"}
1.667407848504993e+09 INFO Starting Controller {"controller": "simple", "controllerGroup": "test.test.org", "controllerKind": "Simple"}
1.6674078486059666e+09 INFO Starting workers {"controller": "simple", "controllerGroup": "test.test.org", "controllerKind": "Simple", "worker count": 1}
1.6674078749255793e+09 INFO Reconciling {"controller": "simple", "controllerGroup": "test.test.org", "controllerKind": "Simple", "simple": {"name":"simple-sample","namespace":"openstack"}, "namespace": "openstack", "name": "simple-sample", "reconcileID": "26c9fffe-35ae-4a45-b0fe-c90579e7f85d", "Spec.Pair": {}}
// terminal2
[fedora@gibi-f35 samples]$ cat test_v1beta1_simple.yaml
apiVersion: test.test.org/v1beta1
kind: Simple
metadata:
name: simple-sample
# spec only has a single field and it is defaulted
[fedora@gibi-f35 samples]$
[fedora@gibi-f35 samples]$ oc kustomize . | oc apply -f -
simple.test.test.org/simple-sample created
[fedora@gibi-f35 samples]$
// --------------------
// I only get the default values of Pair, if it is not left out from the input but has '{}' value
// terminal1
1.6674082712927854e+09 INFO Reconciling {"controller": "simple", "controllerGroup": "test.test.org", "controllerKind": "Simple", "simple": {"name":"simple-sample","namespace":"openstack"}, "namespace": "openstack", "name": "simple-sample", "reconcileID": "952a7fba-6fad-422e-bf56-fef231befa44", "Spec.Pair": {"one":"Example1","two":"Example2"}}
// terminal2
[fedora@gibi-f35 samples]$ oc delete simple.test.test.org/simple-sample
simple.test.test.org "simple-sample" deleted
[fedora@gibi-f35 samples]$
[fedora@gibi-f35 samples]$
[fedora@gibi-f35 samples]$ vim test_v1beta1_simple.yaml
[fedora@gibi-f35 samples]$ cat test_v1beta1_simple.yaml
apiVersion: test.test.org/v1beta1
kind: Simple
metadata:
name: simple-sample
spec:
pair: {}
[fedora@gibi-f35 samples]$ oc kustomize . | oc apply -f -
simple.test.test.org/simple-sample created
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment