From f8091851e37a85001e907f13e116fbc654f336fb Mon Sep 17 00:00:00 2001 From: Pankaj Patil Date: Wed, 12 May 2021 13:02:47 +0530 Subject: [PATCH] modify ip address logic --- pkg/http-server/webhook-scan-logs.go | 2 +- test/e2e/validatingwebhook/certgen.go | 15 +-------------- test/e2e/validatingwebhook/kubeclient.go | 2 +- .../validating_webhook_test.go | 2 +- .../validatingwebhook_utils.go | 18 ++++++++++++++++++ 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/pkg/http-server/webhook-scan-logs.go b/pkg/http-server/webhook-scan-logs.go index b631aded6..c6fecf0d5 100644 --- a/pkg/http-server/webhook-scan-logs.go +++ b/pkg/http-server/webhook-scan-logs.go @@ -69,7 +69,7 @@ type webhookDisplayedShowLog struct { } func (g *APIHandler) getLogs(w http.ResponseWriter, r *http.Request) { - zap.S().Info("handle: validating webhook' get logs request") + zap.S().Info("handle: validating webhook's get logs request") if !config.GetK8sAdmissionControl().Dashboard { apiErrorResponse(w, ErrDashboardDisabled.Error(), http.StatusBadRequest) diff --git a/test/e2e/validatingwebhook/certgen.go b/test/e2e/validatingwebhook/certgen.go index 50b2de4e3..83d7723f8 100644 --- a/test/e2e/validatingwebhook/certgen.go +++ b/test/e2e/validatingwebhook/certgen.go @@ -37,7 +37,7 @@ import ( func GenerateCertificates(certFilePath, privateKeyPath string) error { // ip address of the machine would be required to be added as // subject alternate name - ipAddr, err := getIP() + ipAddr, err := GetIP() if err != nil { return err } @@ -96,16 +96,3 @@ func pemBlockForKey(priv interface{}) *pem.Block { return nil } } - -// getIP finds preferred outbound ip of the machine -func getIP() (net.IP, error) { - conn, err := net.Dial("udp", "8.8.8.8:80") - if err != nil { - return nil, err - } - defer conn.Close() - - localAddr := conn.LocalAddr().(*net.UDPAddr) - - return localAddr.IP, nil -} diff --git a/test/e2e/validatingwebhook/kubeclient.go b/test/e2e/validatingwebhook/kubeclient.go index b662af494..c9a5add00 100644 --- a/test/e2e/validatingwebhook/kubeclient.go +++ b/test/e2e/validatingwebhook/kubeclient.go @@ -98,7 +98,7 @@ func (k *KubernetesClient) CreateValidatingWebhookConfiguration(webhookFile, cer webhook := &webhooks.Webhooks[0] webhook.ClientConfig.CABundle = certData - ip, err := getIP() + ip, err := GetIP() if err != nil { return nil, err } diff --git a/test/e2e/validatingwebhook/validating_webhook_test.go b/test/e2e/validatingwebhook/validating_webhook_test.go index bf338e517..a9e3116d8 100644 --- a/test/e2e/validatingwebhook/validating_webhook_test.go +++ b/test/e2e/validatingwebhook/validating_webhook_test.go @@ -339,7 +339,7 @@ func createPod(session *gexec.Session, webhookName string) { Expect(err).NotTo(HaveOccurred()) pod, err := kubeClient.CreatePod(podYamlAbsPath) - Eventually(session.Err, 10).Should(gbytes.Say("handle: validating webhook request")) + Eventually(session.Err, defaultTimeout).Should(gbytes.Say("handle: validating webhook request")) Expect(err).NotTo(HaveOccurred()) Expect(pod).NotTo(BeNil()) diff --git a/test/e2e/validatingwebhook/validatingwebhook_utils.go b/test/e2e/validatingwebhook/validatingwebhook_utils.go index 866f420e7..37ba40331 100644 --- a/test/e2e/validatingwebhook/validatingwebhook_utils.go +++ b/test/e2e/validatingwebhook/validatingwebhook_utils.go @@ -18,6 +18,7 @@ package validatingwebhook import ( "fmt" + "net" "os" "os/exec" "path/filepath" @@ -103,3 +104,20 @@ func CreateDefaultKindCluster() error { } return nil } + +// GetIP finds preferred outbound ip of the machine +func GetIP() (net.IP, error) { + addrs, err := net.InterfaceAddrs() + if err != nil { + return nil, err + } + + for _, a := range addrs { + if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + return ipnet.IP.To4(), nil + } + } + } + return nil, fmt.Errorf("could not find ip address of the machine") +}