@@ -189,14 +189,14 @@ func (a *CRDTemplateAgentAddon) renderObjects(
189
189
return objects , err
190
190
}
191
191
192
- object , err = a .decorateObject (template , object , presetValues , privateValues )
192
+ object , err = a .decorateObject (addon , template , object , presetValues , privateValues )
193
193
if err != nil {
194
194
return objects , err
195
195
}
196
196
objects = append (objects , object )
197
197
}
198
198
199
- additionalObjects , err := a .injectAdditionalObjects (template , presetValues , privateValues )
199
+ additionalObjects , err := a .injectAdditionalObjects (addon , template , presetValues , privateValues )
200
200
if err != nil {
201
201
return objects , err
202
202
}
@@ -206,14 +206,16 @@ func (a *CRDTemplateAgentAddon) renderObjects(
206
206
}
207
207
208
208
func (a * CRDTemplateAgentAddon ) decorateObject (
209
+ addon * addonapiv1alpha1.ManagedClusterAddOn ,
209
210
template * addonapiv1alpha1.AddOnTemplate ,
210
211
obj * unstructured.Unstructured ,
211
212
orderedValues orderedValues ,
212
- privateValues addonfactory.Values ) (* unstructured.Unstructured , error ) {
213
+ privateValues addonfactory.Values ,
214
+ ) (* unstructured.Unstructured , error ) {
213
215
decorators := []decorator {
214
216
newDeploymentDecorator (a .logger , a .addonName , template , orderedValues , privateValues ),
215
217
newDaemonSetDecorator (a .logger , a .addonName , template , orderedValues , privateValues ),
216
- newNamespaceDecorator (privateValues ),
218
+ newNamespaceDecorator (addon . Spec . InstallNamespace , privateValues ),
217
219
}
218
220
219
221
var err error
@@ -228,6 +230,7 @@ func (a *CRDTemplateAgentAddon) decorateObject(
228
230
}
229
231
230
232
func (a * CRDTemplateAgentAddon ) injectAdditionalObjects (
233
+ addon * addonapiv1alpha1.ManagedClusterAddOn ,
231
234
template * addonapiv1alpha1.AddOnTemplate ,
232
235
orderedValues orderedValues ,
233
236
privateValues addonfactory.Values ) ([]runtime.Object , error ) {
@@ -237,7 +240,7 @@ func (a *CRDTemplateAgentAddon) injectAdditionalObjects(
237
240
238
241
decorators := []decorator {
239
242
// decorate the namespace of the additional objects
240
- newNamespaceDecorator (privateValues ),
243
+ newNamespaceDecorator (addon . Spec . InstallNamespace , privateValues ),
241
244
}
242
245
243
246
var objs []runtime.Object
@@ -296,54 +299,60 @@ func (a *CRDTemplateAgentAddon) getDesiredAddOnTemplateInner(
296
299
return template .DeepCopy (), nil
297
300
}
298
301
299
- // TemplateAgentRegistrationNamespaceFunc reads deployment/daemonset resources in the manifests and use that namespace
300
- // as the default registration namespace. If addonDeploymentConfig is set, uses the namespace in it.
302
+ // TemplateAgentRegistrationNamespaceFunc reads the registration namespace according to the following
303
+ // priorities, from high to low:
304
+ // 1. namespace configured in the addonDeploymentConfig
305
+ // 2. managedClusterAddon.spec.installNamespace
306
+ // 3. namespace of deployment/daemonset resources defined in the addonTemplated manifests
301
307
func (a * CRDTemplateAgentAddon ) TemplateAgentRegistrationNamespaceFunc (
302
308
addon * addonapiv1alpha1.ManagedClusterAddOn ) (string , error ) {
303
- template , err := a .getDesiredAddOnTemplateInner (addon .Name , addon .Status .ConfigReferences )
304
- if err != nil {
305
- return "" , err
306
- }
307
- if template == nil {
308
- return "" , fmt .Errorf ("addon %s template not found in status" , addon .Name )
309
- }
310
309
311
- // pick the namespace of the first deployment, if there is no deployment, pick the namespace of the first daemonset
312
- var desiredNS = "open-cluster-management-agent-addon"
313
- var firstDeploymentNamespace , firstDaemonSetNamespace string
314
- for _ , manifest := range template . Spec . AgentSpec . Workload . Manifests {
315
- object := & unstructured. Unstructured {}
316
- if err := object . UnmarshalJSON ( manifest . Raw ); err != nil {
317
- a . logger . Error ( err , "failed to extract the object" )
318
- continue
310
+ desiredNs := addon . Spec . InstallNamespace
311
+ if len ( desiredNs ) == 0 {
312
+ template , err := a . getDesiredAddOnTemplateInner ( addon . Name , addon . Status . ConfigReferences )
313
+ if err != nil {
314
+ return "" , err
315
+ }
316
+ if template == nil {
317
+ return "" , fmt . Errorf ( "addon %s template not found in status" , addon . Name )
319
318
}
320
319
321
- if firstDeploymentNamespace == "" {
322
- if _ , err = utils .ConvertToDeployment (object ); err == nil {
323
- firstDeploymentNamespace = object .GetNamespace ()
324
- break
320
+ // pick the namespace of the first deployment, if there is no deployment, pick the namespace of the first daemonset
321
+ desiredNs = "open-cluster-management-agent-addon"
322
+ var firstDeploymentNamespace , firstDaemonSetNamespace string
323
+ for _ , manifest := range template .Spec .AgentSpec .Workload .Manifests {
324
+ object := & unstructured.Unstructured {}
325
+ if err := object .UnmarshalJSON (manifest .Raw ); err != nil {
326
+ a .logger .Error (err , "failed to extract the object" )
327
+ continue
325
328
}
326
- }
327
- if firstDaemonSetNamespace == "" {
328
- if _ , err = utils .ConvertToDaemonSet (object ); err == nil {
329
- firstDaemonSetNamespace = object .GetNamespace ()
329
+
330
+ if firstDeploymentNamespace == "" {
331
+ if _ , err = utils .ConvertToDeployment (object ); err == nil {
332
+ firstDeploymentNamespace = object .GetNamespace ()
333
+ break
334
+ }
335
+ }
336
+ if firstDaemonSetNamespace == "" {
337
+ if _ , err = utils .ConvertToDaemonSet (object ); err == nil {
338
+ firstDaemonSetNamespace = object .GetNamespace ()
339
+ }
330
340
}
331
341
}
332
- }
333
342
334
- if firstDeploymentNamespace != "" {
335
- desiredNS = firstDeploymentNamespace
336
- } else if firstDaemonSetNamespace != "" {
337
- desiredNS = firstDaemonSetNamespace
343
+ if firstDeploymentNamespace != "" {
344
+ desiredNs = firstDeploymentNamespace
345
+ } else if firstDaemonSetNamespace != "" {
346
+ desiredNs = firstDaemonSetNamespace
347
+ }
338
348
}
339
-
340
349
overrideNs , err := utils .AgentInstallNamespaceFromDeploymentConfigFunc (
341
350
utils .NewAddOnDeploymentConfigGetter (a .addonClient ))(addon )
342
351
if err != nil {
343
352
return "" , err
344
353
}
345
354
if len (overrideNs ) > 0 {
346
- desiredNS = overrideNs
355
+ desiredNs = overrideNs
347
356
}
348
- return desiredNS , nil
357
+ return desiredNs , nil
349
358
}
0 commit comments