From a9162b6a73ff10bee8259438376dc7c3d13d5e6b Mon Sep 17 00:00:00 2001 From: morvencao Date: Sat, 20 Apr 2024 02:45:50 +0000 Subject: [PATCH] update addon manager. Signed-off-by: morvencao --- go.mod | 4 +- go.sum | 8 +- .../controllers/addontemplate/controller.go | 6 +- .../addontemplate/controller_test.go | 1 + pkg/addon/manager.go | 3 + vendor/modules.txt | 4 +- .../pkg/addonmanager/base_manager.go | 200 +++++++++++++++++ .../pkg/addonmanager/interface.go | 42 ++++ .../pkg/addonmanager/manager.go | 209 +----------------- .../work/source/client/manifestwork.go | 28 +-- 10 files changed, 280 insertions(+), 225 deletions(-) create mode 100644 vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/base_manager.go create mode 100644 vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/interface.go diff --git a/go.mod b/go.mod index 40457b3a1..48b8940d7 100644 --- a/go.mod +++ b/go.mod @@ -34,9 +34,9 @@ require ( k8s.io/klog/v2 v2.120.1 k8s.io/kube-aggregator v0.29.3 k8s.io/utils v0.0.0-20240310230437-4693a0247e57 - open-cluster-management.io/addon-framework v0.9.1-0.20240416063208-ecb7f349df05 + open-cluster-management.io/addon-framework v0.9.1-0.20240419070222-e703fc5a2556 open-cluster-management.io/api v0.13.1-0.20240411131856-8f6aa25f111c - open-cluster-management.io/sdk-go v0.13.1-0.20240416030555-aa744f426379 + open-cluster-management.io/sdk-go v0.13.1-0.20240416062924-20307e6fe090 sigs.k8s.io/controller-runtime v0.17.3 sigs.k8s.io/kube-storage-version-migrator v0.0.6-0.20230721195810-5c8923c5ff96 ) diff --git a/go.sum b/go.sum index bf5a039d3..f3b3c3c33 100644 --- a/go.sum +++ b/go.sum @@ -423,12 +423,12 @@ k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/A k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA= k8s.io/utils v0.0.0-20240310230437-4693a0247e57 h1:gbqbevonBh57eILzModw6mrkbwM0gQBEuevE/AaBsHY= k8s.io/utils v0.0.0-20240310230437-4693a0247e57/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= -open-cluster-management.io/addon-framework v0.9.1-0.20240416063208-ecb7f349df05 h1:oj4IEyxmQFMxdpnOPE/Y7Xkyfkz4hGVkcYZ4dn8WtKU= -open-cluster-management.io/addon-framework v0.9.1-0.20240416063208-ecb7f349df05/go.mod h1:K+/TwCUz2PZ7L+svaodw6029S/iPJ7eVX21F7NMw+Kw= +open-cluster-management.io/addon-framework v0.9.1-0.20240419070222-e703fc5a2556 h1:X3vJEx9agC94l7SitpWZFDshISdL1niqVH0+diyqfJo= +open-cluster-management.io/addon-framework v0.9.1-0.20240419070222-e703fc5a2556/go.mod h1:HayKCznnlyW+0dUJQGj5sNR6i3tvylSySD3YnvZkBtY= open-cluster-management.io/api v0.13.1-0.20240411131856-8f6aa25f111c h1:/iUoY6/PqBmcBq3v0+UBFvIcI39k/QPRGqpOv9XtDIc= open-cluster-management.io/api v0.13.1-0.20240411131856-8f6aa25f111c/go.mod h1:CuCPEzXDvOyxBB0H1d1eSeajbHqaeGEKq9c63vQc63w= -open-cluster-management.io/sdk-go v0.13.1-0.20240416030555-aa744f426379 h1:8jXVHfgy+wgXq1mrWC1mTieoP77WsAAHNpzILMIzWB0= -open-cluster-management.io/sdk-go v0.13.1-0.20240416030555-aa744f426379/go.mod h1:w2OaxtCyegxeyFLU42UQ3oxUz01QdsBQkcHI17T/l48= +open-cluster-management.io/sdk-go v0.13.1-0.20240416062924-20307e6fe090 h1:zFmHuW+ztdfUUNslqNW+H1WEcfdEUQHoRDbmdajX340= +open-cluster-management.io/sdk-go v0.13.1-0.20240416062924-20307e6fe090/go.mod h1:w2OaxtCyegxeyFLU42UQ3oxUz01QdsBQkcHI17T/l48= sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.28.0 h1:TgtAeesdhpm2SGwkQasmbeqDo8th5wOBA5h/AjTKA4I= sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.28.0/go.mod h1:VHVDI/KrK4fjnV61bE2g3sA7tiETLn8sooImelsCx3Y= sigs.k8s.io/controller-runtime v0.17.3 h1:65QmN7r3FWgTxDMz9fvGnO1kbf2nu+acg9p2R9oYYYk= diff --git a/pkg/addon/controllers/addontemplate/controller.go b/pkg/addon/controllers/addontemplate/controller.go index 9d9968c28..a6b39c2ef 100644 --- a/pkg/addon/controllers/addontemplate/controller.go +++ b/pkg/addon/controllers/addontemplate/controller.go @@ -24,6 +24,7 @@ import ( addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions" addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1" clusterv1informers "open-cluster-management.io/api/client/cluster/informers/externalversions" + workv1client "open-cluster-management.io/api/client/work/clientset/versioned" workv1informers "open-cluster-management.io/api/client/work/informers/externalversions" clusterv1 "open-cluster-management.io/api/cluster/v1" @@ -40,6 +41,7 @@ type addonTemplateController struct { kubeConfig *rest.Config addonClient addonv1alpha1client.Interface + workClient workv1client.Interface kubeClient kubernetes.Interface cmaLister addonlisterv1alpha1.ClusterManagementAddOnLister addonInformers addoninformers.SharedInformerFactory @@ -56,6 +58,7 @@ func NewAddonTemplateController( hubKubeconfig *rest.Config, hubKubeClient kubernetes.Interface, addonClient addonv1alpha1client.Interface, + workClient workv1client.Interface, addonInformers addoninformers.SharedInformerFactory, clusterInformers clusterv1informers.SharedInformerFactory, dynamicInformers dynamicinformer.DynamicSharedInformerFactory, @@ -67,6 +70,7 @@ func NewAddonTemplateController( kubeConfig: hubKubeconfig, kubeClient: hubKubeClient, addonClient: addonClient, + workClient: workClient, cmaLister: addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Lister(), addonManagers: make(map[string]context.CancelFunc), addonInformers: addonInformers, @@ -205,7 +209,7 @@ func (c *addonTemplateController) runController( return err } - err = mgr.StartWithInformers(ctx, kubeInformers, c.workInformers, c.addonInformers, c.clusterInformers, c.dynamicInformers) + err = mgr.StartWithInformers(ctx, c.workClient, c.workInformers.Work().V1().ManifestWorks(), kubeInformers, c.addonInformers, c.clusterInformers, c.dynamicInformers) if err != nil { return err } diff --git a/pkg/addon/controllers/addontemplate/controller_test.go b/pkg/addon/controllers/addontemplate/controller_test.go index b090ab120..aa796c1a4 100644 --- a/pkg/addon/controllers/addontemplate/controller_test.go +++ b/pkg/addon/controllers/addontemplate/controller_test.go @@ -176,6 +176,7 @@ func TestReconcile(t *testing.T) { nil, hubKubeClient, fakeAddonClient, + fakeWorkClient, addonInformers, clusterInformers, dynamicInformerFactory, diff --git a/pkg/addon/manager.go b/pkg/addon/manager.go index 2f033c24c..8f20d1283 100644 --- a/pkg/addon/manager.go +++ b/pkg/addon/manager.go @@ -79,6 +79,7 @@ func RunManager(ctx context.Context, controllerContext *controllercmd.Controller ctx, controllerContext, hubKubeClient, addonClient, + workClient, clusterInformerFactory, addonInformerFactory, workInformers, @@ -91,6 +92,7 @@ func RunControllerManagerWithInformers( controllerContext *controllercmd.ControllerContext, hubKubeClient kubernetes.Interface, hubAddOnClient addonv1alpha1client.Interface, + hubWorkClient workv1client.Interface, clusterInformers clusterinformers.SharedInformerFactory, addonInformers addoninformers.SharedInformerFactory, workinformers workv1informers.SharedInformerFactory, @@ -187,6 +189,7 @@ func RunControllerManagerWithInformers( controllerContext.KubeConfig, hubKubeClient, hubAddOnClient, + hubWorkClient, addonInformers, clusterInformers, dynamicInformers, diff --git a/vendor/modules.txt b/vendor/modules.txt index 442383842..24ce8cd97 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1489,7 +1489,7 @@ k8s.io/utils/pointer k8s.io/utils/ptr k8s.io/utils/strings/slices k8s.io/utils/trace -# open-cluster-management.io/addon-framework v0.9.1-0.20240416063208-ecb7f349df05 +# open-cluster-management.io/addon-framework v0.9.1-0.20240419070222-e703fc5a2556 ## explicit; go 1.21 open-cluster-management.io/addon-framework/pkg/addonfactory open-cluster-management.io/addon-framework/pkg/addonmanager @@ -1578,7 +1578,7 @@ open-cluster-management.io/api/utils/work/v1/workapplier open-cluster-management.io/api/utils/work/v1/workvalidator open-cluster-management.io/api/work/v1 open-cluster-management.io/api/work/v1alpha1 -# open-cluster-management.io/sdk-go v0.13.1-0.20240416030555-aa744f426379 +# open-cluster-management.io/sdk-go v0.13.1-0.20240416062924-20307e6fe090 ## explicit; go 1.21 open-cluster-management.io/sdk-go/pkg/apis/cluster/v1alpha1 open-cluster-management.io/sdk-go/pkg/apis/cluster/v1beta1 diff --git a/vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/base_manager.go b/vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/base_manager.go new file mode 100644 index 000000000..5cc585935 --- /dev/null +++ b/vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/base_manager.go @@ -0,0 +1,200 @@ +package addonmanager + +import ( + "context" + "fmt" + + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic/dynamicinformer" + kubeinformers "k8s.io/client-go/informers" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned" + addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions" + clusterv1informers "open-cluster-management.io/api/client/cluster/informers/externalversions" + workclientset "open-cluster-management.io/api/client/work/clientset/versioned" + workv1informers "open-cluster-management.io/api/client/work/informers/externalversions/work/v1" + + "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/addonconfig" + "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/agentdeploy" + "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/certificate" + "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/cmaconfig" + "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/cmamanagedby" + "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/registration" + "open-cluster-management.io/addon-framework/pkg/agent" + "open-cluster-management.io/addon-framework/pkg/basecontroller/factory" + "open-cluster-management.io/addon-framework/pkg/utils" +) + +// BaseAddonManagerImpl is the base implementation of BaseAddonManager +// that manages the addon agents and configs. +type BaseAddonManagerImpl struct { + addonAgents map[string]agent.AgentAddon + addonConfigs map[schema.GroupVersionResource]bool + config *rest.Config + syncContexts []factory.SyncContext +} + +// NewBaseAddonManagerImpl creates a new BaseAddonManagerImpl instance with the given config. +func NewBaseAddonManagerImpl(config *rest.Config) *BaseAddonManagerImpl { + return &BaseAddonManagerImpl{ + config: config, + syncContexts: []factory.SyncContext{}, + addonConfigs: map[schema.GroupVersionResource]bool{}, + addonAgents: map[string]agent.AgentAddon{}, + } +} + +func (a *BaseAddonManagerImpl) GetConfig() *rest.Config { + return a.config +} + +func (a *BaseAddonManagerImpl) GetAddonAgents() map[string]agent.AgentAddon { + return a.addonAgents +} + +func (a *BaseAddonManagerImpl) AddAgent(addon agent.AgentAddon) error { + addonOption := addon.GetAgentAddonOptions() + if len(addonOption.AddonName) == 0 { + return fmt.Errorf("addon name should be set") + } + if _, ok := a.addonAgents[addonOption.AddonName]; ok { + return fmt.Errorf("an agent is added for the addon already") + } + a.addonAgents[addonOption.AddonName] = addon + return nil +} + +func (a *BaseAddonManagerImpl) Trigger(clusterName, addonName string) { + for _, syncContex := range a.syncContexts { + syncContex.Queue().Add(fmt.Sprintf("%s/%s", clusterName, addonName)) + } +} + +func (a *BaseAddonManagerImpl) StartWithInformers(ctx context.Context, + workClient workclientset.Interface, + workInformers workv1informers.ManifestWorkInformer, + kubeInformers kubeinformers.SharedInformerFactory, + addonInformers addoninformers.SharedInformerFactory, + clusterInformers clusterv1informers.SharedInformerFactory, + dynamicInformers dynamicinformer.DynamicSharedInformerFactory) error { + + kubeClient, err := kubernetes.NewForConfig(a.config) + if err != nil { + return err + } + + addonClient, err := addonv1alpha1client.NewForConfig(a.config) + if err != nil { + return err + } + + v1CSRSupported, v1beta1Supported, err := utils.IsCSRSupported(kubeClient) + if err != nil { + return err + } + + for _, agentImpl := range a.addonAgents { + for _, configGVR := range agentImpl.GetAgentAddonOptions().SupportedConfigGVRs { + a.addonConfigs[configGVR] = true + } + } + + deployController := agentdeploy.NewAddonDeployController( + workClient, + addonClient, + clusterInformers.Cluster().V1().ManagedClusters(), + addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), + workInformers, + a.addonAgents, + ) + + registrationController := registration.NewAddonRegistrationController( + addonClient, + clusterInformers.Cluster().V1().ManagedClusters(), + addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), + a.addonAgents, + ) + + // This controller is used during migrating addons to be managed by addon-manager. + // This should be removed when the migration is done. + // The migration plan refer to https://github.com/open-cluster-management-io/ocm/issues/355. + managementAddonController := cmamanagedby.NewCMAManagedByController( + addonClient, + addonInformers.Addon().V1alpha1().ClusterManagementAddOns(), + a.addonAgents, + utils.FilterByAddonName(a.addonAgents), + ) + + var addonConfigController, managementAddonConfigController factory.Controller + if len(a.addonConfigs) != 0 { + addonConfigController = addonconfig.NewAddonConfigController( + addonClient, + addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), + addonInformers.Addon().V1alpha1().ClusterManagementAddOns(), + dynamicInformers, + a.addonConfigs, + utils.FilterByAddonName(a.addonAgents), + ) + managementAddonConfigController = cmaconfig.NewCMAConfigController( + addonClient, + addonInformers.Addon().V1alpha1().ClusterManagementAddOns(), + dynamicInformers, + a.addonConfigs, + utils.FilterByAddonName(a.addonAgents), + ) + } + + var csrApproveController factory.Controller + var csrSignController factory.Controller + // Spawn the following controllers only if v1 CSR api is supported in the + // hub cluster. Under v1beta1 CSR api, all the CSR objects will be signed + // by the kube-controller-manager so custom CSR controller should be + // disabled to avoid conflict. + if v1CSRSupported { + csrApproveController = certificate.NewCSRApprovingController( + kubeClient, + clusterInformers.Cluster().V1().ManagedClusters(), + kubeInformers.Certificates().V1().CertificateSigningRequests(), + nil, + addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), + a.addonAgents, + ) + csrSignController = certificate.NewCSRSignController( + kubeClient, + clusterInformers.Cluster().V1().ManagedClusters(), + kubeInformers.Certificates().V1().CertificateSigningRequests(), + addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), + a.addonAgents, + ) + } else if v1beta1Supported { + csrApproveController = certificate.NewCSRApprovingController( + kubeClient, + clusterInformers.Cluster().V1().ManagedClusters(), + nil, + kubeInformers.Certificates().V1beta1().CertificateSigningRequests(), + addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), + a.addonAgents, + ) + } + + a.syncContexts = append(a.syncContexts, deployController.SyncContext()) + + go deployController.Run(ctx, 1) + go registrationController.Run(ctx, 1) + go managementAddonController.Run(ctx, 1) + + if addonConfigController != nil { + go addonConfigController.Run(ctx, 1) + } + if managementAddonConfigController != nil { + go managementAddonConfigController.Run(ctx, 1) + } + if csrApproveController != nil { + go csrApproveController.Run(ctx, 1) + } + if csrSignController != nil { + go csrSignController.Run(ctx, 1) + } + return nil +} diff --git a/vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/interface.go b/vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/interface.go new file mode 100644 index 000000000..5598a7701 --- /dev/null +++ b/vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/interface.go @@ -0,0 +1,42 @@ +package addonmanager + +import ( + "context" + + "k8s.io/client-go/dynamic/dynamicinformer" + kubeinformers "k8s.io/client-go/informers" + "open-cluster-management.io/addon-framework/pkg/agent" + addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions" + clusterv1informers "open-cluster-management.io/api/client/cluster/informers/externalversions" + workclientset "open-cluster-management.io/api/client/work/clientset/versioned" + workv1informers "open-cluster-management.io/api/client/work/informers/externalversions/work/v1" +) + +// BaseAddonManager is the interface to initialize a manager on hub to manage the addon +// agents on all managedcluster +type BaseAddonManager interface { + // AddAgent register an addon agent to the manager. + AddAgent(addon agent.AgentAddon) error + + // Trigger triggers a reconcile loop in the manager. Currently it + // only trigger the deploy controller. + Trigger(clusterName, addonName string) + + // StartWithInformers starts all registered addon agent with the given informers. + StartWithInformers(ctx context.Context, + workClient workclientset.Interface, + workInformers workv1informers.ManifestWorkInformer, + kubeInformers kubeinformers.SharedInformerFactory, + addonInformers addoninformers.SharedInformerFactory, + clusterInformers clusterv1informers.SharedInformerFactory, + dynamicInformers dynamicinformer.DynamicSharedInformerFactory) error +} + +// AddonManager is the interface based on BaseAddonManager to initialize a manager on hub +// to manage the addon agents and controllers +type AddonManager interface { + BaseAddonManager + + // Start starts all registered addon agents and controllers. + Start(ctx context.Context) error +} diff --git a/vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/manager.go b/vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/manager.go index 57953b722..f98a12310 100644 --- a/vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/manager.go +++ b/vendor/open-cluster-management.io/addon-framework/pkg/addonmanager/manager.go @@ -2,11 +2,9 @@ package addonmanager import ( "context" - "fmt" "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic" "k8s.io/client-go/dynamic/dynamicinformer" kubeinformers "k8s.io/client-go/informers" @@ -21,87 +19,37 @@ import ( workv1client "open-cluster-management.io/api/client/work/clientset/versioned" workv1informers "open-cluster-management.io/api/client/work/informers/externalversions" - "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/addonconfig" - "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/agentdeploy" - "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/certificate" - "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/cmaconfig" - "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/cmamanagedby" - "open-cluster-management.io/addon-framework/pkg/addonmanager/controllers/registration" - "open-cluster-management.io/addon-framework/pkg/agent" - "open-cluster-management.io/addon-framework/pkg/basecontroller/factory" "open-cluster-management.io/addon-framework/pkg/index" - "open-cluster-management.io/addon-framework/pkg/utils" ) -// AddonManager is the interface to initialize a manager on hub to manage the addon -// agents on all managedcluster -type AddonManager interface { - // AddAgent register an addon agent to the manager. - AddAgent(addon agent.AgentAddon) error - - // Trigger triggers a reconcile loop in the manager. Currently it - // only trigger the deploy controller. - Trigger(clusterName, addonName string) - - // Start starts all registered addon agent. - Start(ctx context.Context) error - - // StartWithInformers starts all registered addon agent with the given informers. - StartWithInformers(ctx context.Context, - kubeInformers kubeinformers.SharedInformerFactory, - workInformers workv1informers.SharedInformerFactory, - addonInformers addoninformers.SharedInformerFactory, - clusterInformers clusterv1informers.SharedInformerFactory, - dynamicInformers dynamicinformer.DynamicSharedInformerFactory) error -} - +// addonManager is the implementation of AddonManager with the base implementation. type addonManager struct { - addonAgents map[string]agent.AgentAddon - addonConfigs map[schema.GroupVersionResource]bool - config *rest.Config - syncContexts []factory.SyncContext -} - -func (a *addonManager) AddAgent(addon agent.AgentAddon) error { - addonOption := addon.GetAgentAddonOptions() - if len(addonOption.AddonName) == 0 { - return fmt.Errorf("addon name should be set") - } - if _, ok := a.addonAgents[addonOption.AddonName]; ok { - return fmt.Errorf("an agent is added for the addon already") - } - a.addonAgents[addonOption.AddonName] = addon - return nil -} - -func (a *addonManager) Trigger(clusterName, addonName string) { - for _, syncContex := range a.syncContexts { - syncContex.Queue().Add(fmt.Sprintf("%s/%s", clusterName, addonName)) - } + *BaseAddonManagerImpl } +// Start starts all registered addon agents and controllers. func (a *addonManager) Start(ctx context.Context) error { - kubeClient, err := kubernetes.NewForConfig(a.config) + kubeClient, err := kubernetes.NewForConfig(a.GetConfig()) if err != nil { return err } - workClient, err := workv1client.NewForConfig(a.config) + workClient, err := workv1client.NewForConfig(a.GetConfig()) if err != nil { return err } - dynamicClient, err := dynamic.NewForConfig(a.config) + dynamicClient, err := dynamic.NewForConfig(a.GetConfig()) if err != nil { return err } - addonClient, err := addonv1alpha1client.NewForConfig(a.config) + addonClient, err := addonv1alpha1client.NewForConfig(a.GetConfig()) if err != nil { return err } - clusterClient, err := clusterv1client.NewForConfig(a.config) + clusterClient, err := clusterv1client.NewForConfig(a.GetConfig()) if err != nil { return err } @@ -111,7 +59,7 @@ func (a *addonManager) Start(ctx context.Context) error { dynamicInformers := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, 10*time.Minute) var addonNames []string - for key := range a.addonAgents { + for key := range a.GetAddonAgents() { addonNames = append(addonNames, key) } kubeInformers := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, 10*time.Minute, @@ -176,7 +124,7 @@ func (a *addonManager) Start(ctx context.Context) error { return err } - err = a.StartWithInformers(ctx, kubeInformers, workInformers, addonInformers, clusterInformers, dynamicInformers) + err = a.StartWithInformers(ctx, workClient, workInformers.Work().V1().ManifestWorks(), kubeInformers, addonInformers, clusterInformers, dynamicInformers) if err != nil { return err } @@ -189,144 +137,9 @@ func (a *addonManager) Start(ctx context.Context) error { return nil } -func (a *addonManager) StartWithInformers(ctx context.Context, - kubeInformers kubeinformers.SharedInformerFactory, - workInformers workv1informers.SharedInformerFactory, - addonInformers addoninformers.SharedInformerFactory, - clusterInformers clusterv1informers.SharedInformerFactory, - dynamicInformers dynamicinformer.DynamicSharedInformerFactory) error { - - kubeClient, err := kubernetes.NewForConfig(a.config) - if err != nil { - return err - } - - addonClient, err := addonv1alpha1client.NewForConfig(a.config) - if err != nil { - return err - } - - workClient, err := workv1client.NewForConfig(a.config) - if err != nil { - return err - } - - v1CSRSupported, v1beta1Supported, err := utils.IsCSRSupported(kubeClient) - if err != nil { - return err - } - - for _, agentImpl := range a.addonAgents { - for _, configGVR := range agentImpl.GetAgentAddonOptions().SupportedConfigGVRs { - a.addonConfigs[configGVR] = true - } - } - - deployController := agentdeploy.NewAddonDeployController( - workClient, - addonClient, - clusterInformers.Cluster().V1().ManagedClusters(), - addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), - workInformers.Work().V1().ManifestWorks(), - a.addonAgents, - ) - - registrationController := registration.NewAddonRegistrationController( - addonClient, - clusterInformers.Cluster().V1().ManagedClusters(), - addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), - a.addonAgents, - ) - - // This controller is used during migrating addons to be managed by addon-manager. - // This should be removed when the migration is done. - // The migration plan refer to https://github.com/open-cluster-management-io/ocm/issues/355. - managementAddonController := cmamanagedby.NewCMAManagedByController( - addonClient, - addonInformers.Addon().V1alpha1().ClusterManagementAddOns(), - a.addonAgents, - utils.FilterByAddonName(a.addonAgents), - ) - - var addonConfigController, managementAddonConfigController factory.Controller - if len(a.addonConfigs) != 0 { - addonConfigController = addonconfig.NewAddonConfigController( - addonClient, - addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), - addonInformers.Addon().V1alpha1().ClusterManagementAddOns(), - dynamicInformers, - a.addonConfigs, - utils.FilterByAddonName(a.addonAgents), - ) - managementAddonConfigController = cmaconfig.NewCMAConfigController( - addonClient, - addonInformers.Addon().V1alpha1().ClusterManagementAddOns(), - dynamicInformers, - a.addonConfigs, - utils.FilterByAddonName(a.addonAgents), - ) - } - - var csrApproveController factory.Controller - var csrSignController factory.Controller - // Spawn the following controllers only if v1 CSR api is supported in the - // hub cluster. Under v1beta1 CSR api, all the CSR objects will be signed - // by the kube-controller-manager so custom CSR controller should be - // disabled to avoid conflict. - if v1CSRSupported { - csrApproveController = certificate.NewCSRApprovingController( - kubeClient, - clusterInformers.Cluster().V1().ManagedClusters(), - kubeInformers.Certificates().V1().CertificateSigningRequests(), - nil, - addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), - a.addonAgents, - ) - csrSignController = certificate.NewCSRSignController( - kubeClient, - clusterInformers.Cluster().V1().ManagedClusters(), - kubeInformers.Certificates().V1().CertificateSigningRequests(), - addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), - a.addonAgents, - ) - } else if v1beta1Supported { - csrApproveController = certificate.NewCSRApprovingController( - kubeClient, - clusterInformers.Cluster().V1().ManagedClusters(), - nil, - kubeInformers.Certificates().V1beta1().CertificateSigningRequests(), - addonInformers.Addon().V1alpha1().ManagedClusterAddOns(), - a.addonAgents, - ) - } - - a.syncContexts = append(a.syncContexts, deployController.SyncContext()) - - go deployController.Run(ctx, 1) - go registrationController.Run(ctx, 1) - go managementAddonController.Run(ctx, 1) - - if addonConfigController != nil { - go addonConfigController.Run(ctx, 1) - } - if managementAddonConfigController != nil { - go managementAddonConfigController.Run(ctx, 1) - } - if csrApproveController != nil { - go csrApproveController.Run(ctx, 1) - } - if csrSignController != nil { - go csrSignController.Run(ctx, 1) - } - return nil -} - // New returns a new Manager for creating addon agents. func New(config *rest.Config) (AddonManager, error) { return &addonManager{ - config: config, - syncContexts: []factory.SyncContext{}, - addonConfigs: map[schema.GroupVersionResource]bool{}, - addonAgents: map[string]agent.AgentAddon{}, + NewBaseAddonManagerImpl(config), }, nil } diff --git a/vendor/open-cluster-management.io/sdk-go/pkg/cloudevents/work/source/client/manifestwork.go b/vendor/open-cluster-management.io/sdk-go/pkg/cloudevents/work/source/client/manifestwork.go index c9d6083e3..ed6eb8a46 100644 --- a/vendor/open-cluster-management.io/sdk-go/pkg/cloudevents/work/source/client/manifestwork.go +++ b/vendor/open-cluster-management.io/sdk-go/pkg/cloudevents/work/source/client/manifestwork.go @@ -17,6 +17,7 @@ import ( "open-cluster-management.io/sdk-go/pkg/cloudevents/generic" "open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types" "open-cluster-management.io/sdk-go/pkg/cloudevents/work/common" + "open-cluster-management.io/sdk-go/pkg/cloudevents/work/payload" "open-cluster-management.io/sdk-go/pkg/cloudevents/work/utils" "open-cluster-management.io/sdk-go/pkg/cloudevents/work/watcher" ) @@ -60,13 +61,10 @@ func (c *ManifestWorkSourceClient) Create(ctx context.Context, manifestWork *wor return nil, err } - eventDataType, err := types.ParseCloudEventsDataType(manifestWork.Annotations[common.CloudEventsDataTypeAnnotationKey]) - if err != nil { - return nil, err - } - + // TODO if we support multiple data type in future, we may need to get the data type from + // the cloudevents data type annotation eventType := types.CloudEventsType{ - CloudEventsDataType: *eventDataType, + CloudEventsDataType: payload.ManifestBundleEventDataType, SubResource: types.SubResourceSpec, Action: common.CreateRequestAction, } @@ -106,13 +104,10 @@ func (c *ManifestWorkSourceClient) Delete(ctx context.Context, name string, opts return err } - eventDataType, err := types.ParseCloudEventsDataType(work.Annotations[common.CloudEventsDataTypeAnnotationKey]) - if err != nil { - return err - } - + // TODO if we support multiple data type in future, we may need to get the data type from + // the cloudevents data type annotation eventType := types.CloudEventsType{ - CloudEventsDataType: *eventDataType, + CloudEventsDataType: payload.ManifestBundleEventDataType, SubResource: types.SubResourceSpec, Action: common.DeleteRequestAction, } @@ -175,13 +170,10 @@ func (c *ManifestWorkSourceClient) Patch(ctx context.Context, name string, pt ku return nil, err } - eventDataType, err := types.ParseCloudEventsDataType(lastWork.Annotations[common.CloudEventsDataTypeAnnotationKey]) - if err != nil { - return nil, err - } - + // TODO if we support multiple data type in future, we may need to get the data type from + // the cloudevents data type annotation eventType := types.CloudEventsType{ - CloudEventsDataType: *eventDataType, + CloudEventsDataType: payload.ManifestBundleEventDataType, SubResource: types.SubResourceSpec, Action: common.UpdateRequestAction, }