Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
set OwnerReference for resources
 created by OperandRequest

Signed-off-by: Daniel Fan <[email protected]>
  • Loading branch information
Daniel-Fan committed Oct 18, 2023
commit d37da3827d5b2acf6d1a9524f99b110fc04bd784
18 changes: 13 additions & 5 deletions controllers/operandrequest/reconcile_operand.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func (r *Reconciler) reconcileCRwithRequest(ctx context.Context, requestInstance
crFromRequest.SetAPIVersion(operand.APIVersion)
crFromRequest.SetKind(operand.Kind)
// Set the OperandRequest as the controller of the CR from request
if err := controllerutil.SetControllerReference(requestInstance, &crFromRequest, r.Scheme); err != nil {
if err := controllerutil.SetOwnerReference(requestInstance, &crFromRequest, r.Scheme); err != nil {
merr.Add(errors.Wrapf(err, "failed to set ownerReference for custom resource %s/%s", requestKey.Namespace, name))
}

Expand All @@ -405,7 +405,7 @@ func (r *Reconciler) reconcileCRwithRequest(ctx context.Context, requestInstance
if r.CheckLabel(crFromRequest, map[string]string{constant.OpreqLabel: "true"}) {
// Update or Delete Custom resource
klog.V(3).Info("Found existing custom resource: " + operand.Kind)
if err := r.updateCustomResource(ctx, crFromRequest, requestKey.Namespace, operand.Kind, operand.Spec.Raw, map[string]interface{}{}); err != nil {
if err := r.updateCustomResource(ctx, crFromRequest, requestKey.Namespace, operand.Kind, operand.Spec.Raw, map[string]interface{}{}, requestInstance); err != nil {
return err
}
statusSpec, err := r.getOperandStatus(crFromRequest)
Expand Down Expand Up @@ -632,7 +632,7 @@ func (r *Reconciler) createCustomResource(ctx context.Context, crTemplate unstru

r.EnsureLabel(crTemplate, map[string]string{constant.OpreqLabel: "true"})

// Creat the CR
// Create the CR
crerr := r.Create(ctx, &crTemplate)
if crerr != nil && !apierrors.IsAlreadyExists(crerr) {
return errors.Wrap(crerr, "failed to create custom resource")
Expand Down Expand Up @@ -667,7 +667,7 @@ func (r *Reconciler) existingCustomResource(ctx context.Context, existingCR unst
return nil
}

func (r *Reconciler) updateCustomResource(ctx context.Context, existingCR unstructured.Unstructured, namespace, crName string, crConfig []byte, configFromALM map[string]interface{}) error {
func (r *Reconciler) updateCustomResource(ctx context.Context, existingCR unstructured.Unstructured, namespace, crName string, crConfig []byte, configFromALM map[string]interface{}, owners ...metav1.Object) error {

kind := existingCR.GetKind()
apiversion := existingCR.GetAPIVersion()
Expand All @@ -691,8 +691,16 @@ func (r *Reconciler) updateCustomResource(ctx context.Context, existingCR unstru
return false, errors.Wrapf(err, "failed to get custom resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name)
}

forceUpdate := false
if !r.CheckLabel(existingCR, map[string]string{constant.OpreqLabel: "true"}) {
return true, nil
} else {
for _, owner := range owners {
if err := controllerutil.SetOwnerReference(owner, &existingCR, r.Scheme); err != nil {
return false, errors.Wrapf(err, "failed to set ownerReference for custom resource %s/%s", existingCR.GetNamespace(), existingCR.GetName())
}
forceUpdate = true
}
}

configFromALMRaw, err := json.Marshal(configFromALM)
Expand Down Expand Up @@ -721,7 +729,7 @@ func (r *Reconciler) updateCustomResource(ctx context.Context, existingCR unstru

CRgeneration := existingCR.GetGeneration()

if reflect.DeepEqual(existingCR.Object["spec"], updatedCRSpec) {
if reflect.DeepEqual(existingCR.Object["spec"], updatedCRSpec) && !forceUpdate {
return true, nil
}

Expand Down