Skip to content

Commit

Permalink
Set reload value for dns config and added an explanatory comment besi…
Browse files Browse the repository at this point in the history
…des other changes

Signed-off-by: Hemant <[email protected]>
  • Loading branch information
hkiiita committed Oct 25, 2024
1 parent b7c92c0 commit 9e76e44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
50 changes: 25 additions & 25 deletions test/e2e/fqdn_dns_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (

func TestFQDNPolicyWithCachedDNS(t *testing.T) {
const (
testFQDN = "FQDN-test-pod.lfx.test"
testFQDN = "fqdn-test-pod.lfx.test"
dnsPort = 53
dnsTTL = 5
)
Expand All @@ -52,7 +52,7 @@ func TestFQDNPolicyWithCachedDNS(t *testing.T) {
}
defer teardownTest(t, data)

// create two agnHost pods and get their IPv4 addresses. The IP of these pods will be mapped against the FQDN.
// create two agnHost Pods and get their IPv4 addresses. The IP of these Pods will be mapped against the FQDN.
podCount := 2
agnHostPodIPs := make([]*PodIPs, podCount)
for i := 0; i < podCount; i++ {
Expand All @@ -67,10 +67,10 @@ func TestFQDNPolicyWithCachedDNS(t *testing.T) {
customDnsService, err := data.CreateServiceWithAnnotations("custom-dns-service", data.testNamespace, dnsPort,
dnsPort, corev1.ProtocolUDP, map[string]string{"app": "custom-dns"}, false,
false, corev1.ServiceTypeClusterIP, ptr.To[corev1.IPFamily](corev1.IPv4Protocol), map[string]string{})
require.NoError(t, err, "Error creating customDNS service")
require.NoError(t, err, "Error creating custom DNS Service")
dnsServiceIP := customDnsService.Spec.ClusterIP

// create a ConfigMap for the custom DNS server, mapping IP of agnHost pod 1 to the FQDN.
// create a ConfigMap for the custom DNS server, mapping IP of agnHost Pod 1 to the FQDN.
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "custom-dns-config",
Expand All @@ -79,7 +79,7 @@ func TestFQDNPolicyWithCachedDNS(t *testing.T) {
Data: createDnsConfig(t, map[string]string{agnHostPodOneIP: testFQDN}, dnsTTL),
}
customDnsConfigMap, err := data.CreateConfigMap(configMap)
require.NoError(t, err, "failed to create custom dns ConfigMap")
require.NoError(t, err, "failed to create custom DNS ConfigMap")

createCustomDnsPod(t, data, configMap.Name)

Expand All @@ -103,42 +103,42 @@ func TestFQDNPolicyWithCachedDNS(t *testing.T) {
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
stdout, err := curlFQDN()
assert.NoError(t, err)
t.Logf("Response of curl to FQDN - %s", stdout)
}, 2*time.Second, 100*time.Millisecond, "trying to curl the FQDN : ", testFQDN)
t.Logf("Response of curl to FQDN: %s", stdout)
}, 2*time.Second, 100*time.Millisecond, "trying to curl the FQDN: ", testFQDN)

// confirm that the FQDN resolves to the expected IP address and store it to simulate caching of this IP associated with the FQDN.
t.Logf("Resolving FQDN to simulate caching the current IP inside Toolbox pod...")
t.Logf("Resolving FQDN to simulate caching the current IP inside Toolbox Pod...")
resolvedIP, err := data.runDNSQuery(toolboxPodName, toolboxContainerName, data.testNamespace, testFQDN, false, dnsServiceIP)
fqdnIP := resolvedIP.String()
require.NoError(t, err, "failed to resolve FQDN to an IP from toolbox pod")
require.NoError(t, err, "failed to resolve FQDN to an IP from toolbox Pod")
require.Equalf(t, agnHostPodOneIP, fqdnIP, "The IP set against the FQDN in the DNS server should be the same, but got %s instead of %s", fqdnIP, agnHostPodOneIP)
t.Logf("Successfully received the expected IP %s using the dig command against the FQDN", fqdnIP)

// update the IP address mapped to the FQDN in the custom DNS ConfigMap.
t.Logf("Updating host mapping in DNS server config to use new IP : %s", agnHostPodTwoIP)
t.Logf("Updating host mapping in DNS server config to use new IP: %s", agnHostPodTwoIP)
customDnsConfigMap.Data = createDnsConfig(t, map[string]string{agnHostPodTwoIP: testFQDN}, dnsTTL)
require.NoError(t, data.UpdateConfigMap(customDnsConfigMap), "failed to update configmap with new IP")
t.Logf("Successfully updated dns configMap with new IP : %s", agnHostPodTwoIP)
t.Logf("Successfully updated DNS ConfigMap with new IP: %s", agnHostPodTwoIP)

// try to trigger an immediate refresh of the configmap by setting annotations in custom DNS server pod, this way
// try to trigger an immediate refresh of the configmap by setting annotations in custom DNS server Pod, this way
// we try to bypass the kubelet sync period which may be as long as (1 minute by default) + TTL of ConfigMaps.
// Ref: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically
require.NoError(t, data.setPodAnnotation(data.testNamespace, "custom-dns-server", "test.antrea.io/random-value",
randSeq(8)), "failed to update custom dns pod annotation.")
randSeq(8)), "failed to update custom DNS Pod annotation.")

