-
Notifications
You must be signed in to change notification settings - Fork 5
/
delete.go
108 lines (97 loc) · 3.11 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package configmap
import (
"encoding/json"
"fmt"
"io/ioutil"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
)
// Delete deletes configmap from type string, []byte, *corev1.ConfigMap,
// corev1.ConfigMap, metav1.Object, runtime.Object, *unstructured.Unstructured,
// unstructured.Unstructured or map[string]interface{}.
//
// If passed parameter type is string, it will simply call DeleteByName instead of DeleteFromFile.
// You should always explicitly call DeleteFromFile to delete a configmap from file path.
func (h *Handler) Delete(obj interface{}) error {
switch val := obj.(type) {
case string:
return h.DeleteByName(val)
case []byte:
return h.DeleteFromBytes(val)
case *corev1.ConfigMap:
return h.DeleteFromObject(val)
case corev1.ConfigMap:
return h.DeleteFromObject(&val)
case *unstructured.Unstructured:
return h.DeleteFromUnstructured(val)
case unstructured.Unstructured:
return h.DeleteFromUnstructured(&val)
case map[string]interface{}:
return h.DeleteFromMap(val)
case metav1.Object, runtime.Object:
return h.DeleteFromObject(val)
default:
return ErrInvalidDeleteType
}
}
// DeleteByName deletes configmap by name.
func (h *Handler) DeleteByName(name string) error {
return h.clientset.CoreV1().ConfigMaps(h.namespace).Delete(h.ctx, name, h.Options.DeleteOptions)
}
// DeleteFromFile deletes configmap from yaml or json file.
func (h *Handler) DeleteFromFile(filename string) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return h.DeleteFromBytes(data)
}
// DeleteFromBytes deletes configmap from bytes data.
func (h *Handler) DeleteFromBytes(data []byte) error {
cmJson, err := yaml.ToJSON(data)
if err != nil {
return err
}
cm := &corev1.ConfigMap{}
if err = json.Unmarshal(cmJson, cm); err != nil {
return err
}
return h.deleteConfigmap(cm)
}
// DeleteFromObject deletes configmap from metav1.Object or runtime.Object.
func (h *Handler) DeleteFromObject(obj interface{}) error {
cm, ok := obj.(*corev1.ConfigMap)
if !ok {
return fmt.Errorf("object type is not *corev1.ConfigMap")
}
return h.deleteConfigmap(cm)
}
// DeleteFromUnstructured deletes configmap from *unstructured.Unstructured.
func (h *Handler) DeleteFromUnstructured(u *unstructured.Unstructured) error {
cm := &corev1.ConfigMap{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), cm)
if err != nil {
return err
}
return h.deleteConfigmap(cm)
}
// DeleteFromMap deletes configmap from map[string]interface{}.
func (h *Handler) DeleteFromMap(u map[string]interface{}) error {
cm := &corev1.ConfigMap{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u, cm)
if err != nil {
return err
}
return h.deleteConfigmap(cm)
}
// deleteConfigmap
func (h *Handler) deleteConfigmap(cm *corev1.ConfigMap) error {
namespace := cm.GetNamespace()
if len(namespace) == 0 {
namespace = h.namespace
}
return h.clientset.CoreV1().ConfigMaps(namespace).Delete(h.ctx, cm.Name, h.Options.DeleteOptions)
}