Browse Source

added pvc, pvc class and pvc finalizer

tags/0.0.2
Vladimir Smagin 1 month ago
parent
commit
dfe5ed3730
Signed by: Vladimir Smagin <21h@blindage.org> GPG Key ID: 13BCE12E5618F071

+ 1
- 0
deploy/clusterrole_binding.yaml View File

@@ -5,6 +5,7 @@ metadata:
subjects:
- kind: ServiceAccount
name: redis-operator
namespace: operators
roleRef:
kind: ClusterRole
name: redis-operator

+ 1
- 3
deploy/operator.yaml View File

@@ -21,9 +21,7 @@ spec:
imagePullPolicy: Always
env:
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
value: ""
- name: POD_NAME
valueFrom:
fieldRef:

+ 2
- 0
deploy/test-instance.yaml View File

@@ -8,3 +8,5 @@ spec:
sentinelReplicas: 3
redisReplicas: 3
quorum: 2
persistentVolumeSize: 1Gi
persistentVolumePurge: false

+ 1
- 1
go.mod View File

@@ -36,4 +36,4 @@ replace (
sigs.k8s.io/controller-tools => sigs.k8s.io/controller-tools v0.1.11-0.20190411181648-9d55346c2bde
)

go 1.13
go 1.12

+ 8
- 6
pkg/apis/blindage/v1alpha1/redis_types.go View File

@@ -9,12 +9,14 @@ import (
// RedisSpec defines the desired state of Redis
// +k8s:openapi-gen=true
type RedisSpec struct {
Image string `json:"image,omitempty"` // default redis:latest
PullPolicy *v1.PullPolicy `json:"pullPolicy,omitempty"` // default "if not exists"
SentinelReplicas *int32 `json:"sentinelReplicas"` // default 3
RedisReplicas *int32 `json:"redisReplicas"` // default 3`
Quorum int32 `json:"quorum"` // default 2`
PersistentVolumeSize resource.Quantity `json:"persistentVolumeSize,omitempty"` // default 1Gi
Image string `json:"image,omitempty"` // default redis:latest
PullPolicy *v1.PullPolicy `json:"pullPolicy,omitempty"` // default "if not exists"
SentinelReplicas *int32 `json:"sentinelReplicas"` // default 3
RedisReplicas *int32 `json:"redisReplicas"` // default 3`
Quorum int32 `json:"quorum"` // default 2`
PersistentVolumeSize resource.Quantity `json:"persistentVolumeSize,omitempty"` // default 1Gi
PersistentVolumeClass string `json:"persistentVolumeClass,omitempty"` // default empty
PersistentVolumePurge bool `json:"persistentVolumePurge,omitempty"` // default false (save PVC afrer CR was deleted)

// haproxy
UseHAProxy bool `json:"useHAProxy,omitempty"` // set true to activate HAProxy deployment

+ 20
- 6
pkg/controller/manifests/manifest_statefulset.go View File

@@ -111,12 +111,6 @@ func GenerateStatefulSet(cr *blindagev1alpha1.Redis) v1.StatefulSet {
Affinity: affinity,
Containers: podContainers,
Volumes: []corev1.Volume{
{
Name: "data",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: "failover",
VolumeSource: corev1.VolumeSource{
@@ -146,6 +140,25 @@ func GenerateStatefulSet(cr *blindagev1alpha1.Redis) v1.StatefulSet {
replicas = *cr.Spec.RedisReplicas
}

PVCTemplate := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "data",
// Finalizers: cr.ObjectMeta.Finalizers,
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: cr.Spec.PersistentVolumeSize,
},
},
},
}

if cr.Spec.PersistentVolumeClass != "" {
PVCTemplate.Spec.StorageClassName = &cr.Spec.PersistentVolumeClass
}

sts := v1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name + "-redis",
@@ -163,6 +176,7 @@ func GenerateStatefulSet(cr *blindagev1alpha1.Redis) v1.StatefulSet {
UpdateStrategy: v1.StatefulSetUpdateStrategy{
Type: v1.RollingUpdateStatefulSetStrategyType,
},
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{PVCTemplate},
},
}


+ 19
- 0
pkg/controller/manifests/utils.go View File

@@ -38,3 +38,22 @@ func MergeENV(envs []v1.EnvVar) []v1.EnvVar {
},
}, envs...)
}

func ContainsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}

func RemoveString(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return
}

+ 62
- 0
pkg/controller/redis/finalizers.go View File

@@ -0,0 +1,62 @@
package redis

import (
"context"
"git.blindage.org/21h/redis-operator/pkg/controller/manifests"
blindagev1alpha1 "git.blindage.org/21h/redis-operator/pkg/apis/blindage/v1alpha1"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func (r *ReconcileRedis) deleteDependentResoucePVC(reqLogger logr.Logger, cr *blindagev1alpha1.Redis) (reconcile.Result, error) {
reqLogger.Info("Deleting PVC")
if manifests.ContainsString(cr.ObjectMeta.Finalizers, "PurgePVC") {
foundDependentPVCs := &corev1.PersistentVolumeClaimList{}

labels := manifests.MergeLabels(manifests.BaseLabels(cr))

err := r.client.List(context.TODO(), client.InNamespace(cr.Namespace).MatchingLabels(labels), foundDependentPVCs)
if err != nil {
reqLogger.Info("Listing dependent PVCs error", "Namespace", cr.Namespace, ".Name", cr.Name)
return reconcile.Result{}, err
}

for _, pvc := range foundDependentPVCs.Items {
if err := r.client.Delete(context.Background(), &pvc); err != nil {
reqLogger.Info("Deleting PVC failed")
return reconcile.Result{}, err
}
}
}
return reconcile.Result{}, nil
}

func (r *ReconcileRedis) reconcileFinalizers(reqLogger logr.Logger, instance *blindagev1alpha1.Redis) (reconcile.Result, error) {
reqLogger.Info("Processing finalizers", "Namespace", instance.Namespace, ".Name", instance.Name)
finalizersList := []string{"PurgePVC"}
if instance.ObjectMeta.DeletionTimestamp.IsZero() {
instance.ObjectMeta.Finalizers = finalizersList
if err := r.client.Update(context.Background(), instance); err != nil {
return reconcile.Result{}, err
}

} else {
if instance.Spec.PersistentVolumePurge {
_, err := r.deleteDependentResoucePVC(reqLogger, instance)
if err != nil {
reqLogger.Info("PVC deletion error", "Namespace", instance.Namespace, ".Name", instance.Name)
return reconcile.Result{}, err
}
}

instance.ObjectMeta.Finalizers = manifests.RemoveString(instance.ObjectMeta.Finalizers, "PurgePVC")
if err := r.client.Update(context.Background(), instance); err != nil {
reqLogger.Info("Removing PVC finalizer failed")
return reconcile.Result{}, err
}
}

return reconcile.Result{}, nil
}

+ 6
- 0
pkg/controller/redis/redis_controller.go View File

@@ -101,6 +101,12 @@ func (r *ReconcileRedis) Reconcile(request reconcile.Request) (reconcile.Result,
return reconcile.Result{}, err
}

_, err = r.reconcileFinalizers(reqLogger, instance)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
return reconcile.Result{}, err
}
// Prepare Sentinel config
configSentinelName := instance.Name + "-sentinel"
configSentinelTemplate := `

Loading…
Cancel
Save