Skip to content

Commit

Permalink
feature. implementation importing cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Oct 24, 2023
1 parent 62456c8 commit 1310d28
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -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=
86 changes: 86 additions & 0 deletions internal/commands/cluster-import.go
Original file line number Diff line number Diff line change
@@ -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 <CLUSTERNAME> [--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 <CLUSTERNAME> --contract-id <CONTRACTID>")
}

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
}
1 change: 1 addition & 0 deletions internal/commands/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 1310d28

Please sign in to comment.