forked from rook/rook
-
Notifications
You must be signed in to change notification settings - Fork 9
/
resource.go
325 lines (298 loc) · 9.21 KB
/
resource.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
/*
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 util
import (
"fmt"
cassandrav1alpha1 "github.com/rook/rook/pkg/apis/cassandra.rook.io/v1alpha1"
"github.com/rook/rook/pkg/operator/cassandra/constants"
"github.com/rook/rook/pkg/operator/k8sutil"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
func StatefulSetNameForRack(r cassandrav1alpha1.RackSpec, c *cassandrav1alpha1.Cluster) string {
return fmt.Sprintf("%s-%s-%s", c.Name, c.Spec.Datacenter.Name, r.Name)
}
func ServiceAccountNameForMembers(c *cassandrav1alpha1.Cluster) string {
return fmt.Sprintf("%s-member", c.Name)
}
func HeadlessServiceNameForCluster(c *cassandrav1alpha1.Cluster) string {
return fmt.Sprintf("%s-client", c.Name)
}
func ImageForCluster(c *cassandrav1alpha1.Cluster) string {
var repo string
switch c.Spec.Mode {
case cassandrav1alpha1.ClusterModeScylla:
repo = "scylladb/scylla"
default:
repo = "cassandra"
}
if c.Spec.Repository != nil {
repo = *c.Spec.Repository
}
return fmt.Sprintf("%s:%s", repo, c.Spec.Version)
}
func StatefulSetForRack(r cassandrav1alpha1.RackSpec, c *cassandrav1alpha1.Cluster, rookImage string) *appsv1.StatefulSet {
rackLabels := RackLabels(r, c)
stsName := StatefulSetNameForRack(r, c)
return &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: stsName,
Namespace: c.Namespace,
Labels: rackLabels,
OwnerReferences: []metav1.OwnerReference{NewControllerRef(c)},
},
Spec: appsv1.StatefulSetSpec{
Replicas: RefFromInt32(0),
// Use a common Headless Service for all StatefulSets
ServiceName: HeadlessServiceNameForCluster(c),
Selector: &metav1.LabelSelector{
MatchLabels: rackLabels,
},
PodManagementPolicy: appsv1.OrderedReadyPodManagement,
UpdateStrategy: appsv1.StatefulSetUpdateStrategy{
Type: appsv1.RollingUpdateStatefulSetStrategyType,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: rackLabels,
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
{
Name: "shared",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
},
InitContainers: []corev1.Container{
{
Name: "rook-install",
Image: rookImage,
ImagePullPolicy: "IfNotPresent",
Command: []string{
"/bin/sh",
"-c",
fmt.Sprintf("cp -a /sidecar/* %s", constants.SharedDirName),
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "shared",
MountPath: constants.SharedDirName,
ReadOnly: false,
},
},
},
},
Containers: []corev1.Container{
{
Name: "cassandra",
Image: ImageForCluster(c),
ImagePullPolicy: "IfNotPresent",
Ports: []corev1.ContainerPort{
{
Name: "intra-node",
ContainerPort: 7000,
},
{
Name: "tls-intra-node",
ContainerPort: 7001,
},
{
Name: "jmx",
ContainerPort: 7199,
},
{
Name: "cql",
ContainerPort: 9042,
},
{
Name: "thrift",
ContainerPort: 9160,
},
{
Name: "jolokia",
ContainerPort: 8778,
},
{
Name: "prometheus",
ContainerPort: 9180,
},
},
// TODO: unprivileged entrypoint
Command: []string{
fmt.Sprintf("%s/tini", constants.SharedDirName),
"--",
fmt.Sprintf("%s/rook", constants.SharedDirName),
},
Args: []string{
"cassandra",
"sidecar",
},
Env: []corev1.EnvVar{
{
Name: constants.PodIPEnvVar,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "status.podIP",
},
},
},
{
Name: k8sutil.PodNameEnvVar,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
{
Name: k8sutil.PodNamespaceEnvVar,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
{
Name: constants.ResourceLimitCPUEnvVar,
ValueFrom: &corev1.EnvVarSource{
ResourceFieldRef: &corev1.ResourceFieldSelector{
ContainerName: "cassandra",
Resource: "limits.cpu",
Divisor: resource.MustParse("1"),
},
},
},
{
Name: constants.ResourceLimitMemoryEnvVar,
ValueFrom: &corev1.EnvVarSource{
ResourceFieldRef: &corev1.ResourceFieldSelector{
ContainerName: "cassandra",
Resource: "limits.memory",
Divisor: resource.MustParse("1Mi"),
},
},
},
},
Resources: r.Resources,
VolumeMounts: volumeMountsForRack(r, c),
LivenessProbe: &corev1.Probe{
// Initial delay should be big, because scylla runs benchmarks
// to tune the IO settings.
InitialDelaySeconds: int32(400),
TimeoutSeconds: int32(5),
// TODO: Investigate if it's ok to call status every 10 seconds
PeriodSeconds: int32(10),
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Port: intstr.FromInt(constants.ProbePort),
Path: constants.LivenessProbePath,
},
},
},
ReadinessProbe: &corev1.Probe{
InitialDelaySeconds: int32(15),
TimeoutSeconds: int32(5),
// TODO: Investigate if it's ok to call status every 10 seconds
PeriodSeconds: int32(10),
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Port: intstr.FromInt(constants.ProbePort),
Path: constants.ReadinessProbePath,
},
},
},
// Before a Cassandra Pod is stopped, execute nodetool drain to
// flush the memtable to disk and stop listening for connections.
// This is necessary to ensure we don't lose any data.
Lifecycle: &corev1.Lifecycle{
PreStop: &corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{
"nodetool",
"drain",
},
},
},
},
},
},
// Set GracePeriod to 2 days, should be enough even for the slowest of systems
TerminationGracePeriodSeconds: RefFromInt64(200000),
ServiceAccountName: ServiceAccountNameForMembers(c),
Affinity: affinityForRack(r),
Tolerations: tolerationsForRack(r),
},
},
VolumeClaimTemplates: volumeClaimTemplatesForRack(r.Storage.VolumeClaimTemplates),
},
}
}
// TODO: Maybe move this logic to a defaulter
func volumeClaimTemplatesForRack(claims []corev1.PersistentVolumeClaim) []corev1.PersistentVolumeClaim {
if len(claims) == 0 {
return claims
}
for i := range claims {
claims[i].Spec.AccessModes = []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}
}
return claims
}
// GetDataDir returns the directory used to store the database data
func GetDataDir(c *cassandrav1alpha1.Cluster) string {
if c.Spec.Mode == cassandrav1alpha1.ClusterModeScylla {
return constants.DataDirScylla
}
return constants.DataDirCassandra
}
// volumeMountsForRack returns the VolumeMounts for that a Pod of the
// specific rack should have. Currently, it only supports 1 volume.
// If the user has specified more than 1 volumes, it only uses the
// first one.
// TODO: Modify to handle JBOD
func volumeMountsForRack(r cassandrav1alpha1.RackSpec, c *cassandrav1alpha1.Cluster) []corev1.VolumeMount {
vm := []corev1.VolumeMount{
{
Name: "shared",
MountPath: constants.SharedDirName,
ReadOnly: true,
},
}
if len(r.Storage.VolumeClaimTemplates) > 0 {
vm = append(vm, corev1.VolumeMount{
Name: r.Storage.VolumeClaimTemplates[0].Name,
MountPath: GetDataDir(c),
})
}
return vm
}
func tolerationsForRack(r cassandrav1alpha1.RackSpec) []corev1.Toleration {
if r.Placement == nil {
return nil
}
return r.Placement.Tolerations
}
func affinityForRack(r cassandrav1alpha1.RackSpec) *corev1.Affinity {
if r.Placement == nil {
return nil
}
return &corev1.Affinity{
PodAffinity: r.Placement.PodAffinity,
PodAntiAffinity: r.Placement.PodAntiAffinity,
NodeAffinity: r.Placement.NodeAffinity,
}
}