Skip to content

Commit

Permalink
Set the first daemonset namespace to registration namespace (#536)
Browse files Browse the repository at this point in the history
Signed-off-by: zhujian <[email protected]>
  • Loading branch information
zhujian7 authored Jun 21, 2024
1 parent 21f3399 commit 41197e5
Show file tree
Hide file tree
Showing 5 changed files with 566 additions and 128 deletions.
4 changes: 2 additions & 2 deletions pkg/addon/templateagent/decorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func newDaemonSetDecorator(

func (d *daemonSetDecorator) decorate(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
daemonSet, err := utils.ConvertToDaemonSet(obj)
// not a deployment, directly return
// not a daemonset, directly return
if err != nil {
return obj, nil
}
Expand All @@ -168,7 +168,7 @@ func (d *daemonSetDecorator) decorate(obj *unstructured.Unstructured) (*unstruct
}

type podTemplateSpecDecorator interface {
// decorate modifies the deployment in place
// decorate modifies the pod template in place
decorate(pod *corev1.PodTemplateSpec) error
}

Expand Down
24 changes: 18 additions & 6 deletions pkg/addon/templateagent/template_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (a *CRDTemplateAgentAddon) getDesiredAddOnTemplateInner(
return template.DeepCopy(), nil
}

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

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

if _, err = utils.ConvertToDeployment(object); err != nil {
continue
if firstDeploymentNamespace == "" {
if _, err = utils.ConvertToDeployment(object); err == nil {
firstDeploymentNamespace = object.GetNamespace()
break
}
}
if firstDaemonSetNamespace == "" {
if _, err = utils.ConvertToDaemonSet(object); err == nil {
firstDaemonSetNamespace = object.GetNamespace()
}
}
}

desiredNS = object.GetNamespace()
break
if firstDeploymentNamespace != "" {
desiredNS = firstDeploymentNamespace
} else if firstDaemonSetNamespace != "" {
desiredNS = firstDaemonSetNamespace
}

overrideNs, err := utils.AgentInstallNamespaceFromDeploymentConfigFunc(
Expand Down
Loading

0 comments on commit 41197e5

Please sign in to comment.