Skip to content

Commit

Permalink
Update dependency-watches library for DeepCopy
Browse files Browse the repository at this point in the history
The library now does the DeepCopy for us, to prevent accidental
mutations to objects in the cache.

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
  • Loading branch information
JustinKuli authored and openshift-merge-bot[bot] committed May 2, 2024
1 parent 1ecc347 commit b080562
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
21 changes: 8 additions & 13 deletions controllers/operatorpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,8 @@ func (r *OperatorPolicyReconciler) musthaveOpGroup(
return nil, false, fmt.Errorf("error converting desired OperatorGroup to an Unstructured: %w", err)
}

merged := opGroup.DeepCopy() // Copy it so that the value in the cache is not changed

updateNeeded, skipUpdate, err := r.mergeObjects(
ctx, desiredUnstruct, merged, string(policy.Spec.ComplianceType),
ctx, desiredUnstruct, &opGroup, string(policy.Spec.ComplianceType),
)
if err != nil {
return nil, false, fmt.Errorf("error checking if the OperatorGroup needs an update: %w", err)
Expand Down Expand Up @@ -747,7 +745,7 @@ func (r *OperatorPolicyReconciler) musthaveOpGroup(

desiredOpGroup.ResourceVersion = opGroup.GetResourceVersion()

err = r.Update(ctx, merged)
err = r.Update(ctx, &opGroup)
if err != nil {
return nil, changed, fmt.Errorf("error updating the OperatorGroup: %w", err)
}
Expand Down Expand Up @@ -974,15 +972,13 @@ func (r *OperatorPolicyReconciler) musthaveSubscription(
return nil, nil, false, fmt.Errorf("error converting desired Subscription to an Unstructured: %w", err)
}

merged := foundSub.DeepCopy() // Copy it so that the value in the cache is not changed

updateNeeded, skipUpdate, err := r.mergeObjects(ctx, desiredUnstruct, merged, string(policy.Spec.ComplianceType))
updateNeeded, skipUpdate, err := r.mergeObjects(ctx, desiredUnstruct, foundSub, string(policy.Spec.ComplianceType))
if err != nil {
return nil, nil, false, fmt.Errorf("error checking if the Subscription needs an update: %w", err)
}

mergedSub := new(operatorv1alpha1.Subscription)
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(merged.Object, mergedSub); err != nil {
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(foundSub.Object, mergedSub); err != nil {
return nil, nil, false, fmt.Errorf("error converting the retrieved Subscription to the go type: %w", err)
}

Expand Down Expand Up @@ -1040,14 +1036,14 @@ func (r *OperatorPolicyReconciler) musthaveSubscription(
earlyConds = append(earlyConds, calculateComplianceCondition(policy))
}

err = r.Update(ctx, merged)
err = r.Update(ctx, foundSub)
if err != nil {
return mergedSub, nil, changed, fmt.Errorf("error updating the Subscription: %w", err)
}

merged.SetGroupVersionKind(subscriptionGVK) // Update stripped this information
foundSub.SetGroupVersionKind(subscriptionGVK) // Update stripped this information

updateStatus(policy, updatedCond("Subscription"), updatedObj(merged))
updateStatus(policy, updatedCond("Subscription"), updatedObj(foundSub))

return mergedSub, earlyConds, true, nil
}
Expand Down Expand Up @@ -1696,11 +1692,10 @@ func (r *OperatorPolicyReconciler) handleCatalogSource(

if !isMissing {
// CatalogSource is found, initiate health check
catalogSrcUnstruct := foundCatalogSrc.DeepCopy()
catalogSrc := new(operatorv1alpha1.CatalogSource)

err := runtime.DefaultUnstructuredConverter.
FromUnstructured(catalogSrcUnstruct.Object, catalogSrc)
FromUnstructured(foundCatalogSrc.Object, catalogSrc)
if err != nil {
return false, fmt.Errorf("error converting the retrieved CatalogSource to the Go type: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ require (
github.com/prometheus/client_golang v1.18.0
github.com/spf13/pflag v1.0.5
github.com/stolostron/go-log-utils v0.1.2
github.com/stolostron/go-template-utils/v4 v4.0.2-0.20240426175705-e9547005fb4f
github.com/stolostron/kubernetes-dependency-watches v0.5.2
github.com/stolostron/go-template-utils/v4 v4.1.0
github.com/stolostron/kubernetes-dependency-watches v0.6.0
github.com/stretchr/testify v1.8.4
golang.org/x/mod v0.16.0
k8s.io/api v0.29.2
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stolostron/go-log-utils v0.1.2 h1:7l1aJWvBqU2+DUyimcslT5SJpdygVY/clRDmX5sO29c=
github.com/stolostron/go-log-utils v0.1.2/go.mod h1:8zrB8UJmp1rXhv3Ck9bBl5SpNfKk3SApeElbo96YRtQ=
github.com/stolostron/go-template-utils/v4 v4.0.2-0.20240426175705-e9547005fb4f h1:LKvxEJgNRLetVyMz8HdIRQg+nnojR9OHNL8QpNYiX5I=
github.com/stolostron/go-template-utils/v4 v4.0.2-0.20240426175705-e9547005fb4f/go.mod h1:b93jKpuJtoePu9liTNcp7td+8Hvzk2lROeMqVLC42ks=
github.com/stolostron/kubernetes-dependency-watches v0.5.2 h1:TtctOgPn+TYo1dJ+dr9TLR2rlpy5aFQ/1dzfcYZUDi4=
github.com/stolostron/kubernetes-dependency-watches v0.5.2/go.mod h1:5nwtuleCNR9Mno7SJRz6B2BavvXHbfR4rIwzKLkk9G8=
github.com/stolostron/go-template-utils/v4 v4.1.0 h1:i7S6gTmFixZB25zY03pR5rC30PVJGDFfFuLi15Oqq6g=
github.com/stolostron/go-template-utils/v4 v4.1.0/go.mod h1:pEezMPxvuvllc68oLUejh9tfM8r/Yi7s10ZlZhVkyM4=
github.com/stolostron/kubernetes-dependency-watches v0.6.0 h1:FxXsslLNH5hNojVvKX467iefCEwigdMftfmlYX/tddg=
github.com/stolostron/kubernetes-dependency-watches v0.6.0/go.mod h1:6v54aX8Bxx1m9YETZUxNGUrSHcRIArM/YnOqnUbIB/g=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,11 @@ func main() {
depReconciler, depEvents := depclient.NewControllerRuntimeSource()

watcher, err := depclient.New(cfg, depReconciler,
&depclient.Options{DisableInitialReconcile: true, EnableCache: true})
&depclient.Options{
DisableInitialReconcile: true,
EnableCache: true,
ObjectCacheOptions: depclient.ObjectCacheOptions{UnsafeDisableDeepCopy: false},
})
if err != nil {
log.Error(err, "Unable to create dependency watcher")
os.Exit(1)
Expand Down

0 comments on commit b080562

Please sign in to comment.