-
Notifications
You must be signed in to change notification settings - Fork 5
/
get.go
115 lines (104 loc) · 3.39 KB
/
get.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
109
110
111
112
113
114
115
package configmap
import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
)
// Get gets configmap from type string, []byte, *corev1.ConfigMap,
// corev1.ConfigMap, runtime.Object, *unstructured.Unstructured,
// unstructured.Unstructured or map[string]interface{}.
//
// If passed parameter type is string, it will simply call GetByName instead of GetFromFile.
// You should always explicitly call GetFromFile to get a configmap from file path.
func (h *Handler) Get(obj interface{}) (*corev1.ConfigMap, error) {
switch val := obj.(type) {
case string:
return h.GetByName(val)
case []byte:
return h.GetFromBytes(val)
case *corev1.ConfigMap:
return h.GetFromObject(val)
case corev1.ConfigMap:
return h.GetFromObject(&val)
case runtime.Object:
if reflect.TypeOf(val).String() == "*unstructured.Unstructured" {
return h.GetFromUnstructured(val.(*unstructured.Unstructured))
}
return h.GetFromObject(val)
case *unstructured.Unstructured:
return h.GetFromUnstructured(val)
case unstructured.Unstructured:
return h.GetFromUnstructured(&val)
case map[string]interface{}:
return h.GetFromMap(val)
default:
return nil, ErrInvalidGetType
}
}
// GetByName gets configmap by name.
func (h *Handler) GetByName(name string) (*corev1.ConfigMap, error) {
return h.clientset.CoreV1().ConfigMaps(h.namespace).Get(h.ctx, name, h.Options.GetOptions)
}
// GetFromFile gets configmap from yaml file.
func (h *Handler) GetFromFile(filename string) (*corev1.ConfigMap, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return h.GetFromBytes(data)
}
// GetFromBytes gets configmap from bytes.
func (h *Handler) GetFromBytes(data []byte) (*corev1.ConfigMap, error) {
cmJson, err := yaml.ToJSON(data)
if err != nil {
return nil, err
}
cm := &corev1.ConfigMap{}
if err = json.Unmarshal(cmJson, cm); err != nil {
return nil, err
}
return h.getConfigmap(cm)
}
// GetFromObject gets configmap from runtime.Object.
func (h *Handler) GetFromObject(obj runtime.Object) (*corev1.ConfigMap, error) {
cm, ok := obj.(*corev1.ConfigMap)
if !ok {
return nil, fmt.Errorf("object type is not *corev1.ConfigMap")
}
return h.getConfigmap(cm)
}
// GetFromUnstructured gets configmap from *unstructured.Unstructured.
func (h *Handler) GetFromUnstructured(u *unstructured.Unstructured) (*corev1.ConfigMap, error) {
cm := &corev1.ConfigMap{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), cm)
if err != nil {
return nil, err
}
return h.getConfigmap(cm)
}
// GetFromMap gets configmap from map[string]interface{}.
func (h *Handler) GetFromMap(u map[string]interface{}) (*corev1.ConfigMap, error) {
cm := &corev1.ConfigMap{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u, cm)
if err != nil {
return nil, err
}
return h.getConfigmap(cm)
}
// getConfigmap
// It's necessary to get a new configmap resource from a old configmap resource,
// because old configmap usually don't have configmap.Status field.
func (h *Handler) getConfigmap(cm *corev1.ConfigMap) (*corev1.ConfigMap, error) {
var namespace string
if len(cm.Namespace) != 0 {
namespace = cm.Namespace
} else {
namespace = h.namespace
}
return h.clientset.CoreV1().ConfigMaps(namespace).Get(h.ctx, cm.Name, h.Options.GetOptions)
}