//importCluster import a cluster if autoImportRetry > 0
importCluster import a cluster if autoImportRetry > 0
// importCluster import a cluster if autoImportRetry > 0
func (r *ReconcileManagedCluster) importClusterWithClient(
	managedCluster *clusterv1.ManagedCluster,
	autoImportSecret *corev1.Secret,
	managedClusterClient client.Client,
	managedClusterKubeVersion string) (reconcile.Result, error) {
	klog.Infof("Importing cluster: %s", managedCluster.Name)
	excluded := make([]string, 0)
	sa := &corev1.ServiceAccount{}
	if err := managedClusterClient.Get(context.TODO(),
		types.NamespacedName{
			Name:      "klusterlet",
			Namespace: klusterletNamespace,
		}, sa); err == nil {
		excluded = append(excluded, "klusterlet/service_account.yaml")
	}
	crds, yamls, err := generateImportYAMLs(r.client, managedCluster, excluded)
	if err != nil {
		return reconcile.Result{Requeue: true, RequeueAfter: 30 * time.Second}, err
	}
	var bb [][]byte
	isV1, err := isAPIExtensionV1(managedClusterClient, managedCluster, managedClusterKubeVersion)
	if err != nil {
		return reconcile.Result{Requeue: true, RequeueAfter: 30 * time.Second}, err
	}
	if isV1 {
		bb, err = templateprocessor.ToYAMLsUnstructured(crds["v1"])
	} else {
		bb, err = templateprocessor.ToYAMLsUnstructured(crds["v1beta1"])
	}
	if err != nil {
		return reconcile.Result{}, err
	}
	a, err := applier.NewApplier(
		templateprocessor.NewYamlStringReader(templateprocessor.ConvertArrayOfBytesToString(bb),
			templateprocessor.KubernetesYamlsDelimiter),
		nil,
		managedClusterClient,
		nil,
		nil,
		nil)
	if err != nil {
		return reconcile.Result{}, err
	}
	err = a.CreateOrUpdateInPath(".", nil, false, nil)
	if err != nil {
		return reconcile.Result{Requeue: true, RequeueAfter: 30 * time.Second}, err
	}
	bb, err = templateprocessor.ToYAMLsUnstructured(yamls)
	if err != nil {
		return reconcile.Result{}, err
	}
	a, err = applier.NewApplier(
		templateprocessor.NewYamlStringReader(templateprocessor.ConvertArrayOfBytesToString(bb),
			templateprocessor.KubernetesYamlsDelimiter),
		nil,
		managedClusterClient,
		nil,
		nil,
		nil)
	if err != nil {
		return reconcile.Result{}, err
	}
	err = a.CreateOrUpdateInPath(".", excluded, false, nil)
	if err != nil {
		return reconcile.Result{Requeue: true, RequeueAfter: 30 * time.Second}, err
	}
	if autoImportSecret != nil {
		if err := r.client.Delete(context.TODO(), autoImportSecret); err != nil {
			return reconcile.Result{}, err
		}
	}
	klog.Infof("Successfully imported %s", managedCluster.Name)
	return reconcile.Result{}, nil
}
func ( r * ReconcileManagedCluster ) importClusterWithClient ( managedCluster * clusterv1 . ManagedCluster , autoImportSecret * corev1 . Secret , managedClusterClient client . Client , managedClusterKubeVersion string ) ( reconcile . Result , error ) { klog . Infof ( " " , managedCluster . Name ) excluded := make ( [ ] string , 0 ) sa := & corev1 . ServiceAccount { } if err := managedClusterClient . Get ( context . TODO ( ) , types . NamespacedName { Name : " " , Namespace : klusterletNamespace , } , sa ) ; err == nil { excluded = append ( excluded , " " ) } crds , yamls , err := generateImportYAMLs ( r . client , managedCluster , excluded ) if err != nil { return reconcile . Result { Requeue : true , RequeueAfter : 30 * time . Second } , err } var bb [ ] [ ] byte isV1 , err := isAPIExtensionV1 ( managedClusterClient , managedCluster , managedClusterKubeVersion ) if err != nil { return reconcile . Result { Requeue : true , RequeueAfter : 30 * time . Second } , err } if isV1 { bb , err = templateprocessor . ToYAMLsUnstructured ( crds [ " " ] ) } else { bb , err = templateprocessor . ToYAMLsUnstructured ( crds [ " " ] ) } if err != nil { return reconcile . Result { } , err } a , err := applier . NewApplier ( templateprocessor . NewYamlStringReader ( templateprocessor . ConvertArrayOfBytesToString ( bb ) , templateprocessor . KubernetesYamlsDelimiter ) , nil , managedClusterClient , nil , nil , nil ) if err != nil { return reconcile . Result { } , err } err = a . CreateOrUpdateInPath ( " " , nil , false , nil ) if err != nil { return reconcile . Result { Requeue : true , RequeueAfter : 30 * time . Second } , err } bb , err = templateprocessor . ToYAMLsUnstructured ( yamls ) if err != nil { return reconcile . Result { } , err } a , err = applier . NewApplier ( templateprocessor . NewYamlStringReader ( templateprocessor . ConvertArrayOfBytesToString ( bb ) , templateprocessor . KubernetesYamlsDelimiter ) , nil , managedClusterClient , nil , nil , nil ) if err != nil { return reconcile . Result { } , err } err = a . CreateOrUpdateInPath ( " " , excluded , false , nil ) if err != nil { return reconcile . Result { Requeue : true , RequeueAfter : 30 * time . Second } , err } if autoImportSecret != nil { if err := r . client . Delete ( context . TODO ( ) , autoImportSecret ) ; err != nil { return reconcile . Result { } , err } } klog . Infof ( " " , managedCluster . Name ) return reconcile . Result { } , nil }
importCluster import a cluster if autoImportRetry > 0
importCluster import a cluster if autoImportRetry > 0
//Do not create SA if already exists //Generate crds and yamls //Convert crds to Yaml //create applier for crds //Create the crds resources //Convert yamls to yaml //Create applier for yamls //Create the yamls resources //Succeeded do not retry, then remove the autoImportRetryLabel