Anel Husakovic
added a comment - Testing the client .GET() method: https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/client/example_test.go in case where secret exists in a cluster
$ kubectl get secrets
NAME TYPE DATA AGE
default-token-hqpn5 kubernetes.io/service-account-token 3 14d
mariadb-sample-secret Opaque 1 40m
Testing returned error and the object
root_secret := &corev1.Secret{}
err := r.Get(ctx, req.Namespace, root_secret)
log.Info("Get()", "error: ", err, "root_secret", *root_secret)
2022-06-17T01:59:20.696-0700 INFO controllers.MariaDB1 Get() {"MariaDB: ": "default/mariadb-sample", "error: ": "Secret \"mariadb-sample\" not found", "root_secret": {"metadata":{"creationTimestamp":null}}}
We can see that the object is not nil and there is creationTime Null (what we can use to check), also err is not nil
Update the code with typed object (have to stop controller and run make run again to have visible changes)
err := r.Get(ctx, client.ObjectKey{
Namespace: req.Namespace,
Name: req.Name + "-secret",
}, root_secret)
log.Info("Get()", "error: ", err, "root_secret", *root_secret)
2022-06-17T01:59:56.279-0700 INFO controllers.MariaDB1 Get() {"MariaDB: ": "default/mariadb-sample", "error: ": null, "root_secret": {"kind":"Secret","apiVersion":"v1","metadata":{"name":"mariadb-sample-secret","namespace":"default","uid":"8fda9ce2-d7e2-425d-81e3-33e6a779175a","resourceVersion":"82748","creationTimestamp":"2022-06-17T08:25:44Z","managedFields":[{"manager":"main","operation":"Update","apiVersion":"v1","time":"2022-06-17T08:25:44Z","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:mariadb-root-password":{}},"f:type":{}}}]},"data":{"mariadb-root-password":"bXlzZWNyZXQ="},"type":"Opaque"}}
Note here that err is nil and we got the object
After making changes use make install build run
Testing the client .GET() method: https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/client/example_test.go in case where secret exists in a cluster
$ kubectl get secrets
NAME TYPE DATA AGE
default-token-hqpn5 kubernetes.io/service-account-token 3 14d
mariadb-sample-secret Opaque 1 40m
root_secret := &corev1.Secret{}
err := r.Get(ctx, req.Namespace, root_secret)
log.Info("Get()", "error: ", err, "root_secret", *root_secret)
2022-06-17T01:59:20.696-0700 INFO controllers.MariaDB1 Get() {"MariaDB: ": "default/mariadb-sample", "error: ": "Secret \"mariadb-sample\" not found", "root_secret": {"metadata":{"creationTimestamp":null}}}
We can see that the object is not nil and there is creationTime Null (what we can use to check), also err is not nil
err := r.Get(ctx, client.ObjectKey{
Namespace: req.Namespace,
Name: req.Name + "-secret",
}, root_secret)
log.Info("Get()", "error: ", err, "root_secret", *root_secret)
2022-06-17T01:59:56.279-0700 INFO controllers.MariaDB1 Get() {"MariaDB: ": "default/mariadb-sample", "error: ": null, "root_secret": {"kind":"Secret","apiVersion":"v1","metadata":{"name":"mariadb-sample-secret","namespace":"default","uid":"8fda9ce2-d7e2-425d-81e3-33e6a779175a","resourceVersion":"82748","creationTimestamp":"2022-06-17T08:25:44Z","managedFields":[{"manager":"main","operation":"Update","apiVersion":"v1","time":"2022-06-17T08:25:44Z","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:mariadb-root-password":{}},"f:type":{}}}]},"data":{"mariadb-root-password":"bXlzZWNyZXQ="},"type":"Opaque"}}
Note here that err is nil and we got the object