-
Notifications
You must be signed in to change notification settings - Fork 98
OCPBUGS-91973: Remove internal-registry-pull-secret-ref annotation during cleanup #445
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
vwalek
wants to merge
2
commits into
openshift:master
Choose a base branch
from
vwalek:OCPBUGS-91973-vwalek-fix
base: master
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.
+187
−0
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
180 changes: 180 additions & 0 deletions
180
pkg/operator/internalimageregistry/cleanup_controller_test.go
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,180 @@ | ||
| package internalimageregistry | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/openshift/library-go/pkg/controller/factory" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes" | ||
| corelistersv1 "k8s.io/client-go/listers/core/v1" | ||
| "k8s.io/client-go/rest" | ||
| "k8s.io/client-go/tools/cache" | ||
| ) | ||
|
|
||
| const ( | ||
| testNamespace = "test" | ||
| testServiceAccountName = "builder" | ||
| testImagePullSecretName = "builder-dockercfg-abc12" | ||
| internalRegistryPullRef = "openshift.io/internal-registry-pull-secret-ref" | ||
| ) | ||
|
|
||
| func TestCleanupServiceAccountUpdateConflictDoesNotMutateListerCache(t *testing.T) { | ||
| cachedServiceAccount := newServiceAccountWithInternalRegistryPullSecret( | ||
| testNamespace, | ||
| testServiceAccountName, | ||
| testImagePullSecretName, | ||
| ) | ||
|
|
||
| expectedServiceAccount := cachedServiceAccount.DeepCopy() | ||
|
|
||
| indexer := newServiceAccountIndexer(t, cachedServiceAccount) | ||
|
vwalek marked this conversation as resolved.
|
||
|
|
||
| controller := newCleanupControllerForTest( | ||
| newKubeClientWithServiceAccountUpdateConflict(t), | ||
| corelistersv1.NewServiceAccountLister(indexer), | ||
| corelistersv1.NewSecretLister(newEmptyIndexer()), | ||
| corelistersv1.NewPodLister(newEmptyIndexer()), | ||
| ) | ||
|
|
||
| err := controller.cleanup(context.Background()) | ||
| if !errors.Is(err, factory.SyntheticRequeueError) { | ||
| t.Fatalf("expected SyntheticRequeueError, got %v", err) | ||
| } | ||
|
|
||
| assertServiceAccountHasInternalRegistryPullSecret(t, indexer, expectedServiceAccount) | ||
| } | ||
|
|
||
| func newKubeClientWithServiceAccountUpdateConflict(t *testing.T) *kubernetes.Clientset { | ||
| t.Helper() | ||
|
|
||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method == http.MethodPut && strings.Contains(r.URL.Path, "/serviceaccounts/") { | ||
| status := metav1.Status{ | ||
| TypeMeta: metav1.TypeMeta{ | ||
| APIVersion: "v1", | ||
| Kind: "Status", | ||
| }, | ||
| Status: metav1.StatusFailure, | ||
| Message: "conflict", | ||
| Reason: metav1.StatusReasonConflict, | ||
| Code: http.StatusConflict, | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(http.StatusConflict) | ||
| if err := json.NewEncoder(w).Encode(status); err != nil { | ||
| t.Errorf("failed to encode conflict status: %v", err) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| w.WriteHeader(http.StatusNotFound) | ||
| })) | ||
|
|
||
| t.Cleanup(server.Close) | ||
|
|
||
| client, err := kubernetes.NewForConfig(&rest.Config{Host: server.URL}) | ||
| if err != nil { | ||
| t.Fatalf("failed to create kubernetes client: %v", err) | ||
| } | ||
|
|
||
| return client | ||
| } | ||
|
|
||
| func newServiceAccountWithInternalRegistryPullSecret(namespace, name, secretName string) *corev1.ServiceAccount { | ||
| return &corev1.ServiceAccount{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| Namespace: namespace, | ||
| Annotations: map[string]string{ | ||
| internalRegistryPullRef: secretName, | ||
| }, | ||
| }, | ||
| ImagePullSecrets: []corev1.LocalObjectReference{ | ||
| {Name: secretName}, | ||
| }, | ||
| Secrets: []corev1.ObjectReference{ | ||
| {Name: secretName}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func newServiceAccountIndexer(t *testing.T, serviceAccount *corev1.ServiceAccount) cache.Indexer { | ||
| t.Helper() | ||
|
|
||
| indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{ | ||
| cache.NamespaceIndex: cache.MetaNamespaceIndexFunc, | ||
| }) | ||
| if err := indexer.Add(serviceAccount); err != nil { | ||
| t.Fatalf("failed to add service account to indexer: %v", err) | ||
| } | ||
|
|
||
| return indexer | ||
| } | ||
|
|
||
| func newEmptyIndexer() cache.Indexer { | ||
| return cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{ | ||
| cache.NamespaceIndex: cache.MetaNamespaceIndexFunc, | ||
| }) | ||
| } | ||
|
|
||
| func newCleanupControllerForTest( | ||
| kubeClient *kubernetes.Clientset, | ||
| serviceAccountLister corelistersv1.ServiceAccountLister, | ||
| secretLister corelistersv1.SecretLister, | ||
| podLister corelistersv1.PodLister, | ||
| ) *imagePullSecretCleanupController { | ||
| return &imagePullSecretCleanupController{ | ||
| kubeClient: kubeClient, | ||
| serviceAccountLister: serviceAccountLister, | ||
| secretLister: secretLister, | ||
| podLister: podLister, | ||
| } | ||
| } | ||
|
|
||
| func assertServiceAccountHasInternalRegistryPullSecret(t *testing.T, indexer cache.Indexer, expected *corev1.ServiceAccount) { | ||
| t.Helper() | ||
|
|
||
| cached, exists, err := indexer.GetByKey(expected.Namespace + "/" + expected.Name) | ||
| if err != nil { | ||
| t.Fatalf("failed to get cached service account: %v", err) | ||
| } | ||
| if !exists { | ||
| t.Fatal("expected cached service account to exist") | ||
| } | ||
|
|
||
| cachedServiceAccount := cached.(*corev1.ServiceAccount) | ||
|
|
||
| if got := cachedServiceAccount.Annotations[internalRegistryPullRef]; got != expected.Annotations[internalRegistryPullRef] { | ||
| t.Fatalf( | ||
| "expected cached annotation %q to remain %q, got %q", | ||
| internalRegistryPullRef, | ||
| expected.Annotations[internalRegistryPullRef], | ||
| got, | ||
| ) | ||
| } | ||
|
|
||
| if len(cachedServiceAccount.ImagePullSecrets) != len(expected.ImagePullSecrets) { | ||
| t.Fatalf("expected %d image pull secret references, got %d", len(expected.ImagePullSecrets), len(cachedServiceAccount.ImagePullSecrets)) | ||
| } | ||
| for i, expectedRef := range expected.ImagePullSecrets { | ||
| if cachedServiceAccount.ImagePullSecrets[i].Name != expectedRef.Name { | ||
| t.Fatalf("expected image pull secret reference %q, got %q", expectedRef.Name, cachedServiceAccount.ImagePullSecrets[i].Name) | ||
| } | ||
| } | ||
|
|
||
| if len(cachedServiceAccount.Secrets) != len(expected.Secrets) { | ||
| t.Fatalf("expected %d secret references, got %d", len(expected.Secrets), len(cachedServiceAccount.Secrets)) | ||
| } | ||
| for i, expectedRef := range expected.Secrets { | ||
| if cachedServiceAccount.Secrets[i].Name != expectedRef.Name { | ||
| t.Fatalf("expected secret reference %q, got %q", expectedRef.Name, cachedServiceAccount.Secrets[i].Name) | ||
| } | ||
| } | ||
| } | ||
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.