Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new AgentInstallNamespace option for addon registration #204

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/addonmanager/controllers/registration/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ func (c *addonRegistrationController) sync(ctx context.Context, syncCtx factory.
managedClusterAddonCopy.Status.Namespace = managedClusterAddonCopy.Spec.InstallNamespace
}

if registrationOption.AgentInstallNamespace != nil {
ns := registrationOption.AgentInstallNamespace(managedClusterAddonCopy)
if len(ns) > 0 {
managedClusterAddonCopy.Status.Namespace = ns
} else {
klog.Infof("Namespace for addon %s/%s returned by agent install namespace func is empty",
managedClusterAddonCopy.Namespace, managedClusterAddonCopy.Name)
}
}

meta.SetStatusCondition(&managedClusterAddonCopy.Status.Conditions, metav1.Condition{
Type: addonapiv1alpha1.ManagedClusterAddOnRegistrationApplied,
Status: metav1.ConditionTrue,
Expand Down
47 changes: 43 additions & 4 deletions pkg/addonmanager/controllers/registration/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
)

type testAgent struct {
name string
namespace string
registrations []addonapiv1alpha1.RegistrationConfig
name string
namespace string
registrations []addonapiv1alpha1.RegistrationConfig
agentInstallNamespace func(addon *addonapiv1alpha1.ManagedClusterAddOn) string
}

func (t *testAgent) Manifests(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) ([]runtime.Object, error) {
Expand All @@ -38,7 +39,7 @@ func (t *testAgent) GetAgentAddonOptions() agent.AgentAddonOptions {
AddonName: t.name,
}
}
return agent.AgentAddonOptions{
agentOption := agent.AgentAddonOptions{
AddonName: t.name,
Registration: &agent.RegistrationOption{
CSRConfigurations: func(cluster *clusterv1.ManagedCluster) []addonapiv1alpha1.RegistrationConfig {
Expand All @@ -50,6 +51,11 @@ func (t *testAgent) GetAgentAddonOptions() agent.AgentAddonOptions {
Namespace: t.namespace,
},
}

if t.agentInstallNamespace != nil {
agentOption.Registration.AgentInstallNamespace = t.agentInstallNamespace
}
return agentOption
}

func TestReconcile(t *testing.T) {
Expand Down Expand Up @@ -176,6 +182,39 @@ func TestReconcile(t *testing.T) {
},
}},
},
{
name: "with registrations and override namespace by agentInstallNamespace",
cluster: []runtime.Object{addontesting.NewManagedCluster("cluster1")},
addon: []runtime.Object{
func() *addonapiv1alpha1.ManagedClusterAddOn {
addon := addontesting.NewAddon("test", "cluster1", metav1.OwnerReference{
Kind: "ClusterManagementAddOn",
Name: "test",
})
addon.Spec.InstallNamespace = "default2"
return addon
}(),
},
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
addontesting.AssertActions(t, actions, "patch")
actual := actions[0].(clienttesting.PatchActionImpl).Patch
addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
err := json.Unmarshal(actual, addOn)
if err != nil {
t.Fatal(err)
}
if addOn.Status.Registrations[0].SignerName != "test" {
t.Errorf("Registration config is not updated")
}
if addOn.Status.Namespace != "default3" {
t.Errorf("Namespace in status is not correct")
}
},
testaddon: &testAgent{name: "test", namespace: "default",
registrations: []addonapiv1alpha1.RegistrationConfig{{SignerName: "test"}},
agentInstallNamespace: func(addon *addonapiv1alpha1.ManagedClusterAddOn) string { return "default3" },
},
},
}

for _, c := range cases {
Expand Down
8 changes: 8 additions & 0 deletions pkg/agent/inteface.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,16 @@ type RegistrationOption struct {

// Namespace is the namespace where registraiton credential will be put on the managed cluster. It
// will be overridden by installNamespace on ManagedClusterAddon spec if set
// Deprecated: use AgentInstallNamespace instead
Namespace string

// AgentInstallNamespace returns the namespace where registration credential will be put on the managed cluster.
// It will override the installNamespace on ManagedClusterAddon spec if set and the returned value is not empty.
// Note: Set this very carefully. If this is set, the addon agent must be deployed in the same namespace, which
// means when implementing Manifests function in AgentAddon interface, the namespace of the addon agent manifest
// must be set to the same value returned by this function.
AgentInstallNamespace func(addon *addonapiv1alpha1.ManagedClusterAddOn) string

// CSRApproveCheck checks whether the addon agent registration should be approved by the hub.
// Addon hub controller can implement this func to auto-approve all the CSRs. A better CSR check is
// recommended to include (1) the validity of requester's requesting identity and (2) the other request
Expand Down
Loading