/
apply.go
85 lines (76 loc) · 2.71 KB
/
apply.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
package dynamic
import (
"encoding/json"
"io/ioutil"
"k8s.io/apimachinery/pkg/api/errors"
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"
)
// Apply applies unstructured k8s resource from type string, []byte, metav1.Object,
// runtime.Object, *unstructured.Unstructured, unstructured.Unstructured
// or map[string]interface{}.
//
// It's not necessary to explicitly specify the GVK or GVR by calling WithGVK(),
// Apply() will find the GVK and GVR by RESTMapper and apply the k8s resource
// that defined in yaml file, json file, bytes data, map[string]interface{}
// or runtime.Object.
func (h *Handler) Apply(obj interface{}) (*unstructured.Unstructured, error) {
switch val := obj.(type) {
case string:
return h.ApplyFromFile(val)
case []byte:
return h.ApplyFromBytes(val)
case *unstructured.Unstructured:
return h.applyUnstructured(val)
case unstructured.Unstructured:
return h.applyUnstructured(&val)
case map[string]interface{}:
return h.ApplyFromMap(val)
case metav1.Object, runtime.Object:
return h.ApplyFromObject(val)
default:
return nil, ErrInvalidApplyType
}
}
// ApplyFromFile applies unstructured k8s resource from yaml or json file.
func (h *Handler) ApplyFromFile(filename string) (*unstructured.Unstructured, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return h.ApplyFromBytes(data)
}
// ApplyFromBytes applies unstructured k8s resource from bytes data.
func (h *Handler) ApplyFromBytes(data []byte) (*unstructured.Unstructured, error) {
unstructJson, err := yaml.ToJSON(data)
if err != nil {
return nil, err
}
unstructObj := &unstructured.Unstructured{}
if err = json.Unmarshal(unstructJson, unstructObj); err != nil {
return nil, err
}
return h.applyUnstructured(unstructObj)
}
// ApplyFromObject applies unstructured k8s resource from metav1.Object or runtime.Object.
func (h *Handler) ApplyFromObject(obj interface{}) (*unstructured.Unstructured, error) {
unstructMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}
return h.applyUnstructured(&unstructured.Unstructured{Object: unstructMap})
}
// ApplyFromMap applies unstructured k8s resource from map[string]interface{}.
func (h *Handler) ApplyFromMap(obj map[string]interface{}) (*unstructured.Unstructured, error) {
return h.applyUnstructured(&unstructured.Unstructured{Object: obj})
}
// applyUnstructured
func (h *Handler) applyUnstructured(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
_, err := h.createUnstructured(obj)
if errors.IsAlreadyExists(err) {
return h.Update(obj)
}
return obj, err
}