Merge pull request #2313 from yy158775/unique_id

add ID the unique identifier for push and pull mode's cluster
This commit is contained in:
karmada-bot 2022-08-12 14:57:10 +08:00 committed by GitHub
commit 66af6ef502
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 0 deletions

View File

@ -133,6 +133,13 @@ func run(ctx context.Context, karmadaConfig karmadactl.KarmadaConfig, opts *opti
ControlPlaneConfig: controlPlaneRestConfig,
ClusterConfig: clusterConfig,
}
id, err := util.ObtainClusterID(clusterKubeClient)
if err != nil {
return err
}
registerOption.ClusterID = id
clusterSecret, impersonatorSecret, err := util.ObtainCredentialsFromMemberCluster(clusterKubeClient, registerOption)
if err != nil {
return err
@ -325,6 +332,7 @@ func generateClusterInControllerPlane(opts util.ClusterRegisterOption) (*cluster
cluster.Spec.SyncMode = clusterv1alpha1.Pull
cluster.Spec.APIEndpoint = opts.ClusterAPIEndpoint
cluster.Spec.ProxyURL = opts.ProxyServerAddress
cluster.Spec.ID = opts.ClusterID
if opts.ClusterProvider != "" {
cluster.Spec.Provider = opts.ClusterProvider
}

View File

@ -165,6 +165,18 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
// Start starts an asynchronous loop that monitors the status of cluster.
func (c *Controller) Start(ctx context.Context) error {
// starts a goroutine to collect existed clusters' ID for upgrade scenario
// And can be removed in next version
klog.Infof("Starting collect ID of already existed clusters")
go func() {
err := c.collectIDForClusterObjectIfNeeded(ctx)
if err != nil {
klog.Errorf("Error collecting clusters' ID: %v", err)
}
klog.Infof("Finishing collect ID of already existed clusters")
}()
klog.Infof("Starting cluster health monitor")
defer klog.Infof("Shutting cluster health monitor")
@ -563,3 +575,44 @@ func (c *Controller) taintClusterByCondition(ctx context.Context, cluster *clust
return nil
}
func (c *Controller) collectIDForClusterObjectIfNeeded(ctx context.Context) (err error) {
clusterList := &clusterv1alpha1.ClusterList{}
if err := c.Client.List(ctx, clusterList); err != nil {
return err
}
clusters := clusterList.Items
var errs []error
for i := range clusters {
cluster := &clusters[i]
if cluster.Spec.ID != "" {
continue
}
if cluster.Spec.SyncMode == clusterv1alpha1.Pull {
continue
}
clusterClient, err := util.NewClusterClientSet(cluster.Name, c.Client, nil)
if err != nil {
errs = append(errs, err)
continue
}
id, err := util.ObtainClusterID(clusterClient.KubeClient)
if err != nil {
errs = append(errs, err)
continue
}
cluster.Spec.ID = id
err = c.Client.Update(ctx, cluster)
if err != nil {
errs = append(errs, err)
continue
}
}
return utilerrors.NewAggregate(errs)
}

View File

@ -175,6 +175,12 @@ func JoinCluster(controlPlaneRestConfig, clusterConfig *rest.Config, opts Comman
ClusterConfig: clusterConfig,
}
id, err := util.ObtainClusterID(clusterKubeClient)
if err != nil {
return err
}
registerOption.ClusterID = id
clusterSecret, impersonatorSecret, err := util.ObtainCredentialsFromMemberCluster(clusterKubeClient, registerOption)
if err != nil {
return err
@ -199,6 +205,7 @@ func generateClusterInControllerPlane(opts util.ClusterRegisterOption) (*cluster
clusterObj.Name = opts.ClusterName
clusterObj.Spec.SyncMode = clusterv1alpha1.Push
clusterObj.Spec.APIEndpoint = opts.ClusterConfig.Host
clusterObj.Spec.ID = opts.ClusterID
clusterObj.Spec.SecretRef = &clusterv1alpha1.LocalSecretReference{
Namespace: opts.Secret.Namespace,
Name: opts.Secret.Name,

View File

@ -10,6 +10,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
@ -45,6 +46,7 @@ type ClusterRegisterOption struct {
ClusterConfig *rest.Config
Secret corev1.Secret
ImpersonatorSecret corev1.Secret
ClusterID string
}
// IsKubeCredentialsEnabled represents whether report secret
@ -169,3 +171,12 @@ func updateCluster(controlPlaneClient karmadaclientset.Interface, cluster *clust
return newCluster, nil
}
// ObtainClusterID returns the cluster ID property with clusterKubeClient
func ObtainClusterID(clusterKubeClient *kubernetes.Clientset) (string, error) {
ns, err := clusterKubeClient.CoreV1().Namespaces().Get(context.TODO(), metav1.NamespaceSystem, metav1.GetOptions{})
if err != nil {
return "", err
}
return string(ns.UID), nil
}