diff --git a/multicluster/controller/controller.go b/multicluster/controller/controller.go index 5430c22..51fc105 100644 --- a/multicluster/controller/controller.go +++ b/multicluster/controller/controller.go @@ -38,6 +38,7 @@ import ( "kusionstack.io/kube-utils/multicluster/metrics" ) +// ClusterProvider is used to provide cluster management resource and cluster kubeconfig type ClusterProvider interface { Init(config *rest.Config) // Init is used to initialize the cluster provider, config is the kubeconfig for the fed cluster GetClusterMangementGVR() schema.GroupVersionResource // The GVR will be used to watch cluster management resource @@ -45,6 +46,7 @@ type ClusterProvider interface { GetClusterConfig(obj *unstructured.Unstructured) *rest.Config // Get kubeconfig from cluster management resource } +// Controller is used to manage clusters type Controller struct { config *rest.Config clusterProvider ClusterProvider diff --git a/multicluster/controller/static_controller.go b/multicluster/controller/static_controller.go new file mode 100644 index 0000000..b8f70f8 --- /dev/null +++ b/multicluster/controller/static_controller.go @@ -0,0 +1,57 @@ +/** + * Copyright 2024 KusionStack Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package controller + +import ( + "context" + + "k8s.io/client-go/rest" +) + +// StaticController is a controller that manages a static set of clusters. +type StaticController struct { + clusterToConfig map[string]*rest.Config + addUpdateHandler func(string, *rest.Config) error +} + +func NewStaticController(clusterToConfig map[string]*rest.Config) *StaticController { + return &StaticController{ + clusterToConfig: clusterToConfig, + } +} + +func (c *StaticController) Run(stopCh <-chan struct{}) error { + if c.addUpdateHandler == nil { + return nil + } + + for cluster, config := range c.clusterToConfig { + if err := c.addUpdateHandler(cluster, config); err != nil { + return err + } + } + + return nil +} + +func (c *StaticController) AddEventHandler(addUpdateHandler func(string, *rest.Config) error, deleteHandler func(string)) { + c.addUpdateHandler = addUpdateHandler +} + +func (c *StaticController) WaitForSynced(ctx context.Context) bool { + return true +} diff --git a/multicluster/controller/test_cluster_provider.go b/multicluster/controller/test_cluster_provider.go index 9275d73..83ed29b 100644 --- a/multicluster/controller/test_cluster_provider.go +++ b/multicluster/controller/test_cluster_provider.go @@ -24,6 +24,7 @@ import ( var _ ClusterProvider = &TestClusterProvider{} +// TestClusterProvider is a test implementation of ClusterProvider type TestClusterProvider struct { schema.GroupVersionResource ClusterNameToConfig map[string]*rest.Config // Map from cluster name to kubeconfig