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) {

View File

@ -48,21 +48,16 @@ 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-scheduler. 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) {
ctx, cancel := context.WithCancel(context.Background())
//
// 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)
ctx, cancel := context.WithCancel(ctx)
var errCh chan error
tearDown := func() {
@ -73,7 +68,7 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
if errCh != nil {
err, ok := <-errCh
if ok && err != nil {
klog.ErrorS(err, "Failed to shutdown test server clearly")
logger.Error(err, "Failed to shutdown test server clearly")
}
}
if len(result.TmpDir) != 0 {
@ -108,7 +103,7 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
}
opts.SecureServing.ServerCert.CertDirectory = result.TmpDir
t.Logf("kube-scheduler will listen securely on port %d...", opts.SecureServing.BindPort)
logger.Info("kube-scheduler will listen securely", "port", opts.SecureServing.BindPort)
}
cc, sched, err := app.Setup(ctx, opts)
@ -124,7 +119,7 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
}
}(ctx)
t.Logf("Waiting for /healthz to be ok...")
logger.Info("Waiting for /healthz to be ok...")
client, err := kubernetes.NewForConfig(cc.LoopbackClientConfig)
if err != nil {
return result, fmt.Errorf("failed to create a client: %v", err)
@ -158,14 +153,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) {

View File

@ -33,7 +33,6 @@ import (
"k8s.io/cloud-provider/app/config"
"k8s.io/cloud-provider/options"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/klog/v2"
)
@ -49,20 +48,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 cloud-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
configDoneCh := make(chan struct{})
@ -105,11 +99,11 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
cloudConfig := config.ComponentConfig.KubeCloudShared.CloudProvider
cloud, err := cloudprovider.InitCloudProvider(cloudConfig.Name, cloudConfig.CloudConfigFile)
if err != nil {
t.Fatalf("Cloud provider could not be initialized: %v", err)
panic(fmt.Errorf("Cloud provider could not be initialized: %v", err))
}
s.SecureServing.ServerCert.CertDirectory = result.TmpDir
if cloud == nil {
t.Fatalf("Cloud provider is nil")
panic("Cloud provider is nil")
}
return cloud
}
@ -141,7 +135,7 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
commandArgs = append(commandArgs, fmt.Sprintf("--secure-port=%d", bindPort))
commandArgs = append(commandArgs, fmt.Sprintf("--cert-dir=%s", result.TmpDir))
t.Logf("cloud-controller-manager will listen securely on port %d...", bindPort)
logger.Info("cloud-controller-manager will listen securely", "port", bindPort)
}
for _, listener := range listeners {
listener.Close()
@ -164,7 +158,7 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
return result, err
}
t.Logf("Waiting for /healthz to be ok...")
logger.Info("Waiting for /healthz to be ok...")
client, err := kubernetes.NewForConfig(capturedConfig.LoopbackClientConfig)
if err != nil {
return result, fmt.Errorf("failed to create a client: %v", err)
@ -198,14 +192,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) {

View File

@ -17,6 +17,7 @@ limitations under the License.
package serving
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
@ -32,6 +33,7 @@ import (
cloudprovider "k8s.io/cloud-provider"
cloudctrlmgrtesting "k8s.io/cloud-provider/app/testing"
"k8s.io/cloud-provider/fake"
"k8s.io/klog/v2/ktesting"
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
kubectrlmgrtesting "k8s.io/kubernetes/cmd/kube-controller-manager/app/testing"
kubeschedulertesting "k8s.io/kubernetes/cmd/kube-scheduler/app/testing"
@ -39,15 +41,15 @@ import (
)
type componentTester interface {
StartTestServer(t kubectrlmgrtesting.Logger, customFlags []string) (*options.SecureServingOptionsWithLoopback, *server.SecureServingInfo, func(), error)
StartTestServer(ctx context.Context, customFlags []string) (*options.SecureServingOptionsWithLoopback, *server.SecureServingInfo, func(), error)
}
type kubeControllerManagerTester struct{}
func (kubeControllerManagerTester) StartTestServer(t kubectrlmgrtesting.Logger, customFlags []string) (*options.SecureServingOptionsWithLoopback, *server.SecureServingInfo, func(), error) {
func (kubeControllerManagerTester) StartTestServer(ctx context.Context, customFlags []string) (*options.SecureServingOptionsWithLoopback, *server.SecureServingInfo, func(), error) {
// avoid starting any controller loops, we're just testing serving
customFlags = append([]string{"--controllers="}, customFlags...)
gotResult, err := kubectrlmgrtesting.StartTestServer(t, customFlags)
gotResult, err := kubectrlmgrtesting.StartTestServer(ctx, customFlags)
if err != nil {
return nil, nil, nil, err
}
@ -56,8 +58,8 @@ func (kubeControllerManagerTester) StartTestServer(t kubectrlmgrtesting.Logger,
type cloudControllerManagerTester struct{}
func (cloudControllerManagerTester) StartTestServer(t kubectrlmgrtesting.Logger, customFlags []string) (*options.SecureServingOptionsWithLoopback, *server.SecureServingInfo, func(), error) {
gotResult, err := cloudctrlmgrtesting.StartTestServer(t, customFlags)
func (cloudControllerManagerTester) StartTestServer(ctx context.Context, customFlags []string) (*options.SecureServingOptionsWithLoopback, *server.SecureServingInfo, func(), error) {
gotResult, err := cloudctrlmgrtesting.StartTestServer(ctx, customFlags)
if err != nil {
return nil, nil, nil, err
}
@ -66,8 +68,8 @@ func (cloudControllerManagerTester) StartTestServer(t kubectrlmgrtesting.Logger,
type kubeSchedulerTester struct{}
func (kubeSchedulerTester) StartTestServer(t kubectrlmgrtesting.Logger, customFlags []string) (*options.SecureServingOptionsWithLoopback, *server.SecureServingInfo, func(), error) {
gotResult, err := kubeschedulertesting.StartTestServer(t, customFlags)
func (kubeSchedulerTester) StartTestServer(ctx context.Context, customFlags []string) (*options.SecureServingOptionsWithLoopback, *server.SecureServingInfo, func(), error) {
gotResult, err := kubeschedulertesting.StartTestServer(ctx, customFlags)
if err != nil {
return nil, nil, nil, err
}
@ -222,7 +224,8 @@ func testComponentWithSecureServing(t *testing.T, tester componentTester, kubeco
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
secureOptions, secureInfo, tearDownFn, err := tester.StartTestServer(t, append(append([]string{}, tt.flags...), extraFlags...))
_, ctx := ktesting.NewTestContext(t)
secureOptions, secureInfo, tearDownFn, err := tester.StartTestServer(ctx, append(append([]string{}, tt.flags...), extraFlags...))
if tearDownFn != nil {
defer tearDownFn()
}
@ -286,5 +289,6 @@ func intPtr(x int) *int {
func fakeCloudProviderFactory(io.Reader) (cloudprovider.Interface, error) {
return &fake.Cloud{
DisableRoutes: true, // disable routes for server tests, otherwise --cluster-cidr is required
}, nil
}