Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 702456e

Browse files
committed
kubectl-hns: Add --allowCascadingDeletion to "delete" command
Signed-off-by: Kazuki Suda <[email protected]>
1 parent 250326e commit 702456e

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

docs/user-guide/quickstart.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,12 @@ Now you can (unsafely!) delete `service-1`:
643643
kubectl delete subns service-1 -n team-a
644644
```
645645

646+
You can also do the above steps in one command run with the `kubectl-hns` plugin:
647+
648+
```bash
649+
kubectl hns delete service-1 -n team-a --allowCascadingDeletion
650+
```
651+
646652
There's an important difference between subnamespaces and regular child
647653
namespace, also known as a full namespace. A subnamespace is created by HNC due
648654
to an anchor being created in the parent; when that anchor is deleted, the

internal/kubectl/delete.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,39 @@ import (
2525
var deleteCmd = &cobra.Command{
2626
Use: "delete -n PARENT CHILD",
2727
Short: "Deletes a subnamespace under the given parent.",
28-
Args: cobra.ExactArgs(1),
28+
Example: `# Delete the 'foo' anchor in the parent 'bar' namespace
29+
kubectl hns delete foo -n bar
30+
31+
# Allow 'foo' to be cascading deleted and delete it
32+
kubectl hns delete foo -n bar --allowCascadingDeletion`,
33+
Args: cobra.ExactArgs(1),
2934
Run: func(cmd *cobra.Command, args []string) {
35+
nnm := args[0]
36+
3037
parent, _ := cmd.Flags().GetString("namespace")
3138
if parent == "" {
3239
fmt.Println("Error: parent must be set via --namespace or -n")
3340
os.Exit(1)
3441
}
3542

36-
client.deleteAnchor(parent, args[0])
43+
allowCD, _ := cmd.Flags().GetBool("allowCascadingDeletion")
44+
if allowCD {
45+
hc := client.getHierarchy(nnm)
46+
if hc.Spec.AllowCascadingDeletion {
47+
fmt.Printf("Cascading deletion for '%s' is already set to 'true'; unchanged\n", nnm)
48+
} else {
49+
fmt.Printf("Allowing cascading deletion on '%s'\n", nnm)
50+
hc.Spec.AllowCascadingDeletion = true
51+
client.updateHierarchy(hc, fmt.Sprintf("update the hierarchical configuration of %s", nnm))
52+
}
53+
}
54+
55+
client.deleteAnchor(parent, nnm)
3756
},
3857
}
3958

4059
func newDeleteCmd() *cobra.Command {
4160
deleteCmd.Flags().StringP("namespace", "n", "", "The parent namespace for the new subnamespace")
61+
deleteCmd.Flags().BoolP("allowCascadingDeletion", "a", false, "Allows cascading deletion of its subnamespaces.")
4262
return deleteCmd
4363
}

test/e2e/quickstart_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,21 @@ spec:
269269
"└── [s] " + nsService2
270270
RunShouldContain(expected, defTimeout, "kubectl hns tree", nsTeamA)
271271

272+
// cascading deletion with the kubectl-hns plugin
273+
CreateSubnamespace(nsService1, nsTeamA)
274+
CreateSubnamespace(nsService4, nsService1)
275+
expected = "" +
276+
nsService1 + "\n" +
277+
"└── [s] " + nsService4
278+
RunShouldContain(expected, defTimeout, "kubectl hns tree", nsService1, "-n", nsTeamA)
279+
MustNotRun("kubectl hns delete", nsService1, "-n", nsTeamA)
280+
MustRun("kubectl hns delete", nsService1, "-n", nsTeamA, "--allowCascadingDeletion")
281+
expected = "" +
282+
nsTeamA + "\n" +
283+
"└── [s] " + nsService2
284+
// cascading deletion of its subnamespaces takes time
285+
RunShouldContain(expected, cleanupTimeout, "kubectl hns tree", nsTeamA)
286+
272287
// Show the difference of a subns and regular child ns
273288
CreateSubnamespace(nsService4, nsTeamA)
274289
expected = "" +

0 commit comments

Comments
 (0)