Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
89 changes: 47 additions & 42 deletions controllers/operandrequest/reconcile_operand.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"sync"

"github.com/mohae/deepcopy"
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/pkg/errors"
authorizationv1 "k8s.io/api/authorization/v1"
Expand Down Expand Up @@ -771,12 +772,12 @@ func (r *Reconciler) updateCustomResource(ctx context.Context, existingCR unstru
// Merge spec from update existing CR and OperandConfig spec
updatedCRSpec := util.MergeCR(updatedExistingCRRaw, crConfig)

CRgeneration := existingCR.GetGeneration()

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

CRgeneration := existingCR.GetGeneration()

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

existingCR.Object["spec"] = updatedCRSpec
Expand Down Expand Up @@ -1057,61 +1058,65 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr
return true, nil
}

// isEqual := r.CheckAnnotation(existingK8sRes, newAnnotations) && r.CheckLabel(existingK8sRes, newLabels)
if k8sResConfig != nil {
k8sResConfigDecoded := make(map[string]interface{})
k8sResConfigUnmarshalErr := json.Unmarshal(k8sResConfig.Raw, &k8sResConfigDecoded)
if k8sResConfigUnmarshalErr != nil {
klog.Errorf("failed to unmarshal k8s Resource Config: %v", k8sResConfigUnmarshalErr)
}

for k, v := range k8sResConfigDecoded {
// isEqual = isEqual && reflect.DeepEqual(existingK8sRes.Object[k], v)
existingK8sRes.Object[k] = v
// Convert existing k8s resource to string
existingK8sResRaw, err := json.Marshal(existingK8sRes.Object)
if err != nil {
klog.Error(err)
return false, err
}
}

CRgeneration := existingK8sRes.GetGeneration()
// Merge the existing CR and the CR from the OperandConfig
updatedExistingK8sRes := util.MergeCR(existingK8sResRaw, k8sResConfig.Raw)

// if isEqual {
// return true, nil
// }
// Deep copy the existing k8s resource
originalK8sRes := deepcopy.Copy(existingK8sRes.Object)

r.EnsureAnnotation(existingK8sRes, newAnnotations)
r.EnsureLabel(existingK8sRes, newLabels)
if err := r.setOwnerReferences(ctx, &existingK8sRes, ownerReferences); err != nil {
return false, errors.Wrapf(err, "failed to set ownerReferences for k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name)
}
// Update the existing k8s resource with the merged CR
existingK8sRes.Object = updatedExistingK8sRes

klog.V(2).Infof("updating k8s resource with apiversion: %s, kind: %s, %s/%s", apiversion, kind, namespace, name)
r.EnsureAnnotation(existingK8sRes, newAnnotations)
r.EnsureLabel(existingK8sRes, newLabels)
if err := r.setOwnerReferences(ctx, &existingK8sRes, ownerReferences); err != nil {
return false, errors.Wrapf(err, "failed to set ownerReferences for k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name)
}

err = r.Update(ctx, &existingK8sRes)
if reflect.DeepEqual(originalK8sRes, existingK8sRes.Object) {
return true, nil
}

if err != nil {
return false, errors.Wrapf(err, "failed to update k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name)
}
klog.Infof("updating k8s resource with apiversion: %s, kind: %s, %s/%s", apiversion, kind, namespace, name)

UpdatedK8sRes := unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": apiversion,
"kind": kind,
},
}
CRgeneration := existingK8sRes.GetGeneration()

err = r.Client.Get(ctx, types.NamespacedName{
Name: name,
Namespace: namespace,
}, &UpdatedK8sRes)
err = r.Update(ctx, &existingK8sRes)

if err != nil {
return false, errors.Wrapf(err, "failed to get k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name)
if err != nil {
return false, errors.Wrapf(err, "failed to update k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name)
}

}
UpdatedK8sRes := unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": apiversion,
"kind": kind,
},
}

if UpdatedK8sRes.GetGeneration() != CRgeneration {
klog.V(2).Infof("Finish updating the k8s Resource: -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name)
}
err = r.Client.Get(ctx, types.NamespacedName{
Name: name,
Namespace: namespace,
}, &UpdatedK8sRes)

if err != nil {
return false, errors.Wrapf(err, "failed to get k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name)

}

if UpdatedK8sRes.GetGeneration() != CRgeneration {
klog.Infof("Finish updating the k8s Resource: -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name)
}
}
return true, nil
})

Expand Down
19 changes: 19 additions & 0 deletions controllers/util/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ func checkKeyBeforeMerging(key string, defaultMap interface{}, changedMap interf
checkKeyBeforeMerging(newKey, defaultMapRef[newKey], changedMapRef[newKey], finalMap[key].(map[string]interface{}))
}
}
case []interface{}:
if changedMap == nil {
finalMap[key] = defaultMap
} else if _, ok := changedMap.([]interface{}); ok { //Check that the changed map value is also a slice []interface
defaultMapRef := defaultMap
changedMapRef := changedMap.([]interface{})
for i := range defaultMapRef {
if _, ok := defaultMapRef[i].(map[string]interface{}); ok {
if len(changedMapRef) <= i {
finalMap[key] = append(finalMap[key].([]interface{}), defaultMapRef[i])
} else {

for newKey := range defaultMapRef[i].(map[string]interface{}) {
checkKeyBeforeMerging(newKey, defaultMapRef[i].(map[string]interface{})[newKey], changedMapRef[i].(map[string]interface{})[newKey], finalMap[key].([]interface{})[i].(map[string]interface{}))
}
}
}
}
}
default:
//Check if the value was set, otherwise set it
if changedMap == nil {
Expand Down