@@ -2,6 +2,7 @@ package olm
2
2
3
3
import (
4
4
"fmt"
5
+ "reflect"
5
6
"strings"
6
7
7
8
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
@@ -42,7 +43,7 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
42
43
}
43
44
nsListJoined := strings .Join (nsList , "," )
44
45
45
- if err := a .annotateDeployments (op . GetNamespace () , nsListJoined ); err != nil {
46
+ if err := a .annotateDeployments (op , nsListJoined ); err != nil {
46
47
log .Errorf ("annotateDeployments error: %v" , err )
47
48
return err
48
49
}
@@ -51,58 +52,19 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
51
52
// annotate csvs
52
53
csvsInNamespace := a .csvsInNamespace (op .Namespace )
53
54
for _ , csv := range csvsInNamespace {
54
- a .addAnnotationsToCSV (csv , op , nsListJoined )
55
- // TODO: generate a patch
56
- if _ , err := a .client .OperatorsV1alpha1 ().ClusterServiceVersions (csv .GetNamespace ()).Update (csv ); err != nil {
57
- log .Errorf ("Update for existing CSV failed: %v" , err )
58
- return err
55
+ origCSVannotations := csv .GetAnnotations ()
56
+ a .addAnnotationsToObjectMeta (& csv .ObjectMeta , op , nsListJoined )
57
+ if reflect .DeepEqual (origCSVannotations , csv .GetAnnotations ()) == false {
58
+ // CRDs don't support strategic merge patching, but in the future if they do this should be updated to patch
59
+ if _ , err := a .client .OperatorsV1alpha1 ().ClusterServiceVersions (csv .GetNamespace ()).Update (csv ); err != nil {
60
+ log .Errorf ("Update for existing CSV failed: %v" , err )
61
+ }
59
62
}
60
63
61
64
if err := a .copyCsvToTargetNamespace (csv , op , targetedNamespaces ); err != nil {
62
65
return err
63
66
}
64
67
}
65
-
66
- // // create new CSV instead of DeepCopy as namespace and resource version (and status) will be different
67
- // newCSV := v1alpha1.ClusterServiceVersion{
68
- // ObjectMeta: metav1.ObjectMeta{
69
- // Name: csv.Name,
70
- // },
71
- // Spec: *csv.Spec.DeepCopy(),
72
- // Status: v1alpha1.ClusterServiceVersionStatus{
73
- // Message: "CSV copied to target namespace",
74
- // Reason: v1alpha1.CSVReasonCopied,
75
- // LastUpdateTime: timeNow(),
76
- // },
77
- // }
78
- //
79
- // a.addAnnotationsToCSV(&newCSV, op, nsListJoined)
80
- //
81
- //
82
- // for _, ns := range targetedNamespaces {
83
- // newCSV.SetNamespace(ns.Name)
84
- // if ns.Name != op.Namespace {
85
- // log.Debugf("Copying CSV %v to namespace %v", csv.GetName(), ns.GetName())
86
- // _, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.GetName()).Create(&newCSV)
87
- // if k8serrors.IsAlreadyExists(err) {
88
- // a.addAnnotationsToCSV(csv, op, nsListJoined)
89
- // if _, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.GetName()).Update(csv); err != nil {
90
- // log.Errorf("Update CSV in target namespace failed: %v", err)
91
- // return err
92
- // }
93
- // } else if err != nil {
94
- // log.Errorf("Create for new CSV failed: %v", err)
95
- // return err
96
- // }
97
- // } else {
98
- // a.addAnnotationsToCSV(csv, op, nsListJoined)
99
- // if _, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(ns.GetName()).Update(csv); err != nil {
100
- // log.Errorf("Update for existing CSV failed: %v", err)
101
- // return err
102
- // }
103
- // }
104
- // }
105
- //}
106
68
log .Debug ("CSV annotation completed" )
107
69
//TODO: ensure RBAC on operator serviceaccount
108
70
@@ -142,11 +104,12 @@ func (a *Operator) copyCsvToTargetNamespace(csv *v1alpha1.ClusterServiceVersion,
142
104
if err != nil {
143
105
log .Errorf ("Create failed, yet get failed: %v" , err )
144
106
}
145
- // update the potentially different annotations
146
- fetchedCSV .Annotations = csv .Annotations
147
- if _ , err := a .client .OperatorsV1alpha1 ().ClusterServiceVersions (ns .Name ).Update (fetchedCSV ); err != nil {
148
- log .Errorf ("Update CSV in target namespace failed: %v" , err )
149
- return err
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
+ }
150
113
}
151
114
} else if err != nil {
152
115
log .Errorf ("Create for new CSV failed: %v" , err )
@@ -156,10 +119,10 @@ func (a *Operator) copyCsvToTargetNamespace(csv *v1alpha1.ClusterServiceVersion,
156
119
return nil
157
120
}
158
121
159
- func (a * Operator ) addAnnotationsToCSV ( csv * v1alpha1. ClusterServiceVersion , op * v1alpha2.OperatorGroup , targetNamespaces string ) {
160
- metav1 .SetMetaDataAnnotation (& csv . ObjectMeta , "olm.targetNamespaces" , targetNamespaces )
161
- metav1 .SetMetaDataAnnotation (& csv . ObjectMeta , "olm.operatorNamespace" , op .GetNamespace ())
162
- metav1 .SetMetaDataAnnotation (& csv . ObjectMeta , "olm.operatorGroup" , op .GetName ())
122
+ func (a * Operator ) addAnnotationsToObjectMeta ( obj * metav1. ObjectMeta , op * v1alpha2.OperatorGroup , targetNamespaces string ) {
123
+ metav1 .SetMetaDataAnnotation (obj , "olm.targetNamespaces" , targetNamespaces )
124
+ metav1 .SetMetaDataAnnotation (obj , "olm.operatorNamespace" , op .GetNamespace ())
125
+ metav1 .SetMetaDataAnnotation (obj , "olm.operatorGroup" , op .GetName ())
163
126
}
164
127
165
128
func namespacesChanged (clusterNamespaces []* corev1.Namespace , statusNamespaces []* corev1.Namespace ) bool {
@@ -232,10 +195,12 @@ func (a *Operator) ensureClusterRoles(op *v1alpha2.OperatorGroup) error {
232
195
}
233
196
234
197
clusterRole := & rbacv1.ClusterRole {
198
+ ObjectMeta : metav1.ObjectMeta {
199
+ Name : fmt .Sprintf ("owned-crd-manager-%s" , csv .GetName ()),
200
+ },
235
201
Rules : managerPolicyRules ,
236
202
}
237
203
ownerutil .AddNonBlockingOwner (clusterRole , csv )
238
- clusterRole .SetGenerateName (fmt .Sprintf ("owned-crd-manager-%s-" , csv .Spec .DisplayName ))
239
204
_ , err := a .OpClient .KubernetesInterface ().RbacV1 ().ClusterRoles ().Create (clusterRole )
240
205
if k8serrors .IsAlreadyExists (err ) {
241
206
if _ , err = a .OpClient .UpdateClusterRole (clusterRole ); err != nil {
@@ -284,9 +249,9 @@ func (a *Operator) ensureClusterRoles(op *v1alpha2.OperatorGroup) error {
284
249
return nil
285
250
}
286
251
287
- func (a * Operator ) annotateDeployments (operatorNamespace string , targetNamespaceString string ) error {
252
+ func (a * Operator ) annotateDeployments (op * v1alpha2. OperatorGroup , targetNamespaceString string ) error {
288
253
// write above namespaces to watch in every deployment in operator namespace
289
- deploymentList , err := a .deploymentLister [ operatorNamespace ] .List (labels .Everything ())
254
+ deploymentList , err := a .lister . AppsV1 (). DeploymentLister (). Deployments ( op . GetNamespace ()) .List (labels .Everything ())
290
255
if err != nil {
291
256
log .Errorf ("deployment list failed: %v\n " , err )
292
257
return err
@@ -308,12 +273,11 @@ func (a *Operator) annotateDeployments(operatorNamespace string, targetNamespace
308
273
}
309
274
310
275
originalDeploy := deploy .DeepCopy ()
311
- metav1 . SetMetaDataAnnotation (& deploy .Spec .Template .ObjectMeta , "olm.targetNamespaces" , targetNamespaceString )
276
+ a . addAnnotationsToObjectMeta (& deploy .Spec .Template .ObjectMeta , op , targetNamespaceString )
312
277
if _ , _ , err := a .OpClient .PatchDeployment (originalDeploy , deploy ); err != nil {
313
278
log .Errorf ("patch deployment failed: %v\n " , err )
314
279
return err
315
280
}
316
-
317
281
}
318
282
319
283
return nil
0 commit comments