Skip to content

Commit

Permalink
added function to annotate pods.
Browse files Browse the repository at this point in the history
Signed-off-by: Hemant <[email protected]>
  • Loading branch information
hkiiita committed Oct 4, 2024
1 parent 0081010 commit d1a8227
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
26 changes: 4 additions & 22 deletions test/e2e/fqdn_dns_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
package e2e

import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"k8s.io/utils/ptr"
"strconv"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"

crdv1beta1 "antrea.io/antrea/pkg/apis/crd/v1beta1"
agentconfig "antrea.io/antrea/pkg/config/agent"
Expand Down Expand Up @@ -168,7 +166,7 @@ func TestFQDNPolicyWithCachedDNS(t *testing.T) {
require.NoError(t, err, "failed to update configmap with new IP : %v", err)
t.Logf("successfully updated dns configMap with new IP : %+v", newIP)

updateDnsPodAnnotations(t, data)
require.NoError(t, data.patchPodAnnotation(data.testNamespace, customDnsPodName, nil), "failed to update custom dns pod annotation.")

defer setDnsServerAddressInAntrea(t, data, "")

Expand Down Expand Up @@ -242,14 +240,14 @@ func createDnsPod(t *testing.T, data *TestData) {
label := map[string]string{customDnsLabelKey: customDnsLabelValue}
pb := NewPodBuilder(customDnsPodName, data.testNamespace, customDnsImage)
pb.WithLabels(label)
pb.WithAnnotations(map[string]string{"Foo": "Bar"})
pb.WithContainerName(customDnsContainerName)
pb.WithArgs([]string{"-conf", "/etc/coredns/Corefile"})
pb.AddVolume(volume)
pb.AddVolumeMount(volumeMount)

require.NoError(t, pb.Create(data))
require.NoError(t, data.podWaitForRunning(defaultTimeout, customDnsPodName, data.testNamespace))
require.NoError(t, data.patchPodAnnotation(data.testNamespace, customDnsPodName, nil), "failed to annotate the custom dns pod.")

}

Expand Down Expand Up @@ -301,22 +299,6 @@ func createToolBoxPod(t *testing.T, data *TestData, dnsServiceIP string) {
require.NoError(t, data.podWaitForRunning(defaultTimeout, toolBoxPodName, data.testNamespace))
}

func updateDnsPodAnnotations(t *testing.T, data *TestData) {
dnsPod, err := data.clientset.CoreV1().Pods(data.testNamespace).Get(context.TODO(), customDnsPodName, metav1.GetOptions{})
require.NoError(t, err, "Error getting DNS pod for annotation update.")

if dnsPod.Annotations == nil {
dnsPod.Annotations = make(map[string]string)
}
dnsPod.Annotations["Bar"] = "Foo"
delete(dnsPod.Annotations, "Foo")

updatedPod, err := data.clientset.CoreV1().Pods(data.testNamespace).Update(context.TODO(), dnsPod, metav1.UpdateOptions{})
require.NoError(t, err, "error updating dns pod annotations.")

t.Logf("updated dns pod annotations %+v\n", updatedPod.Annotations)
}

func createHttpAgnhostPod(t *testing.T, data *TestData, podName string, agnLabels map[string]string) *PodIPs {
args := []string{"netexec", "--http-port=" + strconv.Itoa(agnHostPort)}
ports := []corev1.ContainerPort{
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const (
defaultCHDatabaseURL = "tcp://clickhouse-clickhouse.flow-visibility.svc:9000"

statefulSetRestartAnnotationKey = "antrea-e2e/restartedAt"
randomPatchAnnotationKey = "test.antrea.io/random-value"

iperfPort = 5201
iperfSvcPort = 9999
Expand Down Expand Up @@ -3251,3 +3252,43 @@ func (data *TestData) runDNSQuery(
return nil, fmt.Errorf("invalid IP address found %v", stdout)
}
}

// patchPodAnnotation Patches an annotation for a given pod in a given namespace. If no annotation is
// provided as a parameter then it generates a random string value for a predefined key and adds it as annotation.
// The patchType used is MergePatchType since we intend to make updates to annotations in pods instead of updating fields in complex k8s
// resources like deployments.
func (data *TestData) patchPodAnnotation(namespace, podName string, annotation map[string]string) error {
numberOfRunes := 8
annotationPatch := map[string]interface{}{
"metadata": map[string]interface{}{
"annotations": make(map[string]string),
},
}

if annotation == nil {
annotationPatch["metadata"].(map[string]interface{})["annotations"].(map[string]string)[randomPatchAnnotationKey] = randSeq(numberOfRunes)
} else {
for key, value := range annotation {
if key == randomPatchAnnotationKey {
annotationPatch["metadata"].(map[string]interface{})["annotations"].(map[string]string)[key] = randSeq(numberOfRunes)
} else {
annotationPatch["metadata"].(map[string]interface{})["annotations"].(map[string]string)[key] = value
}
}
}

patchData, err := json.Marshal(annotationPatch)
if err != nil {
log.Infof("Error marshalling annotation: %+v", err)
return err
}

_, err = data.clientset.CoreV1().Pods(namespace).Patch(context.TODO(), podName, types.MergePatchType, patchData, metav1.PatchOptions{})
if err != nil {
log.Infof("Error patching pod %s in namespace %s with annotations. Error: %+v", podName, namespace, err)
return err
}

log.Infof("Successfully patched pod %s in namespace %s with provided annotation", podName, namespace)
return nil
}

0 comments on commit d1a8227

Please sign in to comment.