Skip to content

Commit

Permalink
test: Added e2e tests for bidirectional annotation & label sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasK33 committed Oct 14, 2024
1 parent a3f78c4 commit 8430748
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 31 deletions.
1 change: 0 additions & 1 deletion test/e2e/syncer/networkpolicies/networkpolicies.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ var _ = ginkgo.Describe("NetworkPolicies are created as expected", func() {
time.Sleep(time.Second * 10)
framework.DefaultFramework.TestServiceIsEventuallyUnreachable(curlPod, nginxService)
})

})

func updateNetworkPolicyWithRetryOnConflict(f *framework.Framework, networkPolicy *networkingv1.NetworkPolicy, mutator func(np *networkingv1.NetworkPolicy)) error {
Expand Down
139 changes: 136 additions & 3 deletions test/e2e/syncer/pods/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var _ = ginkgo.Describe("Pods are running in the host cluster", func() {

// version 1.22 and lesser than that needs legacy flag enabled
if version != nil {
i, err := strconv.Atoi(strings.Replace(version.Minor, "+", "", -1))
i, err := strconv.Atoi(strings.ReplaceAll(version.Minor, "+", ""))
framework.ExpectNoError(err)
if i > 22 {
vpod.Spec.EphemeralContainers = []corev1.EphemeralContainer{{
Expand Down Expand Up @@ -211,7 +211,8 @@ var _ = ginkgo.Describe("Pods are running in the host cluster", func() {
},
Data: map[string]string{
cmKey: cmKeyValue,
}}, metav1.CreateOptions{})
},
}, metav1.CreateOptions{})
framework.ExpectNoError(err)

pod, err := f.VClusterClient.CoreV1().Pods(ns).Create(f.Context, &corev1.Pod{
Expand Down Expand Up @@ -295,7 +296,8 @@ var _ = ginkgo.Describe("Pods are running in the host cluster", func() {
},
Data: map[string][]byte{
secretKey: []byte(secretKeyValue),
}}, metav1.CreateOptions{})
},
}, metav1.CreateOptions{})
framework.ExpectNoError(err)

pod, err := f.VClusterClient.CoreV1().Pods(ns).Create(f.Context, &corev1.Pod{
Expand Down Expand Up @@ -550,4 +552,135 @@ var _ = ginkgo.Describe("Pods are running in the host cluster", func() {
}
}
})

ginkgo.It("should perform a bidirectional sync on labels and annotations", func() {
podName := "test-annotations"
vPod, err := f.VClusterClient.CoreV1().Pods(ns).Create(f.Context, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Annotations: map[string]string{
"vcluster-annotation": "from vCluster with love",
},
Labels: map[string]string{
"vcluster-specific-label": "with_its_value",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: testingContainerName,
Image: testingContainerImage,
ImagePullPolicy: corev1.PullIfNotPresent,
SecurityContext: f.GetDefaultSecurityContext(),
},
},
},
}, metav1.CreateOptions{})
framework.ExpectNoError(err)

err = f.WaitForPodRunning(podName, ns)
framework.ExpectNoError(err, "A pod created in the vcluster is expected to be in the Running phase eventually.")

// get current physical Pod resource
pPodName := translate.Default.HostName(nil, vPod.Name, vPod.Namespace)
pPod, err := f.HostClient.CoreV1().Pods(pPodName.Namespace).Get(f.Context, pPodName.Name, metav1.GetOptions{})
framework.ExpectNoError(err)

// get current vCluster Pod resource
vPod, err = f.VClusterClient.CoreV1().Pods(ns).Get(f.Context, podName, metav1.GetOptions{})
framework.ExpectNoError(err)

// update host cluster pod with additional information
additionalLabelKey := "another-one"
additionalLabelValue := "good-syncer"
additionalAnnotationKey := "annotation-key"
additionalAnnotationValue := "annotation-value"

err = wait.PollUntilContextTimeout(f.Context, time.Second, framework.PollTimeout, true, func(context.Context) (bool, error) {
if pPod.Labels == nil {
pPod.Labels = map[string]string{}
}
pPod.Labels[additionalLabelKey] = additionalLabelValue

if pPod.Annotations == nil {
pPod.Annotations = map[string]string{}
}
pPod.Annotations[additionalAnnotationKey] = additionalAnnotationValue

pPod, err = f.HostClient.CoreV1().Pods(pPod.Namespace).Update(f.Context, pPod, metav1.UpdateOptions{})
if err != nil {
if kerrors.IsConflict(err) {
return false, nil
}
return false, err
}

return true, nil
})
framework.ExpectNoError(err)

// wait for the syncer to update the pod
err = wait.PollUntilContextTimeout(f.Context, time.Second, framework.PollTimeout, true, func(ctx context.Context) (bool, error) {
pod, err := f.VClusterClient.CoreV1().Pods(vPod.Namespace).Get(ctx, vPod.Name, metav1.GetOptions{})
if err != nil {
return false, err
}

return pod.ResourceVersion != vPod.ResourceVersion && pod.Annotations[additionalAnnotationKey] != "", nil
})
framework.ExpectNoError(err)

vPod, err = f.VClusterClient.CoreV1().Pods(vPod.Namespace).Get(f.Context, vPod.Name, metav1.GetOptions{})
framework.ExpectNoError(err)

framework.ExpectEqual(vPod.Annotations[additionalAnnotationKey], pPod.Annotations[additionalAnnotationKey])
framework.ExpectEqual(vPod.Labels[additionalLabelKey], pPod.Labels[additionalLabelKey])

// Update the vPod
pPod, err = f.HostClient.CoreV1().Pods(pPod.Namespace).Get(f.Context, pPod.Name, metav1.GetOptions{})
framework.ExpectNoError(err)

additionalLabelValueFromVCluster := "good-syncer-from-vcluster"
additionalAnnotationValueFromVCluster := "annotation-value-from-vcluster"

err = wait.PollUntilContextTimeout(f.Context, time.Second, framework.PollTimeout, true, func(context.Context) (bool, error) {
if vPod.Labels == nil {
vPod.Labels = map[string]string{}
}
vPod.Labels[additionalLabelKey] = additionalLabelValueFromVCluster

if vPod.Annotations == nil {
vPod.Annotations = map[string]string{}
}
vPod.Annotations[additionalAnnotationKey] = additionalAnnotationValueFromVCluster

vPod, err = f.VClusterClient.CoreV1().Pods(vPod.Namespace).Update(f.Context, vPod, metav1.UpdateOptions{})
if err != nil {
if kerrors.IsConflict(err) {
return false, nil
}
return false, err
}

return true, nil
})
framework.ExpectNoError(err)

// wait for the syncer to update the pod
err = wait.PollUntilContextTimeout(f.Context, time.Second, framework.PollTimeout, true, func(ctx context.Context) (bool, error) {
pod, err := f.HostClient.CoreV1().Pods(pPod.Namespace).Get(ctx, pPod.Name, metav1.GetOptions{})
if err != nil {
return false, err
}

return pod.ResourceVersion != pPod.ResourceVersion && pod.Annotations[additionalAnnotationKey] != additionalAnnotationValue, nil
})
framework.ExpectNoError(err)

pPod, err = f.HostClient.CoreV1().Pods(pPod.Namespace).Get(f.Context, pPod.Name, metav1.GetOptions{})
framework.ExpectNoError(err)

framework.ExpectEqual(vPod.Annotations[additionalAnnotationKey], pPod.Annotations[additionalAnnotationKey])
framework.ExpectEqual(vPod.Labels[additionalLabelKey], pPod.Labels[additionalLabelKey])
})
})
Loading

0 comments on commit 8430748

Please sign in to comment.