Skip to content

Commit 41197e5

Browse files
authored
Set the first daemonset namespace to registration namespace (#536)
Signed-off-by: zhujian <[email protected]>
1 parent 21f3399 commit 41197e5

File tree

5 files changed

+566
-128
lines changed

5 files changed

+566
-128
lines changed

pkg/addon/templateagent/decorator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func newDaemonSetDecorator(
147147

148148
func (d *daemonSetDecorator) decorate(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
149149
daemonSet, err := utils.ConvertToDaemonSet(obj)
150-
// not a deployment, directly return
150+
// not a daemonset, directly return
151151
if err != nil {
152152
return obj, nil
153153
}
@@ -168,7 +168,7 @@ func (d *daemonSetDecorator) decorate(obj *unstructured.Unstructured) (*unstruct
168168
}
169169

170170
type podTemplateSpecDecorator interface {
171-
// decorate modifies the deployment in place
171+
// decorate modifies the pod template in place
172172
decorate(pod *corev1.PodTemplateSpec) error
173173
}
174174

pkg/addon/templateagent/template_agent.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (a *CRDTemplateAgentAddon) getDesiredAddOnTemplateInner(
230230
return template.DeepCopy(), nil
231231
}
232232

233-
// TemplateAgentRegistrationNamespaceFunc reads deployment resource in the manifests and use that namespace
233+
// TemplateAgentRegistrationNamespaceFunc reads deployment/daemonset resources in the manifests and use that namespace
234234
// as the default registration namespace. If addonDeploymentConfig is set, uses the namespace in it.
235235
func (a *CRDTemplateAgentAddon) TemplateAgentRegistrationNamespaceFunc(
236236
addon *addonapiv1alpha1.ManagedClusterAddOn) (string, error) {
@@ -242,21 +242,33 @@ func (a *CRDTemplateAgentAddon) TemplateAgentRegistrationNamespaceFunc(
242242
return "", fmt.Errorf("addon %s template not found in status", addon.Name)
243243
}
244244

245-
// pick the namespace of the first deployment
245+
// pick the namespace of the first deployment, if there is no deployment, pick the namespace of the first daemonset
246246
var desiredNS = "open-cluster-management-agent-addon"
247+
var firstDeploymentNamespace, firstDaemonSetNamespace string
247248
for _, manifest := range template.Spec.AgentSpec.Workload.Manifests {
248249
object := &unstructured.Unstructured{}
249250
if err := object.UnmarshalJSON(manifest.Raw); err != nil {
250251
a.logger.Error(err, "failed to extract the object")
251252
continue
252253
}
253254

254-
if _, err = utils.ConvertToDeployment(object); err != nil {
255-
continue
255+
if firstDeploymentNamespace == "" {
256+
if _, err = utils.ConvertToDeployment(object); err == nil {
257+
firstDeploymentNamespace = object.GetNamespace()
258+
break
259+
}
260+
}
261+
if firstDaemonSetNamespace == "" {
262+
if _, err = utils.ConvertToDaemonSet(object); err == nil {
263+
firstDaemonSetNamespace = object.GetNamespace()
264+
}
256265
}
266+
}
257267

258-
desiredNS = object.GetNamespace()
259-
break
268+
if firstDeploymentNamespace != "" {
269+
desiredNS = firstDeploymentNamespace
270+
} else if firstDaemonSetNamespace != "" {
271+
desiredNS = firstDaemonSetNamespace
260272
}
261273

262274
overrideNs, err := utils.AgentInstallNamespaceFromDeploymentConfigFunc(

0 commit comments

Comments
 (0)