add ID the unique identifier for push and pull mode's cluster
Signed-off-by: yy158775 <1584616775@qq.com>
This commit is contained in:
parent
371ecba406
commit
474933826b
|
@ -132,6 +132,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
|
||||
|
@ -323,6 +330,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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -177,6 +177,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
|
||||
|
@ -201,6 +207,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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue