forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
197 lines (164 loc) · 5.99 KB
/
wrapper.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 reconciling
import (
appsv1 "k8s.io/api/apps/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
utilpointer "k8s.io/utils/pointer"
)
// OwnerRefWrapper is responsible for wrapping a ObjectCreator function, solely to set the OwnerReference to the cluster object
func OwnerRefWrapper(ref metav1.OwnerReference) ObjectModifier {
return func(create ObjectCreator) ObjectCreator {
return func(existing runtime.Object) (runtime.Object, error) {
obj, err := create(existing)
if err != nil {
return obj, err
}
obj.(metav1.Object).SetOwnerReferences([]metav1.OwnerReference{ref})
return obj, nil
}
}
}
// DefaultContainer defaults all Container attributes to the same values as they would get from the Kubernetes API
func DefaultContainer(c *corev1.Container, procMountType *corev1.ProcMountType) {
if c.ImagePullPolicy == "" {
c.ImagePullPolicy = corev1.PullIfNotPresent
}
if c.TerminationMessagePath == "" {
c.TerminationMessagePath = corev1.TerminationMessagePathDefault
}
if c.TerminationMessagePolicy == "" {
c.TerminationMessagePolicy = corev1.TerminationMessageReadFile
}
for idx := range c.Env {
if c.Env[idx].ValueFrom != nil && c.Env[idx].ValueFrom.FieldRef != nil {
if c.Env[idx].ValueFrom.FieldRef.APIVersion == "" {
c.Env[idx].ValueFrom.FieldRef.APIVersion = "v1"
}
}
}
// This attribut was added in 1.12
if c.SecurityContext != nil {
c.SecurityContext.ProcMount = procMountType
}
}
// DefaultPodSpec defaults all Container attributes to the same values as they would get from the Kubernetes API
func DefaultPodSpec(old, new corev1.PodSpec) (corev1.PodSpec, error) {
// make sure to keep the old procmount types in case a creator overrides the entire PodSpec
initContainerProcMountType := map[string]*corev1.ProcMountType{}
containerProcMountType := map[string]*corev1.ProcMountType{}
for _, container := range old.InitContainers {
if container.SecurityContext != nil {
initContainerProcMountType[container.Name] = container.SecurityContext.ProcMount
}
}
for _, container := range old.Containers {
if container.SecurityContext != nil {
containerProcMountType[container.Name] = container.SecurityContext.ProcMount
}
}
for idx, container := range new.InitContainers {
DefaultContainer(&new.InitContainers[idx], initContainerProcMountType[container.Name])
}
for idx, container := range new.Containers {
DefaultContainer(&new.Containers[idx], containerProcMountType[container.Name])
}
for idx, vol := range new.Volumes {
if vol.VolumeSource.Secret != nil && vol.VolumeSource.Secret.DefaultMode == nil {
new.Volumes[idx].Secret.DefaultMode = utilpointer.Int32Ptr(corev1.SecretVolumeSourceDefaultMode)
}
if vol.VolumeSource.ConfigMap != nil && vol.VolumeSource.ConfigMap.DefaultMode == nil {
new.Volumes[idx].ConfigMap.DefaultMode = utilpointer.Int32Ptr(corev1.ConfigMapVolumeSourceDefaultMode)
}
}
return new, nil
}
// DefaultDeployment defaults all Deployment attributes to the same values as they would get from the Kubernetes API
func DefaultDeployment(creator DeploymentCreator) DeploymentCreator {
return func(d *appsv1.Deployment) (*appsv1.Deployment, error) {
old := d.DeepCopy()
d, err := creator(d)
if err != nil {
return nil, err
}
if d.Spec.Strategy.Type == "" {
d.Spec.Strategy.Type = appsv1.RollingUpdateDeploymentStrategyType
if d.Spec.Strategy.RollingUpdate == nil {
d.Spec.Strategy.RollingUpdate = &appsv1.RollingUpdateDeployment{
MaxSurge: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 1,
},
MaxUnavailable: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 0,
},
}
}
}
d.Spec.Template.Spec, err = DefaultPodSpec(old.Spec.Template.Spec, d.Spec.Template.Spec)
if err != nil {
return nil, err
}
return d, nil
}
}
// DefaultStatefulSet defaults all StatefulSet attributes to the same values as they would get from the Kubernetes API
func DefaultStatefulSet(creator StatefulSetCreator) StatefulSetCreator {
return func(ss *appsv1.StatefulSet) (*appsv1.StatefulSet, error) {
old := ss.DeepCopy()
ss, err := creator(ss)
if err != nil {
return nil, err
}
ss.Spec.Template.Spec, err = DefaultPodSpec(old.Spec.Template.Spec, ss.Spec.Template.Spec)
if err != nil {
return nil, err
}
return ss, nil
}
}
// DefaultDaemonSet defaults all DaemonSet attributes to the same values as they would get from the Kubernetes API
func DefaultDaemonSet(creator DaemonSetCreator) DaemonSetCreator {
return func(ds *appsv1.DaemonSet) (*appsv1.DaemonSet, error) {
old := ds.DeepCopy()
ds, err := creator(ds)
if err != nil {
return nil, err
}
ds.Spec.Template.Spec, err = DefaultPodSpec(old.Spec.Template.Spec, ds.Spec.Template.Spec)
if err != nil {
return nil, err
}
return ds, nil
}
}
// DefaultCronJob defaults all CronJob attributes to the same values as they would get from the Kubernetes API
func DefaultCronJob(creator CronJobCreator) CronJobCreator {
return func(cj *batchv1beta1.CronJob) (*batchv1beta1.CronJob, error) {
old := cj.DeepCopy()
cj, err := creator(cj)
if err != nil {
return nil, err
}
cj.Spec.JobTemplate.Spec.Template.Spec, err = DefaultPodSpec(old.Spec.JobTemplate.Spec.Template.Spec, cj.Spec.JobTemplate.Spec.Template.Spec)
if err != nil {
return nil, err
}
return cj, nil
}
}