Skip to content

Commit f148c02

Browse files
committed
fix linter issues and address reviews
Signed-off-by: Erhan Cagirici <erhan@upbound.io>
1 parent 1fabbe6 commit f148c02

4 files changed

Lines changed: 39 additions & 31 deletions

File tree

config/registry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
// Note(ezgidemirel): we are importing this to embed provider schema document
1010
_ "embed"
11+
1112
"github.com/crossplane/upjet/pkg/config"
1213
"github.com/crossplane/upjet/pkg/registry/reference"
1314
conversiontfjson "github.com/crossplane/upjet/pkg/types/conversion/tfjson"

internal/clients/aws.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package clients
66

77
import (
88
"context"
9+
"k8s.io/apimachinery/pkg/runtime"
910
"reflect"
1011
"unsafe"
1112

@@ -23,7 +24,7 @@ import (
2324
)
2425

2526
const (
26-
keyAccountId = "account_id"
27+
keyAccountID = "account_id"
2728
)
2829

2930
type SetupConfig struct {
@@ -34,15 +35,15 @@ func SelectTerraformSetup(config *SetupConfig) terraform.SetupFn { // nolint:goc
3435
return func(ctx context.Context, c client.Client, mg resource.Managed) (terraform.Setup, error) {
3536
pc := &v1beta1.ProviderConfig{}
3637
if err := c.Get(ctx, types.NamespacedName{Name: mg.GetProviderConfigReference().Name}, pc); err != nil {
37-
return terraform.Setup{}, errors.Wrapf(err, "cannot get referenced Provider: %s", mg.GetProviderConfigReference().Name)
38+
return terraform.Setup{}, errors.Wrapf(err, "cannot get referenced ProviderConfig: %q", mg.GetProviderConfigReference().Name)
3839
}
3940
t := resource.NewProviderConfigUsageTracker(c, &v1beta1.ProviderConfigUsage{})
4041
if err := t.Track(ctx, mg); err != nil {
41-
return terraform.Setup{}, errors.Wrap(err, "cannot track ProviderConfig usage")
42+
return terraform.Setup{}, errors.Wrapf(err, "cannot track ProviderConfig usage for %q", mg.GetProviderConfigReference().Name)
4243
}
4344

4445
ps := terraform.Setup{}
45-
awsCfg, err := GetAWSConfigWithDefaultRegion(ctx, c, mg, pc)
46+
awsCfg, err := getAWSConfigWithDefaultRegion(ctx, c, mg, pc)
4647
if err != nil {
4748
return terraform.Setup{}, errors.Wrap(err, "cannot get aws config")
4849
} else if awsCfg == nil {
@@ -61,12 +62,12 @@ func SelectTerraformSetup(config *SetupConfig) terraform.SetupFn { // nolint:goc
6162
}
6263
}
6364
ps.ClientMetadata = map[string]string{
64-
keyAccountId: account,
65+
keyAccountID: account,
6566
}
6667
if config.TerraformProvider == nil {
6768
return terraform.Setup{}, errors.New("terraform provider cannot be nil")
6869
}
69-
return ps, errors.Wrap(configureNoForkAWSClient(ctx, &ps, config, awsCfg, creds, pc), "could not configure the no-fork AWS client")
70+
return ps, errors.Wrap(configureNoForkAWSClient(ctx, &ps, config, awsCfg.Region, creds, pc), "could not configure the no-fork AWS client")
7071
}
7172
}
7273

@@ -80,10 +81,16 @@ func getAccountId(ctx context.Context, cfg *aws.Config, creds aws.Credentials) (
8081
return *identity.Account, nil
8182
}
8283

83-
func GetAWSConfigWithDefaultRegion(ctx context.Context, c client.Client, mg resource.Managed, pc *v1beta1.ProviderConfig) (*aws.Config, error) {
84-
cfg, err := GetAWSConfigViaProviderConfig(ctx, c, mg, pc)
84+
// getAWSConfigWithDefaultRegion is a utility function that wraps the
85+
// GetAWSConfigWithoutTracking and fills empty region in the returned for
86+
// "iam.aws.upbound.io" group with a default "us-east-1" region. Although
87+
// this does not have an effect on the resource, as IAM group resources
88+
// has no concept of region, this is done to conform with the TF AWS config
89+
// which requires non-empty region
90+
func getAWSConfigWithDefaultRegion(ctx context.Context, c client.Client, obj runtime.Object, pc *v1beta1.ProviderConfig) (*aws.Config, error) {
91+
cfg, err := GetAWSConfigWithoutTracking(ctx, c, obj, pc)
8592
if err != nil {
86-
return nil, errors.Wrap(err, "cannot get AWS config")
93+
return nil, err
8794
}
8895
if cfg.Region == "" && mg.GetObjectKind().GroupVersionKind().Group == "iam.aws.upbound.io" {
8996
cfg.Region = "us-east-1"
@@ -102,22 +109,21 @@ func (m *metaOnlyPrimary) Meta() any {
102109
// configureNoForkAWSClient populates the supplied *terraform.Setup with
103110
// Terraform Plugin SDK style AWS client (Meta) and Terraform Plugin Framework
104111
// style FrameworkProvider
105-
func configureNoForkAWSClient(ctx context.Context, ps *terraform.Setup, config *SetupConfig, awsCfg *aws.Config, creds aws.Credentials, pc *v1beta1.ProviderConfig) error { //nolint:gocyclo
112+
func configureNoForkAWSClient(ctx context.Context, ps *terraform.Setup, config *SetupConfig, region string, creds aws.Credentials, pc *v1beta1.ProviderConfig) error { //nolint:gocyclo
106113
tfAwsConnsCfg := xpprovider.AWSConfig{
107-
AccessKey: creds.AccessKeyID,
108-
EC2MetadataServiceEnableState: imds.ClientDefaultEnableState,
109-
Endpoints: map[string]string{},
110-
Region: awsCfg.Region,
111-
S3UsePathStyle: pc.Spec.S3UsePathStyle,
112-
SecretKey: creds.SecretAccessKey,
113-
SkipCredsValidation: true, // disabled to prevent extra AWS STS call
114-
SkipRegionValidation: pc.Spec.SkipRegionValidation,
115-
SkipRequestingAccountId: true, // disabled to prevent extra AWS STS call
116-
Token: creds.SessionToken,
114+
AccessKey: creds.AccessKeyID,
115+
Endpoints: map[string]string{},
116+
Region: region,
117+
S3UsePathStyle: pc.Spec.S3UsePathStyle,
118+
SecretKey: creds.SecretAccessKey,
119+
SkipCredsValidation: true, // disabled to prevent extra AWS STS call
120+
SkipRegionValidation: pc.Spec.SkipRegionValidation,
121+
SkipRequestingAccountId: true, // disabled to prevent extra AWS STS call
122+
Token: creds.SessionToken,
117123
}
118124

119125
if pc.Spec.SkipMetadataApiCheck {
120-
tfAwsConnsCfg.EC2MetadataServiceEnableState = imds.ClientEnabled
126+
tfAwsConnsCfg.EC2MetadataServiceEnableState = imds.ClientDisabled
121127
}
122128

123129
if pc.Spec.Endpoint != nil {
@@ -147,7 +153,7 @@ func configureNoForkAWSClient(ctx context.Context, ps *terraform.Setup, config *
147153
// the resulting TF AWS Client has empty account ID.
148154
// Fill with previously calculated account ID.
149155
// No need for nil check on ps.ClientMetadata per golang spec.
150-
tfAwsConnsClient.AccountID = ps.ClientMetadata[keyAccountId]
156+
tfAwsConnsClient.AccountID = ps.ClientMetadata[keyAccountID]
151157
ps.Meta = tfAwsConnsClient
152158
fwProvider := xpprovider.GetFrameworkProviderWithMeta(&metaOnlyPrimary{meta: tfAwsConnsClient})
153159
ps.FrameworkProvider = fwProvider

internal/clients/provider_config.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const (
4242
authKeyWebIdentity = "WebIdentity"
4343
authKeyUpbound = "Upbound"
4444
// authKeySAML = "SAML"
45-
// authKeySecret = "Secret"
4645

4746
envWebIdentityTokenFile = "AWS_WEB_IDENTITY_TOKEN_FILE"
4847
envWebIdentityRoleARN = "AWS_ROLE_ARN"
@@ -85,10 +84,12 @@ func getRegion(obj runtime.Object) (string, error) {
8584
return r, err
8685
}
8786

88-
// GetAWSConfigViaProviderConfig produces an AWS config from the specified
89-
// v1beta1.ProviderConfig that can be used to authenticate to AWS
90-
func GetAWSConfigViaProviderConfig(ctx context.Context, c client.Client, mg resource.Managed, pc *v1beta1.ProviderConfig) (*aws.Config, error) { // nolint:gocyclo
91-
region, err := getRegion(mg)
87+
// GetAWSConfigWithoutTracking produces an AWS config from the specified
88+
// v1beta1.ProviderConfig that can be used to authenticate to AWS.
89+
// ProviderConfigUsage is not tracked when this function is called.
90+
// The caller is responsible for tracking the usage if needed.
91+
func GetAWSConfigWithoutTracking(ctx context.Context, c client.Client, obj runtime.Object, pc *v1beta1.ProviderConfig) (*aws.Config, error) { // nolint:gocyclo
92+
region, err := getRegion(obj)
9293
if err != nil {
9394
return nil, errors.Wrap(err, "cannot get region")
9495
}
@@ -127,11 +128,11 @@ func GetAWSConfigViaProviderConfig(ctx context.Context, c client.Client, mg reso
127128
return SetResolver(pc, cfg), nil
128129
}
129130

130-
// GetAWSConfigWithProviderUsage obtains the provider config referenced by the
131+
// GetAWSConfigWithTracking obtains the provider config referenced by the
131132
// specified managed resource and produces a config that can be used to
132133
// authenticate to AWS and tracks the ProviderConfigUsage. Useful for obtaining
133134
// AWS config for non-upjet based MR controllers.
134-
func GetAWSConfigWithProviderUsage(ctx context.Context, c client.Client, mg resource.Managed) (*aws.Config, error) { // nolint:gocyclo
135+
func GetAWSConfigWithTracking(ctx context.Context, c client.Client, mg resource.Managed) (*aws.Config, error) {
135136
if mg.GetProviderConfigReference() == nil {
136137
return nil, errors.New("no providerConfigRef provided")
137138
}
@@ -144,7 +145,7 @@ func GetAWSConfigWithProviderUsage(ctx context.Context, c client.Client, mg reso
144145
if err := t.Track(ctx, mg); err != nil {
145146
return nil, errors.Wrap(err, "cannot track ProviderConfig usage")
146147
}
147-
return GetAWSConfigViaProviderConfig(ctx, c, mg, pc)
148+
return GetAWSConfigWithoutTracking(ctx, c, mg, pc)
148149
}
149150

150151
type awsEndpointResolverAdaptorWithOptions func(service, region string, options interface{}) (aws.Endpoint, error)

internal/controller/eks/clusterauth/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type connector struct {
7575
}
7676

7777
func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
78-
cfg, err := clients.GetAWSConfigWithProviderUsage(ctx, c.kube, mg)
78+
cfg, err := clients.GetAWSConfigWithTracking(ctx, c.kube, mg)
7979
if err != nil {
8080
return nil, err
8181
}

0 commit comments

Comments
 (0)