Repository navigation
fix(kubernetes): guard against panic in RemoveNodePool when slice is empty - #607
Conversation
Signed-off-by: Karthik Rajan <karthikrajanmr@gmail.com>
|
hi @giornetta i have found these minor gap fix the panic state when slice is empty and fix these issue . |
|
Thanks for spotting this, @karthik120710! The guard does stop the panic on an empty slice. Looking at RemoveNodePool more closely the same root cause leads to a more serious bug, and I think we should fix both in this PR. key starts at 0, and the loop only changes it when an ID matches. If no pool matches, the swap and truncate still run, so the function removes s[0]. The caller (cmd/kubernetes/kubernetes_nodepool_delete.go:73) then sends that shortened list to UpdateKubernetesCluster, which means that a command like this: And that passing the same pool ID twice removes the matching pool, then a second, unrelated one. I suggest you to add these testcases into to help you working on the fix. |
…nd issues files Signed-off-by: Karthik Rajan <karthikrajanmr@gmail.com>
|
hi @fulviodenza i have implemented ur suggetions , sorry for the delay . |
fulviodenza
left a comment
There was a problem hiding this comment.
Thanks @karthik120710, this looks good! RemoveNodePool now leaves the pools untouched when nothing matches, a repeated ID no longer removes an unrelated pool, and all the test cases pass (CI is green too).
A few things before we merge:
1. A wrong ID still reports success
RemoveNodePool is fixed, but the caller in cmd/kubernetes/kubernetes_nodepool_delete.go never checks whether anything was actually removed. So civo k8s node-pool delete mycluster typo99 now calls UpdateKubernetesCluster with the unchanged pools, prints The node pool () has been deleted from the cluster (mycluster) and exits 0. With -o json it even lists typo99 as a deleted ID.
Could you fail before calling the API when an ID doesn't match? Something like:
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)
}It would also be nice if the JSON output loop printed the matched full IDs from kubernetesPoolNameList instead of the strings the user typed.
2. PR title and description
They only describe the empty-slice guard. Could you update them to cover the wrong-pool deletion fix and the switch from strings.Contains to strings.HasPrefix? The second one is a behaviour change: a fragment from the middle of an ID used to match and is now rejected.
3. Commit message
The commit message for 26c496a says "add documentation and issues files", but that commit only touches utility/kubernetes.go and the test. We can fix this when squashing on merge, something like fix(kubernetes): RemoveNodePool removed the wrong pool when no ID matched.
Nit (optional)
The len(s) == 0 guard is now redundant: with an empty slice the loop never runs, key stays -1 and the key == -1 check already returns early. Fine to drop it.
|
Whenever you fix these, we are ready to merge |
…dd feature/issue documentation files Signed-off-by: Karthik Rajan <karthikrajanmr@gmail.com>
|
@fulviodenza ready to merge. |
|
LGTM! |
Description
Summary
RemoveNodePoolinutility/kubernetes.gopanicked when called with an empty slice due to an uncheckeds[len(s)-1]index accesslen(s) == 0to prevent the runtime panicChanges
utility/kubernetes.go: Add empty slice guard at the top ofRemoveNodePoolCloses #606