diff --git a/cmd/kubectl-datadog/autoscaling/cluster/install/assets/dd-karpenter-fargate.yaml b/cmd/kubectl-datadog/autoscaling/cluster/apply/assets/dd-karpenter-fargate.yaml similarity index 100% rename from cmd/kubectl-datadog/autoscaling/cluster/install/assets/dd-karpenter-fargate.yaml rename to cmd/kubectl-datadog/autoscaling/cluster/apply/assets/dd-karpenter-fargate.yaml diff --git a/cmd/kubectl-datadog/autoscaling/cluster/install/assets/dd-karpenter.yaml b/cmd/kubectl-datadog/autoscaling/cluster/apply/assets/dd-karpenter.yaml similarity index 100% rename from cmd/kubectl-datadog/autoscaling/cluster/install/assets/dd-karpenter.yaml rename to cmd/kubectl-datadog/autoscaling/cluster/apply/assets/dd-karpenter.yaml diff --git a/cmd/kubectl-datadog/autoscaling/cluster/install/assets/karpenter.yaml b/cmd/kubectl-datadog/autoscaling/cluster/apply/assets/karpenter.yaml similarity index 100% rename from cmd/kubectl-datadog/autoscaling/cluster/install/assets/karpenter.yaml rename to cmd/kubectl-datadog/autoscaling/cluster/apply/assets/karpenter.yaml diff --git a/cmd/kubectl-datadog/autoscaling/cluster/install/create_karpenter_resources.go b/cmd/kubectl-datadog/autoscaling/cluster/apply/create_karpenter_resources.go similarity index 98% rename from cmd/kubectl-datadog/autoscaling/cluster/install/create_karpenter_resources.go rename to cmd/kubectl-datadog/autoscaling/cluster/apply/create_karpenter_resources.go index abb8505ce7..2438362830 100644 --- a/cmd/kubectl-datadog/autoscaling/cluster/install/create_karpenter_resources.go +++ b/cmd/kubectl-datadog/autoscaling/cluster/apply/create_karpenter_resources.go @@ -1,4 +1,4 @@ -package install +package apply import "fmt" diff --git a/cmd/kubectl-datadog/autoscaling/cluster/install/create_karpenter_resources_test.go b/cmd/kubectl-datadog/autoscaling/cluster/apply/create_karpenter_resources_test.go similarity index 99% rename from cmd/kubectl-datadog/autoscaling/cluster/install/create_karpenter_resources_test.go rename to cmd/kubectl-datadog/autoscaling/cluster/apply/create_karpenter_resources_test.go index 0a95cdbf96..54cb50439b 100644 --- a/cmd/kubectl-datadog/autoscaling/cluster/install/create_karpenter_resources_test.go +++ b/cmd/kubectl-datadog/autoscaling/cluster/apply/create_karpenter_resources_test.go @@ -1,4 +1,4 @@ -package install +package apply import ( "testing" diff --git a/cmd/kubectl-datadog/autoscaling/cluster/install/inference_method.go b/cmd/kubectl-datadog/autoscaling/cluster/apply/inference_method.go similarity index 98% rename from cmd/kubectl-datadog/autoscaling/cluster/install/inference_method.go rename to cmd/kubectl-datadog/autoscaling/cluster/apply/inference_method.go index cff35f9412..0a26feee0b 100644 --- a/cmd/kubectl-datadog/autoscaling/cluster/install/inference_method.go +++ b/cmd/kubectl-datadog/autoscaling/cluster/apply/inference_method.go @@ -1,4 +1,4 @@ -package install +package apply import "fmt" diff --git a/cmd/kubectl-datadog/autoscaling/cluster/install/inference_method_test.go b/cmd/kubectl-datadog/autoscaling/cluster/apply/inference_method_test.go similarity index 99% rename from cmd/kubectl-datadog/autoscaling/cluster/install/inference_method_test.go rename to cmd/kubectl-datadog/autoscaling/cluster/apply/inference_method_test.go index 46b20bd0fa..e958b2ba49 100644 --- a/cmd/kubectl-datadog/autoscaling/cluster/install/inference_method_test.go +++ b/cmd/kubectl-datadog/autoscaling/cluster/apply/inference_method_test.go @@ -1,4 +1,4 @@ -package install +package apply import ( "testing" diff --git a/cmd/kubectl-datadog/autoscaling/cluster/install/install_mode.go b/cmd/kubectl-datadog/autoscaling/cluster/apply/install_mode.go similarity index 98% rename from cmd/kubectl-datadog/autoscaling/cluster/install/install_mode.go rename to cmd/kubectl-datadog/autoscaling/cluster/apply/install_mode.go index 47239c3581..1b87047b7c 100644 --- a/cmd/kubectl-datadog/autoscaling/cluster/install/install_mode.go +++ b/cmd/kubectl-datadog/autoscaling/cluster/apply/install_mode.go @@ -1,4 +1,4 @@ -package install +package apply import "fmt" diff --git a/cmd/kubectl-datadog/autoscaling/cluster/install/install_mode_test.go b/cmd/kubectl-datadog/autoscaling/cluster/apply/install_mode_test.go similarity index 98% rename from cmd/kubectl-datadog/autoscaling/cluster/install/install_mode_test.go rename to cmd/kubectl-datadog/autoscaling/cluster/apply/install_mode_test.go index acde9f6e62..e57fb4c129 100644 --- a/cmd/kubectl-datadog/autoscaling/cluster/install/install_mode_test.go +++ b/cmd/kubectl-datadog/autoscaling/cluster/apply/install_mode_test.go @@ -1,4 +1,4 @@ -package install +package apply import ( "testing" diff --git a/cmd/kubectl-datadog/autoscaling/cluster/install/steps.go b/cmd/kubectl-datadog/autoscaling/cluster/apply/run.go similarity index 66% rename from cmd/kubectl-datadog/autoscaling/cluster/install/steps.go rename to cmd/kubectl-datadog/autoscaling/cluster/apply/run.go index 4dfe380465..1d3e7b3db0 100644 --- a/cmd/kubectl-datadog/autoscaling/cluster/install/steps.go +++ b/cmd/kubectl-datadog/autoscaling/cluster/apply/run.go @@ -1,24 +1,27 @@ -package install +// Package apply contains the convergence logic shared between the `install` +// and `update` cobra commands. Both subcommands ultimately call Run with a +// RunOptions describing the desired Karpenter deployment; Run creates or +// updates the CloudFormation stacks, the Helm release, the aws-auth role, the +// optional EC2NodeClass/NodePool resources and the cluster-info ConfigMap. +package apply import ( "context" - "errors" "fmt" "log" "net/url" - "os/signal" "slices" "strconv" "strings" - "syscall" awssdk "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/eks" "github.com/davecgh/go-spew/spew" "github.com/fatih/color" "github.com/pkg/browser" - "github.com/spf13/cobra" "helm.sh/helm/v3/pkg/registry" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/client-go/kubernetes" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/log/zap" @@ -27,8 +30,8 @@ import ( "github.com/DataDog/datadog-operator/cmd/kubectl-datadog/autoscaling/cluster/common/clusterinfo" "github.com/DataDog/datadog-operator/cmd/kubectl-datadog/autoscaling/cluster/common/display" "github.com/DataDog/datadog-operator/cmd/kubectl-datadog/autoscaling/cluster/common/helm" - "github.com/DataDog/datadog-operator/cmd/kubectl-datadog/autoscaling/cluster/install/guess" - "github.com/DataDog/datadog-operator/cmd/kubectl-datadog/autoscaling/cluster/install/k8s" + "github.com/DataDog/datadog-operator/cmd/kubectl-datadog/autoscaling/cluster/guess" + "github.com/DataDog/datadog-operator/cmd/kubectl-datadog/autoscaling/cluster/k8s" "github.com/DataDog/datadog-operator/pkg/version" _ "embed" @@ -49,117 +52,149 @@ var ( DdKarpenterFargateCfn string ) -// installModeTagKey is the CloudFormation stack tag tracking the deployment's -// install-mode. Stacks created before this tag was introduced are treated as -// install-mode=existing-nodes. -const installModeTagKey = "install-mode" - -func (o *options) run(cmd *cobra.Command) error { - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) - defer stop() - - log.SetOutput(cmd.OutOrStderr()) - ctrl.SetLogger(zap.New(zap.UseDevMode(false), zap.WriteTo(cmd.ErrOrStderr()))) - - if clusterName == "" { - if name, err := clients.GetClusterNameFromKubeconfig(o.ConfigFlags); err != nil { - return err - } else if name != "" { - clusterName = name - } else { - return errors.New("cluster name must be specified either via --cluster-name or in the current kubeconfig context") - } +// RunOptions captures every parameter that callers (the install and update +// cobra commands) feed into Run. +type RunOptions struct { + ClusterName string + KarpenterNamespace string + KarpenterVersion string + InstallMode InstallMode + FargateSubnets []string + CreateKarpenterResources CreateKarpenterResources + InferenceMethod InferenceMethod + Debug bool + // ActionLabel prefixes the opening "