curlTarget = fqdnIP
// finally verify that Curling the previously cached IP fails after DNS update.
// The wait time here should be slightly longer than the reload value specified in the custom DNS configuration.
assert.EventuallyWithT(t, func(collectT *assert.CollectT) {
t.Logf("Trying to curl the existing cached IP of the domain - %s", fqdnIP)
t.Logf("Trying to curl the existing cached IP of the domain: %s", fqdnIP)
stdout, err := curlFQDN()
if err != nil {
t.Logf("The test failed because of error : %+v", err)
t.Logf("Curling the cached IP failed")
} else {
t.Logf("Response of curl to cached IP - %+v", stdout)
t.Logf("Response of curl to cached IP: %+v", stdout)
}
assert.Error(collectT, err)

}, 20*time.Second, 1*time.Second)
}, 10*time.Second, 1*time.Second)
}

// setDnsServerAddressInAntrea sets or resets the custom DNS server IP address in Antrea configMap.
Expand All @@ -147,12 +147,12 @@ func setDnsServerAddressInAntrea(t *testing.T, data *TestData, dnsServiceIP stri
config.DNSServerOverride = dnsServiceIP
}
err := data.mutateAntreaConfigMap(nil, agentChanges, false, true)
require.NoError(t, err, "Error when setting up customDNS server IP in Antrea configmap")
require.NoError(t, err, "Error when setting up custom DNS server IP in Antrea configmap")

if dnsServiceIP == "" {
t.Logf("Removing dns server IP from antrea agent as part of teardown")
t.Logf("Removing DNS server IP from antrea agent as part of teardown")
} else {
t.Logf("DNS server value set to %+v in antrea \n", dnsServiceIP)
t.Logf("DNS server value set to %s in antrea \n", dnsServiceIP)
}

}
Expand Down Expand Up @@ -185,7 +185,7 @@ func createFqdnPolicyInNamespace(t *testing.T, data *TestData, testFQDN string,
require.NoError(t, data.waitForANNPRealized(t, annp.Namespace, annp.Name, 10*time.Second))
}

// createToolBoxPod creates the toolbox pod with custom DNS settings for test purpose.
// createToolBoxPod creates the toolbox Pod with custom DNS settings for test purpose.
func createToolboxPod(t *testing.T, data *TestData, dnsServiceIP string) {
mutateSpecForAddingCustomDNS := func(pod *corev1.Pod) {
pod.Spec.DNSPolicy = corev1.DNSNone
Expand All @@ -203,7 +203,7 @@ func createToolboxPod(t *testing.T, data *TestData, dnsServiceIP string) {
require.NoError(t, data.podWaitForRunning(defaultTimeout, toolboxPodName, data.testNamespace))
}

// createHttpAgnHostPod creates an agnHost pod that serves HTTP requests and returns the IP of pod created.
// createHttpAgnHostPod creates an agnHost Pod that serves HTTP requests and returns the IP of Pod created.
func createHttpAgnHostPod(t *testing.T, data *TestData) *PodIPs {
const (
agnHostPort = 80
Expand All @@ -229,7 +229,7 @@ func createHttpAgnHostPod(t *testing.T, data *TestData) *PodIPs {
return podIPs
}

// createDnsPod creates the CoreDNS pod configured to use the custom DNS ConfigMap.
// createDnsPod creates the CoreDNS Pod configured to use the custom DNS ConfigMap.
func createCustomDnsPod(t *testing.T, data *TestData, configName string) {
volume := []corev1.Volume{
{
Expand Down Expand Up @@ -279,7 +279,7 @@ func createDnsConfig(t *testing.T, hosts map[string]string, ttl int) map[string]
ttl {{ .TTL }}
}
loop
reload
reload 2s
}`

generateConfigData := func() (string, error) {
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -3274,8 +3274,7 @@ func (data *TestData) setPodAnnotation(namespace, podName, annotationKey string,
return err
}

_, err = data.clientset.CoreV1().Pods(namespace).Patch(context.TODO(), podName, types.MergePatchType, patchData, metav1.PatchOptions{})
if err != nil {
if _, err := data.clientset.CoreV1().Pods(namespace).Patch(context.TODO(), podName, types.MergePatchType, patchData, metav1.PatchOptions{}); err != nil {
return err
}

Expand Down

0 comments on commit 9e76e44

Please sign in to comment.