diff --git a/go.mod b/go.mod index 25b1d03..b12a6cd 100644 --- a/go.mod +++ b/go.mod @@ -51,4 +51,4 @@ require ( replace github.com/openinfradev/tks-client => ./ -//replace github.com/openinfradev/tks-api => ../tks-api +replace github.com/openinfradev/tks-api => ../tks-api diff --git a/go.work.sum b/go.work.sum index bc526fd..2aa935d 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1,6 +1,9 @@ +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= -github.com/openinfradev/tks-api v0.0.0-20231017022609-fe95bb67dc60 h1:aKcP0zqiuQuruwJ3j+Eb06QKkZKvddFuZ9XPMsANpgg= -github.com/openinfradev/tks-api v0.0.0-20231017022609-fe95bb67dc60/go.mod h1:91WDknrRu9zZZ1rI1OGhj7dqqc4DQJb830y7EU5t85I= github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd h1:e0TwkXOdbnH/1x5rc5MZ/VYyiZ4v+RdVfrGMqEwT68I= +google.golang.org/grpc v1.46.2 h1:u+MLGgVf7vRdjEYZ8wDFhAVNmhkbJ5hmrA1LMWK1CAQ= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= diff --git a/internal/commands/cluster-import.go b/internal/commands/cluster-import.go new file mode 100644 index 0000000..e64082c --- /dev/null +++ b/internal/commands/cluster-import.go @@ -0,0 +1,86 @@ +package commands + +import ( + "errors" + "fmt" + "log" + "os" + + _apiClient "github.com/openinfradev/tks-api/pkg/api-client" + "github.com/openinfradev/tks-api/pkg/domain" + "github.com/openinfradev/tks-client/internal/helper" + "github.com/spf13/cobra" +) + +func NewClusterImportCommand(globalOpts *GlobalOptions) *cobra.Command { + var ( + name string + clusterType string + organizationId string + description string + stackTemplateId string + kubeconfigPath string + ) + + var command = &cobra.Command{ + Use: "import", + Short: "Import a TKS Cluster.", + Long: `Import a TKS Cluster. + + Example: + tks cluster import [--cloud-service AWS] [--template TEMPLATE_NAME]`, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + fmt.Println("You must specify cluster name.") + return errors.New("Usage: tks cluster import --contract-id ") + } + + if len(args) == 1 { + name = args[0] + } + + kubeconfig, err := os.ReadFile(kubeconfigPath) + if err != nil { + log.Fatalf("Failed to read kubeconfig from [%s] path", err) + log.Fatalf("Failed to read kubeconfig from [%s] path", kubeconfigPath) + } + input := domain.ImportClusterRequest{ + OrganizationId: organizationId, + StackTemplateId: stackTemplateId, + Name: name, + Description: description, + ClusterType: clusterType, + Kubeconfig: kubeconfig, + } + + apiClient, err := _apiClient.NewWithToken(globalOpts.ServerAddr, globalOpts.AuthToken) + helper.CheckError(err) + + body, err := apiClient.Post("clusters/import", input) + if err != nil { + return err + } + + var out domain.ImportClusterResponse + helper.Transcode(body, &out) + + fmt.Println("clusterId : ", out.ID) + + return nil + }, + } + command.Flags().StringVarP(&organizationId, "organization-id", "o", "", "the organizationId with clusters") + helper.CheckError(command.MarkFlagRequired("organization-id")) + + command.Flags().StringVar(&clusterType, "cluster-type", "USER", "the cluster type (USER | ADMIN)") + + command.Flags().StringVarP(&stackTemplateId, "stack-template-id", "t", "", "the template for installation") + helper.CheckError(command.MarkFlagRequired("stack-template-id")) + + command.Flags().StringVarP(&name, "name", "n", "", "the name of organization") + command.Flags().StringVarP(&description, "description", "d", "", "the description of organization") + + command.Flags().StringVar(&kubeconfigPath, "kubeconfig-path", "~/.kube/config", "the path of kubeconfig") + + return command +} diff --git a/internal/commands/cluster.go b/internal/commands/cluster.go index 3765cf4..e3f8631 100644 --- a/internal/commands/cluster.go +++ b/internal/commands/cluster.go @@ -18,6 +18,7 @@ func NewClusterCommand(globalOpts *GlobalOptions) *cobra.Command { command.AddCommand(NewClusterListCommand(globalOpts)) command.AddCommand(NewClusterCreateCommand(globalOpts)) + command.AddCommand(NewClusterImportCommand(globalOpts)) command.AddCommand(NewClusterDeleteCommand(globalOpts)) command.AddCommand(NewClusterInstallCommand(globalOpts)) command.AddCommand(NewClusterNodeCommand(globalOpts))