diff --git a/cmd/kubernetes_app_add.go b/cmd/kubernetes_app_add.go index a2d8c108..611a53a9 100644 --- a/cmd/kubernetes_app_add.go +++ b/cmd/kubernetes_app_add.go @@ -14,7 +14,7 @@ var kubernetesClusterApp string var kubernetesAppAddCmd = &cobra.Command{ Use: "add", - Example: "civo kubernetes application add NAME --cluster CLUSTER_NAME", + Example: "civo kubernetes application add NAME:PLAN --cluster CLUSTER_NAME", Aliases: []string{"install"}, Short: "Add the marketplace application to a Kubernetes cluster", Run: func(cmd *cobra.Command, args []string) { @@ -30,8 +30,15 @@ var kubernetesAppAddCmd = &cobra.Command{ os.Exit(1) } + appList, err := client.ListKubernetesMarketplaceApplications() + if err != nil { + utility.Error("Listing all Kubernetes cluster Applications failed with %s", err) + os.Exit(1) + } + + result := utility.RequestedSplit(appList, args[0]) configKubernetes := &civogo.KubernetesClusterConfig{ - Applications: args[0], + Applications: result, } kubeCluster, err := client.UpdateKubernetesCluster(kubernetesFindCluster.ID, configKubernetes) diff --git a/utility/kubernetes.go b/utility/kubernetes.go index d1d9c109..d619b9fc 100644 --- a/utility/kubernetes.go +++ b/utility/kubernetes.go @@ -6,6 +6,8 @@ import ( "os" "os/exec" "strings" + + "github.com/civo/civogo" ) // ObtainKubeConfig is the function to get the kubeconfig from the cluster @@ -95,3 +97,80 @@ func writeConfig(path string, data []byte, suppressMessage bool, mergeConfigs bo } return nil } + +// checkAppPlan is the function to verify if the application to be installed in the cluster +// has a plan or not, in case it has a plan but does not specify it, +// we use the first one in the list +func checkAppPlan(appList []civogo.KubernetesMarketplaceApplication, requested string) (string, error) { + foundIndex := -1 + parts := strings.SplitN(requested, ":", 2) + appName := parts[0] + + var planName string + if len(parts) > 1 { + planName = parts[1] + } + + for i, app := range appList { + if strings.Contains(app.Name, appName) { + if foundIndex != -1 { + fmt.Printf("unable to find %s because there were multiple matches", appName) + } + foundIndex = i + } + } + if foundIndex == -1 { + YellowConfirm("you are trying to install the application %s, but this application does not exist\n", appName) + os.Exit(1) + } + + if len(appList[foundIndex].Plans) > 0 { + allPlan := []string{} + + for pk := range appList[foundIndex].Plans { + allPlan = append(allPlan, appList[foundIndex].Plans[pk].Label) + } + + _, found := find(allPlan, planName) + if !found { + YellowConfirm("the plan you gave doesn't exist for %s; we've picked a default one for you\n", appName) + return fmt.Sprintf("%s:%s", appName, appList[foundIndex].Plans[0].Label), nil + } + + return requested, nil + } + + if planName != "" { + YellowConfirm("you are trying to install %s application with a plan but this application has no plans\n", appName) + os.Exit(1) + } + + return requested, nil +} + +// RequestedSplit is a function to split all app requested to be install +func RequestedSplit(appList []civogo.KubernetesMarketplaceApplication, requested string) string { + allsplit := strings.Split(requested, ",") + allApp := []string{} + + for i := range allsplit { + checkApp, err := checkAppPlan(appList, allsplit[i]) + if err != nil { + fmt.Print(err) + } + + allApp = append(allApp, checkApp) + } + + return strings.Join(allApp, ", ") +} + +// function to search if the string is in the slice +func find(slice []string, val string) (int, bool) { + for i, item := range slice { + if item == val { + return i, true + } + } + return -1, false +}