-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
79 lines (65 loc) · 1.58 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"flag"
"log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
var (
kubeConfig string
namespace string
name string
)
func init() {
flag.StringVar(&kubeConfig, "kubeConfig", "", "Give the namespace name (optional)")
flag.StringVar(&namespace, "namespace", "", "*Give the namespace name")
flag.StringVar(&name, "name", "", "*Give the resource name")
}
func loadKubeConfig() (*rest.Config, error) {
if kubeConfig != "" {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
return nil, err
}
return cfg, nil
}
cfg, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
return cfg, nil
}
func main() {
flag.Parse()
if namespace == "" {
log.Println("Usage")
flag.PrintDefaults()
log.Fatal("Namespce is not given")
}
if name == "" {
log.Println("Usage")
flag.PrintDefaults()
log.Fatal("Resoutce name is not given")
}
log.Printf("Loading kube config")
config, err := loadKubeConfig()
if err != nil {
log.Fatal(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
policy := metav1.DeletePropagationOrphan
noCascade := metav1.DeleteOptions{
PropagationPolicy: &policy,
}
//Note: with client-go v0.18 + need the context parameter
err = clientset.AppsV1().StatefulSets(namespace).Delete(name, &noCascade)
if err != nil {
log.Fatal(err)
}
log.Printf("Deleted the resource <%s> sucessfully, however dependent resources are not deleted such as pods\n", name)
}