Skip to content

Commit

Permalink
modify ip address logic
Browse files Browse the repository at this point in the history
  • Loading branch information
patilpankaj212 committed May 12, 2021
1 parent df1decd commit f809185
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/http-server/webhook-scan-logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 1 addition & 14 deletions test/e2e/validatingwebhook/certgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion test/e2e/validatingwebhook/kubeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/validatingwebhook/validating_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
18 changes: 18 additions & 0 deletions test/e2e/validatingwebhook/validatingwebhook_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package validatingwebhook

import (
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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")
}

0 comments on commit f809185

Please sign in to comment.