chore: changes needed before #111155

This commit is contained in:
Lucas Severo Alves
2023-01-24 17:55:32 +01:00
parent cc60df9595
commit 636f8e1a3e
4 changed files with 46 additions and 61 deletions

View File

@@ -47,20 +47,15 @@ type TestServer struct {
TmpDir string // Temp Dir used, by the apiserver
}
// Logger allows t.Testing and b.Testing to be passed to StartTestServer and StartTestServerOrDie
type Logger interface {
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
Logf(format string, args ...interface{})
}
// StartTestServer starts a kube-controller-manager. A rest client config and a tear-down func,
// and location of the tmpdir are returned.
//
// Note: we return a tear-down func instead of a stop channel because the later will leak temporary
// files that because Golang testing's call to os.Exit will not give a stop channel go routine
// enough time to remove temporary files.
func StartTestServer(t Logger, customFlags []string) (result TestServer, err error) {
//
// files that because Golang testing's call to os.Exit will not give a stop channel go routine
// enough time to remove temporary files.
func StartTestServer(ctx context.Context, customFlags []string) (result TestServer, err error) {
logger := klog.FromContext(ctx)
stopCh := make(chan struct{})
var errCh chan error
tearDown := func() {
@@ -109,7 +104,7 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
}
s.SecureServing.ServerCert.CertDirectory = result.TmpDir
t.Logf("kube-controller-manager will listen securely on port %d...", s.SecureServing.BindPort)
logger.Info("kube-controller-manager will listen securely", "port", s.SecureServing.BindPort)
}
config, err := s.Config(all, disabled)
@@ -126,7 +121,7 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
}
}(stopCh)
t.Logf("Waiting for /healthz to be ok...")
logger.Info("Waiting for /healthz to be ok...")
client, err := kubernetes.NewForConfig(config.LoopbackClientConfig)
if err != nil {
return result, fmt.Errorf("failed to create a client: %v", err)
@@ -160,14 +155,13 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
}
// StartTestServerOrDie calls StartTestServer t.Fatal if it does not succeed.
func StartTestServerOrDie(t Logger, flags []string) *TestServer {
result, err := StartTestServer(t, flags)
func StartTestServerOrDie(ctx context.Context, flags []string) *TestServer {
result, err := StartTestServer(ctx, flags)
if err == nil {
return &result
}
t.Fatalf("failed to launch server: %v", err)
return nil
panic(fmt.Errorf("failed to launch server: %v", err))
}
func createListenerOnFreePort() (net.Listener, int, error) {