Skip to content

Commit 9f8b223

Browse files
committed
PR comments fixes
Fixes instaclustr#213 Fixes instaclustr#208 * Fixed some comments from PR review * Added Env to CRD to allow specifying environment for containers (exists in java version) * Added userConfigMap handling (instaclustr#213) * Restored PrivelegedSupported handling (instaclustr#208) * Backup secret volume allows providing GOOGLE_APPLICATION_CREDENTIALS in a secret * Cloud providers creds can be set using Env field in CRD Signed-off-by: Alex Lourie <[email protected]>
1 parent c8cdba9 commit 9f8b223

File tree

6 files changed

+172
-42
lines changed

6 files changed

+172
-42
lines changed

deploy/crds/cassandraoperator_v1alpha1_cassandradatacenter_crd.yaml

+9-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ spec:
2727
type: object
2828
spec:
2929
properties:
30-
backupSecrets:
31-
type: string
32-
cassandraConfig:
30+
backupSecretVolume:
3331
type: object
3432
cassandraImage:
3533
type: string
@@ -39,6 +37,10 @@ spec:
3937
type: string
4038
dataVolumeClaimSpec:
4139
type: object
40+
env:
41+
items:
42+
type: object
43+
type: array
4244
imagePullPolicy:
4345
type: string
4446
imagePullSecrets:
@@ -48,12 +50,16 @@ spec:
4850
nodes:
4951
format: int32
5052
type: integer
53+
privilegedSupported:
54+
type: boolean
5155
prometheusSupport:
5256
type: boolean
5357
resources:
5458
type: object
5559
sidecarImage:
5660
type: string
61+
userConfigMapVolumeSource:
62+
type: object
5763
required:
5864
- nodes
5965
- cassandraImage

examples/go/example-datacenter.yaml

+21-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,29 @@ metadata:
77
spec:
88
cluster: "test-cluster"
99
nodes: 3
10+
racks: 3
1011
cassandraImage: "gcr.io/cassandra-operator/cassandra:3.11.3"
11-
sidecarImage: "192.168.56.104:5000/cassandra-sidecar:latest"
12+
sidecarImage: "gcr.io/cassandra-operator/cassandra-sidecar:latest"
1213
imagePullPolicy: IfNotPresent
13-
imagePullSecrets: [{name: regcred}]
14-
backupSecrets: backup-secret
14+
imagePullSecrets:
15+
- name: regcred
16+
backupSecretVolume:
17+
secretName: backup-secret
18+
# type is a workaround for https://github.com/kubernetes/kubernetes/issues/68466
19+
type: array
20+
items:
21+
- key: creds.json
22+
path: creds.json
23+
env:
24+
- name: GOOGLE_APPLICATION_CREDENTIALS
25+
value: "/etc/google/creds.json"
26+
userConfigMapVolumeSource:
27+
name: concurrent-data
28+
# type is a workaround for https://github.com/kubernetes/kubernetes/issues/68466
29+
type: array
30+
items:
31+
- key: "concurrent.yaml"
32+
path: "cassandra.yaml.d/100-user.yaml"
1533
resources:
1634
limits:
1735
memory: 1Gi

