-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Template): Added the kubernetes cmd
- Added the possibility the you can list, create, update and delete a kubernetes cluster - Change the spinner presentation - Update civogo lib to 0.2.7 BREAKING CHANGE: No Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
- Loading branch information
1 parent
59a4231
commit 0370dde
Showing
22 changed files
with
892 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/civogo" | ||
"github.com/civo/cli/config" | ||
"github.com/civo/cli/utility" | ||
"github.com/logrusorgru/aurora" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var KubernetesClusterApp string | ||
|
||
var kubernetesAppAddCmd = &cobra.Command{ | ||
Use: "add", | ||
Aliases: []string{"install"}, | ||
Short: "Add the marketplace application to a Kubernetes cluster", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
client, err := config.CivoAPIClient() | ||
if err != nil { | ||
fmt.Printf("Unable to create a Civo API Client: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
kubernetesFindCluster, err := client.FindKubernetesCluster(KubernetesClusterApp) | ||
if err != nil { | ||
fmt.Printf("Unable to find a kubernetes cluster: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
configKubernetes := &civogo.KubernetesClusterConfig{ | ||
Applications: args[0], | ||
} | ||
|
||
kubeCluster, err := client.UpdateKubernetesCluster(kubernetesFindCluster.ID, configKubernetes) | ||
if err != nil { | ||
fmt.Printf("Unable to install the application in the kubernetes cluster: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
ow := utility.NewOutputWriterWithMap(map[string]string{"ID": kubeCluster.ID, "Name": kubeCluster.Name}) | ||
|
||
switch outputFormat { | ||
case "json": | ||
ow.WriteSingleObjectJSON() | ||
case "custom": | ||
ow.WriteCustomOutput(outputFields) | ||
default: | ||
fmt.Printf("The application was install in the kubernetes cluster %s\n", aurora.Green(kubeCluster.Name)) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/cli/config" | ||
"github.com/civo/cli/utility" | ||
"github.com/logrusorgru/aurora" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var kubernetesAppListCmd = &cobra.Command{ | ||
Use: "ls", | ||
Aliases: []string{"list", "all"}, | ||
Short: "List all kubernetes clusters applications", | ||
Long: `List all kubernetes clusters applications. | ||
If you wish to use a custom format, the available fields are: | ||
* Name | ||
* Version | ||
* Category | ||
* Plans | ||
* Dependencies | ||
Example: civo kubernetes applications ls -o custom -f "Name: Version"`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
client, err := config.CivoAPIClient() | ||
if err != nil { | ||
fmt.Printf("Unable to create a Civo API Client: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
kubeApps, err := client.ListKubernetesMarketplaceApplications() | ||
if err != nil { | ||
fmt.Printf("Unable to list kubernetes cluster application: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
ow := utility.NewOutputWriter() | ||
for _, kubeApp := range kubeApps { | ||
ow.StartLine() | ||
|
||
// All plans | ||
var plansApps []string | ||
for _, plan := range kubeApp.Plans { | ||
plansApps = append(plansApps, plan.Label) | ||
} | ||
|
||
ow.AppendData("Name", kubeApp.Name) | ||
ow.AppendData("Version", kubeApp.Version) | ||
ow.AppendData("Category", kubeApp.Category) | ||
ow.AppendData("Plans", strings.Join(plansApps, ", ")) | ||
ow.AppendData("Dependencies", strings.Join(kubeApp.Dependencies, ", ")) | ||
} | ||
|
||
switch outputFormat { | ||
case "json": | ||
ow.WriteMultipleObjectsJSON() | ||
case "custom": | ||
ow.WriteCustomOutput(outputFields) | ||
default: | ||
ow.WriteTable() | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cmd | ||
|
||
import ( | ||
_ "errors" | ||
"fmt" | ||
"github.com/civo/cli/config" | ||
"github.com/civo/cli/utility" | ||
_ "github.com/civo/cli/utility" | ||
"github.com/logrusorgru/aurora" | ||
"github.com/spf13/cobra" | ||
"os" | ||
_ "strconv" | ||
) | ||
|
||
var saveConfig, mergeConfig bool | ||
var localPathConfig string | ||
|
||
var kubernetesConfigCmd = &cobra.Command{ | ||
Use: "config", | ||
Aliases: []string{"conf"}, | ||
Args: cobra.MinimumNArgs(1), | ||
Short: "Get kubernetes clusters config", | ||
Long: `Show current kubernetes config. | ||
If you wish to use a custom format, the available fields are: | ||
* KubeConfig | ||
Example: civo kubernetes config -o custom -f "KubeConfig"`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
client, err := config.CivoAPIClient() | ||
if err != nil { | ||
fmt.Printf("Unable to create a Civo API Client: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
kube, err := client.FindKubernetesCluster(args[0]) | ||
if err != nil { | ||
fmt.Printf("Unable to get kubernetes cluster: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
if saveConfig { | ||
_ = utility.ObtainKubeConfig(localPathConfig, kube.KubeConfig, mergeConfig) | ||
} | ||
|
||
ow := utility.NewOutputWriterWithMap(map[string]string{"KubeConfig": kube.KubeConfig}) | ||
|
||
switch outputFormat { | ||
case "json": | ||
ow.WriteSingleObjectJSON() | ||
case "custom": | ||
ow.WriteCustomOutput(outputFields) | ||
default: | ||
fmt.Println("The configuration was save") | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/briandowns/spinner" | ||
"github.com/civo/civogo" | ||
"github.com/civo/cli/config" | ||
"github.com/civo/cli/utility" | ||
"github.com/logrusorgru/aurora" | ||
"github.com/spf13/cobra" | ||
"os" | ||
_ "strconv" | ||
"time" | ||
) | ||
|
||
var NumTargetNodes int | ||
var waitKubernetes bool | ||
var ( | ||
KubernetesVersion string | ||
TargetNodesSize string | ||
) | ||
|
||
var kubernetesCreateCmd = &cobra.Command{ | ||
Use: "create", | ||
Aliases: []string{"new", "add"}, | ||
Short: "Create a new kubernetes cluster", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
client, err := config.CivoAPIClient() | ||
if err != nil { | ||
fmt.Printf("Unable to create a Civo API Client: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
isValidName := false | ||
|
||
_, err = client.FindKubernetesCluster(args[0]) | ||
if err != nil { | ||
isValidName = true | ||
} | ||
|
||
if !isValidName { | ||
fmt.Printf("The %s is nos valida name for the cluster\n", aurora.Red(args[0])) | ||
os.Exit(1) | ||
} | ||
|
||
configKubernetes := &civogo.KubernetesClusterConfig{ | ||
Name: args[0], | ||
NumTargetNodes: NumTargetNodes, | ||
TargetNodesSize: TargetNodesSize, | ||
KubernetesVersion: KubernetesVersion, | ||
} | ||
|
||
kubernetesCluster, err := client.NewKubernetesClusters(configKubernetes) | ||
if err != nil { | ||
fmt.Printf("Unable to create a kubernetes cluster: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
if waitKubernetes == true { | ||
|
||
stillCreating := true | ||
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) | ||
s.Prefix = "Creating kubernetes cluster... " | ||
s.Start() | ||
|
||
for stillCreating { | ||
kubernetesCheck, _ := client.FindKubernetesCluster(kubernetesCluster.ID) | ||
if kubernetesCheck.Status == "ACTIVE" { | ||
stillCreating = false | ||
s.Stop() | ||
} | ||
time.Sleep(5 * time.Second) | ||
} | ||
} | ||
|
||
ow := utility.NewOutputWriterWithMap(map[string]string{"ID": kubernetesCluster.ID, "Name": kubernetesCluster.Name}) | ||
|
||
switch outputFormat { | ||
case "json": | ||
ow.WriteSingleObjectJSON() | ||
case "custom": | ||
ow.WriteCustomOutput(outputFields) | ||
default: | ||
fmt.Printf("Created a new kubernetes cluster with Name %s with ID %s\n", aurora.Green(kubernetesCluster.Name), aurora.Green(kubernetesCluster.ID)) | ||
} | ||
}, | ||
} |
Oops, something went wrong.