Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/kubernetes/kubernetes_app_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var kubernetesAppAddCmd = &cobra.Command{
os.Exit(1)
}

result := utility.RequestedSplit(appList, args[0])
result := utility.RequestedSplit(appList, args[0], kubernetesFindCluster.ClusterType)
configKubernetes := &civogo.KubernetesClusterConfig{
Applications: result,
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.26.3
go 1.26.4
Comment thread
hlts2 marked this conversation as resolved.

module github.com/civo/cli

Expand Down
40 changes: 34 additions & 6 deletions utility/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func writeConfig(path string, data []byte, suppressMessage bool, mergeConfigs bo
// 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) {
func checkAppPlan(appList []civogo.KubernetesMarketplaceApplication, requested string) (int, string, error) {
foundIndex := -1
parts := strings.SplitN(requested, ":", 2)
appName := parts[0]
Expand Down Expand Up @@ -161,29 +161,35 @@ func checkAppPlan(appList []civogo.KubernetesMarketplaceApplication, requested s
_, 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 foundIndex, fmt.Sprintf("%s:%s", appName, appList[foundIndex].Plans[0].Label), nil
}

return requested, nil
return foundIndex, 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
return foundIndex, requested, nil
}

// RequestedSplit is a function to split all app requested to be installed
func RequestedSplit(appList []civogo.KubernetesMarketplaceApplication, requested string) string {
func RequestedSplit(appList []civogo.KubernetesMarketplaceApplication, requested, clusterType string) string {
allsplit := strings.Split(requested, ",")
allApp := []string{}

for i := range allsplit {
checkApp, err := checkAppPlan(appList, allsplit[i])
foundIdx, checkApp, err := checkAppPlan(appList, allsplit[i])
if err != nil {
fmt.Print(err)
continue
}

if !isAppCompatibleWithClusterType(appList[foundIdx].Name, clusterType) {
Error("%s cannot be installed via the marketplace on %s clusters", appList[foundIdx].Name, clusterType)
os.Exit(1)
}

allApp = append(allApp, checkApp)
Expand All @@ -192,6 +198,28 @@ func RequestedSplit(appList []civogo.KubernetesMarketplaceApplication, requested
return strings.Join(allApp, ",")
}

// unsupportedAppsByClusterType lists marketplace apps that cannot be
// installed on a given cluster type.
//
// TODO: this table is hardcoded because civogo.KubernetesMarketplaceApplication
// does not expose the app's supported cluster_type. The marketplace manifest
// already declares it (e.g.
// https://github.com/civo/kubernetes-marketplace/blob/master/metrics-server/manifest.yaml),
// so once that field is surfaced through the API/civogo, this comparison should
// be replaced by checking the application's own cluster_type and this table removed.
var unsupportedAppsByClusterType = map[string][]string{
"talos": {"metrics-server"},
}

func isAppCompatibleWithClusterType(requestedApp, clusterType string) bool {
for _, app := range unsupportedAppsByClusterType[clusterType] {
if app == requestedApp {
return false
}
}
return true
}

// RemoveApplicationFromInstalledList is a function to split all currently installed apps and remove one being uninstalled
func RemoveApplicationFromInstalledList(current []civogo.KubernetesInstalledApplication, uninstall string) string {
appsToRemove := strings.Split(uninstall, ",")
Expand Down
48 changes: 48 additions & 0 deletions utility/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,51 @@ func TestRemoveApplicationFromInstalledListWithMultiple(t *testing.T) {
// t.Errorf("expected 'mysql', got '%s'", ret)
// }
// }

func TestIsAppCompatibleWithClusterType(t *testing.T) {
tests := []struct {
name string
requestedApp string
clusterType string
want bool
}{
{
name: "metrics-server is not compatible with talos",
requestedApp: "metrics-server",
clusterType: "talos",
want: false,
},
{
name: "metrics-server is compatible with k3s",
requestedApp: "metrics-server",
clusterType: "k3s",
want: true,
},
{
name: "other app is compatible with talos",
requestedApp: "traefik2-nodeport",
clusterType: "talos",
want: true,
},
{
name: "other app is compatible with k3s",
requestedApp: "traefik2-nodeport",
clusterType: "k3s",
want: true,
},
{
name: "unknown cluster type has no restrictions",
requestedApp: "metrics-server",
clusterType: "",
want: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isAppCompatibleWithClusterType(tt.requestedApp, tt.clusterType); got != tt.want {
t.Errorf("isAppCompatibleWithClusterType(%q, %q) = %v, want %v", tt.requestedApp, tt.clusterType, got, tt.want)
}
})
}
}
Loading