Skip to content

Commit 5a7f803

Browse files
authored
Do not modify object from the lister cache (#2562)
Problem: Objects retrieved from the lister should not be modified. Solution: In locations where an object originating from the lister cache must be modified, modify a copy of the object instead. Signed-off-by: Alexander Greene <[email protected]>
1 parent dd0d7fd commit 5a7f803

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

pkg/controller/bundle/bundle_unpacker.go

+1
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ func (c *ConfigMapUnpacker) ensureRole(cmRef *corev1.ObjectReference) (role *rba
646646
return
647647
}
648648
}
649+
role = role.DeepCopy()
649650
role.Rules = append(role.Rules, rule)
650651

651652
role, err = c.client.RbacV1().Roles(role.GetNamespace()).Update(context.TODO(), role, metav1.UpdateOptions{})

pkg/controller/install/apiservice.go

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (i *StrategyDeploymentInstaller) createOrUpdateAPIService(caPEM []byte, des
4141
}
4242
apiService.SetName(apiServiceName)
4343
} else {
44+
apiService = apiService.DeepCopy()
4445
csv, ok := i.owner.(*v1alpha1.ClusterServiceVersion)
4546
if !ok {
4647
return fmt.Errorf("failed to typecast the APIService owner: APIServices require a CSV owner")

pkg/controller/install/webhook.go

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (i *StrategyDeploymentInstaller) createOrUpdateMutatingWebhook(ogNamespacel
106106
}
107107
}
108108
for _, webhook := range existingWebhooks.Items {
109+
webhook = *webhook.DeepCopy()
109110
// Update the list of webhooks
110111
webhook.Webhooks = []admissionregistrationv1.MutatingWebhook{
111112
desc.GetMutatingWebhook(i.owner.GetNamespace(), ogNamespacelabelSelector, caPEM),
@@ -154,6 +155,7 @@ func (i *StrategyDeploymentInstaller) createOrUpdateValidatingWebhook(ogNamespac
154155
return nil
155156
}
156157
for _, webhook := range existingWebhooks.Items {
158+
webhook = *webhook.DeepCopy()
157159
// Update the list of webhooks
158160
webhook.Webhooks = []admissionregistrationv1.ValidatingWebhook{
159161
desc.GetValidatingWebhook(i.owner.GetNamespace(), ogNamespacelabelSelector, caPEM),

pkg/controller/operators/olm/operator.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ func (a *Operator) syncObject(obj interface{}) (syncError error) {
842842
if related {
843843
csvList := a.csvSet(metaObj.GetNamespace(), v1alpha1.CSVPhaseFailed)
844844
for _, csv := range csvList {
845+
csv = csv.DeepCopy()
845846
if csv.Status.Reason != v1alpha1.CSVReasonComponentFailedNoRetry {
846847
continue
847848
}
@@ -1238,7 +1239,7 @@ func (a *Operator) allNamespaceOperatorGroups() ([]*v1.OperatorGroup, error) {
12381239
result := []*v1.OperatorGroup{}
12391240
for _, operatorGroup := range operatorGroups {
12401241
if NewNamespaceSet(operatorGroup.Status.Namespaces).IsAllNamespaces() {
1241-
result = append(result, operatorGroup)
1242+
result = append(result, operatorGroup.DeepCopy())
12421243
}
12431244
}
12441245
return result, nil
@@ -1477,7 +1478,7 @@ func (a *Operator) getCopiedCSVDisabledEventsForCSV(csv *v1alpha1.ClusterService
14771478
event.InvolvedObject.Name == csv.GetName() &&
14781479
event.InvolvedObject.UID == csv.GetUID() &&
14791480
event.Reason == v1.DisabledCopiedCSVsConditionType {
1480-
result = append(result, event)
1481+
result = append(result, *event.DeepCopy())
14811482
}
14821483
}
14831484

@@ -1586,7 +1587,7 @@ func (a *Operator) operatorGroupFromAnnotations(logger *logrus.Entry, csv *v1alp
15861587
return nil
15871588
}
15881589

1589-
return operatorGroup
1590+
return operatorGroup.DeepCopy()
15901591
}
15911592

15921593
func (a *Operator) operatorGroupForCSV(csv *v1alpha1.ClusterServiceVersion, logger *logrus.Entry) (*v1.OperatorGroup, error) {
@@ -1627,7 +1628,7 @@ func (a *Operator) operatorGroupForCSV(csv *v1alpha1.ClusterServiceVersion, logg
16271628
return nil, nil
16281629
}
16291630
logger.Debug("csv in operatorgroup")
1630-
return operatorGroup, nil
1631+
return operatorGroup.DeepCopy(), nil
16311632
default:
16321633
err = fmt.Errorf("csv created in namespace with multiple operatorgroups, can't pick one automatically")
16331634
logger.WithError(err).Warn("csv failed to become an operatorgroup member")

pkg/controller/operators/olm/operatorgroup.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
7373

7474
// Check if there is a stale multiple OG condition and clear it if existed.
7575
if len(groups) == 1 {
76-
og := groups[0]
76+
og := groups[0].DeepCopy()
7777
if c := meta.FindStatusCondition(og.Status.Conditions, v1.MutlipleOperatorGroupCondition); c != nil {
7878
meta.RemoveStatusCondition(&og.Status.Conditions, v1.MutlipleOperatorGroupCondition)
7979
if og.GetName() == op.GetName() {
@@ -93,7 +93,8 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
9393
Reason: v1.MultipleOperatorGroupsReason,
9494
Message: "Multiple OperatorGroup found in the same namespace",
9595
}
96-
for _, og := range groups {
96+
for i := range groups {
97+
og := groups[i].DeepCopy()
9798
if c := meta.FindStatusCondition(og.Status.Conditions, v1.MutlipleOperatorGroupCondition); c != nil {
9899
continue
99100
}
@@ -115,11 +116,12 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
115116
return err
116117
}
117118
if op.Status.ServiceAccountRef != previousRef {
118-
crdList, err := a.lister.OperatorsV1alpha1().ClusterServiceVersionLister().List(labels.Everything())
119+
csvList, err := a.lister.OperatorsV1alpha1().ClusterServiceVersionLister().List(labels.Everything())
119120
if err != nil {
120121
return err
121122
}
122-
for _, csv := range crdList {
123+
for i := range csvList {
124+
csv := csvList[i].DeepCopy()
123125
if group, ok := csv.GetAnnotations()[v1.OperatorGroupAnnotationKey]; !ok || group != op.GetName() {
124126
continue
125127
}

0 commit comments

Comments
 (0)