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:
commit
66af6ef502
|
@ -133,6 +133,13 @@ func run(ctx context.Context, karmadaConfig karmadactl.KarmadaConfig, opts *opti
|
||||||
ControlPlaneConfig: controlPlaneRestConfig,
|
ControlPlaneConfig: controlPlaneRestConfig,
|
||||||
ClusterConfig: clusterConfig,
|
ClusterConfig: clusterConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
id, err := util.ObtainClusterID(clusterKubeClient)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
registerOption.ClusterID = id
|
||||||
|
|
||||||
clusterSecret, impersonatorSecret, err := util.ObtainCredentialsFromMemberCluster(clusterKubeClient, registerOption)
|
clusterSecret, impersonatorSecret, err := util.ObtainCredentialsFromMemberCluster(clusterKubeClient, registerOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -325,6 +332,7 @@ func generateClusterInControllerPlane(opts util.ClusterRegisterOption) (*cluster
|
||||||
cluster.Spec.SyncMode = clusterv1alpha1.Pull
|
cluster.Spec.SyncMode = clusterv1alpha1.Pull
|
||||||
cluster.Spec.APIEndpoint = opts.ClusterAPIEndpoint
|
cluster.Spec.APIEndpoint = opts.ClusterAPIEndpoint
|
||||||
cluster.Spec.ProxyURL = opts.ProxyServerAddress
|
cluster.Spec.ProxyURL = opts.ProxyServerAddress
|
||||||
|
cluster.Spec.ID = opts.ClusterID
|
||||||
if opts.ClusterProvider != "" {
|
if opts.ClusterProvider != "" {
|
||||||
cluster.Spec.Provider = opts.ClusterProvider
|
cluster.Spec.Provider = opts.ClusterProvider
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
// Start starts an asynchronous loop that monitors the status of cluster.
|
||||||
func (c *Controller) Start(ctx context.Context) error {
|
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")
|
klog.Infof("Starting cluster health monitor")
|
||||||
defer klog.Infof("Shutting 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
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -175,6 +175,12 @@ func JoinCluster(controlPlaneRestConfig, clusterConfig *rest.Config, opts Comman
|
||||||
ClusterConfig: clusterConfig,
|
ClusterConfig: clusterConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
id, err := util.ObtainClusterID(clusterKubeClient)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
registerOption.ClusterID = id
|
||||||
|
|
||||||
clusterSecret, impersonatorSecret, err := util.ObtainCredentialsFromMemberCluster(clusterKubeClient, registerOption)
|
clusterSecret, impersonatorSecret, err := util.ObtainCredentialsFromMemberCluster(clusterKubeClient, registerOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -199,6 +205,7 @@ func generateClusterInControllerPlane(opts util.ClusterRegisterOption) (*cluster
|
||||||
clusterObj.Name = opts.ClusterName
|
clusterObj.Name = opts.ClusterName
|
||||||
clusterObj.Spec.SyncMode = clusterv1alpha1.Push
|
clusterObj.Spec.SyncMode = clusterv1alpha1.Push
|
||||||
clusterObj.Spec.APIEndpoint = opts.ClusterConfig.Host
|
clusterObj.Spec.APIEndpoint = opts.ClusterConfig.Host
|
||||||
|
clusterObj.Spec.ID = opts.ClusterID
|
||||||
clusterObj.Spec.SecretRef = &clusterv1alpha1.LocalSecretReference{
|
clusterObj.Spec.SecretRef = &clusterv1alpha1.LocalSecretReference{
|
||||||
Namespace: opts.Secret.Namespace,
|
Namespace: opts.Secret.Namespace,
|
||||||
Name: opts.Secret.Name,
|
Name: opts.Secret.Name,
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
@ -45,6 +46,7 @@ type ClusterRegisterOption struct {
|
||||||
ClusterConfig *rest.Config
|
ClusterConfig *rest.Config
|
||||||
Secret corev1.Secret
|
Secret corev1.Secret
|
||||||
ImpersonatorSecret corev1.Secret
|
ImpersonatorSecret corev1.Secret
|
||||||
|
ClusterID string
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsKubeCredentialsEnabled represents whether report secret
|
// IsKubeCredentialsEnabled represents whether report secret
|
||||||
|
@ -169,3 +171,12 @@ func updateCluster(controlPlaneClient karmadaclientset.Interface, cluster *clust
|
||||||
|
|
||||||
return newCluster, nil
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue