Skip to content

Commit

Permalink
fix: return error for eks config creation (#452)
Browse files Browse the repository at this point in the history
* fix: return error for eks config creation

* update error message in cluster
  • Loading branch information
jokestax authored Oct 28, 2024
1 parent e56e4e2 commit dc86b4a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 19 deletions.
8 changes: 4 additions & 4 deletions extensions/aws/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// CreateEKSKubeconfig
func CreateEKSKubeconfig(awsConfig *aws.Config, clusterName string) *k8s.KubernetesClient {
func CreateEKSKubeconfig(awsConfig *aws.Config, clusterName string) (*k8s.KubernetesClient, error) {
eksSvc := eks.NewFromConfig(*awsConfig)

clusterInput := &eks.DescribeClusterInput{
Expand All @@ -30,19 +30,19 @@ func CreateEKSKubeconfig(awsConfig *aws.Config, clusterName string) *k8s.Kuberne

eksClusterInfo, err := eksSvc.DescribeCluster(context.Background(), clusterInput)
if err != nil {
return &k8s.KubernetesClient{}
return nil, fmt.Errorf("error describing cluster: %w", err)
}

clientset, restConfig, err := newEKSConfig(eksClusterInfo.Cluster)
if err != nil {
return &k8s.KubernetesClient{}
return nil, fmt.Errorf("error creating EKS config: %w", err)
}

return &k8s.KubernetesClient{
Clientset: clientset,
RestConfig: restConfig,
KubeConfigPath: "",
}
}, nil
}

// newEKSConfig
Expand Down
15 changes: 12 additions & 3 deletions internal/controller/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func (clctrl *ClusterController) InstallArgoCD() error {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "akamai", "civo", "digitalocean", "k3s", "vultr":
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
if err != nil {
Expand Down Expand Up @@ -90,7 +93,10 @@ func (clctrl *ClusterController) InitializeArgoCD() error {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "akamai", "civo", "digitalocean", "k3s", "vultr":
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
if err != nil {
Expand Down Expand Up @@ -189,7 +195,10 @@ func (clctrl *ClusterController) DeployRegistryApplication() error {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "akamai", "civo", "digitalocean", "k3s", "vultr":
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
if err != nil {
Expand Down
17 changes: 14 additions & 3 deletions internal/controller/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ func (clctrl *ClusterController) ClusterSecretsBootstrap() error {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "akamai", "civo", "digitalocean", "k3s", "vultr":
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
if err != nil {
Expand Down Expand Up @@ -393,7 +396,11 @@ func (clctrl *ClusterController) ContainerRegistryAuth() (string, error) {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
var err error
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return "", fmt.Errorf("failed to create eks config: %w", err)
}

// Container registry authentication creation
containerRegistryAuth := gitShim.ContainerRegistryAuth{
Expand Down Expand Up @@ -451,7 +458,11 @@ func (clctrl *ClusterController) WaitForClusterReady() error {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
var err error
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "civo", "digitalocean", "vultr", "k3s":
var err error
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
Expand Down
5 changes: 4 additions & 1 deletion internal/controller/kubefirst.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func (clctrl *ClusterController) ExportClusterRecord() error {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "akamai", "civo", "digitalocean", "k3s", "vultr":
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion internal/controller/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func (clctrl *ClusterController) RunUsersTerraform() error {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "akamai", "civo", "digitalocean", "k3s", "vultr":
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
if err != nil {
Expand Down
21 changes: 17 additions & 4 deletions internal/controller/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ func (clctrl *ClusterController) InitializeVault() error {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "akamai", "civo", "digitalocean", "k3s", "vultr":
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
if err != nil {
Expand Down Expand Up @@ -168,7 +171,10 @@ func (clctrl *ClusterController) RunVaultTerraform() error {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "akamai", "civo", "digitalocean", "k3s", "vultr":
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
if err != nil {
Expand Down Expand Up @@ -301,7 +307,10 @@ func (clctrl *ClusterController) WriteVaultSecrets() error {
var kcfg *k8s.KubernetesClient
switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "akamai", "civo", "digitalocean", "k3s", "vultr":
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
if err != nil {
Expand Down Expand Up @@ -385,7 +394,11 @@ func (clctrl *ClusterController) WaitForVault() error {

switch clctrl.CloudProvider {
case "aws":
kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
var err error
kcfg, err = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
case "akamai", "civo", "digitalocean", "k3s", "vultr":
var err error
kcfg, err = k8s.CreateKubeConfig(false, clctrl.ProviderConfig.Kubeconfig)
Expand Down
6 changes: 5 additions & 1 deletion providers/aws/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ func CreateAWSCluster(definition *pkgtypes.ClusterDefinition) error {
// Get Cluster kubeconfig and save to path so we can reference like everything else
// TODO replace constant references to a new config with references to an existing config created here
// for all cloud providers
ctrl.Kcfg = awsext.CreateEKSKubeconfig(&ctrl.AwsClient.Config, ctrl.ClusterName)
ctrl.Kcfg, err = awsext.CreateEKSKubeconfig(&ctrl.AwsClient.Config, ctrl.ClusterName)
if err != nil {
ctrl.UpdateClusterOnError(err.Error())
return fmt.Errorf("failed to create eks config: %w", err)
}
if err := ctrl.WaitForClusterReady(); err != nil {
ctrl.UpdateClusterOnError(err.Error())
return fmt.Errorf("error waiting for cluster to be ready: %w", err)
Expand Down
6 changes: 4 additions & 2 deletions providers/aws/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ func DeleteAWSCluster(cl *pkgtypes.Cluster, telemetryEvent telemetry.TelemetryEv
awsClient := &awsinternal.Configuration{
Config: conf,
}
kcfg := awsext.CreateEKSKubeconfig(&awsClient.Config, cl.ClusterName)

kcfg, err := awsext.CreateEKSKubeconfig(&awsClient.Config, cl.ClusterName)
if err != nil {
return fmt.Errorf("failed to create eks config: %w", err)
}
log.Info().Msg("destroying aws resources with terraform")

// Only port-forward to ArgoCD and delete registry if ArgoCD was installed
Expand Down

0 comments on commit dc86b4a

Please sign in to comment.