-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.go
331 lines (301 loc) · 10.4 KB
/
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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
Copyright 2017 The Rook Authors. All rights reserved.
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 agent to manage Kubernetes storage attach events.
package agent
import (
"encoding/json"
"os"
"strconv"
"strings"
"github.com/coreos/pkg/capnslog"
"github.com/pkg/errors"
k8sutil "github.com/rook/rook/pkg/operator/k8sutil"
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
agentDaemonsetName = "rook-ceph-agent"
flexvolumePathDirEnv = "FLEXVOLUME_DIR_PATH"
libModulesPathDirEnv = "LIB_MODULES_DIR_PATH"
agentMountsEnv = "AGENT_MOUNTS"
flexvolumeDefaultDirPath = "/usr/libexec/kubernetes/kubelet-plugins/volume/exec/"
agentDaemonsetPriorityClassNameEnv = "AGENT_PRIORITY_CLASS_NAME"
agentDaemonsetTolerationEnv = "AGENT_TOLERATION"
agentDaemonsetTolerationKeyEnv = "AGENT_TOLERATION_KEY"
agentDaemonsetTolerationsEnv = "AGENT_TOLERATIONS"
agentDaemonsetNodeAffinityEnv = "AGENT_NODE_AFFINITY"
AgentMountSecurityModeEnv = "AGENT_MOUNT_SECURITY_MODE"
RookEnableSelinuxRelabelingEnv = "ROOK_ENABLE_SELINUX_RELABELING"
RookEnableFSGroupEnv = "ROOK_ENABLE_FSGROUP"
// MountSecurityModeAny "any" security mode for the agent for mount action
MountSecurityModeAny = "Any"
// MountSecurityModeRestricted restricted security mode for the agent for mount action
MountSecurityModeRestricted = "Restricted"
)
var (
logger = capnslog.NewPackageLogger("github.com/rook/rook", "op-agent")
)
// New creates an instance of Agent
func New(clientset kubernetes.Interface) *Agent {
return &Agent{
clientset: clientset,
}
}
// Start the agent
func (a *Agent) Start(namespace, agentImage, serviceAccount string) error {
err := a.createAgentDaemonSet(namespace, agentImage, serviceAccount)
if err != nil {
return errors.Wrap(err, "error starting agent daemonset")
}
return nil
}
func (a *Agent) createAgentDaemonSet(namespace, agentImage, serviceAccount string) error {
flexvolumeDirPath, source := a.discoverFlexvolumeDir()
logger.Infof("discovered flexvolume dir path from source %s. value: %s", source, flexvolumeDirPath)
libModulesDirPath := os.Getenv(libModulesPathDirEnv)
if libModulesDirPath == "" {
libModulesDirPath = "/lib/modules"
}
agentMountSecurityMode := os.Getenv(AgentMountSecurityModeEnv)
if agentMountSecurityMode == "" {
logger.Infof("no agent mount security mode given, defaulting to '%s' mode", MountSecurityModeAny)
agentMountSecurityMode = MountSecurityModeAny
}
if agentMountSecurityMode != MountSecurityModeAny && agentMountSecurityMode != MountSecurityModeRestricted {
return errors.Errorf("invalid agent mount security mode specified (given: %s)", agentMountSecurityMode)
}
rookEnableSelinuxRelabeling := os.Getenv(RookEnableSelinuxRelabelingEnv)
_, err := strconv.ParseBool(rookEnableSelinuxRelabeling)
if err != nil {
logger.Warningf("Invalid %s value \"%s\". Defaulting to \"true\".", RookEnableSelinuxRelabelingEnv, rookEnableSelinuxRelabeling)
rookEnableSelinuxRelabeling = "true"
}
rookEnableFSGroup := os.Getenv(RookEnableFSGroupEnv)
_, err = strconv.ParseBool(rookEnableFSGroup)
if err != nil {
logger.Warningf("Invalid %s value \"%s\". Defaulting to \"true\".", RookEnableFSGroupEnv, rookEnableFSGroup)
rookEnableFSGroup = "true"
}
privileged := true
ds := &apps.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: agentDaemonsetName,
},
Spec: apps.DaemonSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": agentDaemonsetName,
},
},
UpdateStrategy: apps.DaemonSetUpdateStrategy{
Type: apps.RollingUpdateDaemonSetStrategyType,
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": agentDaemonsetName,
},
},
Spec: v1.PodSpec{
ServiceAccountName: serviceAccount,
Containers: []v1.Container{
{
Name: agentDaemonsetName,
Image: agentImage,
Args: []string{"ceph", "agent"},
SecurityContext: &v1.SecurityContext{
Privileged: &privileged,
},
VolumeMounts: []v1.VolumeMount{
{
Name: "flexvolume",
MountPath: "/flexmnt",
},
{
Name: "dev",
MountPath: "/dev",
},
{
Name: "sys",
MountPath: "/sys",
},
{
Name: "libmodules",
MountPath: "/lib/modules",
},
},
Env: []v1.EnvVar{
k8sutil.NamespaceEnvVar(),
k8sutil.NodeEnvVar(),
{Name: AgentMountSecurityModeEnv, Value: agentMountSecurityMode},
{Name: RookEnableSelinuxRelabelingEnv, Value: rookEnableSelinuxRelabeling},
{Name: RookEnableFSGroupEnv, Value: rookEnableFSGroup},
},
},
},
Volumes: []v1.Volume{
{
Name: "flexvolume",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: flexvolumeDirPath,
},
},
},
{
Name: "dev",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/dev",
},
},
},
{
Name: "sys",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/sys",
},
},
},
{
Name: "libmodules",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: libModulesDirPath,
},
},
},
},
HostNetwork: true,
PriorityClassName: os.Getenv(agentDaemonsetPriorityClassNameEnv),
},
},
},
}
// Add agent mounts if any given through environment
agentMounts := os.Getenv(agentMountsEnv)
if agentMounts != "" {
mounts := strings.Split(agentMounts, ",")
for _, mount := range mounts {
mountdef := strings.Split(mount, "=")
if len(mountdef) != 2 {
return errors.Errorf("badly formatted AGENT_MOUNTS %q. The format should be 'mountname=/host/path:/container/path,mountname2=/host/path2:/container/path2'", agentMounts)
}
mountname := mountdef[0]
paths := strings.Split(mountdef[1], ":")
if len(paths) != 2 {
return errors.Errorf("badly formatted AGENT_MOUNTS %q. The format should be 'mountname=/host/path:/container/path,mountname2=/host/path2:/container/path2'", agentMounts)
}
ds.Spec.Template.Spec.Containers[0].VolumeMounts = append(ds.Spec.Template.Spec.Containers[0].VolumeMounts, v1.VolumeMount{
Name: mountname,
MountPath: paths[1],
})
ds.Spec.Template.Spec.Volumes = append(ds.Spec.Template.Spec.Volumes, v1.Volume{
Name: mountname,
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: paths[0],
},
},
})
}
}
// Add toleration if any
tolerationValue := os.Getenv(agentDaemonsetTolerationEnv)
if tolerationValue != "" {
ds.Spec.Template.Spec.Tolerations = []v1.Toleration{
{
Effect: v1.TaintEffect(tolerationValue),
Operator: v1.TolerationOpExists,
Key: os.Getenv(agentDaemonsetTolerationKeyEnv),
},
}
}
tolerationsRaw := os.Getenv(agentDaemonsetTolerationsEnv)
tolerations, err := k8sutil.YamlToTolerations(tolerationsRaw)
if err != nil {
logger.Warningf("failed to parse %q. %v", tolerationsRaw, err)
}
ds.Spec.Template.Spec.Tolerations = append(ds.Spec.Template.Spec.Tolerations, tolerations...)
// Add NodeAffinity if any
nodeAffinity := os.Getenv(agentDaemonsetNodeAffinityEnv)
if nodeAffinity != "" {
v1NodeAffinity, err := k8sutil.GenerateNodeAffinity(nodeAffinity)
if err != nil {
logger.Errorf("failed to create NodeAffinity. %v", err)
} else {
ds.Spec.Template.Spec.Affinity = &v1.Affinity{
NodeAffinity: v1NodeAffinity,
}
}
}
_, err = a.clientset.AppsV1().DaemonSets(namespace).Create(ds)
if err != nil {
if !k8serrors.IsAlreadyExists(err) {
return errors.Wrap(err, "failed to create rook-ceph-agent daemon set")
}
logger.Infof("rook-ceph-agent daemonset already exists, updating ...")
_, err = a.clientset.AppsV1().DaemonSets(namespace).Update(ds)
if err != nil {
return errors.Wrap(err, "failed to update rook-ceph-agent daemon set")
}
} else {
logger.Infof("rook-ceph-agent daemonset started")
}
return nil
}
func (a *Agent) discoverFlexvolumeDir() (flexvolumeDirPath, source string) {
//copy flexvolume to flexvolume dir
nodeName := os.Getenv(k8sutil.NodeNameEnvVar)
if nodeName == "" {
logger.Warningf("cannot detect the node name. Please provide using the downward API in the rook operator manifest file")
return getDefaultFlexvolumeDir()
}
// determining where the path of the flexvolume dir on the node
nodeConfigURI, err := k8sutil.NodeConfigURI()
if err != nil {
logger.Warning(err.Error())
return getDefaultFlexvolumeDir()
}
nodeConfig, err := a.clientset.CoreV1().RESTClient().Get().RequestURI(nodeConfigURI).DoRaw()
if err != nil {
logger.Warningf("unable to query node configuration: %v", err)
return getDefaultFlexvolumeDir()
}
// unmarshal to a KubeletConfiguration
kubeletConfiguration := KubeletConfiguration{}
if err := json.Unmarshal(nodeConfig, &kubeletConfiguration); err != nil {
logger.Warningf("unable to parse node config as kubelet configuration. %v", err)
} else {
flexvolumeDirPath = kubeletConfiguration.KubeletConfig.VolumePluginDir
}
if flexvolumeDirPath != "" {
return flexvolumeDirPath, "KubeletConfiguration"
}
return getDefaultFlexvolumeDir()
}
func getDefaultFlexvolumeDir() (flexvolumeDirPath, source string) {
logger.Infof("getting flexvolume dir path from %s env var", flexvolumePathDirEnv)
flexvolumeDirPath = os.Getenv(flexvolumePathDirEnv)
if flexvolumeDirPath != "" {
return flexvolumeDirPath, "env var"
}
logger.Infof("flexvolume dir path env var %s is not provided. Defaulting to: %s",
flexvolumePathDirEnv, flexvolumeDefaultDirPath)
flexvolumeDirPath = flexvolumeDefaultDirPath
return flexvolumeDirPath, "default"
}