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.
This commit is contained in:
Patrick Ohly 2024-01-19 19:04:02 +01:00
parent 418ae605ec
commit dd0cee4895
2 changed files with 22 additions and 5 deletions

View File

@ -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")
}

View File

@ -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
}