Skip to content

Commit

Permalink
Added the option to install multiple application at the same time in …
Browse files Browse the repository at this point in the history
…the Kubernetes Cluster

Now we check if the application is valid and and if have a plan the plan is valid

Signed-off-by: Alejandro JNM <[email protected]>
  • Loading branch information
alejandrojnm committed Jul 8, 2020
1 parent d6cc1d4 commit 611120b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
11 changes: 9 additions & 2 deletions cmd/kubernetes_app_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
79 changes: 79 additions & 0 deletions utility/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"os/exec"
"strings"

"github.com/civo/civogo"
)

// ObtainKubeConfig is the function to get the kubeconfig from the cluster
Expand Down Expand Up @@ -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
}

0 comments on commit 611120b

Please sign in to comment.