/
spec.go
259 lines (236 loc) · 8.25 KB
/
spec.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
/*
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
import (
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
cephconfig "github.com/rook/rook/pkg/daemon/ceph/config"
"github.com/rook/rook/pkg/operator/ceph/cluster/mon"
"github.com/rook/rook/pkg/operator/ceph/config/keyring"
opspec "github.com/rook/rook/pkg/operator/ceph/spec"
"github.com/rook/rook/pkg/operator/k8sutil"
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
appName = "rook-ceph-nfs"
ganeshaConfigVolume = "ganesha-config"
nfsPort = 2049
ganeshaPid = "/var/run/ganesha/ganesha.pid"
)
func (c *CephNFSController) createCephNFSService(nfs cephv1.CephNFS, cfg daemonConfig) error {
labels := getLabels(nfs, cfg.ID)
svc := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: instanceName(nfs, cfg.ID),
Namespace: nfs.Namespace,
Labels: labels,
OwnerReferences: ownerRefs(nfs),
},
Spec: v1.ServiceSpec{
Selector: labels,
Ports: []v1.ServicePort{
{
Name: "nfs",
Port: nfsPort,
TargetPort: intstr.FromInt(int(nfsPort)),
Protocol: v1.ProtocolTCP,
},
},
},
}
if c.clusterSpec.Network.IsHost() {
svc.Spec.ClusterIP = v1.ClusterIPNone
}
svc, err := c.context.Clientset.CoreV1().Services(nfs.Namespace).Create(svc)
if err != nil {
if !kerrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "failed to create ganesha service")
}
logger.Infof("ceph nfs service already created")
return nil
}
logger.Infof("ceph nfs service running at %s:%d", svc.Spec.ClusterIP, nfsPort)
return nil
}
func (c *CephNFSController) makeDeployment(nfs cephv1.CephNFS, cfg daemonConfig) *apps.Deployment {
deployment := &apps.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: instanceName(nfs, cfg.ID),
Namespace: nfs.Namespace,
Labels: getLabels(nfs, cfg.ID),
OwnerReferences: ownerRefs(nfs),
},
}
k8sutil.AddRookVersionLabelToDeployment(deployment)
opspec.AddCephVersionLabelToDeployment(c.clusterInfo.CephVersion, deployment)
nfs.Spec.Server.Annotations.ApplyToObjectMeta(&deployment.ObjectMeta)
cephConfigVol, _ := cephConfigVolumeAndMount()
nfsConfigVol, _ := nfsConfigVolumeAndMount(cfg.ConfigConfigMap)
dbusVol, _ := dbusVolumeAndMount()
podSpec := v1.PodSpec{
InitContainers: []v1.Container{
c.connectionConfigInitContainer(nfs),
},
Containers: []v1.Container{
c.daemonContainer(nfs, cfg),
c.dbusContainer(nfs), // dbus sidecar
},
RestartPolicy: v1.RestartPolicyAlways,
Volumes: []v1.Volume{
// do not mount usual daemon volumes, as no data is stored for this daemon, and the ceph
// config file is generated by the init container. we don't need to worry about missing
// override configs, because nfs-ganesha is not a Ceph daemon; it wouldn't observe any
// overrides anyway
cephConfigVol,
keyring.Volume().Admin(),
nfsConfigVol,
dbusVol,
},
HostNetwork: c.clusterSpec.Network.IsHost(),
PriorityClassName: nfs.Spec.Server.PriorityClassName,
}
// Replace default unreachable node toleration
k8sutil.AddUnreachableNodeToleration(&podSpec)
if c.clusterSpec.Network.IsHost() {
podSpec.DNSPolicy = v1.DNSClusterFirstWithHostNet
}
nfs.Spec.Server.Placement.ApplyToPodSpec(&podSpec)
podTemplateSpec := v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: instanceName(nfs, cfg.ID),
Labels: getLabels(nfs, cfg.ID),
},
Spec: podSpec,
}
nfs.Spec.Server.Annotations.ApplyToObjectMeta(&podTemplateSpec.ObjectMeta)
// Multiple replicas of the nfs service would be handled by creating a service and a new deployment for each one, rather than increasing the pod count here
replicas := int32(1)
deployment.Spec = apps.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: getLabels(nfs, cfg.ID),
},
Template: podTemplateSpec,
Replicas: &replicas,
}
return deployment
}
func (c *CephNFSController) connectionConfigInitContainer(nfs cephv1.CephNFS) v1.Container {
_, cephConfigMount := cephConfigVolumeAndMount()
return v1.Container{
Name: opspec.ConfigInitContainerName,
// command is already 'rook'
Args: []string{
"ceph", "config-init",
"--username", "client.admin",
"--keyring", keyring.VolumeMount().AdminKeyringFilePath(),
},
Image: c.rookImage,
VolumeMounts: []v1.VolumeMount{
cephConfigMount,
keyring.VolumeMount().Admin(),
},
Env: append(
opspec.DaemonEnvVars(c.clusterSpec.CephVersion.Image),
),
Resources: nfs.Spec.Server.Resources,
}
}
func (c *CephNFSController) daemonContainer(nfs cephv1.CephNFS, cfg daemonConfig) v1.Container {
_, cephConfigMount := cephConfigVolumeAndMount()
_, nfsConfigMount := nfsConfigVolumeAndMount(cfg.ConfigConfigMap)
_, dbusMount := dbusVolumeAndMount()
return v1.Container{
Name: "nfs-ganesha",
Command: []string{
"ganesha.nfsd",
},
Args: []string{
"-F", // foreground
"-L", "STDERR", // log to stderr
"-p", ganeshaPid, // PID file location
},
Image: c.clusterSpec.CephVersion.Image,
VolumeMounts: []v1.VolumeMount{
cephConfigMount,
keyring.VolumeMount().Admin(),
nfsConfigMount,
dbusMount,
},
Env: append(
opspec.DaemonEnvVars(c.clusterSpec.CephVersion.Image),
),
Resources: nfs.Spec.Server.Resources,
SecurityContext: mon.PodSecurityContext(),
}
}
func (c *CephNFSController) dbusContainer(nfs cephv1.CephNFS) v1.Container {
_, dbusMount := dbusVolumeAndMount()
return v1.Container{
Name: "dbus-daemon",
Command: []string{
"dbus-daemon",
},
Args: []string{
"--nofork", // run in foreground
"--system", // use system config file (uses /run/dbus/system_bus_socket)
"--nopidfile", // don't write a pid file
// some dbus-daemon versions have flag --nosyslog to send logs to sterr; not ceph upstream image
},
Image: c.clusterSpec.CephVersion.Image,
VolumeMounts: []v1.VolumeMount{
dbusMount,
},
Env: append(
// do not need access to Ceph env vars b/c not a Ceph daemon
k8sutil.ClusterDaemonEnvVars(c.clusterSpec.CephVersion.Image),
),
Resources: nfs.Spec.Server.Resources,
}
}
func getLabels(n cephv1.CephNFS, name string) map[string]string {
labels := opspec.AppLabels(appName, n.Namespace)
labels["ceph_nfs"] = n.Name
labels["instance"] = name
return labels
}
func cephConfigVolumeAndMount() (v1.Volume, v1.VolumeMount) {
// nfs ganesha produces its own ceph config file, so cannot use opspec.DaemonVolume or
// opspec.DaemonVolumeMounts since that will bring in global ceph config file
cfgDir := cephconfig.DefaultConfigDir
volName := k8sutil.PathToVolumeName(cfgDir)
v := v1.Volume{Name: volName, VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}
m := v1.VolumeMount{Name: volName, MountPath: cfgDir}
return v, m
}
func nfsConfigVolumeAndMount(configConfigMap string) (v1.Volume, v1.VolumeMount) {
cfgDir := "/etc/ganesha" // cfg file: /etc/ganesha/ganesha.conf
cfgVolName := ganeshaConfigVolume
configMapSource := &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{Name: configConfigMap},
Items: []v1.KeyToPath{{Key: "config", Path: "ganesha.conf"}},
}
v := v1.Volume{Name: cfgVolName, VolumeSource: v1.VolumeSource{ConfigMap: configMapSource}}
m := v1.VolumeMount{Name: cfgVolName, MountPath: cfgDir}
return v, m
}
func dbusVolumeAndMount() (v1.Volume, v1.VolumeMount) {
dbusSocketDir := "/run/dbus" // socket file: /run/dbus/system_bus_socket
volName := k8sutil.PathToVolumeName(dbusSocketDir)
v := v1.Volume{Name: volName, VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}
m := v1.VolumeMount{Name: volName, MountPath: dbusSocketDir}
return v, m
}