-
-
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 10 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Package k8s provides client abstractions to the kubernetes API | ||
| package k8s | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
| ) | ||
|
|
||
| type ObjectClient struct { | ||
| k8sClient client.Client | ||
| log *logr.Logger | ||
| } | ||
|
|
||
| func NewObjectClient(k8sClient client.Client, log *logr.Logger) (*ObjectClient, error) { | ||
| if k8sClient == nil { | ||
| return nil, errors.New("k8sClient cannot be nil") | ||
| } | ||
| if log == nil { | ||
| return nil, errors.New("log cannot be nil") | ||
| } | ||
| return &ObjectClient{ | ||
| k8sClient: k8sClient, | ||
| log: log, | ||
| }, nil | ||
| } | ||
|
|
||
| // EnsureFinalizer adds `finalizer` to obj exactly once. | ||
| // - It NO-OPs if the finalizer is already there. | ||
| // - It uses a strategic-merge Patch so you don’t overwrite concurrent changes. | ||
| // - obj must be a pointer that already contains the latest copy from the API server. | ||
| func (s *ObjectClient) EnsureFinalizer(ctx context.Context, key client.ObjectKey, obj client.Object, finalizer string) error { | ||
| err := s.k8sClient.Get(ctx, key, obj) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // if it already contains the finalizer, there's nothing to do | ||
| if controllerutil.ContainsFinalizer(obj, finalizer) { | ||
| return nil | ||
| } | ||
|
|
||
| //nolint:revive // we know this will serialise, even if the compiler doesn't | ||
| base := obj.DeepCopyObject().(client.Object) | ||
| controllerutil.AddFinalizer(obj, finalizer) | ||
|
|
||
| s.log. | ||
| WithValues("finalizer", finalizer). | ||
| WithValues("object", fmt.Sprintf("%s/%s", obj.GetNamespace(), obj.GetName())). | ||
| WithValues("kind", obj.GetObjectKind().GroupVersionKind().Kind). | ||
| Info("creating finalizer") | ||
| if err := s.k8sClient.Patch(ctx, obj, client.MergeFrom(base)); err != nil { | ||
| return fmt.Errorf("could not add finalizer %q: %w", finalizer, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // RemoveFinalizer deletes `finalizer` from obj exactly once. | ||
| // - NO-OPs if the finalizer is already gone. | ||
| // - Uses a strategic-merge Patch so you never clobber concurrent changes. | ||
| // - obj must be a *live* copy fetched from the API server. | ||
| func (s *ObjectClient) RemoveFinalizer(ctx context.Context, key client.ObjectKey, obj client.Object, finalizer string) error { | ||
| err := s.k8sClient.Get(ctx, key, obj) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // if there is no finalizer, there's nothing to do | ||
| if !controllerutil.ContainsFinalizer(obj, finalizer) { | ||
| return nil | ||
| } | ||
|
|
||
| //nolint:revive // we know this will serialise, even if the compiler doesn't | ||
| base := obj.DeepCopyObject().(client.Object) | ||
| controllerutil.RemoveFinalizer(obj, finalizer) | ||
|
|
||
| s.log. | ||
| WithValues("finalizer", finalizer). | ||
| WithValues("object", fmt.Sprintf("%s/%s", obj.GetNamespace(), obj.GetName())). | ||
| WithValues("kind", obj.GetObjectKind().GroupVersionKind().Kind). | ||
| Info("removing finalizer") | ||
| if err := s.k8sClient.Patch(ctx, obj, client.MergeFrom(base)); err != nil { | ||
| return fmt.Errorf("could not remove finalizer %q: %w", finalizer, err) | ||
| } | ||
| return nil | ||
| } |
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
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
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.
This logic is in the wrong place (ref: the error you saw). When this condition is hit, the tunnel is already removed from etcd.
What you need is, instead of the logic in both here and
tunnel_controller.go, move this tocloudflare-operator/internal/controller/generic_tunnel_reconciler.go
Lines 157 to 162 in d1570e5
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.
Oh, derp. Thanks will fix this
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.
Did you get around to this? Would love to merge this in
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.
I haven't sorry, life has been a lot this past month! Hoping to get to this next weekend
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.
All good. Life comes first!
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.
Just wanted to give you another heads up, might be another week