|
| 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 | + 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), |
| 34 | + Run: func(cmd *cobra.Command, args []string) { |
| 35 | + nnm := args[0] |
| 36 | + |
| 37 | + parent, _ := cmd.Flags().GetString("namespace") |
| 38 | + if parent == "" { |
| 39 | + fmt.Println("Error: parent must be set via --namespace or -n") |
| 40 | + os.Exit(1) |
| 41 | + } |
| 42 | + |
| 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) |
| 56 | + }, |
| 57 | +} |
| 58 | + |
| 59 | +func newDeleteCmd() *cobra.Command { |
| 60 | + 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.") |
| 62 | + return deleteCmd |
| 63 | +} |
0 commit comments