From dd0cee4895580c40f2ef7ab9d822757424d9bf09 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Fri, 19 Jan 2024 19:04:02 +0100 Subject: [PATCH] e2e node runner: remove dependency on e2e/framework A stand-alone binary shouldn't import the test/e2e/framework, which is targeted towards usage in a Ginkgo test suite. This currently works, but will break once test/e2e/framework becomes more opinionated about how to configure logging. The simplest solution is to duplicate the one short function that the binary was calling in the framework. --- test/e2e/framework/test_context.go | 6 +++--- test/e2e_node/remote/node_conformance.go | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/test/e2e/framework/test_context.go b/test/e2e/framework/test_context.go index 6ffb4d18bf3..bdca80299a9 100644 --- a/test/e2e/framework/test_context.go +++ b/test/e2e/framework/test_context.go @@ -465,10 +465,10 @@ func RegisterClusterFlags(flags *flag.FlagSet) { flags.DurationVar(&nodeKiller.SimulatedDowntime, "node-killer-simulated-downtime", 10*time.Minute, "A delay between node death and recreation") } -// GenerateSecureToken returns a string of length tokenLen, consisting +// generateSecureToken returns a string of length tokenLen, consisting // of random bytes encoded as base64 for use as a Bearer Token during // communication with an APIServer -func GenerateSecureToken(tokenLen int) (string, error) { +func generateSecureToken(tokenLen int) (string, error) { // Number of bytes to be tokenLen when base64 encoded. tokenSize := math.Ceil(float64(tokenLen) * 6 / 8) rawToken := make([]byte, int(tokenSize)) @@ -548,7 +548,7 @@ func AfterReadingAllFlags(t *TestContextType) { } if len(t.BearerToken) == 0 { var err error - t.BearerToken, err = GenerateSecureToken(16) + t.BearerToken, err = generateSecureToken(16) ExpectNoError(err, "Failed to generate bearer token") } diff --git a/test/e2e_node/remote/node_conformance.go b/test/e2e_node/remote/node_conformance.go index 551499972d6..2f9a9e8100c 100644 --- a/test/e2e_node/remote/node_conformance.go +++ b/test/e2e_node/remote/node_conformance.go @@ -17,7 +17,10 @@ limitations under the License. package remote import ( + "crypto/rand" + "encoding/base64" "fmt" + "math" "os" "os/exec" "path/filepath" @@ -27,7 +30,6 @@ import ( "k8s.io/klog/v2" - "k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e_node/builder" "k8s.io/kubernetes/test/utils" ) @@ -280,7 +282,7 @@ func (c *ConformanceRemote) RunTest(host, workspace, results, imageDesc, junitFi return "", err } - bearerToken, err := framework.GenerateSecureToken(16) + bearerToken, err := generateSecureToken(16) if err != nil { return "", err } @@ -304,3 +306,18 @@ func (c *ConformanceRemote) RunTest(host, workspace, results, imageDesc, junitFi timeout.Seconds(), podManifestPath, podManifestPath, results, junitFilePrefix, extraEnvs, bearerToken, getConformanceTestImageName(systemSpecName)) return SSH(host, "sh", "-c", cmd) } + +// generateSecureToken returns a string of length tokenLen, consisting +// of random bytes encoded as base64 for use as a Bearer Token during +// communication with an APIServer +func generateSecureToken(tokenLen int) (string, error) { + // Number of bytes to be tokenLen when base64 encoded. + tokenSize := math.Ceil(float64(tokenLen) * 6 / 8) + rawToken := make([]byte, int(tokenSize)) + if _, err := rand.Read(rawToken); err != nil { + return "", err + } + encoded := base64.RawURLEncoding.EncodeToString(rawToken) + token := encoded[:tokenLen] + return token, nil +}