/
controller.go
481 lines (419 loc) · 13.6 KB
/
controller.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
Copyright 2018 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 nfs to manage an NFS export.
package nfs
import (
"fmt"
"reflect"
s "strings"
"github.com/coreos/pkg/capnslog"
opkit "github.com/rook/operator-kit"
nfsv1alpha1 "github.com/rook/rook/pkg/apis/nfs.rook.io/v1alpha1"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/operator/k8sutil"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/tools/cache"
)
const (
customResourceName = "nfsserver"
customResourceNamePlural = "nfsservers"
NFSConfigMapPath = "/nfs-ganesha/config"
nfsPort = 2049
rpcPort = 111
noneMode = "none"
)
var logger = capnslog.NewPackageLogger("github.com/rook/rook", "nfs-operator")
// NFSResource represents the nfs export custom resource
var NFSResource = opkit.CustomResource{
Name: customResourceName,
Plural: customResourceNamePlural,
Group: nfsv1alpha1.CustomResourceGroup,
Version: nfsv1alpha1.Version,
Scope: apiextensionsv1beta1.NamespaceScoped,
Kind: reflect.TypeOf(nfsv1alpha1.NFSServer{}).Name(),
}
// Controller represents a controller object for nfs server custom resources
type Controller struct {
context *clusterd.Context
containerImage string
}
// NewController create controller for watching nfsserver custom resources created
func NewController(context *clusterd.Context, containerImage string) *Controller {
return &Controller{
context: context,
containerImage: containerImage,
}
}
// StartWatch watches for instances of nfsserver custom resources and acts on them
func (c *Controller) StartWatch(namespace string, stopCh chan struct{}) error {
resourceHandlerFuncs := cache.ResourceEventHandlerFuncs{
AddFunc: c.onAdd,
UpdateFunc: c.onUpdate,
DeleteFunc: c.onDelete,
}
logger.Infof("start watching nfs server resources in namespace %s", namespace)
watcher := opkit.NewWatcher(NFSResource, namespace, resourceHandlerFuncs, c.context.RookClientset.NfsV1alpha1().RESTClient())
go watcher.Watch(&nfsv1alpha1.NFSServer{}, stopCh)
return nil
}
type nfsServer struct {
name string
context *clusterd.Context
namespace string
spec nfsv1alpha1.NFSServerSpec
ownerRef metav1.OwnerReference
}
func newNfsServer(c *nfsv1alpha1.NFSServer, context *clusterd.Context) *nfsServer {
return &nfsServer{
name: c.Name,
context: context,
namespace: c.Namespace,
spec: c.Spec,
ownerRef: nfsOwnerRef(c.Name, string(c.UID)),
}
}
func nfsOwnerRef(name, nfsServerID string) metav1.OwnerReference {
blockOwner := true
return metav1.OwnerReference{
APIVersion: fmt.Sprintf("%s/%s", NFSResource.Group, NFSResource.Version),
Kind: NFSResource.Kind,
Name: name,
UID: types.UID(nfsServerID),
BlockOwnerDeletion: &blockOwner,
}
}
func getServerConfig(exports []nfsv1alpha1.ExportsSpec) map[string]map[string]string {
claimConfigOpt := make(map[string]map[string]string)
configOpt := make(map[string]string)
for _, export := range exports {
claimName := export.PersistentVolumeClaim.ClaimName
if claimName != "" {
configOpt["accessMode"] = export.Server.AccessMode
configOpt["squash"] = export.Server.Squash
claimConfigOpt[claimName] = configOpt
}
}
return claimConfigOpt
}
func createAppLabels(nfsServer *nfsServer) map[string]string {
return map[string]string{
k8sutil.AppAttr: nfsServer.name,
}
}
func createServicePorts() []v1.ServicePort {
return []v1.ServicePort{
{
Name: "nfs",
Port: int32(nfsPort),
TargetPort: intstr.FromInt(int(nfsPort)),
},
{
Name: "rpc",
Port: int32(rpcPort),
TargetPort: intstr.FromInt(int(rpcPort)),
},
}
}
func (c *Controller) createNFSService(nfsServer *nfsServer) (*v1.Service, error) {
// This service is meant to be used by clients to access NFS.
nfsService := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: nfsServer.name,
Namespace: nfsServer.namespace,
OwnerReferences: []metav1.OwnerReference{nfsServer.ownerRef},
Labels: createAppLabels(nfsServer),
},
Spec: v1.ServiceSpec{
Selector: createAppLabels(nfsServer),
Type: v1.ServiceTypeClusterIP,
Ports: createServicePorts(),
},
}
svc, err := c.context.Clientset.CoreV1().Services(nfsServer.namespace).Create(nfsService)
if err != nil {
if !errors.IsAlreadyExists(err) {
return nil, err
}
logger.Infof("nfs service %s already exists in namespace %s", nfsService.Name, nfsService.Namespace)
} else {
logger.Infof("nfs service %s started in namespace %s", nfsService.Name, nfsService.Namespace)
}
return svc, nil
}
func createCephNFSExport(id int, path string, access string, squash string) string {
var accessType string
// validateNFSServerSpec guarantees `access` will be one of these values at this point
switch s.ToLower(access) {
case "readwrite":
accessType = "RW"
case "readonly":
accessType = "RO"
case noneMode:
accessType = "None"
}
idStr := fmt.Sprintf("%v", id)
nfsGaneshaConfig := `
EXPORT {
Export_Id = ` + idStr + `;
Path = /` + path + `;
Pseudo = /` + path + `;
Protocols = 4;
Transports = TCP;
Sectype = sys;
Access_Type = ` + accessType + `;
Squash = ` + s.ToLower(squash) + `;
FSAL {
Name = VFS;
}
}`
return nfsGaneshaConfig
}
func createCephNFSConfig(spec *nfsv1alpha1.NFSServerSpec) string {
serverConfig := getServerConfig(spec.Exports)
exportsList := make([]string, 0)
id := 10
for claimName, claimConfig := range serverConfig {
exportsList = append(exportsList, createCephNFSExport(id, claimName, claimConfig["accessMode"], claimConfig["squash"]))
id++
}
// fsid_device parameter is important as in case of an overlayfs there is a chance that the fsid of the mounted share is same as that of the fsid of "/"
// so setting this to true uses device number as fsid
// related issue https://github.com/nfs-ganesha/nfs-ganesha/issues/140
exportsList = append(exportsList, `NFS_Core_Param
{
fsid_device = true;
}`)
nfsGaneshaConfig := s.Join(exportsList, "\n")
return nfsGaneshaConfig
}
func (c *Controller) createNFSConfigMap(nfsServer *nfsServer) error {
nfsGaneshaConfig := createCephNFSConfig(&nfsServer.spec)
configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: nfsServer.name,
Namespace: nfsServer.namespace,
OwnerReferences: []metav1.OwnerReference{nfsServer.ownerRef},
Labels: createAppLabels(nfsServer),
},
Data: map[string]string{
nfsServer.name: nfsGaneshaConfig,
},
}
_, err := c.context.Clientset.CoreV1().ConfigMaps(nfsServer.namespace).Create(configMap)
if err != nil {
return err
}
return nil
}
func getPVCMount(spec *nfsv1alpha1.NFSServerSpec) map[string]string {
pvcMounts := map[string]string{}
for _, export := range spec.Exports {
claimName := export.PersistentVolumeClaim.ClaimName
if claimName != "" {
pvcMounts[export.Name] = export.PersistentVolumeClaim.ClaimName
}
}
return pvcMounts
}
func createPVCSpecList(nfsServer *nfsServer) []v1.Volume {
pvcSpecList := make([]v1.Volume, 0)
pvcNameList := getPVCMount(&nfsServer.spec)
for shareName, claimName := range pvcNameList {
pvcSpecList = append(pvcSpecList, v1.Volume{
Name: shareName,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: claimName,
},
},
})
}
configMapSrc := &v1.ConfigMapVolumeSource{
Items: []v1.KeyToPath{
{
Key: nfsServer.name,
Path: nfsServer.name,
},
},
}
configMapSrc.Name = nfsServer.name
configMapVol := v1.Volume{
Name: nfsServer.name,
VolumeSource: v1.VolumeSource{
ConfigMap: configMapSrc,
},
}
pvcSpecList = append(pvcSpecList, configMapVol)
return pvcSpecList
}
func createVolumeMountList(nfsServer *nfsServer) []v1.VolumeMount {
volumeMountList := make([]v1.VolumeMount, 0)
pvcMount := getPVCMount(&nfsServer.spec)
for shareName, claimName := range pvcMount {
volumeMountList = append(volumeMountList, v1.VolumeMount{
Name: shareName,
MountPath: "/" + claimName,
})
}
configMapVolMount := v1.VolumeMount{
Name: nfsServer.name,
MountPath: NFSConfigMapPath,
}
volumeMountList = append(volumeMountList, configMapVolMount)
return volumeMountList
}
func (c *Controller) createNfsPodSpec(nfsServer *nfsServer, service *v1.Service) v1.PodTemplateSpec {
nfsPodSpec := v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: nfsServer.name,
Namespace: nfsServer.namespace,
Labels: createAppLabels(nfsServer),
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
ImagePullPolicy: "IfNotPresent",
Name: nfsServer.name,
Image: c.containerImage,
Args: []string{"nfs", "server", "--ganeshaConfigPath=" + NFSConfigMapPath + "/" + nfsServer.name},
Ports: []v1.ContainerPort{
{
Name: "nfs-port",
ContainerPort: int32(nfsPort),
},
{
Name: "rpc-port",
ContainerPort: int32(rpcPort),
},
},
VolumeMounts: createVolumeMountList(nfsServer),
SecurityContext: &v1.SecurityContext{
Capabilities: &v1.Capabilities{
Add: []v1.Capability{
"SYS_ADMIN",
"DAC_READ_SEARCH",
},
},
},
},
},
Volumes: createPVCSpecList(nfsServer),
},
}
return nfsPodSpec
}
func (c *Controller) createNfsStatefulSet(nfsServer *nfsServer, replicas int32, service *v1.Service) error {
appsClient := c.context.Clientset.AppsV1()
nfsPodSpec := c.createNfsPodSpec(nfsServer, service)
statefulSet := appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: nfsServer.name,
Namespace: nfsServer.namespace,
Labels: createAppLabels(nfsServer),
OwnerReferences: []metav1.OwnerReference{nfsServer.ownerRef},
},
Spec: appsv1.StatefulSetSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: createAppLabels(nfsServer),
},
Template: nfsPodSpec,
ServiceName: nfsServer.name,
},
}
nfsServer.spec.Annotations.ApplyToObjectMeta(&statefulSet.Spec.Template.ObjectMeta)
nfsServer.spec.Annotations.ApplyToObjectMeta(&statefulSet.ObjectMeta)
if _, err := appsClient.StatefulSets(nfsServer.namespace).Create(&statefulSet); err != nil {
if !errors.IsAlreadyExists(err) {
return err
}
logger.Infof("stateful set %s already exists in namespace %s", statefulSet.Name, statefulSet.Namespace)
} else {
logger.Infof("stateful set %s created in namespace %s", statefulSet.Name, statefulSet.Namespace)
}
return nil
}
func (c *Controller) onAdd(obj interface{}) {
nfsObj := obj.(*nfsv1alpha1.NFSServer).DeepCopy()
nfsServer := newNfsServer(nfsObj, c.context)
logger.Infof("new NFS server %s added to namespace %s", nfsObj.Name, nfsServer.namespace)
logger.Infof("validating nfs server spec in namespace %s", nfsServer.namespace)
if err := validateNFSServerSpec(nfsServer.spec); err != nil {
logger.Errorf("Invalid NFS Server spec: %+v", err)
return
}
logger.Infof("creating nfs server service in namespace %s", nfsServer.namespace)
svc, err := c.createNFSService(nfsServer)
if err != nil {
logger.Errorf("Unable to create NFS service %+v", err)
}
logger.Infof("creating nfs server configuration in namespace %s", nfsServer.namespace)
if err := c.createNFSConfigMap(nfsServer); err != nil {
logger.Errorf("Unable to create NFS ConfigMap %+v", err)
}
logger.Infof("creating nfs server stateful set in namespace %s", nfsServer.namespace)
if err := c.createNfsStatefulSet(nfsServer, int32(nfsServer.spec.Replicas), svc); err != nil {
logger.Errorf("Unable to create NFS stateful set %+v", err)
}
}
func (c *Controller) onUpdate(oldObj, newObj interface{}) {
oldNfsServ := oldObj.(*nfsv1alpha1.NFSServer).DeepCopy()
logger.Infof("Received update on NFS server %s in namespace %s. This is currently unsupported.", oldNfsServ.Name, oldNfsServ.Namespace)
}
func (c *Controller) onDelete(obj interface{}) {
cluster, ok := obj.(*nfsv1alpha1.NFSServer)
if !ok {
return
}
cluster = cluster.DeepCopy()
logger.Infof("cluster %s deleted from namespace %s", cluster.Name, cluster.Namespace)
}
func validateNFSServerSpec(spec nfsv1alpha1.NFSServerSpec) error {
serverConfig := spec.Exports
for _, export := range serverConfig {
if err := validateAccessMode(export.Server.AccessMode); err != nil {
return err
}
if err := validateSquashMode(export.Server.Squash); err != nil {
return err
}
}
return nil
}
func validateAccessMode(mode string) error {
switch s.ToLower(mode) {
case "readonly":
case "readwrite":
case noneMode:
default:
return fmt.Errorf("Invalid value (%s) for accessMode, valid values are (ReadOnly, ReadWrite, none)", mode)
}
return nil
}
func validateSquashMode(mode string) error {
switch s.ToLower(mode) {
case "rootid":
case "root":
case "all":
case noneMode:
default:
return fmt.Errorf("Invalid value (%s) for squash, valid values are (none, rootId, root, all)", mode)
}
return nil
}