-
-
Notifications
You must be signed in to change notification settings - Fork 63
Fix 154: adding a finalizer to the tunnel's associated secret #158
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
cyclingwithelephants
wants to merge
11
commits into
adyanth:main
Choose a base branch
from
cyclingwithelephants:154-secret-finaliser
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.
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ab0cbbc
initial commit
cyclingwithelephants d883dfa
remove docs/deletion.md
cyclingwithelephants 5e48ba2
appease the linter
cyclingwithelephants d089494
convert secretClient to generic objectClient
cyclingwithelephants 0a478e6
appease the linter
cyclingwithelephants f7134ed
merge with main
cyclingwithelephants e450d01
make objectClient accept concrete object type
cyclingwithelephants 80f22e3
make objectClient accept concrete object type; appease the linter
cyclingwithelephants c033468
tested working
cyclingwithelephants 92077b4
add kind annotation to logs
cyclingwithelephants 46901dc
move secret finalizer removal to generic tunnel reconciler
cyclingwithelephants 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 was deleted.
Oops, something went wrong.
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,84 @@ | ||
| package k8s | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| corev1 "k8s.io/api/core/v1" | ||
| k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| type SecretClient struct { | ||
| k8sClient client.Client | ||
| log *logr.Logger | ||
| } | ||
|
|
||
| func NewSecretClient(k8sClient client.Client, log *logr.Logger) (*SecretClient, error) { | ||
| if k8sClient == nil { | ||
| return nil, errors.New("k8sClient cannot be nil") | ||
| } | ||
| if log == nil { | ||
| return nil, errors.New("logger cannot be nil") | ||
| } | ||
| return &SecretClient{ | ||
| k8sClient: k8sClient, | ||
| log: log, | ||
| }, nil | ||
| } | ||
|
|
||
| // EnsureFinalizer idempotently adds the specified finalizer to the specified secret | ||
| func (s *SecretClient) EnsureFinalizer(ctx context.Context, secretName, secretNamespace, finalizer string) error { | ||
| var secret corev1.Secret | ||
| secretKey := client.ObjectKey{ | ||
| Name: secretName, | ||
| Namespace: secretNamespace, | ||
| } | ||
| if err := s.k8sClient.Get(ctx, secretKey, &secret); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // if finalizer already exists, we are happy | ||
| for _, existingFinalizer := range secret.Finalizers { | ||
| if finalizer == existingFinalizer { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| s.log.WithValues("finalizer", finalizer).Info("creating finalizer") | ||
| secret.Finalizers = append(secret.Finalizers, finalizer) | ||
| return s.k8sClient.Update(ctx, &secret) | ||
| } | ||
|
|
||
| // RemoveFinalizer removes the first instance of the finalizer from the given secret | ||
| func (s *SecretClient) RemoveFinalizer(ctx context.Context, secretName, secretNamespace, finalizer string) error { | ||
|
cyclingwithelephants marked this conversation as resolved.
Outdated
|
||
| var secret corev1.Secret | ||
| secretKey := client.ObjectKey{ | ||
| Name: secretName, | ||
| Namespace: secretNamespace, | ||
| } | ||
| if err := s.k8sClient.Get(ctx, secretKey, &secret); err != nil { | ||
| if k8serrors.IsNotFound(err) { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| s.log.WithValues("finalizer", finalizer).Info("deleting finalizer") | ||
| secret.Finalizers = removeString(secret.Finalizers, finalizer) | ||
| return s.k8sClient.Update(ctx, &secret) | ||
| } | ||
|
|
||
| // removeString returns a copy of list with the first (only) occurrence | ||
| // of target removed. If target is not present, the original slice is | ||
| // returned unchanged. | ||
| func removeString(list []string, target string) []string { | ||
| for i, v := range list { | ||
| if v == target { | ||
| // splice out the element at index i | ||
| return append(list[:i], list[i+1:]...) | ||
|
adyanth marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| return list | ||
| } | ||
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.