pkg/apis/cassandraoperator/v1alpha1/cassandradatacenter.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ import (
1010
type CassandraDataCenterSpec struct {
1111
// Cluster is either a string or v1.LocalObjectReference
1212
//Cluster interface{} `json:"cluster,omitempty"`
13-
Cluster string `json:"cluster,omitempty"`
14-
Nodes int32 `json:"nodes"`
15-
CassandraImage string `json:"cassandraImage"`
16-
SidecarImage string `json:"sidecarImage"`
17-
ImagePullPolicy v1.PullPolicy `json:"imagePullPolicy"`
18-
ImagePullSecrets []v1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
19-
BackupSecrets string `json:"backupSecrets,omitempty"`
20-
UserConfigMap v1.ConfigMapVolumeSource `json:"userConfigMap,omitempty"`
21-
Resources v1.ResourceRequirements `json:"resources"`
22-
DataVolumeClaimSpec v1.PersistentVolumeClaimSpec `json:"dataVolumeClaimSpec"`
23-
PrometheusSupport bool `json:"prometheusSupport"`
13+
Cluster string `json:"cluster,omitempty"`
14+
Nodes int32 `json:"nodes"`
15+
CassandraImage string `json:"cassandraImage"`
16+
SidecarImage string `json:"sidecarImage"`
17+
ImagePullPolicy v1.PullPolicy `json:"imagePullPolicy"`
18+
ImagePullSecrets []v1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
19+
BackupSecretVolume v1.SecretVolumeSource `json:"backupSecretVolume,omitempty"`
20+
UserConfigMapVolumeSource v1.ConfigMapVolumeSource `json:"userConfigMapVolumeSource,omitempty"`
21+
Resources v1.ResourceRequirements `json:"resources"`
22+
DataVolumeClaimSpec v1.PersistentVolumeClaimSpec `json:"dataVolumeClaimSpec"`
23+
PrivilegedSupported bool `json:"privilegedSupported,omitempty"`
24+
PrometheusSupport bool `json:"prometheusSupport"`
25+
Env []v1.EnvVar `json:"env,omitempty"`
2426
}
2527

2628
// CassandraDataCenterStatus defines the observed state of CassandraDataCenter

pkg/apis/cassandraoperator/v1alpha1/zz_generated.deepcopy.go

+30-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/cassandraoperator/v1alpha1/zz_generated.openapi.go

+23-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/cassandradatacenter/statefulset.go

+76-17
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,32 @@ import (
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313
"k8s.io/apimachinery/pkg/labels"
1414
"k8s.io/apimachinery/pkg/runtime"
15+
"path"
1516
"sigs.k8s.io/controller-runtime/pkg/client"
1617
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1718
"strings"
1819
"sync"
1920
)
2021

21-
const DataVolumeMountPath = "/var/lib/cassandra"
22+
const (
23+
DataVolumeMountPath = "/var/lib/cassandra"
24+
OperatorConfigVolumeMountPath = "/tmp/operator-config"
25+
UserConfigVolumeMountPath = "/tmp/user-config"
26+
)
2227

2328
const SidecarApiPort = 4567
2429

30+
const GOOGLE_APPLICATION_CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS"
31+
2532
var sidecarClientOptions = sidecar.ClientOptions{
2633
Port: SidecarApiPort,
2734
Secure: false,
2835
}
2936

37+
func boolRef(b bool) *bool {
38+
return &b
39+
}
40+
3041
func createOrUpdateStatefulSet(rctx *reconciliationRequestContext, configVolume *corev1.Volume) (*v1beta2.StatefulSet, error) {
3142
statefulSet := &v1beta2.StatefulSet{ObjectMeta: DataCenterResourceMetadata(rctx.cdc)}
3243

@@ -40,9 +51,11 @@ func createOrUpdateStatefulSet(rctx *reconciliationRequestContext, configVolume
4051

4152
dataVolumeClaim := newDataVolumeClaim(&rctx.cdc.Spec.DataVolumeClaimSpec)
4253
podInfoVolume := newPodInfoVolume()
54+
backupSecretVolume := newBackupSecretVolume(rctx)
55+
userConfigVolume := newUserConfigVolume(rctx)
4356

44-
cassandraContainer := newCassandraContainer(rctx.cdc, dataVolumeClaim, configVolume)
45-
sidecarContainer := newSidecarContainer(rctx.cdc, dataVolumeClaim, podInfoVolume)
57+
cassandraContainer := newCassandraContainer(rctx.cdc, dataVolumeClaim, configVolume, userConfigVolume)
58+
sidecarContainer := newSidecarContainer(rctx.cdc, dataVolumeClaim, podInfoVolume, backupSecretVolume)
4659

4760
sysctlLimitsContainer := newSysctlLimitsContainer(rctx.cdc)
4861

@@ -51,6 +64,14 @@ func createOrUpdateStatefulSet(rctx *reconciliationRequestContext, configVolume
5164
[]corev1.Container{*cassandraContainer, *sidecarContainer},
5265
[]corev1.Container{*sysctlLimitsContainer})
5366

67+
if backupSecretVolume != nil {
68+
podSpec.Volumes = append(podSpec.Volumes, *backupSecretVolume)
69+
}
70+
71+
if userConfigVolume != nil {
72+
podSpec.Volumes = append(podSpec.Volumes, *userConfigVolume)
73+
}
74+
5475
statefulSetSpec := newStatefulSetSpec(rctx.cdc, podSpec, dataVolumeClaim)
5576

5677
if statefulSet.CreationTimestamp.IsZero() {
@@ -99,17 +120,12 @@ func newPodSpec(cdc *cassandraoperatorv1alpha1.CassandraDataCenter, volumes []co
99120
return podSpec
100121
}
101122

102-
func newCassandraContainer(cdc *cassandraoperatorv1alpha1.CassandraDataCenter, dataVolumeClaim *corev1.PersistentVolumeClaim, configVolume *corev1.Volume) *corev1.Container {
103-
const (
104-
OperatorConfigVolumeMountPath = "/tmp/operator-config"
105-
UserConfigVolumePath = "/etc/cassandra/cassandra.yaml.d/003-user-overrides.yaml"
106-
)
107-
123+
func newCassandraContainer(cdc *cassandraoperatorv1alpha1.CassandraDataCenter, dataVolumeClaim *corev1.PersistentVolumeClaim, configVolume *corev1.Volume, userConfigVolume *corev1.Volume) *corev1.Container {
108124
container := &corev1.Container{
109125
Name: "cassandra",
110126
Image: cdc.Spec.CassandraImage,
111127
ImagePullPolicy: cdc.Spec.ImagePullPolicy,
112-
Args: []string{OperatorConfigVolumeMountPath, UserConfigVolumePath},
128+
Args: []string{OperatorConfigVolumeMountPath},
113129
Ports: []corev1.ContainerPort{
114130
{Name: "internode", ContainerPort: 7000},
115131
{Name: "cql", ContainerPort: 9042},
@@ -139,8 +155,9 @@ func newCassandraContainer(cdc *cassandraoperatorv1alpha1.CassandraDataCenter, d
139155
},
140156
}
141157

142-
if cdc.Spec.UserConfigMap.Size() != 0 {
143-
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{Name: cdc.Spec.UserConfigMap.Name, MountPath: UserConfigVolumePath})
158+
if userConfigVolume != nil {
159+
container.Args = append(container.Args, UserConfigVolumeMountPath)
160+
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{Name: userConfigVolume.Name, MountPath: UserConfigVolumeMountPath})
144161
}
145162

146163
if cdc.Spec.PrometheusSupport == true {
@@ -150,7 +167,7 @@ func newCassandraContainer(cdc *cassandraoperatorv1alpha1.CassandraDataCenter, d
150167
return container
151168
}
152169

153-
func newSidecarContainer(cdc *cassandraoperatorv1alpha1.CassandraDataCenter, dataVolumeClaim *corev1.PersistentVolumeClaim, podInfoVolume *corev1.Volume) *corev1.Container {
170+
func newSidecarContainer(cdc *cassandraoperatorv1alpha1.CassandraDataCenter, dataVolumeClaim *corev1.PersistentVolumeClaim, podInfoVolume *corev1.Volume, backupSecretVolume *corev1.Volume) *corev1.Container {
154171
container := &corev1.Container{
155172
Name: "sidecar",
156173
Image: cdc.Spec.SidecarImage,
@@ -164,10 +181,26 @@ func newSidecarContainer(cdc *cassandraoperatorv1alpha1.CassandraDataCenter, dat
164181
},
165182
}
166183

167-
if len(cdc.Spec.BackupSecrets) != 0 {
168-
container.EnvFrom = []corev1.EnvFromSource{
169-
{SecretRef: &corev1.SecretEnvSource{LocalObjectReference: corev1.LocalObjectReference{Name: cdc.Spec.BackupSecrets}}},
184+
if len(cdc.Spec.Env) > 0 {
185+
container.Env = cdc.Spec.Env
186+
}
187+
188+
if backupSecretVolume != nil {
189+
// find GOOGLE_APPLICATION_CREDENTIALS in env:
190+
google_creds_path := "/etc/gcp"
191+
for _, env := range cdc.Spec.Env {
192+
if env.Name == GOOGLE_APPLICATION_CREDENTIALS {
193+
google_creds_path = path.Dir(env.Value)
194+
}
195+
}
196+
197+
if google_creds_path == "/etc/gcp" {
198+
// environment not set, log it, but it may be not an issue
199+
log.Info("Warning: backupSecretVolume is set, but GOOGLE_APPLICATION_CREDENTIALS env is missing")
170200
}
201+
202+
// Use it for the mount
203+
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{Name: backupSecretVolume.Name, MountPath: google_creds_path})
171204
}
172205

173206
return container
@@ -179,7 +212,7 @@ func newSysctlLimitsContainer(cdc *cassandraoperatorv1alpha1.CassandraDataCenter
179212
Image: cdc.Spec.CassandraImage,
180213
ImagePullPolicy: cdc.Spec.ImagePullPolicy,
181214
SecurityContext: &corev1.SecurityContext{
182-
Privileged: func() *bool { b := true; return &b }(),
215+
Privileged: boolRef(cdc.Spec.PrivilegedSupported),
183216
},
184217
Command: []string{"bash", "-xuec"},
185218
Args: []string{
@@ -188,6 +221,32 @@ func newSysctlLimitsContainer(cdc *cassandraoperatorv1alpha1.CassandraDataCenter
188221
}
189222
}
190223

224+
func newUserConfigVolume(rctx *reconciliationRequestContext) *corev1.Volume {
225+
226+
// check if set
227+
if len(rctx.cdc.Spec.UserConfigMapVolumeSource.Name) == 0 {
228+
return nil
229+
}
230+
231+
return &corev1.Volume{
232+
Name: rctx.cdc.Spec.UserConfigMapVolumeSource.Name,
233+
VolumeSource: corev1.VolumeSource{ConfigMap: &rctx.cdc.Spec.UserConfigMapVolumeSource},
234+
}
235+
}
236+
237+
func newBackupSecretVolume(rctx *reconciliationRequestContext) *corev1.Volume {
238+
239+
// check if set
240+
if len(rctx.cdc.Spec.BackupSecretVolume.SecretName) == 0 {
241+
return nil
242+
}
243+
244+
return &corev1.Volume{
245+
Name: rctx.cdc.Spec.BackupSecretVolume.SecretName,
246+
VolumeSource: corev1.VolumeSource{Secret: &rctx.cdc.Spec.BackupSecretVolume},
247+
}
248+
}
249+
191250
func newPodInfoVolume() *corev1.Volume {
192251
return &corev1.Volume{
193252
Name: "pod-info",

0 commit comments

Comments
 (0)