Merge pull request #115854 from kerthcet/cleanup/apiserver-cleanup

Cleanup resources when initializing error in integration
This commit is contained in:
Kubernetes Prow Robot 2023-02-24 15:58:05 -08:00 committed by GitHub
commit 70fee660fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 7 deletions

View File

@ -65,12 +65,12 @@ func StartTestServer(t testing.TB, setup TestServerSetup) (client.Interface, *re
t.Fatalf("Couldn't create temp dir: %v", err)
}
stopCh := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
var errCh chan error
tearDownFn := func() {
// Closing stopCh is stopping apiserver and cleaning up
// Calling cancel function is stopping apiserver and cleaning up
// after itself, including shutting down its storage layer.
close(stopCh)
cancel()
// If the apiserver was started, let's wait for it to
// shutdown clearly.
@ -173,7 +173,7 @@ func StartTestServer(t testing.TB, setup TestServerSetup) (client.Interface, *re
errCh = make(chan error)
go func() {
defer close(errCh)
if err := kubeAPIServer.GenericAPIServer.PrepareRun().Run(stopCh); err != nil {
if err := kubeAPIServer.GenericAPIServer.PrepareRun().Run(ctx.Done()); err != nil {
errCh <- err
}
}()
@ -203,15 +203,15 @@ func StartTestServer(t testing.TB, setup TestServerSetup) (client.Interface, *re
}
healthStatus := 0
kubeClient.Discovery().RESTClient().Get().AbsPath("/healthz").Do(context.TODO()).StatusCode(&healthStatus)
kubeClient.Discovery().RESTClient().Get().AbsPath("/healthz").Do(ctx).StatusCode(&healthStatus)
if healthStatus != http.StatusOK {
return false, nil
}
if _, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), "default", metav1.GetOptions{}); err != nil {
if _, err := kubeClient.CoreV1().Namespaces().Get(ctx, "default", metav1.GetOptions{}); err != nil {
return false, nil
}
if _, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), "kube-system", metav1.GetOptions{}); err != nil {
if _, err := kubeClient.CoreV1().Namespaces().Get(ctx, "kube-system", metav1.GetOptions{}); err != nil {
return false, nil
}

View File

@ -2556,6 +2556,8 @@ func initTestSchedulerForFrameworkTest(t *testing.T, testCtx *testutils.TestCont
if nodeCount > 0 {
if _, err := createAndWaitForNodesInCache(testCtx, "test-node", st.MakeNode(), nodeCount); err != nil {
// Make sure to cleanup the resources when initializing error.
testutils.CleanupTest(t, testCtx)
t.Fatal(err)
}
}