Skip to content

Commit a41eea4

Browse files
committed
[test]Handle PVC in StatefulSet and Deployment
The helper simulating Pods for StatefulSets and Deployments are changed to support PVCs. Before this patch if PVC was present the Pod creation failed with missing volumes. Now the helper simply drops the volumes and mounts from the Pod it creates. Later on if we need a better solution we can translate PVCs from to volumes when creating the Pod, but I believe we will not need this in our tests.
1 parent 6df6d98 commit a41eea4

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

modules/test/helpers/deployment.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package helpers
1515

1616
import (
1717
"encoding/json"
18+
1819
networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
1920
"github.com/onsi/gomega"
2021
corev1 "k8s.io/api/core/v1"
@@ -57,14 +58,22 @@ func (tc *TestHelper) SimulateDeploymentReplicaReady(name types.NamespacedName)
5758

5859
// SimulateDeploymentReadyWithPods -
5960
func (tc *TestHelper) SimulateDeploymentReadyWithPods(name types.NamespacedName, networkIPs map[string][]string) {
60-
ss := tc.GetDeployment(name)
61-
for i := 0; i < int(*ss.Spec.Replicas); i++ {
61+
depl := tc.GetDeployment(name)
62+
for i := 0; i < int(*depl.Spec.Replicas); i++ {
6263
pod := &corev1.Pod{
63-
ObjectMeta: ss.Spec.Template.ObjectMeta,
64-
Spec: ss.Spec.Template.Spec,
64+
ObjectMeta: depl.Spec.Template.ObjectMeta,
65+
Spec: depl.Spec.Template.Spec,
6566
}
6667
pod.ObjectMeta.Namespace = name.Namespace
6768
pod.ObjectMeta.GenerateName = name.Name
69+
// NOTE(gibi): If there is a mount that refers to a volume created via
70+
// persistent volume claim then that mount won't have a corresponding
71+
// volume created in EnvTest as we are not simulating the k8s volume
72+
// claim logic here at the moment. Therefore the Pod create would fail
73+
// with a missing volume. So to avoid that we remove every mount and
74+
// volume from the pod we create here.
75+
pod.Spec.Volumes = []corev1.Volume{}
76+
pod.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{}
6877

6978
var netStatus []networkv1.NetworkStatus
7079
for network, IPs := range networkIPs {
@@ -84,10 +93,10 @@ func (tc *TestHelper) SimulateDeploymentReadyWithPods(name types.NamespacedName,
8493
}
8594

8695
gomega.Eventually(func(g gomega.Gomega) {
87-
ss := tc.GetDeployment(name)
88-
ss.Status.Replicas = 1
89-
ss.Status.ReadyReplicas = 1
90-
g.Expect(tc.K8sClient.Status().Update(tc.Ctx, ss)).To(gomega.Succeed())
96+
depl := tc.GetDeployment(name)
97+
depl.Status.Replicas = 1
98+
depl.Status.ReadyReplicas = 1
99+
g.Expect(tc.K8sClient.Status().Update(tc.Ctx, depl)).To(gomega.Succeed())
91100

92101
}, tc.Timeout, tc.Interval).Should(gomega.Succeed())
93102

modules/test/helpers/statefulset.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package helpers
1515

1616
import (
1717
"encoding/json"
18+
1819
networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
1920
t "github.com/onsi/gomega"
2021
appsv1 "k8s.io/api/apps/v1"
@@ -63,6 +64,15 @@ func (tc *TestHelper) SimulateStatefulSetReplicaReadyWithPods(name types.Namespa
6364
pod.ObjectMeta.Namespace = name.Namespace
6465
pod.ObjectMeta.GenerateName = name.Name
6566

67+
// NOTE(gibi): If there is a mount that refers to a volume created via
68+
// persistent volume claim then that mount won't have a corresponding
69+
// volume created in EnvTest as we are not simulating the k8s volume
70+
// claim logic here at the moment. Therefore the Pod create would fail
71+
// with a missing volume. So to avoid that we remove every mount and
72+
// volume from the pod we create here.
73+
pod.Spec.Volumes = []corev1.Volume{}
74+
pod.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{}
75+
6676
var netStatus []networkv1.NetworkStatus
6777
for network, IPs := range networkIPs {
6878
netStatus = append(

0 commit comments

Comments
 (0)