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

Commit 250326e

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

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

docs/user-guide/how-to.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ Instead, you must delete its anchor (note that `subns` is a short form of
236236
$ kubectl delete subns child -n parent
237237
```
238238

239+
You can also use the `kubectl-hns` plugin to delete subnamespaces.
240+
241+
```
242+
$ kubectl hns delete child -n parent
243+
```
244+
239245
This _seems_ to imply that if you delete a _parent_ namespace, all its
240246
subnamespace children (and their descendants) will be deleted as well, since all
241247
objects in a namespace (such as anchors) are deleted along with the namespace.

docs/user-guide/quickstart.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,11 @@ kubectl delete subns service-3 -n team-a
605605
Note that `subns` is a short form for `subnamespaceanchor` or
606606
`subnamespaceanchor.hnc.x-k8s.io`.
607607

608+
You can also use the `kubectl-hns` plugin to delete subnamespaces.
609+
610+
```bash
611+
kubectl hns delete service-3 -n team-a
612+
```
608613

609614
Now try to delete `service-1` in the same way, but you'll see it doesn't work:
610615

internal/kubectl/delete.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package kubectl
17+
18+
import (
19+
"fmt"
20+
"os"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
var deleteCmd = &cobra.Command{
26+
Use: "delete -n PARENT CHILD",
27+
Short: "Deletes a subnamespace under the given parent.",
28+
Args: cobra.ExactArgs(1),
29+
Run: func(cmd *cobra.Command, args []string) {
30+
parent, _ := cmd.Flags().GetString("namespace")
31+
if parent == "" {
32+
fmt.Println("Error: parent must be set via --namespace or -n")
33+
os.Exit(1)
34+
}
35+
36+
client.deleteAnchor(parent, args[0])
37+
},
38+
}
39+
40+
func newDeleteCmd() *cobra.Command {
41+
deleteCmd.Flags().StringP("namespace", "n", "", "The parent namespace for the new subnamespace")
42+
return deleteCmd
43+
}

internal/kubectl/root.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Client interface {
4949
getHierarchy(nnm string) *api.HierarchyConfiguration
5050
updateHierarchy(hier *api.HierarchyConfiguration, reason string)
5151
createAnchor(nnm string, hnnm string)
52+
deleteAnchor(nnm string, hnnm string)
5253
getAnchorStatus(nnm string) anchorStatus
5354
getHNCConfig() *api.HNCConfiguration
5455
updateHNCConfig(*api.HNCConfiguration)
@@ -102,6 +103,7 @@ func init() {
102103
rootCmd.AddCommand(newDescribeCmd())
103104
rootCmd.AddCommand(newTreeCmd())
104105
rootCmd.AddCommand(newCreateCmd())
106+
rootCmd.AddCommand(newDeleteCmd())
105107
rootCmd.AddCommand(newConfigCmd())
106108
rootCmd.AddCommand(newVersionCmd())
107109
rootCmd.AddCommand(newHrqCmd())
@@ -186,6 +188,18 @@ func (cl *realClient) createAnchor(nnm string, hnnm string) {
186188
fmt.Printf("Successfully created %q subnamespace anchor in %q namespace\n", hnnm, nnm)
187189
}
188190

191+
func (cl *realClient) deleteAnchor(nnm string, hnnm string) {
192+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
193+
defer cancel()
194+
195+
err := hncClient.Delete().Resource(api.Anchors).Namespace(nnm).Name(hnnm).Do(ctx).Error()
196+
if err != nil {
197+
fmt.Printf("\nCould not delete subnamespace anchor.\nReason: %s\n", err)
198+
os.Exit(1)
199+
}
200+
fmt.Printf("Successfully deleted %q subnamespace anchor in %q namespace\n", hnnm, nnm)
201+
}
202+
189203
func (cl *realClient) getHNCConfig() *api.HNCConfiguration {
190204
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
191205
defer cancel()

test/e2e/quickstart_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ spec:
253253
// show how to delete a subns correctly
254254
MustNotRun("kubectl delete ns", nsService3)
255255
MustRun("kubectl delete subns", nsService3, "-n", nsTeamA)
256+
257+
// show how to delete a subns correctly with the kubectl-hns plugin
258+
CreateSubnamespace(nsService4, nsTeamA)
259+
MustNotRun("kubectl hns delete", nsService4)
260+
MustRun("kubectl hns delete", nsService4, "-n", nsTeamA)
261+
256262
// This should not run because service-1 contains its own subnamespace that would be deleted with it,
257263
MustNotRun("kubectl delete subns", nsService1, "-n", nsTeamA)
258264

0 commit comments

Comments
 (0)