-
Notifications
You must be signed in to change notification settings - Fork 138
[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
Merged
gh-worker-dd-mergequeue-cf854d
merged 14 commits into
main
from
lenaic/CASCL-647_karpenter_uninstaller
Jan 21, 2026
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7448887
[CASCL-647] Add a subcommand to kubectl plugin to uninstall Karpenter
L3n41c f208c10
Merge branch 'main' of github.com:DataDog/datadog-operator into lenai…
L3n41c df49976
Fix linter errors
L3n41c 101161d
Merge branch 'main' of github.com:DataDog/datadog-operator into lenai…
L3n41c d67a083
Accumulate and report errors from cleanup steps instead of silently i…
L3n41c f580791
Factorize all the code shared between `install` and `uninstall`
L3n41c 4c5bbde
Do not make Karpenter uninstalling fail if CRDs are not present
L3n41c a2f039c
Factorize the code to put a border around the most important messages
L3n41c 8578ad3
Merge branch 'main' of github.com:DataDog/datadog-operator into lenai…
L3n41c a86f477
Improve status messages
L3n41c d459e1a
Print a more detailed view of which resources will be deleted
L3n41c 9881734
Re-order functions
L3n41c f8616f7
Implement code review comments
L3n41c ea85b71
Do not prematurely abort cleanup at the first error
L3n41c 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
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
File renamed without changes.
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
119 changes: 119 additions & 0 deletions
119
cmd/kubectl-datadog/autoscaling/cluster/common/clients/clients.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,119 @@ | ||
| // Package clients provides shared AWS and Kubernetes client initialization | ||
| // for the Karpenter install and uninstall commands. | ||
| package clients | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| awssdk "github.com/aws/aws-sdk-go-v2/aws" | ||
| "github.com/aws/aws-sdk-go-v2/config" | ||
| "github.com/aws/aws-sdk-go-v2/service/cloudformation" | ||
| "github.com/aws/aws-sdk-go-v2/service/ec2" | ||
| "github.com/aws/aws-sdk-go-v2/service/eks" | ||
| "github.com/aws/aws-sdk-go-v2/service/sts" | ||
| karpawsv1 "github.com/aws/karpenter-provider-aws/pkg/apis/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/cli-runtime/pkg/genericclioptions" | ||
| "k8s.io/client-go/kubernetes" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| "k8s.io/client-go/rest" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
| karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1" | ||
|
|
||
| "github.com/DataDog/datadog-operator/cmd/kubectl-datadog/autoscaling/cluster/install/guess" | ||
| ) | ||
|
|
||
| // Clients holds all AWS and Kubernetes client instances needed for | ||
| // Karpenter installation and uninstallation operations. | ||
| type Clients struct { | ||
| // AWS clients | ||
| Config awssdk.Config | ||
| CloudFormation *cloudformation.Client | ||
| EC2 *ec2.Client | ||
| EKS *eks.Client | ||
| STS *sts.Client | ||
|
|
||
| // Kubernetes clients | ||
| K8sClient client.Client // controller-runtime client | ||
| K8sClientset *kubernetes.Clientset // typed Kubernetes client | ||
| } | ||
|
|
||
| // Build creates AWS and Kubernetes clients for Karpenter operations. | ||
| func Build(ctx context.Context, configFlags *genericclioptions.ConfigFlags, k8sClientset *kubernetes.Clientset) (*Clients, error) { | ||
| awsConfig, err := config.LoadDefaultConfig(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to load AWS config: %w", err) | ||
| } | ||
|
|
||
| sch := runtime.NewScheme() | ||
|
|
||
| if err = scheme.AddToScheme(sch); err != nil { | ||
| return nil, fmt.Errorf("failed to add base scheme: %w", err) | ||
| } | ||
|
|
||
| sch.AddKnownTypes( | ||
| schema.GroupVersion{Group: "karpenter.sh", Version: "v1"}, | ||
| &karpv1.NodePool{}, | ||
| &karpv1.NodePoolList{}, | ||
| ) | ||
| metav1.AddToGroupVersion(sch, schema.GroupVersion{Group: "karpenter.sh", Version: "v1"}) | ||
|
|
||
| sch.AddKnownTypes( | ||
| schema.GroupVersion{Group: "karpenter.k8s.aws", Version: "v1"}, | ||
| &karpawsv1.EC2NodeClass{}, | ||
| &karpawsv1.EC2NodeClassList{}, | ||
| ) | ||
| metav1.AddToGroupVersion(sch, schema.GroupVersion{Group: "karpenter.k8s.aws", Version: "v1"}) | ||
|
|
||
| restConfig, err := configFlags.ToRESTConfig() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get REST config: %w", err) | ||
| } | ||
|
|
||
| httpClient, err := rest.HTTPClientFor(restConfig) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to create http client: %w", err) | ||
| } | ||
|
|
||
| mapper, err := apiutil.NewDynamicRESTMapper(restConfig, httpClient) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to instantiate mapper: %w", err) | ||
| } | ||
|
|
||
| k8sClient, err := client.New(restConfig, client.Options{ | ||
| Scheme: sch, | ||
| Mapper: mapper, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create Karpenter client: %w", err) | ||
| } | ||
|
|
||
| return &Clients{ | ||
| Config: awsConfig, | ||
| CloudFormation: cloudformation.NewFromConfig(awsConfig), | ||
| EC2: ec2.NewFromConfig(awsConfig), | ||
| EKS: eks.NewFromConfig(awsConfig), | ||
| STS: sts.NewFromConfig(awsConfig), | ||
| K8sClient: k8sClient, | ||
| K8sClientset: k8sClientset, | ||
| }, nil | ||
| } | ||
|
|
||
| // GetClusterNameFromKubeconfig extracts the EKS cluster name from the current kubeconfig context. | ||
| func GetClusterNameFromKubeconfig(ctx context.Context, configFlags *genericclioptions.ConfigFlags) (string, error) { | ||
| kubeRawConfig, err := configFlags.ToRawKubeConfigLoader().RawConfig() | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to get raw kubeconfig: %w", err) | ||
| } | ||
|
|
||
| kubeContext := "" | ||
| if configFlags.Context != nil { | ||
| kubeContext = *configFlags.Context | ||
| } | ||
|
|
||
| return guess.GetClusterNameFromKubeconfig(ctx, kubeRawConfig, kubeContext), nil | ||
| } |
44 changes: 44 additions & 0 deletions
44
cmd/kubectl-datadog/autoscaling/cluster/common/display/display.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,44 @@ | ||
| // Package display provides utilities for formatting text output | ||
| // in the kubectl-datadog CLI tool. | ||
| package display | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/mattn/go-runewidth" | ||
| "github.com/samber/lo" | ||
| ) | ||
|
|
||
| // ansiEscapeRegex matches ANSI escape sequences (e.g., color codes). | ||
| var ansiEscapeRegex = regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`) | ||
|
|
||
| // stripANSI removes ANSI escape sequences from a string. | ||
| func stripANSI(s string) string { | ||
| return ansiEscapeRegex.ReplaceAllString(s, "") | ||
| } | ||
|
|
||
| // visualWidth returns the display width of a string in terminal columns, | ||
| // correctly handling ANSI escape sequences and wide Unicode characters. | ||
| func visualWidth(s string) int { | ||
| return runewidth.StringWidth(stripANSI(s)) | ||
| } | ||
|
|
||
| // PrintBox prints text inside a Unicode box to the given writer. | ||
| // For multiple lines, all lines are padded to the width of the longest line. | ||
| // This function correctly handles ANSI escape sequences and wide Unicode | ||
| // characters (such as emojis and East Asian characters). | ||
| func PrintBox(w io.Writer, lines ...string) { | ||
| maxWidth := lo.Max(lo.Map(lines, func(line string, _ int) int { | ||
| return visualWidth(line) | ||
| })) | ||
|
|
||
| fmt.Fprintln(w, "╭─"+strings.Repeat("─", maxWidth)+"─╮") | ||
| for _, line := range lines { | ||
| padding := maxWidth - visualWidth(line) | ||
| fmt.Fprintf(w, "│ %s%s │\n", line, strings.Repeat(" ", padding)) | ||
AlexanderYastrebov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| fmt.Fprintln(w, "╰─"+strings.Repeat("─", maxWidth)+"─╯") | ||
| } | ||
Oops, something went wrong.
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.
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