Skip to content

Commit

Permalink
- Added the merge flag to the create command
Browse files Browse the repository at this point in the history
- Add some verification to kubernetes create, now you need pick the right size of Instance
- Add auto-complete to the kubernetes show command

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Feb 19, 2021
1 parent 0aa3163 commit 9c259c3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func init() {
kubernetesCreateCmd.Flags().StringVarP(&removeapplications, "remove-applications", "r", "", "optional, remove default application names shown by running 'civo kubernetes applications ls'")
kubernetesCreateCmd.Flags().BoolVarP(&waitKubernetes, "wait", "w", false, "a simple flag (e.g. --wait) that will cause the CLI to spin and wait for the cluster to be ACTIVE")
kubernetesCreateCmd.Flags().BoolVarP(&saveConfigKubernetes, "save", "", false, "save the config")
kubernetesCreateCmd.Flags().BoolVarP(&mergeConfigKubernetes, "merge", "m", false, "merge the config with existing kubeconfig if it already exists.")
kubernetesCreateCmd.Flags().BoolVarP(&switchConfigKubernetes, "switch", "", false, "switch context to newly-created cluster")

kubernetesRenameCmd.Flags().StringVarP(&kubernetesNewName, "name", "n", "", "the new name for the cluster.")
Expand Down
40 changes: 32 additions & 8 deletions cmd/kubernetes_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

var numTargetNodes int
var waitKubernetes, saveConfigKubernetes, switchConfigKubernetes bool
var waitKubernetes, saveConfigKubernetes, mergeConfigKubernetes, switchConfigKubernetes bool
var kubernetesVersion, targetNodesSize, clusterName, applications, removeapplications, installApplications, networkID string
var kubernetesCluster *civogo.KubernetesCluster

var kubernetesCreateCmd = &cobra.Command{
Use: "create",
Expand Down Expand Up @@ -46,12 +47,21 @@ var kubernetesCreateCmd = &cobra.Command{
os.Exit(1)
}

if !strings.Contains(targetNodesSize, "k3s") {
utility.Error("You can create a cluster with this %s size, you need choise one with k3s in the name", targetNodesSize)
os.Exit(1)
}

if !waitKubernetes {
if saveConfigKubernetes || switchConfigKubernetes {
utility.Error("you can't use --save or --switch without --wait")
if saveConfigKubernetes || switchConfigKubernetes || mergeConfigKubernetes {
utility.Error("you can't use --save, --switch or --merge without --wait")
os.Exit(1)
}
} else {
if mergeConfigKubernetes && !saveConfigKubernetes {
utility.Error("you can't use --merge without --save")
os.Exit(1)
}
if switchConfigKubernetes && !saveConfigKubernetes {
utility.Error("you can't use --switch without --save")
os.Exit(1)
Expand Down Expand Up @@ -115,10 +125,23 @@ var kubernetesCreateCmd = &cobra.Command{
configKubernetes.Applications = installApplications
}

kubernetesCluster, err := client.NewKubernetesClusters(configKubernetes)
if err != nil {
utility.Error("%s", err)
os.Exit(1)
if !mergeConfigKubernetes {
if utility.UserConfirmedOverwrite("kubernetes config", defaultYes) == true {
kubernetesCluster, err = client.NewKubernetesClusters(configKubernetes)
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}
} else {
fmt.Println("Operation aborted.")
os.Exit(1)
}
} else {
kubernetesCluster, err = client.NewKubernetesClusters(configKubernetes)
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}
}

var executionTime string
Expand Down Expand Up @@ -155,11 +178,12 @@ var kubernetesCreateCmd = &cobra.Command{
os.Exit(1)
}

err = utility.ObtainKubeConfig(localPathConfig, kube.KubeConfig, true, switchConfigKubernetes, kube.Name)
err = utility.ObtainKubeConfig(localPathConfig, kube.KubeConfig, mergeConfigKubernetes, switchConfigKubernetes, kube.Name)
if err != nil {
utility.Error("Saving the cluster config failed with %s", err)
os.Exit(1)
}

}

ow := utility.NewOutputWriterWithMap(map[string]string{"ID": kubernetesCluster.ID, "Name": kubernetesCluster.Name})
Expand Down
48 changes: 48 additions & 0 deletions cmd/kubernetes_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ If you wish to use a custom format, the available fields are:
* APIEndPoint
* MasterIP
* DNSEntry`,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return getAllKubernetesList(), cobra.ShellCompDirectiveNoFileComp
}
return getKubernetesList(toComplete), cobra.ShellCompDirectiveNoFileComp
},
Run: func(cmd *cobra.Command, args []string) {
client, err := config.CivoAPIClient()
if regionSet != "" {
Expand Down Expand Up @@ -131,3 +137,45 @@ If you wish to use a custom format, the available fields are:

},
}

func getKubernetesList(value string) []string {
client, err := config.CivoAPIClient()
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}

cluster, err := client.FindKubernetesCluster(value)
if err != nil {
utility.Error("Unable to list domains %s", err)
os.Exit(1)
}

var clusterList []string
clusterList = append(clusterList, cluster.Name)

return clusterList

}

func getAllKubernetesList() []string {
client, err := config.CivoAPIClient()
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}

cluster, err := client.ListKubernetesClusters()
if err != nil {
utility.Error("Unable to list domains %s", err)
os.Exit(1)
}

var clusterList []string
for _, v := range cluster.Items {
clusterList = append(clusterList, v.Name)
}

return clusterList

}

0 comments on commit 9c259c3

Please sign in to comment.