-
Notifications
You must be signed in to change notification settings - Fork 5
/
dynamic_patch.go
167 lines (148 loc) · 6.81 KB
/
dynamic_patch.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"log"
"os"
"time"
"github.com/forbearing/k8s/deployment"
"github.com/forbearing/k8s/dynamic"
"github.com/forbearing/k8s/util/signals"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)
func Dynamic_Patch() {
var (
deployFile = "../../testdata/examples/deployment-patch.yaml"
strategicYamlFile = "../../testdata/examples/deployment-patch-strategic.yaml"
strategicJsonFile = "../../testdata/examples/deployment-patch-strategic.json"
jsontypeYamlFile = "../../testdata/examples/deployment-patch-jsontype.yaml"
jsontypeJsonFile = "../../testdata/examples/deployment-patch-jsontype.json"
deployName = "mydep-patch"
)
ctx := signals.NewSignalContext()
deployHandler := deployment.NewOrDie(ctx, "", namespace)
handler := dynamic.NewOrDie(ctx, "", namespace)
original, err := handler.Apply(deployFile)
if err != nil {
log.Fatal(err)
}
modified := original.DeepCopy()
deployHandler.WaitReady(deployName)
{
log.Println("1.1 **JSON Patch** patch data is a filename and the file content is yaml document")
if _, err := handler.Patch(original, jsontypeYamlFile, types.JSONPatchType); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("1.2 **JSON Patch** patch data is a filename and the file content is json document")
if _, err := handler.Patch(original, jsontypeJsonFile, types.JSONPatchType); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("1.3 **Strategic Merge Patch** patch data is a filename and the file content is yaml document")
if _, err := handler.Patch(original, strategicYamlFile); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("1.4 **Strategic Merge Patch** patch data is a filename and the json content is json document")
if _, err := handler.Patch(original, strategicJsonFile); err != nil {
log.Fatal(err)
}
log.Println("1.5 **JSON Merge Patch** patch data is a filename and the file content is yaml document")
if _, err := handler.Patch(original, strategicYamlFile, types.MergePatchType); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("1.6 **JSON Merge Patch** patch data is a filename and the json content is json document")
if _, err := handler.Patch(original, strategicJsonFile, types.MergePatchType); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
}
{
log.Println("2.1 **Default to Strategic Merge Patch** patch data is []byte and the content is yaml document")
var data []byte
if data, err = os.ReadFile(strategicYamlFile); err != nil {
log.Fatal(err)
}
if _, err := handler.Patch(original, data); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("2.2 **Default to Strategic Merge Patch** patch data is []byte and the content is json document")
if data, err = os.ReadFile(strategicJsonFile); err != nil {
log.Fatal(err)
}
if _, err := handler.Patch(original, data); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
}
{
log.Println("3. **Default to Strategic Merge Patch** patch data is *appsv1.Deployment")
if _, err := handler.Patch(original, modified); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("4. **Default to Strategic Merge Patch** patch data is appsv1.Deployment")
if _, err := handler.Patch(original, *modified); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("5. **Default To Strategic Merge Patch** patch data is map[string]interface{}")
handler.Apply(deployFile)
unstructMap := make(map[string]interface{})
if unstructMap, err = runtime.DefaultUnstructuredConverter.ToUnstructured(modified); err != nil {
log.Fatal(err)
}
if _, err := handler.Patch(original, unstructMap); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("6. **Default to Strategic Merge Patch** patch data is *unstructObj.Unstructured")
handler.Apply(deployFile)
unstructObj := &unstructured.Unstructured{Object: unstructMap}
if _, err := handler.Patch(original, unstructObj); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("7. **Default to Strategic Merge Patch** patch data is unstructObj.Unstructured")
handler.Apply(deployFile)
if _, err := handler.Patch(original, *unstructObj); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("8.1 **Default to Strategic Merge Patch** patch data is runtime.Object(convert from *appsv1.Deployment)")
handler.Apply(deployFile)
object := runtime.Object(modified)
if _, err := handler.Patch(original, object); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
log.Println("8.2 **Default to Strategic Merge Patch** patch data is runtime.Object(convert from *unstructured.Unstructured)")
handler.Apply(deployFile)
object = runtime.Object(unstructObj)
if _, err := handler.Patch(original, object); err != nil {
log.Fatal(err)
}
deployHandler.WaitReady(deployName)
time.Sleep(time.Second * 5)
handler.WithGVK(deployment.GVK).Delete(deployName)
}
// Output
//2022/09/16 09:33:14 1.1 **JSON Patch** patch data is a filename and the file content is yaml document
//2022/09/16 09:33:18 1.2 **JSON Patch** patch data is a filename and the file content is json document
//2022/09/16 09:33:37 1.3 **Strategic Merge Patch** patch data is a filename and the file content is yaml document
//2022/09/16 09:33:46 1.4 **Strategic Merge Patch** patch data is a filename and the json content is json document
//2022/09/16 09:33:46 1.5 **JSON Merge Patch** patch data is a filename and the file content is yaml document
//2022/09/16 09:33:54 1.6 **JSON Merge Patch** patch data is a filename and the json content is json document
//2022/09/16 09:34:03 2.1 **Default to Strategic Merge Patch** patch data is []byte and the content is yaml document
//2022/09/16 09:34:16 2.2 **Default to Strategic Merge Patch** patch data is []byte and the content is json document
//2022/09/16 09:34:16 3. **Default to Strategic Merge Patch** patch data is *appsv1.Deployment
//2022/09/16 09:34:16 4. **Default to Strategic Merge Patch** patch data is appsv1.Deployment
//2022/09/16 09:34:16 5. **Default To Strategic Merge Patch** patch data is map[string]interface{}
//2022/09/16 09:34:21 6. **Default to Strategic Merge Patch** patch data is *unstructObj.Unstructured
//2022/09/16 09:34:21 7. **Default to Strategic Merge Patch** patch data is unstructObj.Unstructured
//2022/09/16 09:34:21 8.1 **Default to Strategic Merge Patch** patch data is runtime.Object(convert from *appsv1.Deployment)
//2022/09/16 09:34:21 8.2 **Default to Strategic Merge Patch** patch data is runtime.Object(convert from *unstructured.Unstructured)
}