@@ -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,57 @@ 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
302
+ // TemplateAgentRegistrationNamespaceFunc reads the managedclusteraddon.spec.installnamespace
300
303
// as the default registration namespace. If addonDeploymentConfig is set, uses the namespace in it.
301
304
func (a * CRDTemplateAgentAddon ) TemplateAgentRegistrationNamespaceFunc (
302
305
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
306
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
307
+ desiredNs := addon . Spec . InstallNamespace
308
+ if len ( desiredNs ) == 0 {
309
+ template , err := a . getDesiredAddOnTemplateInner ( addon . Name , addon . Status . ConfigReferences )
310
+ if err != nil {
311
+ return "" , err
312
+ }
313
+ if template == nil {
314
+ return "" , fmt . Errorf ( "addon %s template not found in status" , addon . Name )
319
315
}
320
316
321
- if firstDeploymentNamespace == "" {
322
- if _ , err = utils .ConvertToDeployment (object ); err == nil {
323
- firstDeploymentNamespace = object .GetNamespace ()
324
- break
317
+ // pick the namespace of the first deployment, if there is no deployment, pick the namespace of the first daemonset
318
+ desiredNs = "open-cluster-management-agent-addon"
319
+ var firstDeploymentNamespace , firstDaemonSetNamespace string
320
+ for _ , manifest := range template .Spec .AgentSpec .Workload .Manifests {
321
+ object := & unstructured.Unstructured {}
322
+ if err := object .UnmarshalJSON (manifest .Raw ); err != nil {
323
+ a .logger .Error (err , "failed to extract the object" )
324
+ continue
325
325
}
326
- }
327
- if firstDaemonSetNamespace == "" {
328
- if _ , err = utils .ConvertToDaemonSet (object ); err == nil {
329
- firstDaemonSetNamespace = object .GetNamespace ()
326
+
327
+ if firstDeploymentNamespace == "" {
328
+ if _ , err = utils .ConvertToDeployment (object ); err == nil {
329
+ firstDeploymentNamespace = object .GetNamespace ()
330
+ break
331
+ }
332
+ }
333
+ if firstDaemonSetNamespace == "" {
334
+ if _ , err = utils .ConvertToDaemonSet (object ); err == nil {
335
+ firstDaemonSetNamespace = object .GetNamespace ()
336
+ }
330
337
}
331
338
}
332
- }
333
339
334
- if firstDeploymentNamespace != "" {
335
- desiredNS = firstDeploymentNamespace
336
- } else if firstDaemonSetNamespace != "" {
337
- desiredNS = firstDaemonSetNamespace
340
+ if firstDeploymentNamespace != "" {
341
+ desiredNs = firstDeploymentNamespace
342
+ } else if firstDaemonSetNamespace != "" {
343
+ desiredNs = firstDaemonSetNamespace
344
+ }
338
345
}
339
-
340
346
overrideNs , err := utils .AgentInstallNamespaceFromDeploymentConfigFunc (
341
347
utils .NewAddOnDeploymentConfigGetter (a .addonClient ))(addon )
342
348
if err != nil {
343
349
return "" , err
344
350
}
345
351
if len (overrideNs ) > 0 {
346
- desiredNS = overrideNs
352
+ desiredNs = overrideNs
347
353
}
348
- return desiredNS , nil
354
+ return desiredNs , nil
349
355
}
0 commit comments