-
Notifications
You must be signed in to change notification settings - Fork 254
/
util.go
135 lines (108 loc) · 3.58 KB
/
util.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
/*
Copyright 2018 The CDI Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"encoding/json"
"fmt"
"os"
"reflect"
"strings"
jsondiff "github.com/appscode/jsonpatch"
jsonpatch "github.com/evanphx/json-patch"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/jsonmergepatch"
"k8s.io/apimachinery/pkg/util/mergepatch"
)
func mergeLabelsAndAnnotations(src, dest metav1.Object) {
// allow users to add labels but not change ours
for k, v := range src.GetLabels() {
if dest.GetLabels() == nil {
dest.SetLabels(map[string]string{})
}
dest.GetLabels()[k] = v
}
// same for annotations
for k, v := range src.GetAnnotations() {
if dest.GetAnnotations() == nil {
dest.SetAnnotations(map[string]string{})
}
dest.GetAnnotations()[k] = v
}
}
func mergeObject(desiredObj, currentObj runtime.Object) (runtime.Object, error) {
desiredObj = desiredObj.DeepCopyObject()
desiredMetaObj := desiredObj.(metav1.Object)
currentMetaObj := currentObj.(metav1.Object)
v, ok := currentMetaObj.GetAnnotations()[lastAppliedConfigAnnotation]
if !ok {
return nil, fmt.Errorf("%T %s/%s missing last applied config",
currentMetaObj, currentMetaObj.GetNamespace(), currentMetaObj.GetName())
}
original := []byte(v)
// setting the timestamp saves unnecessary updates because creation timestamp is nulled
desiredMetaObj.SetCreationTimestamp(currentMetaObj.GetCreationTimestamp())
modified, err := json.Marshal(desiredObj)
if err != nil {
return nil, err
}
current, err := json.Marshal(currentObj)
if err != nil {
return nil, err
}
preconditions := []mergepatch.PreconditionFunc{
mergepatch.RequireKeyUnchanged("apiVersion"),
mergepatch.RequireKeyUnchanged("kind"),
mergepatch.RequireMetadataKeyUnchanged("name"),
}
patch, err := jsonmergepatch.CreateThreeWayJSONMergePatch(original, modified, current, preconditions...)
if err != nil {
return nil, err
}
newCurrent, err := jsonpatch.MergePatch(current, patch)
if err != nil {
return nil, err
}
result := newDefaultInstance(currentObj)
if err = json.Unmarshal(newCurrent, result); err != nil {
return nil, err
}
return result, nil
}
func deployClusterResources() bool {
return strings.ToLower(os.Getenv("DEPLOY_CLUSTER_RESOURCES")) != "false"
}
func logJSONDiff(logger logr.Logger, objA, objB interface{}) {
aBytes, _ := json.Marshal(objA)
bBytes, _ := json.Marshal(objB)
patches, _ := jsondiff.CreatePatch(aBytes, bBytes)
pBytes, _ := json.Marshal(patches)
logger.Info("DIFF", "obj", objA, "patch", string(pBytes))
}
func checkDeploymentReady(deployment *appsv1.Deployment) bool {
desiredReplicas := deployment.Spec.Replicas
if desiredReplicas == nil {
desiredReplicas = &[]int32{1}[0]
}
if *desiredReplicas != deployment.Status.Replicas ||
deployment.Status.Replicas != deployment.Status.ReadyReplicas {
return false
}
return true
}
func newDefaultInstance(obj runtime.Object) runtime.Object {
typ := reflect.ValueOf(obj).Elem().Type()
return reflect.New(typ).Interface().(runtime.Object)
}