Skip to content

Commit

Permalink
add a static cluster controller
Browse files Browse the repository at this point in the history
  • Loading branch information
shaofan-hs committed Mar 14, 2024
1 parent f52e02d commit 4e590a0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions multicluster/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ 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
GetClusterName(obj *unstructured.Unstructured) string // Get cluster name from cluster management resource, cluster name is used to identify the cluster
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
Expand Down
41 changes: 41 additions & 0 deletions multicluster/controller/static_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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
}
1 change: 1 addition & 0 deletions multicluster/controller/test_cluster_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4e590a0

Please sign in to comment.