Prevent env deletion if any function exists (#2343)

* Added force deletion flag for env
This commit is contained in:
Ankit Chawla
2022-02-07 16:20:54 +05:30
committed by GitHub
parent 5cb3ebf262
commit e8ff79e448
4 changed files with 18 additions and 1 deletions
+1 -1
View File
@@ -72,7 +72,7 @@ func Commands() *cobra.Command {
}
wrapper.SetFlags(deleteCmd, flag.FlagSet{
Required: []flag.Flag{flag.EnvName},
Optional: []flag.Flag{flag.NamespaceEnvironment, flag.IgnoreNotFound},
Optional: []flag.Flag{flag.NamespaceEnvironment, flag.IgnoreNotFound, flag.EnvForce},
})
listCmd := &cobra.Command{
+15
View File
@@ -42,6 +42,20 @@ func (opts *DeleteSubCommand) do(input cli.Input) error {
Namespace: input.String(flagkey.NamespaceEnvironment),
}
if !input.Bool(flagkey.EnvForce) {
fns, err := opts.Client().V1().Function().List(metav1.NamespaceAll)
if err != nil {
return errors.Wrap(err, "Error getting functions wrt environment.")
}
for _, fn := range fns {
if fn.Spec.Environment.Name == m.Name &&
fn.Spec.Environment.Namespace == m.Namespace {
return errors.New("Environment is used by atleast one function.")
}
}
}
err := opts.Client().V1().Environment().Delete(m)
if err != nil {
if input.Bool(flagkey.IgnoreNotFound) && util.IsNotFound(err) {
@@ -51,5 +65,6 @@ func (opts *DeleteSubCommand) do(input cli.Input) error {
}
fmt.Printf("environment '%v' deleted\n", m.Name)
return nil
}