Merge pull request #111317 from wojtek-t/fix_leaking_goroutines_12

Clean shutdown of cloud controllers in integration tests
This commit is contained in:
Kubernetes Prow Robot 2022-07-26 04:45:09 -07:00 committed by GitHub
commit 28fc2991a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -31,6 +31,8 @@ import (
"k8s.io/kubernetes/cmd/kube-controller-manager/app"
kubecontrollerconfig "k8s.io/kubernetes/cmd/kube-controller-manager/app/config"
"k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
"k8s.io/klog/v2"
)
// TearDownFunc is to be called to tear down a test server.
@ -60,8 +62,18 @@ type Logger interface {
// enough time to remove temporary files.
func StartTestServer(t Logger, customFlags []string) (result TestServer, err error) {
stopCh := make(chan struct{})
var errCh chan error
tearDown := func() {
close(stopCh)
// If the kube-controller-manager was started, let's wait for
// it to shutdown clearly.
if errCh != nil {
err, ok := <-errCh
if ok && err != nil {
klog.Errorf("Failed to shutdown test server clearly: %v", err)
}
}
if len(result.TmpDir) != 0 {
os.RemoveAll(result.TmpDir)
}
@ -105,8 +117,10 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
return result, fmt.Errorf("failed to create config from options: %v", err)
}
errCh := make(chan error)
errCh = make(chan error)
go func(stopCh <-chan struct{}) {
defer close(errCh)
if err := app.Run(config.Complete(), stopCh); err != nil {
errCh <- err
}

View File

@ -33,6 +33,8 @@ import (
"k8s.io/cloud-provider/app/config"
"k8s.io/cloud-provider/options"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/klog/v2"
)
// TearDownFunc is to be called to tear down a test server.
@ -62,10 +64,20 @@ type Logger interface {
// enough time to remove temporary files.
func StartTestServer(t Logger, customFlags []string) (result TestServer, err error) {
stopCh := make(chan struct{})
var errCh chan error
configDoneCh := make(chan struct{})
var capturedConfig config.CompletedConfig
tearDown := func() {
close(stopCh)
// If cloud-controller-manager was started, let's wait for
// it to shutdown clearly.
if errCh != nil {
err, ok := <-errCh
if ok && err != nil {
klog.Errorf("Failed to shutdown test server clearly: %v", err)
}
}
if len(result.TmpDir) != 0 {
os.RemoveAll(result.TmpDir)
}
@ -135,13 +147,14 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
listener.Close()
}
errCh := make(chan error)
errCh = make(chan error)
go func() {
defer close(errCh)
command.SetArgs(commandArgs)
if err := command.Execute(); err != nil {
errCh <- err
}
close(errCh)
}()
select {