Repository navigation
fix: stop leaking a branch worker when nothing needs it any more #390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 78.7 | ||
| 78.9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| k8stypes "k8s.io/apimachinery/pkg/types" | ||
| clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/interceptor" | ||
|
|
||
| configbutleraiv1alpha3 "github.com/ConfigButler/gitops-reverser/api/v1alpha3" | ||
| "github.com/ConfigButler/gitops-reverser/internal/git" | ||
| "github.com/ConfigButler/gitops-reverser/internal/types" | ||
| ) | ||
|
|
||
| // TestCleanupDeletedGitTarget_StopsAWorkerNothingNeedsAnyMore is the wiring, and the wiring is the | ||
| // whole bug: the sweep existed and was never called, so a branch worker — its goroutine, its event | ||
| // queue and its on-disk clone — outlived every GitTarget that justified it, until the process | ||
| // restarted. The reconcile that the delete watch event produces is where the set of needed workers | ||
| // can have shrunk, so that is where the sweep belongs. | ||
| func TestCleanupDeletedGitTarget_StopsAWorkerNothingNeedsAnyMore(t *testing.T) { | ||
| scheme := runtime.NewScheme() | ||
| require.NoError(t, clientgoscheme.AddToScheme(scheme)) | ||
| require.NoError(t, configbutleraiv1alpha3.AddToScheme(scheme)) | ||
| k8sClient := fake.NewClientBuilder().WithScheme(scheme).Build() | ||
|
|
||
| workers := git.NewWorkerManager(k8sClient, logr.Discard(), git.BranchWorkerLimits{}, | ||
| types.SensitiveResourcePolicy{}) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| go func() { _ = workers.Start(ctx) }() | ||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| require.NoError(t, workers.EnsureWorker(ctx, "repo1", "shop", "main")) | ||
| _, exists := workers.GetWorkerForTarget("repo1", "shop", "main") | ||
| require.True(t, exists) | ||
|
|
||
| r := &GitTargetReconciler{Client: k8sClient, WorkerManager: workers} | ||
| _ = r.cleanupDeletedGitTarget(ctx, | ||
| k8stypes.NamespacedName{Name: "checkout", Namespace: "shop"}, logr.Discard()) | ||
|
|
||
| _, exists = workers.GetWorkerForTarget("repo1", "shop", "main") | ||
| assert.False(t, exists, "the GitTarget is gone and nothing else lists this branch") | ||
| } | ||
|
|
||
| // TestCleanupDeletedGitTarget_LeavesAWorkerAnotherGitTargetStillUses. The sweep reads the API | ||
| // rather than counting registrations, and this is why: one worker serves every GitTarget on its | ||
| // (provider, branch), so one deletion says nothing about the rest. | ||
| func TestCleanupDeletedGitTarget_LeavesAWorkerAnotherGitTargetStillUses(t *testing.T) { | ||
| scheme := runtime.NewScheme() | ||
| require.NoError(t, clientgoscheme.AddToScheme(scheme)) | ||
| require.NoError(t, configbutleraiv1alpha3.AddToScheme(scheme)) | ||
|
|
||
| sibling := &configbutleraiv1alpha3.GitTarget{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "invoices", Namespace: "shop"}, | ||
| } | ||
| sibling.Spec.GitProviderRef.Name = "repo1" | ||
| sibling.Spec.Branch = "main" | ||
| k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(sibling).Build() | ||
|
|
||
| workers := git.NewWorkerManager(k8sClient, logr.Discard(), git.BranchWorkerLimits{}, | ||
| types.SensitiveResourcePolicy{}) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| go func() { _ = workers.Start(ctx) }() | ||
| time.Sleep(100 * time.Millisecond) | ||
| require.NoError(t, workers.EnsureWorker(ctx, "repo1", "shop", "main")) | ||
|
|
||
| r := &GitTargetReconciler{Client: k8sClient, WorkerManager: workers} | ||
| _ = r.cleanupDeletedGitTarget(ctx, | ||
| k8stypes.NamespacedName{Name: "checkout", Namespace: "shop"}, logr.Discard()) | ||
|
|
||
| _, exists := workers.GetWorkerForTarget("repo1", "shop", "main") | ||
| assert.True(t, exists, "invoices still writes to this branch") | ||
| } | ||
|
|
||
| // TestCleanupDeletedGitTarget_WithoutAWorkerManager covers the CLI and the tests: the cleanup runs | ||
| // on a reconciler with no data plane wired, and must not reach for one. | ||
| func TestCleanupDeletedGitTarget_WithoutAWorkerManager(t *testing.T) { | ||
| r := &GitTargetReconciler{} | ||
| assert.NotPanics(t, func() { | ||
| _ = r.cleanupDeletedGitTarget(context.Background(), | ||
| k8stypes.NamespacedName{Name: "checkout", Namespace: "shop"}, logr.Discard()) | ||
| }) | ||
| } | ||
|
|
||
| // TestCleanupDeletedGitTarget_RetriesWhenTheSweepCannotRead. The sweep is the only thing that ever | ||
| // retires a worker, so a read that failed must come back. A deleted object still has a reconcile | ||
| // key, so returning the error requeues this same pass; swallowing it would leave the worker and | ||
| // its clone standing until some other GitTarget happens to be deleted, which nothing guarantees. | ||
| func TestCleanupDeletedGitTarget_RetriesWhenTheSweepCannotRead(t *testing.T) { | ||
| scheme := runtime.NewScheme() | ||
| require.NoError(t, clientgoscheme.AddToScheme(scheme)) | ||
| require.NoError(t, configbutleraiv1alpha3.AddToScheme(scheme)) | ||
| k8sClient := fake.NewClientBuilder().WithScheme(scheme). | ||
| WithInterceptorFuncs(interceptor.Funcs{ | ||
| List: func(context.Context, client.WithWatch, client.ObjectList, ...client.ListOption) error { | ||
| return errors.New("the cache is not having it") | ||
| }, | ||
| }).Build() | ||
|
|
||
| workers := git.NewWorkerManager(k8sClient, logr.Discard(), git.BranchWorkerLimits{}, | ||
| types.SensitiveResourcePolicy{}) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| go func() { _ = workers.Start(ctx) }() | ||
| time.Sleep(100 * time.Millisecond) | ||
| require.NoError(t, workers.EnsureWorker(ctx, "repo1", "shop", "main")) | ||
|
|
||
| r := &GitTargetReconciler{Client: k8sClient, WorkerManager: workers} | ||
| err := r.cleanupDeletedGitTarget(ctx, | ||
| k8stypes.NamespacedName{Name: "checkout", Namespace: "shop"}, logr.Discard()) | ||
|
|
||
| require.Error(t, err, "the caller has to come back, or nothing ever retires this worker") | ||
| _, exists := workers.GetWorkerForTarget("repo1", "shop", "main") | ||
| assert.True(t, exists, "and the worker is still there, which is why the retry matters") | ||
| } | ||
|
|
||
| // TestHandleFetchError_RequeuesAFailedCleanup is the wiring of the rule above: the reconcile must | ||
| // surface the error rather than reporting success for a pass that did not finish. | ||
| func TestHandleFetchError_RequeuesAFailedCleanup(t *testing.T) { | ||
| scheme := runtime.NewScheme() | ||
| require.NoError(t, clientgoscheme.AddToScheme(scheme)) | ||
| require.NoError(t, configbutleraiv1alpha3.AddToScheme(scheme)) | ||
| k8sClient := fake.NewClientBuilder().WithScheme(scheme). | ||
| WithInterceptorFuncs(interceptor.Funcs{ | ||
| List: func(context.Context, client.WithWatch, client.ObjectList, ...client.ListOption) error { | ||
| return errors.New("the cache is not having it") | ||
| }, | ||
| }).Build() | ||
|
|
||
| workers := git.NewWorkerManager(k8sClient, logr.Discard(), git.BranchWorkerLimits{}, | ||
| types.SensitiveResourcePolicy{}) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| go func() { _ = workers.Start(ctx) }() | ||
| time.Sleep(100 * time.Millisecond) | ||
| require.NoError(t, workers.EnsureWorker(ctx, "repo1", "shop", "main")) | ||
|
|
||
| r := &GitTargetReconciler{Client: k8sClient, WorkerManager: workers} | ||
| _, err := r.handleFetchError(ctx, apierrors.NewNotFound( | ||
| schema.GroupResource{Resource: "gittargets"}, "checkout"), | ||
| logr.Discard(), k8stypes.NamespacedName{Name: "checkout", Namespace: "shop"}) | ||
|
|
||
| assert.Error(t, err) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.