-
Notifications
You must be signed in to change notification settings - Fork 5
/
apply.go
93 lines (84 loc) · 2.79 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
86
87
88
89
90
91
92
93
package daemonset
import (
"fmt"
appsv1 "k8s.io/api/apps/v1"
k8serrors "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"
)
// Apply applies daemonset from type string, []byte, *appsv1.DaemonSet,
// appsv1.DaemonSet, metav1.Object, runtime.Object, *unstructured.Unstructured,
// unstructured.Unstructured or map[string]interface{}.
func (h *Handler) Apply(obj interface{}) (*appsv1.DaemonSet, error) {
switch val := obj.(type) {
case string:
return h.ApplyFromFile(val)
case []byte:
return h.ApplyFromBytes(val)
case *appsv1.DaemonSet:
return h.ApplyFromObject(val)
case appsv1.DaemonSet:
return h.ApplyFromObject(&val)
case *unstructured.Unstructured:
return h.ApplyFromUnstructured(val)
case unstructured.Unstructured:
return h.ApplyFromUnstructured(&val)
case map[string]interface{}:
return h.ApplyFromMap(val)
case metav1.Object, runtime.Object:
return h.ApplyFromObject(val)
default:
return nil, ErrInvalidApplyType
}
}
// ApplyFromFile applies daemonset from yaml or json file.
func (h *Handler) ApplyFromFile(filename string) (ds *appsv1.DaemonSet, err error) {
ds, err = h.CreateFromFile(filename)
if k8serrors.IsAlreadyExists(err) { // if daemonset already exist, update it.
ds, err = h.UpdateFromFile(filename)
}
return
}
// ApplyFromBytes pply daemonset from bytes data.
func (h *Handler) ApplyFromBytes(data []byte) (ds *appsv1.DaemonSet, err error) {
ds, err = h.CreateFromBytes(data)
if k8serrors.IsAlreadyExists(err) {
ds, err = h.UpdateFromBytes(data)
}
return
}
// ApplyFromObject applies daemonset from metav1.Object or runtime.Object.
func (h *Handler) ApplyFromObject(obj interface{}) (*appsv1.DaemonSet, error) {
ds, ok := obj.(*appsv1.DaemonSet)
if !ok {
return nil, fmt.Errorf("object type is not *appsv1.DaemonSet")
}
return h.applyDaemonset(ds)
}
// ApplyFromUnstructured applies daemonset from *unstructured.Unstructured.
func (h *Handler) ApplyFromUnstructured(u *unstructured.Unstructured) (*appsv1.DaemonSet, error) {
ds := &appsv1.DaemonSet{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), ds)
if err != nil {
return nil, err
}
return h.applyDaemonset(ds)
}
// ApplyFromMap applies daemonset from map[string]interface{}.
func (h *Handler) ApplyFromMap(u map[string]interface{}) (*appsv1.DaemonSet, error) {
ds := &appsv1.DaemonSet{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u, ds)
if err != nil {
return nil, err
}
return h.applyDaemonset(ds)
}
// applyDaemonset
func (h *Handler) applyDaemonset(ds *appsv1.DaemonSet) (*appsv1.DaemonSet, error) {
_, err := h.createDaemonset(ds)
if k8serrors.IsAlreadyExists(err) {
return h.updateDaemonset(ds)
}
return ds, err
}