-
Notifications
You must be signed in to change notification settings - Fork 1
/
kube.go
276 lines (238 loc) · 7.19 KB
/
kube.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package install
import (
"context"
"encoding/base64"
"fmt"
"github.com/pkg/errors"
"github.com/pmezard/go-difflib/difflib"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/yaml"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
var FlagSet *pflag.FlagSet = nil
var showResourceDiffs = false
func init() {
FlagSet = pflag.NewFlagSet("util", pflag.ExitOnError)
FlagSet.BoolVar(&showResourceDiffs, "print-resource-diffs", false, "Enable printing resource diffs for resources that get updated.")
}
func InstallImporterIntoOpenshift(o Options, scope RenderScope) error {
resources, err := Render(importerOpenshiftTemplate, scope)
if err != nil {
return err
}
var obj interface{} = nil
err = yaml.Unmarshal([]byte(resources), &obj)
if err != nil {
return err
}
client, err := o.GetClient()
if err != nil {
return err
}
switch v := obj.(type) {
case []interface{}:
for _, value := range v {
if x, ok := value.(map[string]interface{}); ok {
resouce := unstructured.Unstructured{x}
_, _, err := CreateOrUpdate(context.Background(), client, &resouce)
if err != nil {
return err
}
}
}
default:
panic("Yaml did not result in an array.")
}
return nil
}
func GetClientNamespace(configPath string) (string, error) {
var clientConfig clientcmd.ClientConfig
var apiConfig *clientcmdapi.Config
var err error
if configPath != "" {
apiConfig, err = clientcmd.LoadFromFile(configPath)
if err != nil {
return "", fmt.Errorf("failed to load user provided kubeconfig: %v", err)
}
} else {
apiConfig, err = clientcmd.NewDefaultClientConfigLoadingRules().Load()
if err != nil {
return "", fmt.Errorf("failed to get kubeconfig: %v", err)
}
}
clientConfig = clientcmd.NewDefaultClientConfig(*apiConfig, &clientcmd.ConfigOverrides{})
namespace, _, err := clientConfig.Namespace()
if err != nil {
return "", err
}
return namespace, nil
}
func ToUnstructured(obj runtime.Object) (*unstructured.Unstructured, error) {
// It might be already Unstructured..
if u, ok := obj.(*unstructured.Unstructured); ok {
return u, nil
}
// Convert it..
fields, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, errors.WithStack(err)
}
return &unstructured.Unstructured{fields}, nil
}
func CreateOrUpdate(ctx context.Context, cl client.Client, o runtime.Object, skipFields ...string) (*unstructured.Unstructured, controllerutil.OperationResult, error) {
desired, err := ToUnstructured(o)
if err != nil {
return desired, controllerutil.OperationResultNone, err
}
originalYaml := ""
updatedYaml := ""
createdCopy := desired.DeepCopy()
modType, err := controllerutil.CreateOrUpdate(ctx, cl, createdCopy, func(o runtime.Object) error {
existing := o.(*unstructured.Unstructured)
originalYaml = string(MustMarshal(yaml.Marshal(existing)))
mergePath := desired.GetAPIVersion() + "/" + desired.GetKind()
if len(skipFields) == 0 {
skipFields = append(skipFields, "kind", "apiVersion", "status")
}
skip := map[string]bool{}
for _, value := range skipFields {
skip[mergePath+"/"+value] = true
}
mergeMap(mergePath, existing.Object, desired.Object, skip)
updatedYaml = string(MustMarshal(yaml.Marshal(existing)))
return nil
})
if showResourceDiffs && modType == controllerutil.OperationResultUpdated {
fmt.Println("resource", desired.GetKind(), "update:", desired.GetName())
fmt.Println(UnifiedDiff(originalYaml, updatedYaml))
}
return createdCopy, modType, err
}
func MustMarshal( data []byte, err error) ([]byte) {
if err!=nil {
panic(err)
}
return data
}
func UnifiedDiff(a string, b string) (string, error) {
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(a),
B: difflib.SplitLines(b),
FromFile: "a",
FromDate: "00-00-00 00:00:00",
ToFile: "b",
ToDate: "00-00-00 00:00:00",
Context: 3,
}
return difflib.GetUnifiedDiffString(diff)
}
func mergeMap(path string, to map[string]interface{}, from map[string]interface{}, skip map[string]bool) {
if path == "v1/Secret" {
mergeSecretValues(to, from)
}
for key, value := range from {
field := path + "/" + key
if skip[field] {
continue
}
// handle cases like https://issues.jboss.org/browse/ENTESB-11711 setting a env value to "" does not work well, k8s gives delete
// the value field under the covers, and we keep trying to set it again to the "" value.
if field == "apps.openshift.io/v1/DeploymentConfig/spec/template/spec/containers/#/env/#/value" && (value == nil || value == "") {
delete(to, key)
continue
}
to[key] = mergeValue(field, to[key], value, skip)
}
}
func mergeArray(path string, to []interface{}, from []interface{}, skip map[string]bool) []interface{} {
nexPath := path + "/#"
for key, value := range from {
if key < len(to) {
to[key] = mergeValue(nexPath, to[key], value, skip)
} else {
to = append(to, mergeValue(nexPath, nil, value, skip))
}
}
return to
}
func mergeValue(path string, to interface{}, from interface{}, skip map[string]bool) interface{} {
if skip[path] {
return to
}
switch from := from.(type) {
case map[string]interface{}:
if to, ok := to.(map[string]interface{}); ok {
mergeMap(path, to, from, skip)
return to
}
case []interface{}:
if toMap, ok := to.([]interface{}); ok {
return mergeArray(path, toMap, from, skip)
}
}
if from == to || from == nil {
return to
}
if to == nil {
return from
}
// Looks like we might have a different value...
// Apply special handling for some fields.
switch path {
case "apps.openshift.io/v1/DeploymentConfig/spec/template/spec/containers/#/image":
return to
case "apps.openshift.io/v1/DeploymentConfig/spec/triggers/#/imageChangeParams/from/namespace":
return to
case "v1/PersistentVolumeClaim/spec/resources/requests/storage":
return to
case "apps.openshift.io/v1/DeploymentConfig/spec/template/spec/containers/#/resources/limits/memory":
// This might be the same value, in a different format.
fromQ := resource.MustParse(fmt.Sprint(from))
fromI, _ := fromQ.AsInt64()
toQ := resource.MustParse(fmt.Sprint(to))
toI, _ := toQ.AsInt64()
if fromI == toI {
return to
} else {
return from
}
}
fromT := reflect.TypeOf(from)
toT := reflect.TypeOf(to)
if fromT != toT && fromT.ConvertibleTo(toT) {
from = reflect.ValueOf(from).Convert(toT).Interface()
if from == to {
return to
} else {
return from
}
}
return from
}
func mergeSecretValues(to map[string]interface{}, from map[string]interface{}) {
if from["stringData"] != nil && to["data"] != nil {
stringData := from["stringData"].(map[string]interface{})
data := to["data"].(map[string]interface{})
updates := map[string]interface{}{}
for key, value := range stringData {
if plain, ok := value.(string); ok {
encoded := base64.StdEncoding.EncodeToString([]byte(plain))
if data[key] != encoded {
updates[key] = value
}
}
}
if len(updates) > 0 {
from["stringData"] = updates
} else {
delete(from, "stringData")
}
}
}