Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/ctrlc/root/sync/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/MakeNowJust/heredoc/v2"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/ec2"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/eks"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/networks"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/rds"
"github.com/ctrlplanedev/cli/internal/cliutil"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,5 +34,6 @@ func NewAWSCmd() *cobra.Command {
cmd.AddCommand(cliutil.AddIntervalSupport(ec2.NewSyncEC2Cmd(), ""))
cmd.AddCommand(cliutil.AddIntervalSupport(eks.NewSyncEKSCmd(), ""))
cmd.AddCommand(cliutil.AddIntervalSupport(rds.NewSyncRDSCmd(), ""))
cmd.AddCommand(cliutil.AddIntervalSupport(networks.NewSyncNetworksCmd(), ""))
return cmd
}
42 changes: 42 additions & 0 deletions cmd/ctrlc/root/sync/aws/common/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package common

import (
"context"
"fmt"
"github.com/charmbracelet/log"
"strings"
)

// EnsureProviderDetails generates a provider name and region string based on the provided parameters.
// The
func EnsureProviderDetails(
ctx context.Context, prefix string, regions []string, name *string,
) {
providerRegion := "all-regions"
// Use regions for name if none provided
if regions != nil && len(regions) > 0 {
providerRegion = strings.Join(regions, "-")
}

// If name is not provided, try to get account ID to include in the provider name
if name == nil {
name = new(string)
}
if *name == "" {
// Get AWS account ID for provider name using common package
cfg, err := InitAWSConfig(ctx, regions[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Potential panic with empty regions slice.

The function tries to access regions[0] without checking if the slice has any elements, which could cause a panic if regions is empty.

-		cfg, err := InitAWSConfig(ctx, regions[0])
+		// Default to first region, but handle empty regions case
+		region := "us-east-1" // Default region
+		if len(regions) > 0 {
+			region = regions[0]
+		}
+		cfg, err := InitAWSConfig(ctx, region)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
cfg, err := InitAWSConfig(ctx, regions[0])
// Default to first region, but handle empty regions case
region := "us-east-1" // Default region
if len(regions) > 0 {
region = regions[0]
}
cfg, err := InitAWSConfig(ctx, region)

if err != nil {
log.Warn("Failed to load AWS config for account ID retrieval", "error", err)
*name = fmt.Sprintf("%s-%s", prefix, providerRegion)
} else {
accountID, err := GetAccountID(ctx, cfg)
if err == nil {
log.Info("Retrieved AWS account ID", "account_id", accountID)
*name = fmt.Sprintf("%s-%s-%s", prefix, accountID, providerRegion)
} else {
log.Warn("Failed to get AWS account ID", "error", err)
*name = fmt.Sprintf("%s-%s", prefix, providerRegion)
}
}
}
}
47 changes: 10 additions & 37 deletions cmd/ctrlc/root/sync/aws/eks/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,33 +113,10 @@ func runSync(regions *[]string, name *string) func(cmd *cobra.Command, args []st
return nil
}

// Use regions for name if none provided
providerRegion := "all-regions"
if regions != nil && len(*regions) > 0 {
providerRegion = strings.Join(*regions, "-")
}

// If name is not provided, try to get account ID to include in the provider name
if *name == "" {
// Get AWS account ID for provider name using common package
cfg, err := common.InitAWSConfig(ctx, regionsToSync[0])
if err != nil {
log.Warn("Failed to load AWS config for account ID retrieval", "error", err)
*name = fmt.Sprintf("aws-eks-%s", providerRegion)
} else {
accountID, err := common.GetAccountID(ctx, cfg)
if err == nil {
log.Info("Retrieved AWS account ID", "account_id", accountID)
*name = fmt.Sprintf("aws-eks-%s-%s", accountID, providerRegion)
} else {
log.Warn("Failed to get AWS account ID", "error", err)
*name = fmt.Sprintf("aws-eks-%s", providerRegion)
}
}
}
common.EnsureProviderDetails(ctx, "aws-eks", regionsToSync, name)

// Upsert resources to Ctrlplane
return upsertToCtrlplane(ctx, allResources, &providerRegion, name)
return upsertToCtrlplane(ctx, allResources, name)
}
}

Expand Down Expand Up @@ -239,18 +216,18 @@ func initClusterMetadata(cluster *types.Cluster, region string) map[string]strin
log.Error("Failed to parse Kubernetes version", "version", *cluster.Version, "error", err)
}

noramlizedStatus := "unknown"
normalizedStatus := "unknown"
switch cluster.Status {
case types.ClusterStatusActive:
noramlizedStatus = "running"
normalizedStatus = "running"
case types.ClusterStatusUpdating:
noramlizedStatus = "updating"
normalizedStatus = "updating"
case types.ClusterStatusCreating:
noramlizedStatus = "creating"
normalizedStatus = "creating"
case types.ClusterStatusDeleting:
noramlizedStatus = "deleting"
normalizedStatus = "deleting"
case types.ClusterStatusFailed:
noramlizedStatus = "failed"
normalizedStatus = "failed"
}

metadata := map[string]string{
Expand All @@ -264,7 +241,7 @@ func initClusterMetadata(cluster *types.Cluster, region string) map[string]strin
kinds.K8SMetadataVersionMinor: strconv.FormatUint(uint64(version.Minor()), 10),
kinds.K8SMetadataVersionPatch: strconv.FormatUint(uint64(version.Patch()), 10),
kinds.K8SMetadataVersionPrerelease: version.Prerelease(),
kinds.K8SMetadataStatus: noramlizedStatus,
kinds.K8SMetadataStatus: normalizedStatus,

"aws/region": region,
"aws/resource-type": "eks:cluster",
Expand Down Expand Up @@ -320,11 +297,7 @@ var relationshipRules = []api.CreateResourceRelationshipRule{
},
}

func upsertToCtrlplane(ctx context.Context, resources []api.CreateResource, region, name *string) error {
if *name == "" {
*name = fmt.Sprintf("aws-eks-%s", *region)
}

func upsertToCtrlplane(ctx context.Context, resources []api.CreateResource, name *string) error {
apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspaceId := viper.GetString("workspace")
Expand Down
Loading