-
Notifications
You must be signed in to change notification settings - Fork 40
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 annotation if addon uses InstallStrategy #236
Merged
openshift-merge-bot
merged 2 commits into
open-cluster-management-io:main
from
haoqing0110:br_force-annotation
Feb 2, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
95 changes: 95 additions & 0 deletions
95
pkg/addonmanager/controllers/managementaddon/controller.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,95 @@ | ||
package managementaddon | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/tools/cache" | ||
addonapiv1alpha1 "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/sdk-go/pkg/patcher" | ||
|
||
"open-cluster-management.io/addon-framework/pkg/agent" | ||
"open-cluster-management.io/addon-framework/pkg/basecontroller/factory" | ||
) | ||
|
||
const ( | ||
controllerName = "management-addon-controller" | ||
) | ||
|
||
// clusterManagementAddonController reconciles cma on the hub. | ||
type clusterManagementAddonController struct { | ||
addonClient addonv1alpha1client.Interface | ||
clusterManagementAddonLister addonlisterv1alpha1.ClusterManagementAddOnLister | ||
agentAddons map[string]agent.AgentAddon | ||
addonFilterFunc factory.EventFilterFunc | ||
addonPatcher patcher.Patcher[*addonapiv1alpha1.ClusterManagementAddOn, | ||
addonapiv1alpha1.ClusterManagementAddOnSpec, | ||
addonapiv1alpha1.ClusterManagementAddOnStatus] | ||
} | ||
|
||
func NewManagementAddonController( | ||
addonClient addonv1alpha1client.Interface, | ||
clusterManagementAddonInformers addoninformerv1alpha1.ClusterManagementAddOnInformer, | ||
agentAddons map[string]agent.AgentAddon, | ||
addonFilterFunc factory.EventFilterFunc, | ||
) factory.Controller { | ||
syncCtx := factory.NewSyncContext(controllerName) | ||
|
||
c := &clusterManagementAddonController{ | ||
addonClient: addonClient, | ||
clusterManagementAddonLister: clusterManagementAddonInformers.Lister(), | ||
agentAddons: agentAddons, | ||
addonFilterFunc: addonFilterFunc, | ||
addonPatcher: patcher.NewPatcher[*addonapiv1alpha1.ClusterManagementAddOn, | ||
addonapiv1alpha1.ClusterManagementAddOnSpec, | ||
addonapiv1alpha1.ClusterManagementAddOnStatus](addonClient.AddonV1alpha1().ClusterManagementAddOns()), | ||
} | ||
|
||
return factory.New(). | ||
WithSyncContext(syncCtx). | ||
WithFilteredEventsInformersQueueKeysFunc( | ||
func(obj runtime.Object) []string { | ||
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) | ||
return []string{key} | ||
}, | ||
c.addonFilterFunc, clusterManagementAddonInformers.Informer()). | ||
WithSync(c.sync).ToController(controllerName) | ||
} | ||
|
||
func (c *clusterManagementAddonController) sync(ctx context.Context, syncCtx factory.SyncContext, key string) error { | ||
_, addonName, err := cache.SplitMetaNamespaceKey(key) | ||
if err != nil { | ||
// ignore addon whose key is invalid | ||
return nil | ||
} | ||
|
||
cma, err := c.clusterManagementAddonLister.Get(addonName) | ||
if errors.IsNotFound(err) { | ||
// addon cloud be deleted, ignore | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
addon := c.agentAddons[cma.GetName()] | ||
if addon.GetAgentAddonOptions().InstallStrategy == nil { | ||
return nil | ||
} | ||
|
||
// If the addon defines install strategy via WithInstallStrategy(), force add annotation "addon.open-cluster-management.io/lifecycle: self" to cma. | ||
// The annotation with value "self" will be removed when remove WithInstallStrategy() in addon-framework. | ||
// 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{} | ||
} | ||
cmaCopy.Annotations[addonapiv1alpha1.AddonLifecycleAnnotationKey] = addonapiv1alpha1.AddonLifecycleSelfManageAnnotationValue | ||
|
||
_, err = c.addonPatcher.PatchLabelAnnotations(ctx, cmaCopy, cmaCopy.ObjectMeta, cma.ObjectMeta) | ||
return err | ||
} |
151 changes: 151 additions & 0 deletions
151
pkg/addonmanager/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,151 @@ | ||
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" | ||
"open-cluster-management.io/addon-framework/pkg/utils" | ||
addonapiv1alpha1 "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" | ||
"open-cluster-management.io/sdk-go/pkg/patcher" | ||
) | ||
|
||
type testAgent struct { | ||
name string | ||
strategy *agent.InstallStrategy | ||
} | ||
|
||
func (t *testAgent) Manifests(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.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) *addonapiv1alpha1.ClusterManagementAddOn { | ||
cma := addontesting.NewClusterManagementAddon(name, "", "").Build() | ||
cma.Annotations = annotations | ||
return cma | ||
} | ||
|
||
func TestReconcile(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
cma []runtime.Object | ||
testaddons map[string]agent.AgentAddon | ||
validateAddonActions func(t *testing.T, actions []clienttesting.Action) | ||
}{ | ||
{ | ||
name: "add annotation when uses install strategy", | ||
cma: []runtime.Object{newClusterManagementAddonWithAnnotation("test", map[string]string{ | ||
"test": "test", | ||
})}, | ||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) { | ||
addontesting.AssertActions(t, actions, "patch") | ||
patch := actions[0].(clienttesting.PatchActionImpl).Patch | ||
cma := &addonapiv1alpha1.ClusterManagementAddOn{} | ||
err := json.Unmarshal(patch, cma) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(cma.Annotations) != 1 || cma.Annotations[addonapiv1alpha1.AddonLifecycleAnnotationKey] != addonapiv1alpha1.AddonLifecycleSelfManageAnnotationValue { | ||
t.Errorf("cma annotation is not correct, expected self but got %s", cma.Annotations[addonapiv1alpha1.AddonLifecycleAnnotationKey]) | ||
} | ||
}, | ||
testaddons: map[string]agent.AgentAddon{ | ||
"test": &testAgent{name: "test", strategy: agent.InstallAllStrategy("test")}, | ||
}, | ||
}, | ||
{ | ||
name: "override annotation when uses install strategy", | ||
cma: []runtime.Object{newClusterManagementAddonWithAnnotation("test", map[string]string{ | ||
"test": "test", | ||
addonapiv1alpha1.AddonLifecycleAnnotationKey: addonapiv1alpha1.AddonLifecycleAddonManagerAnnotationValue, | ||
})}, | ||
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) { | ||
addontesting.AssertActions(t, actions, "patch") | ||
patch := actions[0].(clienttesting.PatchActionImpl).Patch | ||
cma := &addonapiv1alpha1.ClusterManagementAddOn{} | ||
err := json.Unmarshal(patch, cma) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(cma.Annotations) != 1 || cma.Annotations[addonapiv1alpha1.AddonLifecycleAnnotationKey] != addonapiv1alpha1.AddonLifecycleSelfManageAnnotationValue { | ||
t.Errorf("cma annotation is not correct, expected self but got %s", cma.Annotations[addonapiv1alpha1.AddonLifecycleAnnotationKey]) | ||
} | ||
}, | ||
testaddons: map[string]agent.AgentAddon{ | ||
"test": &testAgent{name: "test", strategy: agent.InstallAllStrategy("test")}, | ||
}, | ||
}, | ||
{ | ||
name: "no patch annotation if managed by self", | ||
cma: []runtime.Object{newClusterManagementAddonWithAnnotation("test", map[string]string{ | ||
"test": "test", | ||
addonapiv1alpha1.AddonLifecycleAnnotationKey: addonapiv1alpha1.AddonLifecycleSelfManageAnnotationValue, | ||
})}, | ||
validateAddonActions: addontesting.AssertNoActions, | ||
testaddons: map[string]agent.AgentAddon{ | ||
"test": &testAgent{name: "test", strategy: agent.InstallAllStrategy("test")}, | ||
}, | ||
}, | ||
{ | ||
name: "no patch annotation if no install strategy", | ||
cma: []runtime.Object{newClusterManagementAddonWithAnnotation("test", map[string]string{ | ||
"test": "test", | ||
addonapiv1alpha1.AddonLifecycleAnnotationKey: addonapiv1alpha1.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) | ||
} | ||
} | ||
|
||
controller := clusterManagementAddonController{ | ||
addonClient: fakeAddonClient, | ||
clusterManagementAddonLister: addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Lister(), | ||
agentAddons: c.testaddons, | ||
addonFilterFunc: utils.FilterByAddonName(c.testaddons), | ||
addonPatcher: patcher.NewPatcher[*addonapiv1alpha1.ClusterManagementAddOn, | ||
addonapiv1alpha1.ClusterManagementAddOnSpec, | ||
addonapiv1alpha1.ClusterManagementAddOnStatus](fakeAddonClient.AddonV1alpha1().ClusterManagementAddOns()), | ||
} | ||
|
||
for _, obj := range c.cma { | ||
cma := obj.(*addonapiv1alpha1.ClusterManagementAddOn) | ||
syncContext := addontesting.NewFakeSyncContext(t) | ||
err := controller.sync(context.TODO(), syncContext, cma.Name) | ||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note, we will need users to update the clusterrole when upgrading the library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be reflected in the release notes.