Skip to content

Commit

Permalink
pull mode register add check cluster exists
Browse files Browse the repository at this point in the history
Signed-off-by: huangyanfeng <huangyanfeng1992@gmail.com>
  • Loading branch information
yanfeng1992 committed Jan 22, 2024
1 parent 697170b commit 0e46397
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/karmadactl/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"github.com/karmada-io/karmada/pkg/apis/cluster/validation"
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
"github.com/karmada-io/karmada/pkg/karmadactl/options"
cmdutil "github.com/karmada-io/karmada/pkg/karmadactl/util"
"github.com/karmada-io/karmada/pkg/karmadactl/util/apiclient"
Expand Down Expand Up @@ -364,6 +365,18 @@ func (o *CommandRegisterOption) Run(parentCommand string) error {
return err
}

fmt.Println("[karmada-agent-start] Waiting to check cluster exists")
karmadaClient, err := ToKarmadaClient(karmadaAgentCfg)
if err != nil {
return err
}
_, exist, err := karmadautil.GetClusterWithKarmadaClient(karmadaClient, o.ClusterName)
if err != nil {
return err
} else if exist {
return fmt.Errorf("failed to register as cluster with name %s already exists", o.ClusterName)
}

// It's necessary to set the label of namespace to make sure that the namespace is created by Karmada.
labels := map[string]string{
util.ManagedByKarmadaLabel: util.ManagedByKarmadaLabelValue,
Expand Down Expand Up @@ -1021,3 +1034,19 @@ func ToClientSet(config *clientcmdapi.Config) (*kubeclient.Clientset, error) {
}
return client, nil
}

// ToKarmadaClient converts a KubeConfig object to a client
func ToKarmadaClient(config *clientcmdapi.Config) (*karmadaclientset.Clientset, error) {
overrides := clientcmd.ConfigOverrides{Timeout: "10s"}
clientConfig, err := clientcmd.NewDefaultClientConfig(*config, &overrides).ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to create API client configuration from kubeconfig: %w", err)
}

karmadaClient, err := karmadaclientset.NewForConfig(clientConfig)
if err != nil {
return nil, err
}

return karmadaClient, nil
}

0 comments on commit 0e46397

Please sign in to comment.