Skip to content

Commit 3727017

Browse files
authored
Fix Bug : Operator Crash when persistence is false (#519)
Signed-off-by: Shubham Gupta <iamshubhamgupta2001@gmail.com>
1 parent 2142b25 commit 3727017

File tree

6 files changed

+58
-5
lines changed

6 files changed

+58
-5
lines changed

k8sutils/finalizer.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,15 @@ func finalizeRedisClusterPVC(cr *redisv1beta1.RedisCluster) error {
232232
logger := finalizerLogger(cr.Namespace, RedisClusterFinalizer)
233233
for _, role := range []string{"leader", "follower"} {
234234
for i := 0; i < int(cr.Spec.GetReplicaCounts(role)); i++ {
235-
PVCName := cr.Name + "-" + role + "-" + cr.Name + "-" + role + "-" + strconv.Itoa(i)
235+
PVCName := "data" + cr.Name + "-" + role + "-" + strconv.Itoa(i)
236+
err := generateK8sClient().CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})
237+
if err != nil && !errors.IsNotFound(err) {
238+
logger.Error(err, "Could not delete Persistent Volume Claim "+PVCName)
239+
return err
240+
}
241+
}
242+
for i := 0; i < int(cr.Spec.GetReplicaCounts(role)); i++ {
243+
PVCName := "node-conf" + cr.Name + "-" + role + "-" + strconv.Itoa(i)
236244
err := generateK8sClient().CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})
237245
if err != nil && !errors.IsNotFound(err) {
238246
logger.Error(err, "Could not delete Persistent Volume Claim "+PVCName)

k8sutils/redis-cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func generateRedisClusterParams(cr *redisv1beta1.RedisCluster, replicas int32, e
2929
res := statefulSetParameters{
3030
Metadata: cr.ObjectMeta,
3131
Replicas: &replicas,
32+
ClusterMode: true,
3233
NodeSelector: params.NodeSelector,
3334
PodSecurityContext: cr.Spec.PodSecurityContext,
3435
PriorityClassName: cr.Spec.PriorityClassName,

k8sutils/redis-replication.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func generateRedisReplicationParams(cr *redisv1beta1.RedisReplication) statefulS
6969
replicas := cr.Spec.GetReplicationCounts("Replication")
7070
res := statefulSetParameters{
7171
Replicas: &replicas,
72+
ClusterMode: false,
7273
NodeSelector: cr.Spec.NodeSelector,
7374
PodSecurityContext: cr.Spec.PodSecurityContext,
7475
PriorityClassName: cr.Spec.PriorityClassName,

k8sutils/redis-sentinel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func generateRedisSentinelParams(cr *redisv1beta1.RedisSentinel, replicas int32,
8888
res := statefulSetParameters{
8989
Metadata: cr.ObjectMeta,
9090
Replicas: &replicas,
91+
ClusterMode: false,
9192
NodeSelector: cr.Spec.NodeSelector,
9293
PodSecurityContext: cr.Spec.PodSecurityContext,
9394
PriorityClassName: cr.Spec.PriorityClassName,

k8sutils/redis-standalone.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func generateRedisStandaloneParams(cr *redisv1beta1.Redis) statefulSetParameters
7373
replicas := int32(1)
7474
res := statefulSetParameters{
7575
Replicas: &replicas,
76+
ClusterMode: false,
7677
NodeSelector: cr.Spec.NodeSelector,
7778
PodSecurityContext: cr.Spec.PodSecurityContext,
7879
PriorityClassName: cr.Spec.PriorityClassName,

k8sutils/statefulset.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
corev1 "k8s.io/api/core/v1"
1818
apiequality "k8s.io/apimachinery/pkg/api/equality"
1919
apierrors "k8s.io/apimachinery/pkg/api/errors"
20+
"k8s.io/apimachinery/pkg/api/resource"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
"k8s.io/apimachinery/pkg/labels"
2223
)
@@ -28,6 +29,7 @@ const (
2829
// statefulSetParameters will define statefulsets input params
2930
type statefulSetParameters struct {
3031
Replicas *int32
32+
ClusterMode bool
3133
Metadata metav1.ObjectMeta
3234
NodeSelector map[string]string
3335
PodSecurityContext *corev1.PodSecurityContext
@@ -241,6 +243,9 @@ func generateStatefulSetsDef(stsMeta metav1.ObjectMeta, params statefulSetParame
241243
if params.ImagePullSecrets != nil {
242244
statefulset.Spec.Template.Spec.ImagePullSecrets = *params.ImagePullSecrets
243245
}
246+
if containerParams.PersistenceEnabled != nil && params.ClusterMode {
247+
statefulset.Spec.VolumeClaimTemplates = append(statefulset.Spec.VolumeClaimTemplates, createPVCTemplateNodeConf(stsMeta))
248+
}
244249
if containerParams.PersistenceEnabled != nil && *containerParams.PersistenceEnabled {
245250
statefulset.Spec.VolumeClaimTemplates = append(statefulset.Spec.VolumeClaimTemplates, createPVCTemplate(stsMeta, params.PersistentVolumeClaim))
246251
}
@@ -295,6 +300,35 @@ func getExternalConfig(configMapName string) []corev1.Volume {
295300
}
296301
}
297302

303+
// createPVCTemplate to store node.conf in durable manner
304+
func createPVCTemplateNodeConf(stsMeta metav1.ObjectMeta) corev1.PersistentVolumeClaim {
305+
// 1Mb Resource requirement for the node.conf
306+
pvcNodeConf := corev1.PersistentVolumeClaim{
307+
ObjectMeta: metav1.ObjectMeta{
308+
Name: "node-conf",
309+
},
310+
Spec: corev1.PersistentVolumeClaimSpec{
311+
AccessModes: []corev1.PersistentVolumeAccessMode{
312+
corev1.ReadWriteOnce,
313+
},
314+
Resources: corev1.ResourceRequirements{
315+
Requests: corev1.ResourceList{
316+
corev1.ResourceStorage: resource.MustParse("1Mi"),
317+
},
318+
},
319+
VolumeMode: func() *corev1.PersistentVolumeMode {
320+
volumeMode := corev1.PersistentVolumeFilesystem
321+
return &volumeMode
322+
}(),
323+
},
324+
}
325+
// Load the labels from the statefulset
326+
pvcNodeConf.Labels = stsMeta.GetLabels()
327+
// We want the same annoations as the StatefulSet here
328+
pvcNodeConf.Annotations = generateStatefulSetsAnots(stsMeta)
329+
return pvcNodeConf
330+
}
331+
298332
// createPVCTemplate will create the persistent volume claim template
299333
func createPVCTemplate(stsMeta metav1.ObjectMeta, storageSpec corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim {
300334
pvcTemplate := storageSpec
@@ -339,7 +373,7 @@ func generateContainerDef(name string, containerParams containerParameters, enab
339373
),
340374
ReadinessProbe: getProbeInfo(containerParams.ReadinessProbe),
341375
LivenessProbe: getProbeInfo(containerParams.LivenessProbe),
342-
VolumeMounts: getVolumeMount(name, containerParams.PersistenceEnabled, externalConfig, mountpath, containerParams.TLSConfig, containerParams.ACLConfig),
376+
VolumeMounts: getVolumeMount(name, containerParams.PersistenceEnabled, true, externalConfig, mountpath, containerParams.TLSConfig, containerParams.ACLConfig),
343377
},
344378
}
345379

@@ -388,7 +422,7 @@ func generateInitContainerDef(name string, initcontainerParams initContainerPara
388422
ImagePullPolicy: initcontainerParams.ImagePullPolicy,
389423
Command: initcontainerParams.Command,
390424
Args: initcontainerParams.Arguments,
391-
VolumeMounts: getVolumeMount(name, initcontainerParams.PersistenceEnabled, nil, mountpath, nil, nil),
425+
VolumeMounts: getVolumeMount(name, initcontainerParams.PersistenceEnabled, false, nil, mountpath, nil, nil),
392426
},
393427
}
394428

@@ -458,7 +492,7 @@ func enableRedisMonitoring(params containerParameters) corev1.Container {
458492
params.TLSConfig,
459493
params.ACLConfig,
460494
),
461-
VolumeMounts: getVolumeMount("", nil, nil, params.AdditionalMountPath, params.TLSConfig, params.ACLConfig), // We need/want the tls-certs but we DON'T need the PVC (if one is available)
495+
VolumeMounts: getVolumeMount("", nil, false, nil, params.AdditionalMountPath, params.TLSConfig, params.ACLConfig), // We need/want the tls-certs but we DON'T need the PVC (if one is available)
462496
Ports: []corev1.ContainerPort{
463497
{
464498
Name: redisExporterPortName,
@@ -474,9 +508,16 @@ func enableRedisMonitoring(params containerParameters) corev1.Container {
474508
}
475509

476510
// getVolumeMount gives information about persistence mount
477-
func getVolumeMount(name string, persistenceEnabled *bool, externalConfig *string, mountpath []corev1.VolumeMount, tlsConfig *redisv1beta1.TLSConfig, aclConfig *redisv1beta1.ACLConfig) []corev1.VolumeMount {
511+
func getVolumeMount(name string, persistenceEnabled *bool, cluster bool, externalConfig *string, mountpath []corev1.VolumeMount, tlsConfig *redisv1beta1.TLSConfig, aclConfig *redisv1beta1.ACLConfig) []corev1.VolumeMount {
478512
var VolumeMounts []corev1.VolumeMount
479513

514+
if persistenceEnabled != nil && cluster {
515+
VolumeMounts = append(VolumeMounts, corev1.VolumeMount{
516+
Name: "node-conf",
517+
MountPath: "/node-conf",
518+
})
519+
}
520+
480521
if persistenceEnabled != nil && *persistenceEnabled {
481522
VolumeMounts = append(VolumeMounts, corev1.VolumeMount{
482523
Name: name,

0 commit comments

Comments
 (0)