|
| 1 | +//go:build integration |
| 2 | +// +build integration |
| 3 | + |
| 4 | +package controllers |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + appsv1 "k8s.io/api/apps/v1" |
| 13 | + batchv1 "k8s.io/api/batch/v1" |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | + rbacv1 "k8s.io/api/rbac/v1" |
| 16 | + storagev1 "k8s.io/api/storage/v1" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + "k8s.io/apimachinery/pkg/runtime" |
| 19 | + "k8s.io/utils/pointer" |
| 20 | + ctrl "sigs.k8s.io/controller-runtime" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + testLabelName = "mylabel" |
| 27 | +) |
| 28 | + |
| 29 | +func TestInplaceController(t *testing.T) { |
| 30 | + ctx, cancel := context.WithCancel(context.Background()) |
| 31 | + defer cancel() |
| 32 | + c, stop := startInplaceTestReconciler(t, ctx, "") |
| 33 | + defer stop() |
| 34 | + |
| 35 | + t.Run("InplaceE2E", func(t *testing.T) { |
| 36 | + |
| 37 | + sc := &storagev1.StorageClass{ |
| 38 | + ObjectMeta: metav1.ObjectMeta{ |
| 39 | + Name: "mysc", |
| 40 | + Annotations: map[string]string{ |
| 41 | + "storageclass.kubernetes.io/is-default-class": "true", |
| 42 | + }, |
| 43 | + }, |
| 44 | + Provisioner: "mysc", |
| 45 | + AllowVolumeExpansion: pointer.Bool(true), |
| 46 | + } |
| 47 | + |
| 48 | + require.NoError(t, c.Create(ctx, sc)) |
| 49 | + |
| 50 | + t.Run("Don't touch correct PVCs", func(t *testing.T) { |
| 51 | + t.Parallel() |
| 52 | + ctx := context.Background() |
| 53 | + ns := "e2e1" |
| 54 | + require := require.New(t) |
| 55 | + require.NoError(c.Create(ctx, &corev1.Namespace{ |
| 56 | + ObjectMeta: metav1.ObjectMeta{ |
| 57 | + Name: ns, |
| 58 | + }, |
| 59 | + })) |
| 60 | + pvcSize := "2G" |
| 61 | + sts := newTestStatefulSet(ns, "test", 1, pvcSize) |
| 62 | + sts.Labels = map[string]string{ |
| 63 | + testLabelName: "true", |
| 64 | + } |
| 65 | + |
| 66 | + pvc := applyResizablePVC(ctx, "data-test-0", ns, pvcSize, sts, c, require) |
| 67 | + |
| 68 | + require.NoError(c.Create(ctx, sts)) |
| 69 | + |
| 70 | + consistently(t, func() bool { |
| 71 | + return pvcEqualSize(ctx, c, pvc, pvcSize) |
| 72 | + }, duration, interval, "PVCs equal size") |
| 73 | + |
| 74 | + }) |
| 75 | + t.Run("Ignore STS without the label", func(t *testing.T) { |
| 76 | + t.Parallel() |
| 77 | + ctx := context.Background() |
| 78 | + ns := "e2e2" |
| 79 | + require := require.New(t) |
| 80 | + require.NoError(c.Create(ctx, &corev1.Namespace{ |
| 81 | + ObjectMeta: metav1.ObjectMeta{ |
| 82 | + Name: ns, |
| 83 | + }, |
| 84 | + })) |
| 85 | + sts := newTestStatefulSet(ns, "test", 1, "2G") |
| 86 | + |
| 87 | + pvc := applyResizablePVC(ctx, "data-test-0", ns, "1G", sts, c, require) |
| 88 | + |
| 89 | + require.NoError(c.Create(ctx, sts)) |
| 90 | + |
| 91 | + consistently(t, func() bool { |
| 92 | + return pvcEqualSize(ctx, c, pvc, "1G") |
| 93 | + }, duration, interval, "PVCs equal size") |
| 94 | + }) |
| 95 | + t.Run("Change PVCs if they not match", func(t *testing.T) { |
| 96 | + t.Parallel() |
| 97 | + ctx := context.Background() |
| 98 | + ns := "e2e3" |
| 99 | + require := require.New(t) |
| 100 | + require.NoError(c.Create(ctx, &corev1.Namespace{ |
| 101 | + ObjectMeta: metav1.ObjectMeta{ |
| 102 | + Name: ns, |
| 103 | + }, |
| 104 | + })) |
| 105 | + sts := newTestStatefulSet(ns, "test", 1, "2G") |
| 106 | + sts.Labels = map[string]string{ |
| 107 | + testLabelName: "true", |
| 108 | + } |
| 109 | + |
| 110 | + pvc := applyResizablePVC(ctx, "data-test-0", ns, "1G", sts, c, require) |
| 111 | + |
| 112 | + require.NoError(c.Create(ctx, sts)) |
| 113 | + |
| 114 | + consistently(t, func() bool { |
| 115 | + return pvcEqualSize(ctx, c, pvc, "2G") |
| 116 | + }, duration, interval, "PVCs equal size") |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +// startInplaceTestReconciler sets up a separate test env and starts the controller |
| 123 | +func startInplaceTestReconciler(t *testing.T, ctx context.Context, crname string) (client.Client, func() error) { |
| 124 | + req := require.New(t) |
| 125 | + |
| 126 | + testEnv := &envtest.Environment{} |
| 127 | + conf, err := testEnv.Start() |
| 128 | + req.NoError(err) |
| 129 | + |
| 130 | + s := runtime.NewScheme() |
| 131 | + req.NoError(appsv1.AddToScheme(s)) |
| 132 | + req.NoError(corev1.AddToScheme(s)) |
| 133 | + req.NoError(batchv1.AddToScheme(s)) |
| 134 | + req.NoError(rbacv1.AddToScheme(s)) |
| 135 | + req.NoError(storagev1.AddToScheme(s)) |
| 136 | + |
| 137 | + mgr, err := ctrl.NewManager(conf, ctrl.Options{ |
| 138 | + Scheme: s, |
| 139 | + }) |
| 140 | + req.NoError(err) |
| 141 | + req.NoError((&InplaceReconciler{ |
| 142 | + Client: mgr.GetClient(), |
| 143 | + Scheme: mgr.GetScheme(), |
| 144 | + Recorder: mgr.GetEventRecorderFor("statefulset-resize-controller"), |
| 145 | + RequeueAfter: time.Second, |
| 146 | + LabelName: testLabelName, |
| 147 | + }).SetupWithManager(mgr)) |
| 148 | + go func() { |
| 149 | + req.NoError(mgr.Start(ctx)) |
| 150 | + }() |
| 151 | + |
| 152 | + return mgr.GetClient(), testEnv.Stop |
| 153 | +} |
| 154 | + |
| 155 | +func applyResizablePVC(ctx context.Context, name, ns, size string, sts *appsv1.StatefulSet, c client.Client, require *require.Assertions) *corev1.PersistentVolumeClaim { |
| 156 | + pvc := newSource(ns, name, size, |
| 157 | + func(pvc *corev1.PersistentVolumeClaim) *corev1.PersistentVolumeClaim { |
| 158 | + pvc.Labels = sts.Spec.Selector.MatchLabels |
| 159 | + return pvc |
| 160 | + }) |
| 161 | + |
| 162 | + pvc.Spec.StorageClassName = pointer.String("mysc") |
| 163 | + require.NoError(c.Create(ctx, pvc)) |
| 164 | + |
| 165 | + // we need to set the PVC to bound in order for the resize to work |
| 166 | + pvc.Status.Phase = corev1.ClaimBound |
| 167 | + require.NoError(c.Status().Update(ctx, pvc)) |
| 168 | + return pvc |
| 169 | +} |
0 commit comments