-
Notifications
You must be signed in to change notification settings - Fork 131
[CASCL-647] Add a subcommand to kubectl plugin to uninstall Karpenter #2424
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
Changes from 3 commits
7448887
f208c10
df49976
101161d
d67a083
f580791
4c5bbde
a2f039c
8578ad3
a86f477
d459e1a
9881734
f8616f7
ea85b71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ import ( | |
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| func createOrUpdate(ctx context.Context, cli client.Client, object client.Object) error { | ||
| func CreateOrUpdate(ctx context.Context, cli client.Client, object client.Object) error { | ||
| resourceVersion, err := getResourceVersion(ctx, cli, object) | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -58,3 +58,59 @@ func update(ctx context.Context, cli client.Client, object client.Object) error | |
|
|
||
| return nil | ||
| } | ||
|
|
||
| func Delete(ctx context.Context, cli client.Client, object client.Object) error { | ||
| log.Printf("Deleting %s %s…", object.GetObjectKind().GroupVersionKind().Kind, object.GetName()) | ||
|
|
||
| if err := cli.Delete(ctx, object); err != nil { | ||
| if apierrors.IsNotFound(err) { | ||
| log.Printf("%s %s not found, skipping deletion.", object.GetObjectKind().GroupVersionKind().Kind, object.GetName()) | ||
| return nil | ||
| } | ||
|
Comment on lines
+66
to
+69
Contributor
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. Is it a good idea to mask 404? E.g. update bubbles it up.
Member
Author
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. This is inside the The question might then be: why would we try to delete an object that doesn’t exist? |
||
| return fmt.Errorf("failed to delete %s %s: %w", object.GetObjectKind().GroupVersionKind().Kind, object.GetName(), err) | ||
| } | ||
|
|
||
| log.Printf("Deleted %s %s.", object.GetObjectKind().GroupVersionKind().Kind, object.GetName()) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func DeleteAllWithLabel(ctx context.Context, cli client.Client, list client.ObjectList, labelSelector client.MatchingLabels) error { | ||
| gvk := list.GetObjectKind().GroupVersionKind() | ||
| kind := gvk.Kind | ||
|
|
||
| log.Printf("Listing %s resources with labels %v…", kind, labelSelector) | ||
|
|
||
| if err := cli.List(ctx, list, labelSelector); err != nil { | ||
| return fmt.Errorf("failed to list %s resources: %w", kind, err) | ||
| } | ||
|
|
||
| items, err := extractListItems(list) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(items) == 0 { | ||
| log.Printf("No %s resources found with labels %v, skipping deletion.", kind, labelSelector) | ||
| return nil | ||
| } | ||
|
|
||
| log.Printf("Found %d %s resource(s) to delete.", len(items), kind) | ||
|
|
||
| for _, item := range items { | ||
| if err := Delete(ctx, cli, item); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func extractListItems(list client.ObjectList) ([]client.Object, error) { | ||
| switch v := list.(type) { | ||
| case interface{ GetItems() []client.Object }: | ||
| return v.GetItems(), nil | ||
| default: | ||
| return nil, fmt.Errorf("unsupported list type: %T", list) | ||
L3n41c marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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.
nit: could use slices.DeleteFunc:
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.
That’s much better! Thanks!
Implemented in f8616f7#diff-75e34b5bb02db7ae8e1635b717b48c7090c87a7f1a2d2a63b955847f3db9acd3