/
update_agent.go
166 lines (147 loc) · 4.66 KB
/
update_agent.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
/*
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 resources
import (
nodelabelerapi "k8c.io/kubermatic/v3/pkg/controller/user-cluster-controller-manager/node-labeler/api"
"k8c.io/kubermatic/v3/pkg/resources"
"k8c.io/kubermatic/v3/pkg/resources/registry"
"k8c.io/reconciler/pkg/reconciling"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
AgentDaemonSetName = "flatcar-linux-update-agent"
)
var (
daemonSetMaxUnavailable = intstr.FromInt(1)
hostPathType = corev1.HostPathUnset
)
func AgentDaemonSetReconciler(imageRewriter registry.ImageRewriter) reconciling.NamedDaemonSetReconcilerFactory {
var userCore int64 = 500 // UID of the flatcar admin user 'core'
return func() (string, reconciling.DaemonSetReconciler) {
return AgentDaemonSetName, func(ds *appsv1.DaemonSet) (*appsv1.DaemonSet, error) {
ds.Spec.UpdateStrategy.Type = appsv1.RollingUpdateDaemonSetStrategyType
// be careful to not override any defaulting a k8s 1.21 with feature gate
// DaemonSetUpdateSurge might perform on the .MaxSurge field
if ds.Spec.UpdateStrategy.RollingUpdate == nil {
ds.Spec.UpdateStrategy.RollingUpdate = &appsv1.RollingUpdateDaemonSet{}
}
ds.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable = &daemonSetMaxUnavailable
labels := map[string]string{"app.kubernetes.io/name": AgentDaemonSetName}
ds.Spec.Selector = &metav1.LabelSelector{MatchLabels: labels}
ds.Spec.Template.ObjectMeta.Labels = labels
// The agent should only run on Flatcar nodes
ds.Spec.Template.Spec.NodeSelector = map[string]string{nodelabelerapi.DistributionLabelKey: nodelabelerapi.FlatcarLabelValue}
ds.Spec.Template.Spec.ServiceAccountName = AgentServiceAccountName
ds.Spec.Template.Spec.Containers = []corev1.Container{
{
Name: "update-agent",
Image: registry.Must(imageRewriter(resources.RegistryQuay + "/kinvolk/flatcar-linux-update-operator:v0.7.3")),
Command: []string{"/bin/update-agent"},
SecurityContext: &corev1.SecurityContext{
RunAsUser: &userCore,
},
Env: []corev1.EnvVar{
{
Name: "UPDATE_AGENT_NODE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "spec.nodeName",
},
},
},
{
Name: "POD_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "metadata.namespace",
},
},
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "var-run-dbus",
MountPath: "/var/run/dbus",
},
{
Name: "etc-flatcar",
MountPath: "/etc/flatcar",
},
{
Name: "usr-share-flatcar",
MountPath: "/usr/share/flatcar",
},
{
Name: "etc-os-release",
MountPath: "/etc/os-release",
},
},
},
}
ds.Spec.Template.Spec.Tolerations = []corev1.Toleration{
{
Effect: corev1.TaintEffectNoSchedule,
Operator: corev1.TolerationOpExists,
},
{
Effect: corev1.TaintEffectNoExecute,
Operator: corev1.TolerationOpExists,
},
}
ds.Spec.Template.Spec.Volumes = []corev1.Volume{
{
Name: "var-run-dbus",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/var/run/dbus",
Type: &hostPathType,
},
},
},
{
Name: "etc-flatcar",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/flatcar",
Type: &hostPathType,
},
},
},
{
Name: "usr-share-flatcar",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/usr/share/flatcar",
Type: &hostPathType,
},
},
},
{
Name: "etc-os-release",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/os-release",
Type: &hostPathType,
},
},
},
}
return ds, nil
}
}
}