Skip to content

Commit 5c0f9c8

Browse files
authored
Introduce OwnReference API (#1025)
* Introduce OwnReference API Signed-off-by: Daniel Fan <[email protected]> * Enable OwnerReference API for resources created by OperandConfig Signed-off-by: Daniel Fan <[email protected]> --------- Signed-off-by: Daniel Fan <[email protected]>
1 parent 6b51bab commit 5c0f9c8

12 files changed

+264
-22
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ VERSION ?= $(shell git describe --exact-match 2> /dev/null || \
3535
RELEASE_VERSION ?= $(shell cat ./version/version.go | grep "Version =" | awk '{ print $$3}' | tr -d '"')
3636
LATEST_VERSION ?= latest
3737
OPERATOR_SDK_VERSION=v1.32.0
38-
YQ_VERSION=v4.17.2
38+
YQ_VERSION=v4.42.1
3939
DEFAULT_CHANNEL ?= v$(shell cat ./version/version.go | grep "Version =" | awk '{ print $$3}' | tr -d '"' | cut -d '.' -f1,2)
4040
CHANNELS ?= $(DEFAULT_CHANNEL)
4141

@@ -208,14 +208,14 @@ bundle-manifests:
208208
$(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle \
209209
-q --overwrite --version $(OPERATOR_VERSION) $(BUNDLE_METADATA_OPTS)
210210
$(OPERATOR_SDK) bundle validate ./bundle
211-
$(YQ) eval-all -i '.spec.relatedImages = load("config/manifests/bases/operand-deployment-lifecycle-manager.clusterserviceversion.yaml").spec.relatedImages' bundle/manifests/operand-deployment-lifecycle-manager.clusterserviceversion.yaml
211+
$(YQ) eval-all -i '.spec.relatedImages |= load("config/manifests/bases/operand-deployment-lifecycle-manager.clusterserviceversion.yaml").spec.relatedImages' bundle/manifests/operand-deployment-lifecycle-manager.clusterserviceversion.yaml
212212
@# Need to replace fields this way to avoid changing PROJECT name and CSV file name, which may or may not impact CICD automation
213213
$(YQ) e -i '.annotations["operators.operatorframework.io.bundle.package.v1"] = "ibm-odlm"' bundle/metadata/annotations.yaml
214214
sed -i'' s/operand-deployment-lifecycle-manager/ibm-odlm/ bundle.Dockerfile
215215

216-
generate-all: manifests kustomize operator-sdk ## Generate bundle manifests, metadata and package manifests
216+
generate-all: yq manifests kustomize operator-sdk ## Generate bundle manifests, metadata and package manifests
217217
$(OPERATOR_SDK) generate kustomize manifests -q
218-
- make bundle-manifests CHANNELS=v4.2 DEFAULT_CHANNEL=v4.2
218+
- make bundle-manifests CHANNELS=v4.3 DEFAULT_CHANNEL=v4.3
219219

220220
##@ Test
221221

api/v1alpha1/operandconfig_types.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type ConfigService struct {
4949
Resources []ConfigResource `json:"resources,omitempty"`
5050
}
5151

52+
// +kubebuilder:pruning:PreserveUnknownFields
5253
// ConfigResource defines the resource needed for the service
5354
type ConfigResource struct {
5455
// Name is the resource name.
@@ -75,6 +76,28 @@ type ConfigResource struct {
7576
// +nullable
7677
// +optional
7778
Data *runtime.RawExtension `json:"data,omitempty"`
79+
// OwnerReferences is the list of owner references.
80+
// +optional
81+
OwnerReferences []OwnerReference `json:"ownerReferences,omitempty"`
82+
}
83+
84+
type OwnerReference struct {
85+
// API version of the referent.
86+
APIVersion string `json:"apiVersion"`
87+
// Kind of the referent.
88+
Kind string `json:"kind"`
89+
// Name of the referent.
90+
Name string `json:"name"`
91+
// If true, this reference points to the managing controller.
92+
// Default is false.
93+
// +optional
94+
Controller *bool `json:"controller,omitempty"`
95+
// If true, AND if the owner has the "foregroundDeletion" finalizer, then
96+
// the owner cannot be deleted from the key-value store until this
97+
// reference is removed.
98+
// Defaults to false.
99+
// +optional
100+
BlockOwnerDeletion *bool `json:"blockOwnerDeletion,omitempty"`
78101
}
79102

80103
// OperandConfigStatus defines the observed state of OperandConfig.

bundle.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LABEL operators.operatorframework.io.bundle.metadata.v1=metadata/
77
LABEL operators.operatorframework.io.bundle.package.v1=ibm-odlm
88
LABEL operators.operatorframework.io.bundle.channels.v1=v4.3
99
LABEL operators.operatorframework.io.bundle.channel.default.v1=v4.3
10-
LABEL operators.operatorframework.io.metrics.builder=operator-sdk-v1.29.0
10+
LABEL operators.operatorframework.io.metrics.builder=operator-sdk-v1.32.0
1111
LABEL operators.operatorframework.io.metrics.mediatype.v1=metrics+v1
1212
LABEL operators.operatorframework.io.metrics.project_layout=go.kubebuilder.io/v3
1313

bundle/manifests/operand-deployment-lifecycle-manager.clusterserviceversion.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ metadata:
134134
nss.operator.ibm.com/managed-operators: ibm-odlm
135135
olm.skipRange: '>=1.2.0 <4.3.0'
136136
operators.openshift.io/infrastructure-features: '["disconnected"]'
137-
operators.operatorframework.io/builder: operator-sdk-v1.29.0
137+
operators.operatorframework.io/builder: operator-sdk-v1.32.0
138138
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
139139
repository: https://github.com/IBM/operand-deployment-lifecycle-manager
140140
support: IBM
@@ -149,7 +149,7 @@ spec:
149149
apiservicedefinitions: {}
150150
customresourcedefinitions:
151151
owned:
152-
- description: OperandBindInfo is the Schema for the operandbindinfoes API. Documentation For additional details regarding install parameters check https://ibm.biz/icpfs39install. License By installing this product you accept the license terms https://ibm.biz/icpfs39license
152+
- description: OperandBindInfo is the Schema for the operandbindinfoes API.
153153
displayName: OperandBindInfo
154154
kind: OperandBindInfo
155155
name: operandbindinfos.operator.ibm.com
@@ -160,7 +160,7 @@ spec:
160160
x-descriptors:
161161
- urn:alm:descriptor:io.kubernetes.phase
162162
version: v1alpha1
163-
- description: OperandConfig is the Schema for the operandconfigs API. Documentation For additional details regarding install parameters check https://ibm.biz/icpfs39install. License By installing this product you accept the license terms https://ibm.biz/icpfs39license
163+
- description: OperandConfig is the Schema for the operandconfigs API.
164164
displayName: OperandConfig
165165
kind: OperandConfig
166166
name: operandconfigs.operator.ibm.com
@@ -175,7 +175,7 @@ spec:
175175
x-descriptors:
176176
- urn:alm:descriptor:io.kubernetes.phase
177177
version: v1alpha1
178-
- description: OperandRegistry is the Schema for the operandregistries API. Documentation For additional details regarding install parameters check https://ibm.biz/icpfs39install. License By installing this product you accept the license terms https://ibm.biz/icpfs39license
178+
- description: OperandRegistry is the Schema for the operandregistries API.
179179
displayName: OperandRegistry
180180
kind: OperandRegistry
181181
name: operandregistries.operator.ibm.com
@@ -195,7 +195,7 @@ spec:
195195
x-descriptors:
196196
- urn:alm:descriptor:io.kubernetes.phase
197197
version: v1alpha1
198-
- description: OperandRequest is the Schema for the operandrequests API. Documentation For additional details regarding install parameters check https://ibm.biz/icpfs39install. License By installing this product you accept the license terms https://ibm.biz/icpfs39license
198+
- description: OperandRequest is the Schema for the operandrequests API.
199199
displayName: OperandRequest
200200
kind: OperandRequest
201201
name: operandrequests.operator.ibm.com

bundle/manifests/operator.ibm.com_operandconfigs.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,41 @@ spec:
102102
namespace:
103103
description: Namespace is the namespace of the resource.
104104
type: string
105+
ownerReferences:
106+
description: OwnerReferences is the list of owner references.
107+
items:
108+
properties:
109+
apiVersion:
110+
description: API version of the referent.
111+
type: string
112+
blockOwnerDeletion:
113+
description: If true, AND if the owner has the "foregroundDeletion"
114+
finalizer, then the owner cannot be deleted from
115+
the key-value store until this reference is removed.
116+
Defaults to false.
117+
type: boolean
118+
controller:
119+
description: If true, this reference points to the
120+
managing controller. Default is false.
121+
type: boolean
122+
kind:
123+
description: Kind of the referent.
124+
type: string
125+
name:
126+
description: Name of the referent.
127+
type: string
128+
required:
129+
- apiVersion
130+
- kind
131+
- name
132+
type: object
133+
type: array
105134
required:
106135
- apiVersion
107136
- kind
108137
- name
109138
type: object
139+
x-kubernetes-preserve-unknown-fields: true
110140
type: array
111141
spec:
112142
additionalProperties:

bundle/metadata/annotations.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ annotations:
66
operators.operatorframework.io.bundle.package.v1: ibm-odlm
77
operators.operatorframework.io.bundle.channels.v1: v4.3
88
operators.operatorframework.io.bundle.channel.default.v1: v4.3
9-
operators.operatorframework.io.metrics.builder: operator-sdk-v1.29.0
9+
operators.operatorframework.io.metrics.builder: operator-sdk-v1.32.0
1010
operators.operatorframework.io.metrics.mediatype.v1: metrics+v1
1111
operators.operatorframework.io.metrics.project_layout: go.kubebuilder.io/v3
1212
# Annotations for testing.

config/crd/bases/operator.ibm.com_operandconfigs.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,41 @@ spec:
100100
namespace:
101101
description: Namespace is the namespace of the resource.
102102
type: string
103+
ownerReferences:
104+
description: OwnerReferences is the list of owner references.
105+
items:
106+
properties:
107+
apiVersion:
108+
description: API version of the referent.
109+
type: string
110+
blockOwnerDeletion:
111+
description: If true, AND if the owner has the "foregroundDeletion"
112+
finalizer, then the owner cannot be deleted from
113+
the key-value store until this reference is removed.
114+
Defaults to false.
115+
type: boolean
116+
controller:
117+
description: If true, this reference points to the
118+
managing controller. Default is false.
119+
type: boolean
120+
kind:
121+
description: Kind of the referent.
122+
type: string
123+
name:
124+
description: Name of the referent.
125+
type: string
126+
required:
127+
- apiVersion
128+
- kind
129+
- name
130+
type: object
131+
type: array
103132
required:
104133
- apiVersion
105134
- kind
106135
- name
107136
type: object
137+
x-kubernetes-preserve-unknown-fields: true
108138
type: array
109139
spec:
110140
additionalProperties:

config/manifests/bases/operand-deployment-lifecycle-manager.clusterserviceversion.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ spec:
2828
apiservicedefinitions: {}
2929
customresourcedefinitions:
3030
owned:
31-
- description: OperandBindInfo is the Schema for the operandbindinfoes API. Documentation For additional details regarding install parameters check https://ibm.biz/icpfs39install. License By installing this product you accept the license terms https://ibm.biz/icpfs39license
31+
- description: OperandBindInfo is the Schema for the operandbindinfoes API.
3232
displayName: OperandBindInfo
3333
kind: OperandBindInfo
3434
name: operandbindinfos.operator.ibm.com
@@ -39,7 +39,7 @@ spec:
3939
x-descriptors:
4040
- urn:alm:descriptor:io.kubernetes.phase
4141
version: v1alpha1
42-
- description: OperandConfig is the Schema for the operandconfigs API. Documentation For additional details regarding install parameters check https://ibm.biz/icpfs39install. License By installing this product you accept the license terms https://ibm.biz/icpfs39license
42+
- description: OperandConfig is the Schema for the operandconfigs API.
4343
displayName: OperandConfig
4444
kind: OperandConfig
4545
name: operandconfigs.operator.ibm.com
@@ -54,7 +54,7 @@ spec:
5454
x-descriptors:
5555
- urn:alm:descriptor:io.kubernetes.phase
5656
version: v1alpha1
57-
- description: OperandRegistry is the Schema for the operandregistries API. Documentation For additional details regarding install parameters check https://ibm.biz/icpfs39install. License By installing this product you accept the license terms https://ibm.biz/icpfs39license
57+
- description: OperandRegistry is the Schema for the operandregistries API.
5858
displayName: OperandRegistry
5959
kind: OperandRegistry
6060
name: operandregistries.operator.ibm.com
@@ -74,7 +74,7 @@ spec:
7474
x-descriptors:
7575
- urn:alm:descriptor:io.kubernetes.phase
7676
version: v1alpha1
77-
- description: OperandRequest is the Schema for the operandrequests API. Documentation For additional details regarding install parameters check https://ibm.biz/icpfs39install. License By installing this product you accept the license terms https://ibm.biz/icpfs39license
77+
- description: OperandRequest is the Schema for the operandrequests API.
7878
displayName: OperandRequest
7979
kind: OperandRequest
8080
name: operandrequests.operator.ibm.com

controllers/operandrequest/reconcile_operand.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,14 @@ func (r *Reconciler) reconcileCRwithConfig(ctx context.Context, service *operato
265265
if err != nil && !apierrors.IsNotFound(err) {
266266
merr.Add(errors.Wrapf(err, "failed to get k8s resource %s/%s", k8sResNs, res.Name))
267267
} else if apierrors.IsNotFound(err) {
268-
if err := r.createK8sResource(ctx, k8sRes, res.Data, res.Labels, res.Annotations); err != nil {
268+
if err := r.createK8sResource(ctx, k8sRes, res.Data, res.Labels, res.Annotations, &res.OwnerReferences); err != nil {
269269
merr.Add(err)
270270
}
271271
} else {
272272
if res.Force {
273273
// Update k8s resource
274274
klog.V(3).Info("Found existing k8s resource: " + res.Name)
275-
if err := r.updateK8sResource(ctx, k8sRes, res.Data, res.Labels, res.Annotations); err != nil {
275+
if err := r.updateK8sResource(ctx, k8sRes, res.Data, res.Labels, res.Annotations, &res.OwnerReferences); err != nil {
276276
merr.Add(err)
277277
}
278278
} else {
@@ -939,7 +939,7 @@ func (r *Reconciler) checkCustomResource(ctx context.Context, requestInstance *o
939939
return nil
940940
}
941941

942-
func (r *Reconciler) createK8sResource(ctx context.Context, k8sResTemplate unstructured.Unstructured, k8sResConfig *runtime.RawExtension, newLabels, newAnnotations map[string]string) error {
942+
func (r *Reconciler) createK8sResource(ctx context.Context, k8sResTemplate unstructured.Unstructured, k8sResConfig *runtime.RawExtension, newLabels, newAnnotations map[string]string, ownerReferences *[]operatorv1alpha1.OwnerReference) error {
943943
kind := k8sResTemplate.GetKind()
944944
name := k8sResTemplate.GetName()
945945
namespace := k8sResTemplate.GetNamespace()
@@ -959,6 +959,9 @@ func (r *Reconciler) createK8sResource(ctx context.Context, k8sResTemplate unstr
959959
r.EnsureLabel(k8sResTemplate, map[string]string{constant.OpreqLabel: "true"})
960960
r.EnsureLabel(k8sResTemplate, newLabels)
961961
r.EnsureAnnotation(k8sResTemplate, newAnnotations)
962+
if err := r.setOwnerReferences(ctx, &k8sResTemplate, ownerReferences); err != nil {
963+
return errors.Wrap(err, "failed to set ownerReferences for k8s resource")
964+
}
962965

963966
// Create the k8s resource
964967
err := r.Create(ctx, &k8sResTemplate)
@@ -971,7 +974,7 @@ func (r *Reconciler) createK8sResource(ctx context.Context, k8sResTemplate unstr
971974
return nil
972975
}
973976

974-
func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstructured.Unstructured, k8sResConfig *runtime.RawExtension, newLabels, newAnnotations map[string]string) error {
977+
func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstructured.Unstructured, k8sResConfig *runtime.RawExtension, newLabels, newAnnotations map[string]string, ownerReferences *[]operatorv1alpha1.OwnerReference) error {
975978
kind := existingK8sRes.GetKind()
976979
apiversion := existingK8sRes.GetAPIVersion()
977980
name := existingK8sRes.GetName()
@@ -1023,7 +1026,7 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr
10231026
if err := r.deleteK8sResource(ctx, existingK8sRes, namespace); err != nil {
10241027
return errors.Wrap(err, "failed to update k8s resource")
10251028
}
1026-
if err := r.createK8sResource(ctx, templatek8sRes, k8sResConfig, newLabels, newAnnotations); err != nil {
1029+
if err := r.createK8sResource(ctx, templatek8sRes, k8sResConfig, newLabels, newAnnotations, ownerReferences); err != nil {
10271030
return errors.Wrap(err, "failed to update k8s resource")
10281031
}
10291032
}
@@ -1076,6 +1079,9 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr
10761079

10771080
r.EnsureAnnotation(existingK8sRes, newAnnotations)
10781081
r.EnsureLabel(existingK8sRes, newLabels)
1082+
if err := r.setOwnerReferences(ctx, &existingK8sRes, ownerReferences); err != nil {
1083+
return false, errors.Wrapf(err, "failed to set ownerReferences for k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name)
1084+
}
10791085

10801086
klog.V(2).Infof("updating k8s resource with apiversion: %s, kind: %s, %s/%s", apiversion, kind, namespace, name)
10811087

@@ -1279,3 +1285,32 @@ func (r *Reconciler) ResourceForKind(gvk schema.GroupVersionKind, namespace stri
12791285
}
12801286
return &mapping.Resource, nil
12811287
}
1288+
1289+
func (r *Reconciler) setOwnerReferences(ctx context.Context, controlledRes *unstructured.Unstructured, ownerReferences *[]operatorv1alpha1.OwnerReference) error {
1290+
if ownerReferences != nil {
1291+
for _, owner := range *ownerReferences {
1292+
ownerObj := unstructured.Unstructured{}
1293+
ownerObj.SetAPIVersion(owner.APIVersion)
1294+
ownerObj.SetKind(owner.Kind)
1295+
ownerObj.SetName(owner.Name)
1296+
1297+
if err := r.Reader.Get(ctx, types.NamespacedName{
1298+
Name: owner.Name,
1299+
Namespace: controlledRes.GetNamespace(),
1300+
}, &ownerObj); err != nil {
1301+
return errors.Wrapf(err, "failed to get owner object -- Kind: %s, NamespacedName: %s/%s", owner.Kind, controlledRes.GetNamespace(), owner.Name)
1302+
}
1303+
if owner.Controller != nil && *owner.Controller {
1304+
if err := controllerutil.SetControllerReference(&ownerObj, controlledRes, r.Scheme); err != nil {
1305+
return errors.Wrapf(err, "failed to set controller ownerReference for k8s resource -- Kind: %s, NamespacedName: %s/%s", controlledRes.GetKind(), controlledRes.GetNamespace(), controlledRes.GetName())
1306+
}
1307+
} else {
1308+
if err := controllerutil.SetOwnerReference(&ownerObj, controlledRes, r.Scheme); err != nil {
1309+
return errors.Wrapf(err, "failed to set ownerReference for k8s resource -- Kind: %s, NamespacedName: %s/%s", controlledRes.GetKind(), controlledRes.GetNamespace(), controlledRes.GetName())
1310+
}
1311+
}
1312+
klog.Infof("Set %s with name %s as Owner for k8s resource -- Kind: %s, NamespacedName: %s/%s", owner.Kind, owner.Name, controlledRes.GetKind(), controlledRes.GetNamespace(), controlledRes.GetName())
1313+
}
1314+
}
1315+
return nil
1316+
}

0 commit comments

Comments
 (0)