Use builtin JoinHostPort function

This commit is contained in:
Arda Güçlü 2021-05-18 14:49:02 +03:00
parent 2ae12e6345
commit 0965cad63a

View File

@ -19,7 +19,8 @@ package auth
import (
"context"
"fmt"
netutil "k8s.io/utils/net"
"net"
"strconv"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -61,8 +62,8 @@ var _ = SIGDescribe("[Feature:NodeAuthenticator]", func() {
pod := createNodeAuthTestPod(f)
for _, nodeIP := range nodeIPs {
// Anonymous authentication is disabled by default
nodeIP = getFormattedNodeIP(nodeIP)
result := framework.RunHostCmdOrDie(ns, pod.Name, fmt.Sprintf("curl -sIk -o /dev/null -w '%s' https://%s:%v/metrics", "%{http_code}", nodeIP, ports.KubeletPort))
host := net.JoinHostPort(nodeIP, strconv.Itoa(ports.KubeletPort))
result := framework.RunHostCmdOrDie(ns, pod.Name, fmt.Sprintf("curl -sIk -o /dev/null -w '%s' https://%s/metrics", "%{http_code}", host))
gomega.Expect(result).To(gomega.Or(gomega.Equal("401"), gomega.Equal("403")), "the kubelet's main port 10250 should reject requests with no credentials")
}
})
@ -83,25 +84,18 @@ var _ = SIGDescribe("[Feature:NodeAuthenticator]", func() {
pod := createNodeAuthTestPod(f)
for _, nodeIP := range nodeIPs {
nodeIP = getFormattedNodeIP(nodeIP)
host := net.JoinHostPort(nodeIP, strconv.Itoa(ports.KubeletPort))
result := framework.RunHostCmdOrDie(ns,
pod.Name,
fmt.Sprintf("curl -sIk -o /dev/null -w '%s' --header \"Authorization: Bearer `%s`\" https://%s:%v/metrics",
fmt.Sprintf("curl -sIk -o /dev/null -w '%s' --header \"Authorization: Bearer `%s`\" https://%s/metrics",
"%{http_code}",
"cat /var/run/secrets/kubernetes.io/serviceaccount/token",
nodeIP, ports.KubeletPort))
host))
gomega.Expect(result).To(gomega.Or(gomega.Equal("401"), gomega.Equal("403")), "the kubelet can delegate ServiceAccount tokens to the API server")
}
})
})
func getFormattedNodeIP(nodeIP string) string {
if netutil.IsIPv6String(nodeIP) {
return fmt.Sprintf("[%s]", nodeIP)
}
return nodeIP
}
func createNodeAuthTestPod(f *framework.Framework) *v1.Pod {
pod := e2epod.NewAgnhostPod(f.Namespace.Name, "agnhost-pod", nil, nil, nil)
pod.ObjectMeta.GenerateName = "test-node-authn-"