Skip to content

Commit 22a29b3

Browse files
author
Jeff Peeler
committed
fix(olm): refactor e2e and check annotations
1 parent 2af56a7 commit 22a29b3

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

pkg/controller/operators/olm/operatorgroup.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
5757
return err
5858
}
5959

60-
if err := a.copyCsvToTargetNamespace(csv, op, targetedNamespaces); err!=nil {
60+
if err := a.copyCsvToTargetNamespace(csv, op, targetedNamespaces); err != nil {
6161
return err
6262
}
6363
}
@@ -109,35 +109,41 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
109109
}
110110

111111
func (a *Operator) copyCsvToTargetNamespace(csv *v1alpha1.ClusterServiceVersion, operatorGroup *v1alpha2.OperatorGroup, targetNamespaces []*corev1.Namespace) error {
112-
var nsList []string
113-
for _, ns := range targetNamespaces {
114-
nsList = append(nsList, ns.Name)
115-
}
116-
nsListJoined := strings.Join(nsList, ",")
117-
118-
119112
for _, ns := range targetNamespaces {
120113
if ns.Name == operatorGroup.GetNamespace() {
121114
continue
122115
}
116+
// create new CSV instead of DeepCopy as namespace and resource version (and status) will be different
123117
newCSV := v1alpha1.ClusterServiceVersion{
124118
ObjectMeta: metav1.ObjectMeta{
125-
Name: csv.Name,
119+
Name: csv.Name,
120+
Annotations: csv.Annotations,
126121
},
127122
Spec: *csv.Spec.DeepCopy(),
128-
Status: v1alpha1.ClusterServiceVersionStatus{
123+
}
124+
newCSV.SetNamespace(ns.Name)
125+
126+
log.Debugf("Copying/updating CSV %v to/in namespace %v", csv.GetName(), ns.Name)
127+
createdCSV, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).Create(&newCSV)
128+
if err == nil {
129+
createdCSV.Status = v1alpha1.ClusterServiceVersionStatus{
129130
Message: "CSV copied to target namespace",
130131
Reason: v1alpha1.CSVReasonCopied,
131132
LastUpdateTime: timeNow(),
132-
},
133+
}
134+
if _, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).UpdateStatus(createdCSV); err != nil {
135+
log.Errorf("Status update for CSV failed: %v", err)
136+
return err
137+
}
133138
}
134-
a.addAnnotationsToCSV(&newCSV, operatorGroup, nsListJoined)
135-
newCSV.SetNamespace(ns.Name)
136-
137-
log.Debugf("Copying CSV %v to namespace %v", csv.GetName(), ns.Name)
138-
_, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).Create(&newCSV)
139139
if k8serrors.IsAlreadyExists(err) {
140-
if _, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).Update(&newCSV); err != nil {
140+
fetchedCSV, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).Get(csv.GetName(), metav1.GetOptions{})
141+
if err != nil {
142+
log.Errorf("Create failed, yet get failed: %v", err)
143+
}
144+
// update the potentially different annotations
145+
fetchedCSV.Annotations = csv.Annotations
146+
if _, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.Name).Update(fetchedCSV); err != nil {
141147
log.Errorf("Update CSV in target namespace failed: %v", err)
142148
return err
143149
}

test/e2e/operator_groups_e2e_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,23 @@ func patchOlmDeployment(t *testing.T, c operatorclient.ClientInterface, newNames
6363
return command
6464
}
6565

66+
func checkOperatorGroupAnnotations(obj metav1.Object, op *v1alpha2.OperatorGroup, targetNamespaces string) error {
67+
if annotation, ok := obj.GetAnnotations()["olm.targetNamespaces"]; !ok || annotation != targetNamespaces {
68+
return fmt.Errorf("missing targetNamespaces annotation on %v", obj.GetName())
69+
}
70+
if annotation, ok := obj.GetAnnotations()["olm.operatorNamespace"]; !ok || annotation != op.GetNamespace() {
71+
return fmt.Errorf("missing operatorNamespace on %v", obj.GetName())
72+
}
73+
if annotation, ok := obj.GetAnnotations()["olm.operatorGroup"]; !ok || annotation != op.GetName() {
74+
return fmt.Errorf("missing operatorGroup annotation on %v", obj.GetName())
75+
}
76+
77+
return nil
78+
}
79+
6680
func TestCreateOperatorGroupWithMatchingNamespace(t *testing.T) {
6781
// Create namespace with specific label
68-
// Create CSV in opoerator namespace
82+
// Create CSV in operator namespace
6983
// Create operator group that watches namespace and uses specific label
7084
// Verify operator group status contains correct status
7185
// Verify csv in target namespace exists, has copied status, has annotations
@@ -171,8 +185,7 @@ func TestCreateOperatorGroupWithMatchingNamespace(t *testing.T) {
171185
fmt.Println(fetchErr)
172186
return false, fetchErr
173187
}
174-
//TODO: check other annotations
175-
if fetchedCSV.Annotations["olm.targetNamespaces"] == otherNamespaceName {
188+
if checkOperatorGroupAnnotations(fetchedCSV, &operatorGroup, otherNamespaceName) == nil {
176189
return true, nil
177190
}
178191
return false, nil
@@ -185,16 +198,17 @@ func TestCreateOperatorGroupWithMatchingNamespace(t *testing.T) {
185198
fmt.Println(fetchErr)
186199
return false, fetchErr
187200
}
188-
//TODO: check other annotations
189-
if fetchedCSV.Annotations["olm.targetNamespaces"] == otherNamespaceName {
201+
if checkOperatorGroupAnnotations(fetchedCSV, &operatorGroup, otherNamespaceName) == nil {
190202
return true, nil
191203
}
204+
192205
return false, nil
193206
})
194-
195-
196-
// TODO: verify CSV in target namespace has correct annotations and status
197-
// TODO: verify CSV in operator namespace has correct annotations
207+
// since annotations are set along with status, no reason to poll for this check as done above
208+
log.Debug("Checking status on CSV in target namespace")
209+
fetchedCSV, err := crc.OperatorsV1alpha1().ClusterServiceVersions(otherNamespaceName).Get(csvName, metav1.GetOptions{})
210+
require.NoError(t, err)
211+
require.EqualValues(t, v1alpha1.CSVReasonCopied, fetchedCSV.Status.Reason)
198212

199213
//log.Debug("Waiting on deployment to have correct annotation")
200214
//err = wait.Poll(pollInterval, pollDuration, func() (bool, error) {

0 commit comments

Comments
 (0)