-
Notifications
You must be signed in to change notification settings - Fork 5
/
create.go
107 lines (97 loc) · 3.02 KB
/
create.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
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"
)
// Create creates configmap from type string, []byte, *corev1.ConfigMap,
// corev1.ConfigMap, runtime.Object, *unstructured.Unstructured,
// unstructured.Unstructured or map[string]interface{}.
func (h *Handler) Create(obj interface{}) (*corev1.ConfigMap, error) {
switch val := obj.(type) {
case string:
return h.CreateFromFile(val)
case []byte:
return h.CreateFromBytes(val)
case *corev1.ConfigMap:
return h.CreateFromObject(val)
case corev1.ConfigMap:
return h.CreateFromObject(&val)
case runtime.Object:
if reflect.TypeOf(val).String() == "*unstructured.Unstructured" {
return h.CreateFromUnstructured(val.(*unstructured.Unstructured))
}
return h.CreateFromObject(val)
case *unstructured.Unstructured:
return h.CreateFromUnstructured(val)
case unstructured.Unstructured:
return h.CreateFromUnstructured(&val)
case map[string]interface{}:
return h.CreateFromMap(val)
default:
return nil, ErrInvalidCreateType
}
}
// CreateFromFile creates configmap from yaml file.
func (h *Handler) CreateFromFile(filename string) (*corev1.ConfigMap, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return h.CreateFromBytes(data)
}
// CreateFromBytes creates configmap from bytes.
func (h *Handler) CreateFromBytes(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.createConfigmap(cm)
}
// CreateFromObject creates configmap from runtime.Object.
func (h *Handler) CreateFromObject(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.createConfigmap(cm)
}
// CreateFromUnstructured creates configmap from *unstructured.Unstructured.
func (h *Handler) CreateFromUnstructured(u *unstructured.Unstructured) (*corev1.ConfigMap, error) {
cm := &corev1.ConfigMap{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), cm)
if err != nil {
return nil, err
}
return h.createConfigmap(cm)
}
// CreateFromMap creates configmap from map[string]interface{}.
func (h *Handler) CreateFromMap(u map[string]interface{}) (*corev1.ConfigMap, error) {
cm := &corev1.ConfigMap{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u, cm)
if err != nil {
return nil, err
}
return h.createConfigmap(cm)
}
// createConfigmap
func (h *Handler) createConfigmap(cm *corev1.ConfigMap) (*corev1.ConfigMap, error) {
var namespace string
if len(cm.Namespace) != 0 {
namespace = cm.Namespace
} else {
namespace = h.namespace
}
cm.ResourceVersion = ""
cm.UID = ""
return h.clientset.CoreV1().ConfigMaps(namespace).Create(h.ctx, cm, h.Options.CreateOptions)
}