-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set cma managed by addon-manager if not configured
Signed-off-by: haoqing0110 <qhao@redhat.com>
- Loading branch information
1 parent
b1b734a
commit 1f99fa2
Showing
4 changed files
with
232 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package managementaddon | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/klog/v2" | ||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" | ||
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned" | ||
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1" | ||
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1" | ||
"open-cluster-management.io/ocm/pkg/common/queue" | ||
"open-cluster-management.io/sdk-go/pkg/patcher" | ||
|
||
"github.com/openshift/library-go/pkg/controller/factory" | ||
"github.com/openshift/library-go/pkg/operator/events" | ||
) | ||
|
||
// clusterManagementAddonController reconciles cma on the hub. | ||
type clusterManagementAddonController struct { | ||
patcher patcher.Patcher[ | ||
*addonv1alpha1.ClusterManagementAddOn, addonv1alpha1.ClusterManagementAddOnSpec, addonv1alpha1.ClusterManagementAddOnStatus] | ||
clusterManagementAddonLister addonlisterv1alpha1.ClusterManagementAddOnLister | ||
} | ||
|
||
func NewManagementAddonController( | ||
addonClient addonv1alpha1client.Interface, | ||
clusterManagementAddonInformers addoninformerv1alpha1.ClusterManagementAddOnInformer, | ||
recorder events.Recorder, | ||
) factory.Controller { | ||
c := &clusterManagementAddonController{ | ||
patcher: patcher.NewPatcher[ | ||
*addonv1alpha1.ClusterManagementAddOn, addonv1alpha1.ClusterManagementAddOnSpec, addonv1alpha1.ClusterManagementAddOnStatus]( | ||
addonClient.AddonV1alpha1().ClusterManagementAddOns()), | ||
clusterManagementAddonLister: clusterManagementAddonInformers.Lister(), | ||
} | ||
|
||
return factory.New().WithInformersQueueKeysFunc( | ||
queue.QueueKeyByMetaName, | ||
clusterManagementAddonInformers.Informer()). | ||
WithSync(c.sync).ToController("management-addon-controller", recorder) | ||
|
||
} | ||
|
||
func (c *clusterManagementAddonController) sync(ctx context.Context, syncCtx factory.SyncContext) error { | ||
addonName := syncCtx.QueueKey() | ||
logger := klog.FromContext(ctx) | ||
logger.V(4).Info("Reconciling addon", "addonName", addonName) | ||
|
||
cma, err := c.clusterManagementAddonLister.Get(addonName) | ||
switch { | ||
case errors.IsNotFound(err): | ||
return nil | ||
case err != nil: | ||
return err | ||
} | ||
|
||
// If cma annotation "addon.open-cluster-management.io/lifecycle: self" is not set, | ||
// force add "addon.open-cluster-management.io/lifecycle: addon-manager" . | ||
// The migration plan refer to https://github.com/open-cluster-management-io/ocm/issues/355. | ||
cmaCopy := cma.DeepCopy() | ||
if cmaCopy.Annotations == nil { | ||
cmaCopy.Annotations = map[string]string{} | ||
} | ||
if cmaCopy.Annotations[addonv1alpha1.AddonLifecycleAnnotationKey] != addonv1alpha1.AddonLifecycleSelfManageAnnotationValue { | ||
cmaCopy.Annotations[addonv1alpha1.AddonLifecycleAnnotationKey] = addonv1alpha1.AddonLifecycleAddonManagerAnnotationValue | ||
} | ||
|
||
_, err = c.patcher.PatchLabelAnnotations(ctx, cmaCopy, cmaCopy.ObjectMeta, cma.ObjectMeta) | ||
return err | ||
} |
149 changes: 149 additions & 0 deletions
149
pkg/addon/controllers/managementaddon/controller_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package managementaddon | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
clienttesting "k8s.io/client-go/testing" | ||
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting" | ||
"open-cluster-management.io/addon-framework/pkg/agent" | ||
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" | ||
fakeaddon "open-cluster-management.io/api/client/addon/clientset/versioned/fake" | ||
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions" | ||
clusterv1 "open-cluster-management.io/api/cluster/v1" | ||
testingcommon "open-cluster-management.io/ocm/pkg/common/testing" | ||
) | ||
|
||
type testAgent struct { | ||
name string | ||
strategy *agent.InstallStrategy | ||
} | ||
|
||
func (t *testAgent) Manifests(cluster *clusterv1.ManagedCluster, addon *addonv1alpha1.ManagedClusterAddOn) ([]runtime.Object, error) { | ||
return nil, nil | ||
} | ||
|
||
func (t *testAgent) GetAgentAddonOptions() agent.AgentAddonOptions { | ||
return agent.AgentAddonOptions{ | ||
AddonName: t.name, | ||
InstallStrategy: t.strategy, | ||
} | ||
} | ||
|
||
func newClusterManagementAddonWithAnnotation(name string, annotations map[string]string) *addonv1alpha1.ClusterManagementAddOn { | ||
cma := addontesting.NewClusterManagementAddon(name, "", "").Build() | ||
cma.Annotations = annotations | ||
return cma | ||
} | ||
|
||
func TestReconcile(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
syncKey string | ||
cma []runtime.Object | ||
testaddons map[string]agent.AgentAddon | ||
validateAddonActions func(t *testing.T, actions []clienttesting.Action) | ||
}{ | ||
{ | ||
name: "add annotation if no annotation", | ||
syncKey: "test", | ||
cma: []runtime.Object{newClusterManagementAddonWithAnnotation("test", nil)}, | ||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) { | ||
addontesting.AssertActions(t, actions, "patch") | ||
patch := actions[0].(clienttesting.PatchActionImpl).Patch | ||
cma := &addonv1alpha1.ClusterManagementAddOn{} | ||
err := json.Unmarshal(patch, cma) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(cma.Annotations) != 1 || cma.Annotations[addonv1alpha1.AddonLifecycleAnnotationKey] != addonv1alpha1.AddonLifecycleAddonManagerAnnotationValue { | ||
t.Errorf("cma annotation is not correct, expected addon-manager but got %s", cma.Annotations[addonv1alpha1.AddonLifecycleAnnotationKey]) | ||
} | ||
}, | ||
testaddons: map[string]agent.AgentAddon{ | ||
"test": &testAgent{name: "test", strategy: agent.InstallAllStrategy("test")}, | ||
}, | ||
}, | ||
{ | ||
name: "add annotation if addon.open-cluster-management.io/lifecycle is empty", | ||
syncKey: "test", | ||
cma: []runtime.Object{newClusterManagementAddonWithAnnotation("test", map[string]string{ | ||
"test": "test", | ||
addonv1alpha1.AddonLifecycleAnnotationKey: "", | ||
})}, | ||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) { | ||
addontesting.AssertActions(t, actions, "patch") | ||
patch := actions[0].(clienttesting.PatchActionImpl).Patch | ||
cma := &addonv1alpha1.ClusterManagementAddOn{} | ||
err := json.Unmarshal(patch, cma) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(cma.Annotations) != 1 || cma.Annotations[addonv1alpha1.AddonLifecycleAnnotationKey] != addonv1alpha1.AddonLifecycleAddonManagerAnnotationValue { | ||
t.Errorf("cma annotation is not correct, expected addon-manager but got %s", cma.Annotations[addonv1alpha1.AddonLifecycleAnnotationKey]) | ||
} | ||
}, | ||
testaddons: map[string]agent.AgentAddon{ | ||
"test": &testAgent{name: "test", strategy: agent.InstallAllStrategy("test")}, | ||
}, | ||
}, | ||
{ | ||
name: "no patch annotation if managed by self", | ||
syncKey: "test", | ||
cma: []runtime.Object{newClusterManagementAddonWithAnnotation("test", map[string]string{ | ||
"test": "test", | ||
addonv1alpha1.AddonLifecycleAnnotationKey: addonv1alpha1.AddonLifecycleSelfManageAnnotationValue, | ||
})}, | ||
validateAddonActions: addontesting.AssertNoActions, | ||
testaddons: map[string]agent.AgentAddon{ | ||
"test": &testAgent{name: "test", strategy: agent.InstallAllStrategy("test")}, | ||
}, | ||
}, | ||
{ | ||
name: "no patch annotation if managed by addon-manager", | ||
syncKey: "test", | ||
cma: []runtime.Object{newClusterManagementAddonWithAnnotation("test", map[string]string{ | ||
"test": "test", | ||
addonv1alpha1.AddonLifecycleAnnotationKey: addonv1alpha1.AddonLifecycleAddonManagerAnnotationValue, | ||
})}, | ||
validateAddonActions: addontesting.AssertNoActions, | ||
testaddons: map[string]agent.AgentAddon{ | ||
"test": &testAgent{name: "test"}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
fakeAddonClient := fakeaddon.NewSimpleClientset(c.cma...) | ||
addonInformers := addoninformers.NewSharedInformerFactory(fakeAddonClient, 10*time.Minute) | ||
|
||
for _, obj := range c.cma { | ||
if err := addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer().GetStore().Add(obj); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
syncContext := testingcommon.NewFakeSyncContext(t, c.syncKey) | ||
recorder := syncContext.Recorder() | ||
|
||
controller := NewManagementAddonController( | ||
fakeAddonClient, | ||
addonInformers.Addon().V1alpha1().ClusterManagementAddOns(), | ||
recorder, | ||
) | ||
|
||
err := controller.Sync(context.TODO(), syncContext) | ||
if err != nil { | ||
t.Errorf("expected no error when sync: %v", err) | ||
} | ||
c.validateAddonActions(t, fakeAddonClient.Actions()) | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters