This repository has been archived by the owner on Nov 2, 2023. It is now read-only.
/
adapter_util.go
163 lines (132 loc) · 5.12 KB
/
adapter_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
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
/*
Copyright 2021 The OpenYurt Authors.
Copyright 2019 The Kruise 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 adapter
import (
"encoding/json"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/klog"
appsv1alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1"
)
func getPoolPrefix(controllerName, poolName string) string {
prefix := fmt.Sprintf("%s-%s-", controllerName, poolName)
if len(validation.NameIsDNSSubdomain(prefix, true)) != 0 {
prefix = fmt.Sprintf("%s-", controllerName)
}
return prefix
}
func attachNodeAffinityAndTolerations(podSpec *corev1.PodSpec, pool *appsv1alpha1.Pool) {
attachNodeAffinity(podSpec, pool)
attachTolerations(podSpec, pool)
}
func attachNodeAffinity(podSpec *corev1.PodSpec, pool *appsv1alpha1.Pool) {
if podSpec.Affinity == nil {
podSpec.Affinity = &corev1.Affinity{}
}
if podSpec.Affinity.NodeAffinity == nil {
podSpec.Affinity.NodeAffinity = &corev1.NodeAffinity{}
}
if podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil {
podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution = &corev1.NodeSelector{}
}
if podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms == nil {
podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = []corev1.NodeSelectorTerm{}
}
if len(podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms) == 0 {
podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = append(podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms, corev1.NodeSelectorTerm{})
}
for _, matchExpression := range pool.NodeSelectorTerm.MatchExpressions {
for i, term := range podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms {
term.MatchExpressions = append(term.MatchExpressions, matchExpression)
podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[i] = term
}
}
}
func attachTolerations(podSpec *corev1.PodSpec, poolConfig *appsv1alpha1.Pool) {
if poolConfig.Tolerations == nil {
return
}
if podSpec.Tolerations == nil {
podSpec.Tolerations = []corev1.Toleration{}
}
for _, toleration := range poolConfig.Tolerations {
podSpec.Tolerations = append(podSpec.Tolerations, toleration)
}
return
}
func getRevision(objMeta metav1.Object) string {
if objMeta.GetLabels() == nil {
return ""
}
return objMeta.GetLabels()[appsv1alpha1.ControllerRevisionHashLabelKey]
}
// getCurrentPartition calculates current partition by counting the pods not having the updated revision
func getCurrentPartition(pods []*corev1.Pod, revision string) *int32 {
var partition int32
for _, pod := range pods {
if getRevision(&pod.ObjectMeta) != revision {
partition++
}
}
return &partition
}
func StrategicMergeByPatches(oldobj interface{}, patch *runtime.RawExtension, newPatched interface{}) error {
patchMap := make(map[string]interface{})
if err := json.Unmarshal(patch.Raw, &patchMap); err != nil {
klog.Errorf("Unmarshal pool patch error %v, patch Raw %v", err, string(patch.Raw))
return err
}
originalObjMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(oldobj)
if err != nil {
klog.Errorf("ToUnstructured error %v", err)
return err
}
patchedObjMap, err := strategicpatch.StrategicMergeMapPatch(originalObjMap, patchMap, newPatched)
if err != nil {
klog.Errorf("StartegicMergeMapPatch error %v", err)
return err
}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(patchedObjMap, newPatched); err != nil {
klog.Errorf("FromUnstructured error %v", err)
return err
}
return nil
}
func PoolHasPatch(poolConfig *appsv1alpha1.Pool, set metav1.Object) bool {
if poolConfig.Patch == nil {
// If No Patches, Must Set patches annotation to ""
if anno := set.GetAnnotations(); anno != nil {
anno[appsv1alpha1.AnnotationPatchKey] = ""
}
return false
}
return true
}
func CreateNewPatchedObject(patchInfo *runtime.RawExtension, set metav1.Object, newPatched metav1.Object) error {
if err := StrategicMergeByPatches(set, patchInfo, newPatched); err != nil {
return err
}
if anno := newPatched.GetAnnotations(); anno == nil {
newPatched.SetAnnotations(map[string]string{
appsv1alpha1.AnnotationPatchKey: string(patchInfo.Raw),
})
} else {
anno[appsv1alpha1.AnnotationPatchKey] = string(patchInfo.Raw)
}
return nil
}