Repository navigation
atecontroller: scope controller cache to deployments managed by workerpools #181
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
Open
danielle-mustillo
wants to merge
2
commits into
main
Choose a base branch
from
danielle-mustillo/scopeDeployCacheFork
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−34
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright 2026 The Agent Substrate Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controllers | ||
|
|
||
| import ( | ||
| appsv1 "k8s.io/api/apps/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/fields" | ||
| "k8s.io/apimachinery/pkg/labels" | ||
| "k8s.io/apimachinery/pkg/selection" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/cache" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| // CacheOptions limits the manager cache to the egressMITMCAPool Secret and to | ||
| // Deployments that carry workerPoolLabel. Other kinds are cached in full. | ||
|
iplay88keys marked this conversation as resolved.
Resolved
|
||
| func CacheOptions(egressMITMCAPool types.NamespacedName) cache.Options { | ||
| hasWorkerPoolLabel, err := labels.NewRequirement(workerPoolLabel, selection.Exists, nil) | ||
| utilruntime.Must(err) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: calling utilruntime.Must seems incorrect here looking at the rest of the main.go flow. for scheme registration, fine, but as you get further into the main.go flow, it uses explicit error handling |
||
| return cache.Options{ | ||
| ByObject: map[client.Object]cache.ByObject{ | ||
| &corev1.Secret{}: { | ||
| Namespaces: map[string]cache.Config{ | ||
| egressMITMCAPool.Namespace: { | ||
| FieldSelector: fields.OneTermEqualSelector("metadata.name", egressMITMCAPool.Name), | ||
| }, | ||
| }, | ||
| }, | ||
| &appsv1.Deployment{}: {Label: labels.NewSelector().Add(*hasWorkerPoolLabel)}, | ||
| }, | ||
| } | ||
| } | ||
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,144 @@ | ||
| // Copyright 2026 The Agent Substrate Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controllers | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| appsv1 "k8s.io/api/apps/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| k8errors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/labels" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| appsv1ac "k8s.io/client-go/applyconfigurations/apps/v1" | ||
| corev1ac "k8s.io/client-go/applyconfigurations/core/v1" | ||
| metav1ac "k8s.io/client-go/applyconfigurations/meta/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/cache" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| // cachedClient reads from the manager cache, as the reconcilers do. TestMain sets it. | ||
| var cachedClient client.Client | ||
|
|
||
| // TestCacheOptionsScope checks the Secret and Deployment rules of CacheOptions. | ||
| func TestCacheOptionsScope(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ref := types.NamespacedName{Namespace: "ate-system", Name: "pool"} | ||
| opts := CacheOptions(ref) | ||
|
|
||
| var secrets, deployments cache.ByObject | ||
| hasSecrets, hasDeployments := false, false | ||
| for obj, by := range opts.ByObject { | ||
| switch obj.(type) { | ||
| case *corev1.Secret: | ||
| secrets, hasSecrets = by, true | ||
| case *appsv1.Deployment: | ||
| deployments, hasDeployments = by, true | ||
| } | ||
| } | ||
| if !hasSecrets { | ||
| t.Fatal("no cache scope for Secrets") | ||
| } | ||
| scope, ok := secrets.Namespaces[ref.Namespace] | ||
| if !ok || len(secrets.Namespaces) != 1 { | ||
| t.Fatalf("Secret cache namespaces = %v, want only %q", secrets.Namespaces, ref.Namespace) | ||
| } | ||
| if got := scope.FieldSelector.String(); got != "metadata.name="+ref.Name { | ||
| t.Errorf("Secret field selector = %q, want %q", got, "metadata.name="+ref.Name) | ||
| } | ||
|
|
||
| if !hasDeployments || deployments.Label == nil { | ||
| t.Fatal("no label-selected cache scope for Deployments") | ||
| } | ||
| if !deployments.Label.Matches(labels.Set{"ate.dev/worker-pool": "pool"}) { | ||
| t.Errorf("Deployment label selector %q does not match the label ate.dev/worker-pool", deployments.Label) | ||
| } | ||
| if deployments.Label.Matches(labels.Set{}) { | ||
| t.Errorf("Deployment label selector %q matches a Deployment with no labels", deployments.Label) | ||
| } | ||
| } | ||
|
|
||
| // TestDeploymentCacheScope checks that the manager cache holds a pool's | ||
| // Deployment and no Deployment without the ate.dev/worker-pool label. | ||
| func TestDeploymentCacheScope(t *testing.T) { | ||
| t.Parallel() | ||
| ctx := t.Context() | ||
| const label = "ate.dev/worker-pool" | ||
| wp := makeWorkerPool("cache-scope", "default", 2, "worker:v1") | ||
|
|
||
| // The bystander is created first, so a cache of every Deployment holds it | ||
| // before it holds the labeled Deployment of the pool. | ||
| bystander := applyDeploymentWithoutLabels(t, "cache-scope-bystander", wp.Namespace, map[string]string{"app": "bystander"}, "someone-else") | ||
| applyDeploymentWithoutLabels(t, wp.Name, wp.Namespace, map[string]string{label: wp.Name}, workerPoolFieldOwner) | ||
|
|
||
| if err := k8sClient.Create(ctx, wp); err != nil { | ||
| t.Fatalf("create WorkerPool: %v", err) | ||
| } | ||
| deleteOnCleanup(t, wp) | ||
|
|
||
| eventually(t, func(ctx context.Context) (bool, error) { | ||
| dep := &appsv1.Deployment{} | ||
| if err := cachedClient.Get(ctx, client.ObjectKeyFromObject(wp), dep); err != nil { | ||
| return false, nil | ||
| } | ||
| return dep.Labels[label] == wp.Name && metav1.IsControlledBy(dep, wp), nil | ||
| }) | ||
|
|
||
| err := cachedClient.Get(ctx, client.ObjectKeyFromObject(bystander), &appsv1.Deployment{}) | ||
| if !k8errors.IsNotFound(err) { | ||
| t.Errorf("cached read of a Deployment without %s: err = %v, want NotFound", label, err) | ||
| } | ||
| if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(bystander), &appsv1.Deployment{}); err != nil { | ||
| t.Fatalf("live read of the bystander Deployment: %v", err) | ||
| } | ||
|
|
||
| dep, err := getDeployment(ctx, wp) | ||
| if err != nil { | ||
| t.Fatalf("get Deployment: %v", err) | ||
| } | ||
| strip := client.RawPatch(types.MergePatchType, []byte(`{"metadata":{"labels":{"`+label+`":null}}}`)) | ||
| if err := k8sClient.Patch(ctx, dep, strip); err != nil { | ||
| t.Fatalf("strip %s: %v", label, err) | ||
| } | ||
| if _, has := dep.Labels[label]; has { | ||
| t.Fatalf("label %s survived the patch", label) | ||
| } | ||
| eventually(t, func(ctx context.Context) (bool, error) { | ||
| dep, err := getDeployment(ctx, wp) | ||
| return err == nil && dep.Labels[label] == wp.Name, nil | ||
| }) | ||
| } | ||
|
|
||
| // applyDeploymentWithoutLabels applies a Deployment whose metadata carries no | ||
| // labels, and deletes it on cleanup. | ||
| func applyDeploymentWithoutLabels(t *testing.T, name, namespace string, podLabels map[string]string, fieldOwner string) *appsv1.Deployment { | ||
| t.Helper() | ||
| ac := appsv1ac.Deployment(name, namespace). | ||
| WithSpec(appsv1ac.DeploymentSpec(). | ||
| WithSelector(metav1ac.LabelSelector().WithMatchLabels(podLabels)). | ||
| WithTemplate(corev1ac.PodTemplateSpec(). | ||
| WithLabels(podLabels). | ||
| WithSpec(corev1ac.PodSpec(). | ||
| WithContainers(corev1ac.Container().WithName("worker").WithImage("worker:v0"))))) | ||
| if err := k8sClient.Apply(t.Context(), ac, client.FieldOwner(fieldOwner)); err != nil { | ||
| t.Fatalf("apply Deployment %s: %v", name, err) | ||
| } | ||
| dep := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace}} | ||
| deleteOnCleanup(t, dep) | ||
| return dep | ||
| } |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily opposed to a new file here, but the cache_test.go is insufficient for the existing secret cache functionality. TestManagerOptionsScopeCache also exists from main_test.go as well. And then we're modifying that test case as well with these diffs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed the test functionality belongs here. I moved the tests into cache_test.go from main_test.go that are relevant. I think this pretty much addresses the comment.