Skip to content

Commit b49c9bc

Browse files
author
Jeff Peeler
committed
fix(olm): make operator group copy CSVs more efficiently
Essentially, optimize for the update case rather than create since creates only happen once.
1 parent ab27044 commit b49c9bc

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

pkg/controller/operators/olm/operatorgroup.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,36 @@ func (a *Operator) copyCsvToTargetNamespace(csv *v1alpha1.ClusterServiceVersion,
7676
if ns.Name == operatorGroup.GetNamespace() {
7777
continue
7878
}
79-
// create new CSV instead of DeepCopy as namespace and resource version (and status) will be different
80-
newCSV := v1alpha1.ClusterServiceVersion{
81-
ObjectMeta: metav1.ObjectMeta{
82-
Name: csv.Name,
83-
Annotations: csv.Annotations,
84-
},
85-
Spec: *csv.Spec.DeepCopy(),
86-
}
87-
newCSV.SetNamespace(ns.Name)
79+
fetchedCSV, err := a.csvLister[ns.GetName()].ClusterServiceVersions(ns.GetName()).Get(csv.GetName())
80+
if k8serrors.IsAlreadyExists(err) {
81+
log.Debugf("Found existing CSV (%v), checking annotations", fetchedCSV.GetName())
82+
if reflect.DeepEqual(fetchedCSV.Annotations, csv.Annotations) == false {
83+
fetchedCSV.Annotations = csv.Annotations
84+
// CRDs don't support strategic merge patching, but in the future if they do this should be updated to patch
85+
log.Debugf("Updating CSV %v in namespace %v", fetchedCSV.GetName(), ns.GetName())
86+
if _, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).Update(fetchedCSV); err != nil {
87+
log.Errorf("Update CSV in target namespace failed: %v", err)
88+
return err
89+
}
90+
}
91+
continue
92+
} else if k8serrors.IsNotFound(err) {
93+
// create new CSV instead of DeepCopy as namespace and resource version (and status) will be different
94+
newCSV := v1alpha1.ClusterServiceVersion{
95+
ObjectMeta: metav1.ObjectMeta{
96+
Name: csv.Name,
97+
Annotations: csv.Annotations,
98+
},
99+
Spec: *csv.Spec.DeepCopy(),
100+
}
101+
newCSV.SetNamespace(ns.Name)
88102

89-
log.Debugf("Copying/updating CSV %v to/in namespace %v", csv.GetName(), ns.Name)
90-
createdCSV, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).Create(&newCSV)
91-
if err == nil {
103+
log.Debugf("Copying CSV %v to namespace %v", csv.GetName(), ns.GetName())
104+
createdCSV, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).Create(&newCSV)
105+
if err != nil {
106+
log.Errorf("Create for new CSV failed: %v", err)
107+
return err
108+
}
92109
createdCSV.Status = v1alpha1.ClusterServiceVersionStatus{
93110
Message: "CSV copied to target namespace",
94111
Reason: v1alpha1.CSVReasonCopied,
@@ -98,21 +115,8 @@ func (a *Operator) copyCsvToTargetNamespace(csv *v1alpha1.ClusterServiceVersion,
98115
log.Errorf("Status update for CSV failed: %v", err)
99116
return err
100117
}
101-
}
102-
if k8serrors.IsAlreadyExists(err) {
103-
fetchedCSV, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).Get(csv.GetName(), metav1.GetOptions{})
104-
if err != nil {
105-
log.Errorf("Create failed, yet get failed: %v", err)
106-
}
107-
if reflect.DeepEqual(fetchedCSV.Annotations, csv.Annotations) == false {
108-
// CRDs don't support strategic merge patching, but in the future if they do this should be updated to patch
109-
if _, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).Update(fetchedCSV); err != nil {
110-
log.Errorf("Update CSV in target namespace failed: %v", err)
111-
return err
112-
}
113-
}
114118
} else if err != nil {
115-
log.Errorf("Create for new CSV failed: %v", err)
119+
log.Errorf("CSV fetch for %v failed: %v", csv.GetName(), err)
116120
return err
117121
}
118122
}

0 commit comments

Comments
 (0)