/
delete.go
52 lines (43 loc) · 1.04 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package client
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
)
// Test-ability: isolate deleteCtl to build and return exec.Cmd from DeleteOpts
func (k Kubectl) deleteCtl(namespace, kind, name string, opts DeleteOpts) *exec.Cmd {
argv := []string{
"-n", namespace,
kind, name,
}
if opts.Force {
argv = append(argv, "--force")
}
if opts.DryRun != "" {
dryRun := fmt.Sprintf("--dry-run=%s", opts.DryRun)
argv = append(argv, dryRun)
}
return k.ctl("delete", argv...)
}
// Delete deletes the given Kubernetes resource from the cluster
func (k Kubectl) Delete(namespace, kind, name string, opts DeleteOpts) error {
cmd := k.deleteCtl(namespace, kind, name, opts)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
if strings.Contains(stderr.String(), "Error from server (NotFound):") {
print("Delete failed: " + stderr.String())
return nil
}
return err
}
if opts.DryRun != "" {
print(stdout.String())
}
return nil
}