Skip to content

Commit

Permalink
add e2e test for PVC and resize
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Kvapil <[email protected]>
  • Loading branch information
kvaps committed Jul 18, 2024
1 parent 0976d1e commit cce266a
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ rules:
- storageclasses
verbs:
- get
- list
- apiGroups:
- etcd.aenix.io
resources:
Expand Down
33 changes: 17 additions & 16 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ rules:
- apiGroups:
- ""
resources:
- secrets
- persistentvolumeclaims
verbs:
- get
- list
- patch
- watch
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- list
- watch
- apiGroups:
- ""
Expand All @@ -56,21 +65,6 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- persistentvolumeclaims
verbs:
- get
- list
- patch
- watch
- apiGroups:
- "storage.k8s.io"
resources:
- storageclasses
verbs:
- get
- apiGroups:
- etcd.aenix.io
resources:
Expand Down Expand Up @@ -109,3 +103,10 @@ rules:
- patch
- update
- watch
- apiGroups:
- storage.k8s.io
resources:
- storageclasses
verbs:
- get
- list
5 changes: 2 additions & 3 deletions examples/manifests/etcdcluster-persistent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ apiVersion: etcd.aenix.io/v1alpha1
kind: EtcdCluster
metadata:
name: test
namespace: default
spec:
replicas: 3
storage:
volumeClaimTemplate:
spec:
storageClassName: gp3
storageClassName: standard-with-expansion
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 10Gi
storage: 4Gi
4 changes: 2 additions & 2 deletions internal/controller/etcdcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ type EtcdClusterReconciler struct {
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;watch;delete;patch
// +kubebuilder:rbac:groups="",resources=services,verbs=get;create;delete;update;patch;list;watch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=view;list;watch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
// +kubebuilder:rbac:groups="apps",resources=statefulsets,verbs=get;create;delete;update;patch;list;watch
// +kubebuilder:rbac:groups="policy",resources=poddisruptionbudgets,verbs=get;create;delete;update;patch;list;watch
// +kubebuilder:rbac:groups="",resources=persistentvolumeclaims,verbs=get;list;patch;watch
// +kubebuilder:rbac:groups=storage.k8s.io,resources=storageclasses,verbs=get
// +kubebuilder:rbac:groups=storage.k8s.io,resources=storageclasses,verbs=get;list

// Reconcile checks CR and current cluster state and performs actions to transform current state to desired.
func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/factory/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func GetPVCName(cluster *etcdaenixiov1alpha1.EtcdCluster) string {
// UpdatePersistentVolumeClaims checks and updates the sizes of PVCs in an EtcdCluster if the specified storage size is larger than the current.
func UpdatePersistentVolumeClaims(ctx context.Context, cluster *etcdaenixiov1alpha1.EtcdCluster, rclient client.Client) error {
labelSelector := labels.SelectorFromSet(labels.Set{
"app.kubernetes.io/name": cluster.Name,
"app.kubernetes.io/instance": cluster.Name,
})
listOptions := &client.ListOptions{
Namespace: cluster.Namespace,
Expand Down
85 changes: 80 additions & 5 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -60,7 +61,6 @@ var _ = Describe("etcd-operator", Ordered, func() {
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
})

})

if os.Getenv("DO_CLEANUP_AFTER_E2E") == "true" {
Expand Down Expand Up @@ -117,14 +117,13 @@ var _ = Describe("etcd-operator", Ordered, func() {
By("check etcd cluster is healthy", func() {
Expect(utils.IsEtcdClusterHealthy(ctx, client)).To(BeTrue())
})

})
})

Context("With emptyDir", func() {
It("should deploy etcd cluster", func() {
var err error
const namespace = "test-emtydir-etcd-cluster"
const namespace = "test-emptydir-etcd-cluster"
var wg sync.WaitGroup
wg.Add(1)

Expand Down Expand Up @@ -166,7 +165,85 @@ var _ = Describe("etcd-operator", Ordered, func() {
By("check etcd cluster is healthy", func() {
Expect(utils.IsEtcdClusterHealthy(ctx, client)).To(BeTrue())
})
})
})

Context("With PVC and resize", func() {
const namespace = "test-pvc-and-resize-etcd-cluster"
const storageClass = `
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: standard-with-expansion
provisioner: rancher.io/local-path
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
`

It("should resize PVCs of the etcd cluster", func() {
var err error
By("create namespace", func() {
cmd := exec.Command("kubectl", "create", "namespace", namespace)
_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred())
})

By("create StorageClass", func() {
cmd := exec.Command("kubectl", "apply", "-f", "-")
cmd.Stdin = strings.NewReader(storageClass)
_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred())
})

By("deploying etcd cluster with initial PVC size", func() {
dir, _ := utils.GetProjectDir()
cmd := exec.Command("kubectl", "apply",
"--filename", dir+"/examples/manifests/etcdcluster-persistent.yaml",
"--namespace", namespace,
)
_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred())
})

By("waiting for statefulset to be ready", func() {
Eventually(func() error {
cmd := exec.Command("kubectl", "wait",
"statefulset/test",
"--for", "jsonpath={.status.readyReplicas}=3",
"--namespace", namespace,
"--timeout", "5m",
)
_, err = utils.Run(cmd)
return err
}, 5*time.Minute, 10*time.Second).Should(Succeed())
})

By("updating the storage request", func() {
// Patch the EtcdCluster to increase storage size
patch := `{"spec": {"storage": {"volumeClaimTemplate": {"spec": {"resources": {"requests": {"storage": "8Gi"}}}}}}}`
cmd := exec.Command("kubectl", "patch", "etcdcluster", "test", "--namespace", namespace, "--type", "merge", "--patch", patch) //nolint:lll
_, err = utils.Run(cmd)
Expect(err).NotTo(HaveOccurred())
})

By("checking that PVC sizes have been updated", func() {
Eventually(func() bool {
cmd := exec.Command("kubectl", "get", "pvc", "-n", namespace, "-o", "jsonpath={.items[*].spec.resources.requests.storage}") //nolint:lll
output, err := utils.Run(cmd)
if err != nil {
return false
}
// Split the output into individual sizes and check each one
sizes := strings.Fields(string(output))
for _, size := range sizes {
if size != "8Gi" {
return false
}
}
return true
}, 5*time.Minute, 10*time.Second).Should(BeTrue(), "PVCs should be resized to 8Gi")
})
})
})

Expand Down Expand Up @@ -235,8 +312,6 @@ var _ = Describe("etcd-operator", Ordered, func() {
Expect(err).NotTo(HaveOccurred())
Expect(authStatus.Enabled).To(BeTrue())
})

})
})

})

0 comments on commit cce266a

Please sign in to comment.