Skip to content

Commit

Permalink
feat: add field to keep pvc after cr be deleted (#772)
Browse files Browse the repository at this point in the history
* feat: add field to keep pvc after cr be deleted

Signed-off-by: drivebyer <wuyangmuc@gmail.com>

* fix kind

Signed-off-by: drivebyer <wuyangmuc@gmail.com>

* fix delete name

Signed-off-by: drivebyer <wuyangmuc@gmail.com>

---------

Signed-off-by: drivebyer <wuyangmuc@gmail.com>
  • Loading branch information
drivebyer authored Feb 9, 2024
1 parent bb69d00 commit df2df6d
Show file tree
Hide file tree
Showing 22 changed files with 915 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/e2e-chainsaw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- ./tests/e2e-chainsaw/v1beta2/ha-setup/
- ./tests/e2e-chainsaw/v1beta2/nodeport/
- ./tests/e2e-chainsaw/v1beta2/pvc-name/
- ./tests/e2e-chainsaw/v1beta2/keep-pvc/

steps:
- name: Checkout code
Expand Down
1 change: 1 addition & 0 deletions api/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type RedisConfig struct {
// Storage is the inteface to add pvc and pv support in redis
// +k8s:deepcopy-gen=true
type Storage struct {
KeepAfterDelete bool `json:"keepAfterDelete,omitempty"`
VolumeClaimTemplate corev1.PersistentVolumeClaim `json:"volumeClaimTemplate,omitempty"`
VolumeMount AdditionalVolume `json:"volumeMount,omitempty"`
}
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/redis.redis.opstreelabs.in_redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,8 @@ spec:
description: Storage is the inteface to add pvc and pv support in
redis
properties:
keepAfterDelete:
type: boolean
volumeClaimTemplate:
description: PersistentVolumeClaim is a user's request for and
claim to a persistent volume
Expand Down Expand Up @@ -6098,6 +6100,8 @@ spec:
description: Storage is the inteface to add pvc and pv support in
redis
properties:
keepAfterDelete:
type: boolean
volumeClaimTemplate:
description: PersistentVolumeClaim is a user's request for and
claim to a persistent volume
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2884,6 +2884,8 @@ spec:
description: Storage is the inteface to add pvc and pv support in
redis
properties:
keepAfterDelete:
type: boolean
volumeClaimTemplate:
description: PersistentVolumeClaim is a user's request for and
claim to a persistent volume
Expand Down Expand Up @@ -8657,6 +8659,8 @@ spec:
storage:
description: Node-conf needs to be added only in redis cluster
properties:
keepAfterDelete:
type: boolean
nodeConfVolume:
default: false
type: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,8 @@ spec:
description: Storage is the inteface to add pvc and pv support in
redis
properties:
keepAfterDelete:
type: boolean
volumeClaimTemplate:
description: PersistentVolumeClaim is a user's request for and
claim to a persistent volume
Expand Down Expand Up @@ -6103,6 +6105,8 @@ spec:
description: Storage is the inteface to add pvc and pv support in
redis
properties:
keepAfterDelete:
type: boolean
volumeClaimTemplate:
description: PersistentVolumeClaim is a user's request for and
claim to a persistent volume
Expand Down
18 changes: 12 additions & 6 deletions k8sutils/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ func finalizerLogger(namespace string, name string) logr.Logger {
func HandleRedisFinalizer(ctrlclient client.Client, k8sClient kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.Redis) error {
if cr.GetDeletionTimestamp() != nil {
if controllerutil.ContainsFinalizer(cr, RedisFinalizer) {
if err := finalizeRedisPVC(k8sClient, logger, cr); err != nil {
return err
if !cr.Spec.Storage.KeepAfterDelete {
if err := finalizeRedisPVC(k8sClient, logger, cr); err != nil {
return err
}
}
controllerutil.RemoveFinalizer(cr, RedisFinalizer)
if err := ctrlclient.Update(context.TODO(), cr); err != nil {
Expand All @@ -48,8 +50,10 @@ func HandleRedisFinalizer(ctrlclient client.Client, k8sClient kubernetes.Interfa
func HandleRedisClusterFinalizer(ctrlclient client.Client, k8sClient kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisCluster) error {
if cr.GetDeletionTimestamp() != nil {
if controllerutil.ContainsFinalizer(cr, RedisClusterFinalizer) {
if err := finalizeRedisClusterPVC(k8sClient, logger, cr); err != nil {
return err
if !cr.Spec.Storage.KeepAfterDelete {
if err := finalizeRedisClusterPVC(k8sClient, logger, cr); err != nil {
return err
}
}
controllerutil.RemoveFinalizer(cr, RedisClusterFinalizer)
if err := ctrlclient.Update(context.TODO(), cr); err != nil {
Expand All @@ -65,8 +69,10 @@ func HandleRedisClusterFinalizer(ctrlclient client.Client, k8sClient kubernetes.
func HandleRedisReplicationFinalizer(ctrlclient client.Client, k8sClient kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisReplication) error {
if cr.GetDeletionTimestamp() != nil {
if controllerutil.ContainsFinalizer(cr, RedisReplicationFinalizer) {
if err := finalizeRedisReplicationPVC(k8sClient, logger, cr); err != nil {
return err
if !cr.Spec.Storage.KeepAfterDelete {
if err := finalizeRedisReplicationPVC(k8sClient, logger, cr); err != nil {
return err
}
}
controllerutil.RemoveFinalizer(cr, RedisReplicationFinalizer)
if err := ctrlclient.Update(context.TODO(), cr); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json

apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: keep-pvc
spec:
steps:
- name: Install
try:
- apply:
file: cluster.yaml
- assert:
file: ready-cluster.yaml
- assert:
file: ready-sts.yaml
- assert:
file: ready-svc.yaml
- assert:
file: ready-pvc.yaml

- name: Uninstall
try:
- delete:
ref:
name: redis-cluster-v1beta2
kind: RedisCluster
apiVersion: redis.redis.opstreelabs.in/v1beta2
- error:
file: ready-sts.yaml
- error:
file: ready-svc.yaml
- assert:
file: ready-pvc.yaml
48 changes: 48 additions & 0 deletions tests/e2e-chainsaw/v1beta2/keep-pvc/redis-cluster/cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
apiVersion: redis.redis.opstreelabs.in/v1beta2
kind: RedisCluster
metadata:
name: redis-cluster-v1beta2
spec:
clusterSize: 3
clusterVersion: v7
persistenceEnabled: true
podSecurityContext:
runAsUser: 1000
fsGroup: 1000
kubernetesConfig:
image: quay.io/opstree/redis:latest
imagePullPolicy: Always
resources:
requests:
cpu: 101m
memory: 128Mi
limits:
cpu: 101m
memory: 128Mi
redisExporter:
enabled: true
image: quay.io/opstree/redis-exporter:v1.44.0
imagePullPolicy: Always
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 100m
memory: 128Mi
storage:
keepAfterDelete: true
volumeClaimTemplate:
spec:
# storageClassName: standard
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
nodeConfVolume: true
nodeConfVolumeClaimTemplate:
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: redis.redis.opstreelabs.in/v1beta2
kind: RedisCluster
metadata:
name: redis-cluster-v1beta2
status:
readyFollowerReplicas: 3
readyLeaderReplicas: 3
181 changes: 181 additions & 0 deletions tests/e2e-chainsaw/v1beta2/keep-pvc/redis-cluster/ready-pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: node-conf-redis-cluster-v1beta2-leader-0
labels:
app: redis-cluster-v1beta2-leader
redis_setup_type: cluster
role: leader
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: node-conf-redis-cluster-v1beta2-leader-1
labels:
app: redis-cluster-v1beta2-leader
redis_setup_type: cluster
role: leader
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: node-conf-redis-cluster-v1beta2-leader-2
labels:
app: redis-cluster-v1beta2-leader
redis_setup_type: cluster
role: leader
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: node-conf-redis-cluster-v1beta2-follower-0
labels:
app: redis-cluster-v1beta2-follower
redis_setup_type: cluster
role: follower
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: node-conf-redis-cluster-v1beta2-follower-1
labels:
app: redis-cluster-v1beta2-follower
redis_setup_type: cluster
role: follower
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: node-conf-redis-cluster-v1beta2-follower-2
labels:
app: redis-cluster-v1beta2-follower
redis_setup_type: cluster
role: follower
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-cluster-v1beta2-leader-redis-cluster-v1beta2-leader-0
labels:
app: redis-cluster-v1beta2-leader
redis_setup_type: cluster
role: leader
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-cluster-v1beta2-leader-redis-cluster-v1beta2-leader-1
labels:
app: redis-cluster-v1beta2-leader
redis_setup_type: cluster
role: leader
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-cluster-v1beta2-leader-redis-cluster-v1beta2-leader-2
labels:
app: redis-cluster-v1beta2-leader
redis_setup_type: cluster
role: leader
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-cluster-v1beta2-follower-redis-cluster-v1beta2-follower-0
labels:
app: redis-cluster-v1beta2-follower
redis_setup_type: cluster
role: follower
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-cluster-v1beta2-follower-redis-cluster-v1beta2-follower-1
labels:
app: redis-cluster-v1beta2-follower
redis_setup_type: cluster
role: follower
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-cluster-v1beta2-follower-redis-cluster-v1beta2-follower-2
labels:
app: redis-cluster-v1beta2-follower
redis_setup_type: cluster
role: follower
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
phase: Bound
Loading

0 comments on commit df2df6d

Please sign in to comment.