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
16 changes: 13 additions & 3 deletions cmd/kubernetes/kubernetes_nodepool_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,18 @@ var kubernetesNodePoolDeleteCmd = &cobra.Command{
}

kubernetesPoolNameList = nil
notFound := []string{}
for _, kubeList := range kubernetesNodePoolList {
before := len(kubernetesPoolNameList)
nodePool, kubernetesPoolNameList = utility.RemoveNodePool(nodePool, kubeList.Name, kubernetesPoolNameList)
if len(kubernetesPoolNameList) == before {
notFound = append(notFound, kubeList.Name)
}
}

if len(notFound) > 0 {
utility.Error("No node pool found matching %s in cluster %s", strings.Join(notFound, ", "), kubernetesFindCluster.Name)
os.Exit(1)
}

configKubernetes := &civogo.KubernetesClusterConfig{
Expand All @@ -85,14 +95,14 @@ var kubernetesNodePoolDeleteCmd = &cobra.Command{

ow := utility.NewOutputWriter()

for _, v := range kubernetesNodePoolList {
for _, v := range kubernetesPoolNameList {
ow.StartLine()
ow.AppendDataWithLabel("id", v.Name, "ID")
ow.AppendDataWithLabel("id", v, "ID")
}

switch common.OutputFormat {
case "json":
if len(kubernetesNodePoolList) == 1 {
if len(kubernetesPoolNameList) == 1 {
ow.WriteSingleObjectJSON(common.PrettySet)
} else {
ow.WriteMultipleObjectsJSON(common.PrettySet)
Expand Down
8 changes: 6 additions & 2 deletions utility/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,19 @@ func checkKubeDir() {

// RemoveNodePool is a utility function to remove node pool from a kuberentes cluster
func RemoveNodePool(s []civogo.KubernetesClusterPoolConfig, id string, names []string) ([]civogo.KubernetesClusterPoolConfig, []string) {
key := 0
key := -1
for k, v := range s {
if strings.Contains(v.ID, id) {
if strings.HasPrefix(v.ID, id) {
key = k
names = append(names, v.ID)
break
}
}

if key == -1 {
return s, names
}

s[len(s)-1], s[key] = s[key], s[len(s)-1]
return s[:len(s)-1], names
}
Expand Down
98 changes: 98 additions & 0 deletions utility/kubernetes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utility

import (
"slices"
"testing"

"github.com/civo/civogo"
Expand Down Expand Up @@ -72,6 +73,103 @@ func TestRemoveApplicationFromInstalledListWithMultiple(t *testing.T) {
// }
// }

func TestRemoveNodePool(t *testing.T) {
pools := func(ids ...string) []civogo.KubernetesClusterPoolConfig {
s := []civogo.KubernetesClusterPoolConfig{}
for _, id := range ids {
s = append(s, civogo.KubernetesClusterPoolConfig{ID: id})
}
return s
}

tests := []struct {
name string
pools []civogo.KubernetesClusterPoolConfig
remove []string
wantPools []string
wantRemove []string
}{
{
name: "removes the first pool",
pools: pools("aaaaaa-1", "bbbbbb-2", "cccccc-3"),
remove: []string{"aaaaaa"},
wantPools: []string{"bbbbbb-2", "cccccc-3"},
wantRemove: []string{"aaaaaa-1"},
},
{
name: "removes a middle pool",
pools: pools("aaaaaa-1", "bbbbbb-2", "cccccc-3"),
remove: []string{"bbbbbb"},
wantPools: []string{"aaaaaa-1", "cccccc-3"},
wantRemove: []string{"bbbbbb-2"},
},
{
name: "removes the last pool",
pools: pools("aaaaaa-1", "bbbbbb-2", "cccccc-3"),
remove: []string{"cccccc"},
wantPools: []string{"aaaaaa-1", "bbbbbb-2"},
wantRemove: []string{"cccccc-3"},
},
{
name: "empty slice does not panic",
pools: pools(),
remove: []string{"aaaaaa"},
wantPools: []string{},
wantRemove: []string{},
},
{
name: "unknown ID leaves pools unchanged",
pools: pools("aaaaaa-1", "bbbbbb-2"),
remove: []string{"zzzzzz"},
wantPools: []string{"aaaaaa-1", "bbbbbb-2"},
wantRemove: []string{},
},
{
name: "same ID twice removes only the matching pool",
pools: pools("aaaaaa-1", "bbbbbb-2", "cccccc-3"),
remove: []string{"bbbbbb", "bbbbbb"},
wantPools: []string{"aaaaaa-1", "cccccc-3"},
wantRemove: []string{"bbbbbb-2"},
},
{
name: "ID fragment in the middle of another ID does not match",
pools: pools("aaaaaa-bbbbbb", "cccccc-1"),
remove: []string{"bbbbbb"},
wantPools: []string{"aaaaaa-bbbbbb", "cccccc-1"},
wantRemove: []string{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("RemoveNodePool panicked: %v", r)
}
}()

got := tt.pools
names := []string{}
for _, id := range tt.remove {
got, names = RemoveNodePool(got, id, names)
}

gotIDs := []string{}
for _, p := range got {
gotIDs = append(gotIDs, p.ID)
}
slices.Sort(gotIDs)

if !slices.Equal(gotIDs, tt.wantPools) {
t.Errorf("remaining pools = %v, want %v", gotIDs, tt.wantPools)
}
if !slices.Equal(names, tt.wantRemove) {
t.Errorf("removed names = %v, want %v", names, tt.wantRemove)
}
})
}
}

func TestIsAppCompatibleWithClusterType(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading