-
Notifications
You must be signed in to change notification settings - Fork 5
/
update.go
102 lines (92 loc) · 2.78 KB
/
update.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
package pod
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"
)
// Update updates pod from type string, []byte, *corev1.pod, corev1.pod,
// metav1.Object, runtime.Object, *unstructured.Unstructured, unstructured.Unstructured
// or map[string]interface{}.
func (h *Handler) Update(obj interface{}) (*corev1.Pod, error) {
switch val := obj.(type) {
case string:
return h.UpdateFromFile(val)
case []byte:
return h.UpdateFromBytes(val)
case *corev1.Pod:
return h.UpdateFromObject(val)
case corev1.Pod:
return h.UpdateFromObject(&val)
case *unstructured.Unstructured:
return h.UpdateFromUnstructured(val)
case unstructured.Unstructured:
return h.UpdateFromUnstructured(&val)
case map[string]interface{}:
return h.UpdateFromMap(val)
case metav1.Object, runtime.Object:
return h.UpdateFromObject(val)
default:
return nil, ErrInvalidUpdateType
}
}
// UpdateFromFile updates pod from yaml or json file.
func (h *Handler) UpdateFromFile(filename string) (*corev1.Pod, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return h.UpdateFromBytes(data)
}
// UpdateFromBytes updates pod from bytes data.
func (h *Handler) UpdateFromBytes(data []byte) (*corev1.Pod, error) {
podJson, err := yaml.ToJSON(data)
if err != nil {
return nil, err
}
pod := &corev1.Pod{}
if err = json.Unmarshal(podJson, pod); err != nil {
return nil, err
}
return h.updatePod(pod)
}
// UpdateFromObject updates pod from metav1.Object or runtime.Object.
func (h *Handler) UpdateFromObject(obj interface{}) (*corev1.Pod, error) {
pod, ok := obj.(*corev1.Pod)
if !ok {
return nil, fmt.Errorf("object type is not *corev1.Pod")
}
return h.updatePod(pod)
}
// UpdateFromUnstructured updates pod from *unstructured.Unstructured.
func (h *Handler) UpdateFromUnstructured(u *unstructured.Unstructured) (*corev1.Pod, error) {
pod := &corev1.Pod{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), pod)
if err != nil {
return nil, err
}
return h.updatePod(pod)
}
// UpdateFromMap updates pod from map[string]interface{}.
func (h *Handler) UpdateFromMap(u map[string]interface{}) (*corev1.Pod, error) {
pod := &corev1.Pod{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u, pod)
if err != nil {
return nil, err
}
return h.updatePod(pod)
}
// updatePod
func (h *Handler) updatePod(pod *corev1.Pod) (*corev1.Pod, error) {
namespace := pod.GetNamespace()
if len(namespace) == 0 {
namespace = h.namespace
}
pod.UID = ""
pod.ResourceVersion = ""
return h.clientset.CoreV1().Pods(namespace).Update(h.ctx, pod, h.Options.UpdateOptions)
}