diff --git a/pkg/resource/cluster/aws_session.go b/pkg/resource/cluster/aws_session.go index f80b26f..dab29df 100644 --- a/pkg/resource/cluster/aws_session.go +++ b/pkg/resource/cluster/aws_session.go @@ -6,7 +6,7 @@ import ( ) func AWSSessionFromCluster(cluster *Cluster) *session.Session { - sess, _ := sdk.AWSCredsFromConfig(cluster.Region, cluster.Profile, cluster.AssumeRoleConfig) + sess, _ := sdk.AWSCredsFromValues(cluster.Region, cluster.Profile, cluster.AssumeRoleConfig) return sess } diff --git a/pkg/resource/cluster/context.go b/pkg/resource/cluster/context.go index 73675e1..2e74ea3 100644 --- a/pkg/resource/cluster/context.go +++ b/pkg/resource/cluster/context.go @@ -5,7 +5,7 @@ import ( ) func mustNewContext(cluster *Cluster) *sdk.Context { - sess, creds := sdk.AWSCredsFromConfig(cluster.Region, cluster.Profile, cluster.AssumeRoleConfig) + sess, creds := sdk.AWSCredsFromValues(cluster.Region, cluster.Profile, cluster.AssumeRoleConfig) return &sdk.Context{Sess: sess, Creds: creds} } diff --git a/pkg/resource/iamserviceaccount/context.go b/pkg/resource/iamserviceaccount/context.go index f6d72f1..b6508e4 100644 --- a/pkg/resource/iamserviceaccount/context.go +++ b/pkg/resource/iamserviceaccount/context.go @@ -5,7 +5,7 @@ import ( ) func mustContext(a *IAMServiceAccount) *sdk.Context { - sess, creds := sdk.AWSCredsFromConfig(a.Region, a.Profile, a.AssumeRoleConfig) + sess, creds := sdk.AWSCredsFromValues(a.Region, a.Profile, a.AssumeRoleConfig) return &sdk.Context{Sess: sess, Creds: creds} } diff --git a/pkg/sdk/assume_role_session.go b/pkg/sdk/assume_role_session.go index b1816b1..6c309bf 100644 --- a/pkg/sdk/assume_role_session.go +++ b/pkg/sdk/assume_role_session.go @@ -5,7 +5,11 @@ import ( "github.com/aws/aws-sdk-go/service/sts" ) -func AWSCredsFromConfig(region, profile string, assumeRole *AssumeRoleConfig) (*session.Session, *sts.Credentials) { +func AWSCredsFromConfig(conf *Config) (*session.Session, *sts.Credentials) { + return AWSCredsFromValues(conf.Region, conf.Profile, conf.AssumeRole) +} + +func AWSCredsFromValues(region, profile string, assumeRole *AssumeRoleConfig) (*session.Session, *sts.Credentials) { sess := NewSession(region, profile) if assumeRole == nil { diff --git a/pkg/sdk/config.go b/pkg/sdk/config.go new file mode 100644 index 0000000..e6724d7 --- /dev/null +++ b/pkg/sdk/config.go @@ -0,0 +1,8 @@ +package sdk + +type Config struct { + Region string + Profile string + AssumeRole *AssumeRoleConfig +} + diff --git a/pkg/sdk/job.go b/pkg/sdk/job.go index 7dd55dd..871a756 100644 --- a/pkg/sdk/job.go +++ b/pkg/sdk/job.go @@ -6,11 +6,11 @@ import ( ) type Job struct { - ContextConfigFunc func() (string, string, *AssumeRoleConfig) + Conf *Config } -func NewJob(f func() (string, string, *AssumeRoleConfig)) *Job { - return &Job{ContextConfigFunc: f} +func NewJob(conf *Config) *Job { + return &Job{Conf: conf} } func (s *Job) Task(name string, f func(*Context) error) (err error) { @@ -35,9 +35,14 @@ func (s *Job) Task(name string, f func(*Context) error) (err error) { } func (s *Job) newContext() *Context { - region, profile, assumeRoleConfig := s.ContextConfigFunc() + return ContextConfig(s.Conf) +} - sess, creds := AWSCredsFromConfig(region, profile, assumeRoleConfig) +func ContextConfig(conf *Config) *Context { + sess, creds := AWSCredsFromConfig(conf) - return &Context{Sess: sess, Creds: creds} + return &Context{ + Sess: sess, + Creds: creds, + } } diff --git a/pkg/sdk/run.go b/pkg/sdk/run.go index 123e7ad..7be23a4 100644 --- a/pkg/sdk/run.go +++ b/pkg/sdk/run.go @@ -69,8 +69,8 @@ func Run(cmd *exec.Cmd) (*CommandResult, error) { // so that helmfile could return its own exit code accordingly waitStatus := ee.Sys().(syscall.WaitStatus) exitStatus = waitStatus.ExitStatus() - if exitStatus != 0 { - return nil, fmt.Errorf("running %q: %v\n%s", cmdToLog, runErr, out) + if exitStatus != 2 { + return nil, fmt.Errorf("%s: %v\n%s", cmd.Path, runErr, out) } default: return nil, fmt.Errorf("running %q: %v\n%s", cmdToLog, runErr, out) diff --git a/pkg/sdk/tfsdk/assume_role_config_get.go b/pkg/sdk/tfsdk/assume_role_config_get.go index d8cf5bf..1e56c22 100644 --- a/pkg/sdk/tfsdk/assume_role_config_get.go +++ b/pkg/sdk/tfsdk/assume_role_config_get.go @@ -7,8 +7,10 @@ import ( "log" ) -func GetAssumeRoleConfig(d api.Getter) (config *sdk.AssumeRoleConfig) { - if l, ok := d.Get(KeyAssumeRole).([]interface{}); ok && len(l) > 0 && l[0] != nil { +func GetAssumeRoleConfig(d api.Getter, opts ...SchemaOption) (config *sdk.AssumeRoleConfig) { + sc := CreateSchema(opts...) + + if l, ok := d.Get(sc.KeyAWSAssumeRole).([]interface{}); ok && len(l) > 0 && l[0] != nil { config = &sdk.AssumeRoleConfig{} m := l[0].(map[string]interface{}) diff --git a/pkg/sdk/tfsdk/aws_env_get.go b/pkg/sdk/tfsdk/aws_env_get.go new file mode 100644 index 0000000..29a2b1f --- /dev/null +++ b/pkg/sdk/tfsdk/aws_env_get.go @@ -0,0 +1,18 @@ +package tfsdk + +import ( + "github.com/mumoshu/terraform-provider-eksctl/pkg/sdk" + "github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api" +) + +func ConfigFromResourceData(d api.Getter, opts ...SchemaOption) *sdk.Config { + region, profile := GetAWSRegionAndProfile(d, opts...) + + assumeRoleConfig := GetAssumeRoleConfig(d, opts...) + + return &sdk.Config{ + Region: region, + Profile: profile, + AssumeRole: assumeRoleConfig, + } +} diff --git a/pkg/sdk/tfsdk/aws_region_profile_get.go b/pkg/sdk/tfsdk/aws_region_profile_get.go index 08492e8..32109a3 100644 --- a/pkg/sdk/tfsdk/aws_region_profile_get.go +++ b/pkg/sdk/tfsdk/aws_region_profile_get.go @@ -2,16 +2,18 @@ package tfsdk import "github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api" -func GetAWSRegionAndProfile(d api.Getter) (string, string) { +func GetAWSRegionAndProfile(d api.Getter, opts ...SchemaOption) (string, string) { + schema := CreateSchema(opts...) + var region string - if v := d.Get(KeyRegion); v != nil { + if v := d.Get(schema.KeyAWSRegion); v != nil { region = v.(string) } var profile string - if v := d.Get(KeyProfile); v != nil { + if v := d.Get(schema.KeyAWSProfile); v != nil { profile = v.(string) } diff --git a/pkg/sdk/tfsdk/aws_session_get.go b/pkg/sdk/tfsdk/aws_session_get.go index 1199dd5..7ba7115 100644 --- a/pkg/sdk/tfsdk/aws_session_get.go +++ b/pkg/sdk/tfsdk/aws_session_get.go @@ -6,12 +6,12 @@ import ( "github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api" ) -func AWSSessionFromResourceData(d api.Getter) *session.Session { - region, profile := GetAWSRegionAndProfile(d) +func AWSSessionFromResourceData(d api.Getter, opts ...SchemaOption) *session.Session { + region, profile := GetAWSRegionAndProfile(d, opts...) sess := sdk.NewSession(region, profile) - assumeRoleConfig := GetAssumeRoleConfig(d) + assumeRoleConfig := GetAssumeRoleConfig(d, opts...) if assumeRoleConfig == nil { return sess } diff --git a/pkg/sdk/tfsdk/schema.go b/pkg/sdk/tfsdk/schema.go new file mode 100644 index 0000000..52b981d --- /dev/null +++ b/pkg/sdk/tfsdk/schema.go @@ -0,0 +1,65 @@ +package tfsdk + +type Schema struct { + KeyAWSRegion string + KeyAWSProfile string + KeyAWSAssumeRole string +} + +func defaultSchema() *Schema { + return &Schema{ + KeyAWSRegion: KeyRegion, + KeyAWSProfile: KeyProfile, + KeyAWSAssumeRole: KeyAssumeRole, + } +} + +type SchemaOption interface { + Apply(*Schema) +} + +func (s *Schema) Apply(other *Schema) { + *other = *s +} + +type schemaOptionFunc struct { + f func(*Schema) +} + +func (f *schemaOptionFunc) Apply(schema *Schema) { + f.f(schema) +} + +func SchemaOptionFunc(f func(*Schema)) SchemaOption { + return &schemaOptionFunc{ + f: f, + } +} + +func SchemaOptionAWSRegionKey(k string) SchemaOption { + return SchemaOptionFunc(func(schema *Schema) { + schema.KeyAWSRegion = k + }) +} + +func SchemaOptionAWSProfileKey(k string) SchemaOption { + return SchemaOptionFunc(func(schema *Schema) { + schema.KeyAWSProfile = k + }) +} + +func SchemaOptionAWSAssumeRole(k string) SchemaOption { + return SchemaOptionFunc(func(schema *Schema) { + schema.KeyAWSAssumeRole = k + }) +} + +func CreateSchema(opts ...SchemaOption) *Schema { + schema := defaultSchema() + + for _, o := range opts { + o.Apply(schema) + } + + return schema +}