...
1Object References
2=================
3
4The `ObjectReference` type is provided by Kubernetes Core API
5`"k8s.io/api/core/v1"` but the functions to set and find an `ObjectReference`
6are provided in this package. This is useful if you would like
7to include in the Status of your Custom Resource a list of objects
8that are managed by your operator (ie. Deployments, Services, other
9Custom Resources, etc.).
10
11For example, we can add `RelatedObjects` to our Status struct:
12
13```golang
14// ExampleAppStatus defines the observed state of ExampleApp
15type ExampleAppStatus struct {
16 ...
17 // RelatedObjects is a list of objects that are "interesting" or related to this operator.
18 RelatedObjects []corev1.ObjectReference `json:"relatedObjects,omitempty"`
19}
20```
21
22Then, through Reconcile, when an object we manage has been found we can add it to
23the `RelatedObjects` slice.
24
25```golang
26found := &someAPI.SomeObject{}
27err := r.client.Get(context.TODO(), types.NamespacedName{Name: object.Name, Namespace: object.Namespace}, found)
28...handle err
29
30// Add it to the list of RelatedObjects if found
31// import "k8s.io/client-go/tools/reference"
32objectRef, err := reference.GetReference(r.scheme, found)
33if err != nil {
34 return err
35}
36objectreferencesv1.SetObjectReference(&instance.Status.RelatedObjects, *objectRef)
37
38// Update the status
39err = r.client.Status().Update(context.TODO(), instance)
40...handle err
41```
42
43**NOTE**: This package specifies a minimum for what constitutes a valid object
44reference. The minimum valid object reference consists of non-empty strings
45for the object's:
46
47* APIVersion
48* Kind
49* Name
View as plain text