Skip to content

Commit

Permalink
Extract more parts into the sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Jan 24, 2021
1 parent b1343e0 commit 52d0d0f
Show file tree
Hide file tree
Showing 24 changed files with 347 additions and 286 deletions.
8 changes: 4 additions & 4 deletions pkg/courier/courier_r53_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
"log"
Expand All @@ -16,7 +16,7 @@ import (
func CreateOrUpdateCourierRoute53Record(d api.Getter, mSchema *MetricSchema) error {
ctx := context.Background()

sess := sdk.AWSSessionFromResourceData(d)
sess := tfsdk.AWSSessionFromResourceData(d)

if v := d.Get("address"); v != nil {
sess.Config.Endpoint = aws.String(v.(string))
Expand All @@ -33,7 +33,7 @@ func CreateOrUpdateCourierRoute53Record(d api.Getter, mSchema *MetricSchema) err
return xerrors.Errorf("calling route53.GetHostedZone: %w", err)
}

region, profile := sdk.GetAWSRegionAndProfile(d)
region, profile := tfsdk.GetAWSRegionAndProfile(d)

recordName := d.Get("name").(string)

Expand Down Expand Up @@ -74,7 +74,7 @@ func CreateOrUpdateCourierRoute53Record(d api.Getter, mSchema *MetricSchema) err
stepWeight = v.(int)
}

assumeRoleConfig := sdk.GetAssumeRoleConfig(d)
assumeRoleConfig := tfsdk.GetAssumeRoleConfig(d)

r := &Route53RecordSetRouter{
Service: svc,
Expand Down
8 changes: 4 additions & 4 deletions pkg/courier/read_alb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package courier

import (
"fmt"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
"golang.org/x/xerrors"
"strconv"
"time"
Expand All @@ -28,14 +28,14 @@ type ALBSchema struct {
}

func ReadCourierALB(d api.Lister, schema *ALBSchema, metricSchema *MetricSchema) (*CourierALB, error) {
region, profile := sdk.GetAWSRegionAndProfile(d)
region, profile := tfsdk.GetAWSRegionAndProfile(d)

sess := sdk.AWSSessionFromResourceData(d)
sess := tfsdk.AWSSessionFromResourceData(d)

conf := CourierALB{
Region: region,
Profile: profile,
AssumeRoleConfig: sdk.GetAssumeRoleConfig(d),
AssumeRoleConfig: tfsdk.GetAssumeRoleConfig(d),
Session: sess,
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/provider/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
)

Expand All @@ -13,7 +12,7 @@ type ProviderInstance struct {

func providerConfigure() func(*schema.ResourceData) (interface{}, error) {
return func(d *schema.ResourceData) (interface{}, error) {
s := sdk.AWSSessionFromResourceData(&tfsdk.Resource{d})
s := tfsdk.AWSSessionFromResourceData(&tfsdk.Resource{d})

return &ProviderInstance{
AWSSession: s,
Expand Down
3 changes: 1 addition & 2 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/mumoshu/terraform-provider-eksctl/pkg/resource/cluster"
"github.com/mumoshu/terraform-provider-eksctl/pkg/resource/courier"
"github.com/mumoshu/terraform-provider-eksctl/pkg/resource/iamserviceaccount"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
)

Expand All @@ -16,7 +15,7 @@ func Provider() terraform.ResourceProvider {
// The actual provider
return &schema.Provider{
Schema: map[string]*schema.Schema{
sdk.KeyAssumeRole: tfsdk.SchemaAssumeRole(),
tfsdk.KeyAssumeRole: tfsdk.SchemaAssumeRole(),
},
ResourcesMap: map[string]*schema.Resource{
"eksctl_cluster": cluster.ResourceCluster(),
Expand Down
1 change: 0 additions & 1 deletion pkg/resource/cluster/aws_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ func AWSSessionFromCluster(cluster *Cluster) *session.Session {

return sess
}

106 changes: 2 additions & 104 deletions pkg/resource/cluster/binary_eksctl.go
Original file line number Diff line number Diff line change
@@ -1,111 +1,9 @@
package cluster

import (
"bufio"
"fmt"
"github.com/mumoshu/shoal"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
"io"
"log"
"path/filepath"
"sync"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk"
)

var prepareEksctlMu sync.Mutex

func prepareEksctlBinary(cluster *Cluster) (*string, error) {
return prepareEksctlBinaryInternal(cluster.EksctlBin, cluster.EksctlVersion)
}

func prepareEksctlBinaryInternal(eksctlBin, eksctlVersion string) (*string, error) {
log.Print("Preparing eksctl binary")

conf := shoal.Config{
Git: shoal.Git{
Provider: "go-git",
},
}

rig := "https://github.com/fishworks/fish-food"

installEksctl := eksctlVersion != ""

if installEksctl {
log.Printf("Installing eksctl %s", eksctlVersion)

conf.Dependencies = append(conf.Dependencies,
shoal.Dependency{
Rig: rig,
Food: "eksctl",
Version: eksctlVersion,
},
)
}

log.Print("Started taking exclusive lock on shoal")

prepareEksctlMu.Lock()
defer prepareEksctlMu.Unlock()

log.Print("Took exclusive lock on shoal")

logReader, logWriter := io.Pipe()

s, err := shoal.New(shoal.LogOutput(logWriter))
if err != nil {
return nil, err
}

log.Print("Shoal instance created")

if len(conf.Dependencies) > 0 {
eg := errgroup.Group{}

scanner := bufio.NewScanner(logReader)

eg.Go(func() error {
for scanner.Scan() {
log.Printf("shoal] %s", scanner.Text())
}

return nil
})

if err := s.Init(); err != nil {
return nil, fmt.Errorf("initializing shoal: %w", err)
}

log.Print("Shoal initialized")

if err := s.InitGitProvider(conf); err != nil {
return nil, fmt.Errorf("initializing shoal git provider: %w", err)
}

log.Print("Shoal's Git provider initialized")

eg.Go(func() error {
defer logWriter.Close()

if err := s.Sync(conf); err != nil {
return xerrors.Errorf("running shoal-sync: %w", err)
}

return nil
})

if err := eg.Wait(); err != nil {
return nil, xerrors.Errorf("calling shoal: %w", err)
}

log.Println("Shoal sync finished")
}

binPath := s.BinPath()

if eksctlVersion != "" {
eksctlBin = filepath.Join(binPath, "eksctl")
}

return &eksctlBin, nil
return sdk.PrepareExecutable(cluster.EksctlBin, "eksctl", cluster.EksctlVersion)
}
9 changes: 5 additions & 4 deletions pkg/resource/cluster/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
"io/ioutil"
"log"
"os"
Expand All @@ -13,7 +14,7 @@ import (
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
)

func (m *Manager) createCluster(d *schema.ResourceData) (*ClusterSet, error) {
id := newClusterID()
Expand Down Expand Up @@ -67,7 +68,7 @@ func (m *Manager) createCluster(d *schema.ResourceData) (*ClusterSet, error) {
return set, nil
}

func (m *Manager) doPlanKubeconfig(d *DiffReadWrite) error {
func (m *Manager) doPlanKubeconfig(d *tfsdk.DiffReadWrite) error {
var path string

if v := d.Get(KeyKubeconfigPath); v != nil {
Expand All @@ -81,7 +82,7 @@ func (m *Manager) doPlanKubeconfig(d *DiffReadWrite) error {
return nil
}

func doWriteKubeconfig(ctx *sdk.Context, d ReadWrite, clusterName, region string) error {
func doWriteKubeconfig(ctx *sdk.Context, d api.ReadWrite, clusterName, region string) error {
var path string

if v := d.Get(KeyKubeconfigPath); v != nil {
Expand Down Expand Up @@ -141,7 +142,7 @@ func doWriteKubeconfig(ctx *sdk.Context, d ReadWrite, clusterName, region string
return nil
}

func createIAMIdentityMapping(ctx *sdk.Context, d ReadWrite, cluster *Cluster) error {
func createIAMIdentityMapping(ctx *sdk.Context, d api.ReadWrite, cluster *Cluster) error {
iams, err := runGetIAMIdentityMapping(ctx, d, cluster)
if err != nil {
return fmt.Errorf("can not get iamidentitymapping from eks cluster: %w", err)
Expand Down
43 changes: 6 additions & 37 deletions pkg/resource/cluster/cluster_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
"golang.org/x/xerrors"
"log"
"os"
Expand All @@ -15,39 +16,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

type ReadWrite interface {
api.Getter

Id() string

Set(string, interface{}) error
}

type DiffReadWrite struct {
D *schema.ResourceDiff
}

func (d *DiffReadWrite) Get(k string) interface{} {
return d.D.Get(k)
}

func (d *DiffReadWrite) List(k string) []interface{} {
return nil
}

func (d *DiffReadWrite) Set(k string, v interface{}) error {
return d.D.SetNew(k, v)
}

func (d *DiffReadWrite) SetNewComputed(k string) error {
return d.D.SetNewComputed(k)
}

func (d *DiffReadWrite) Id() string {
return d.D.Id()
}

func (m *Manager) readCluster(d ReadWrite) (*Cluster, error) {
func (m *Manager) readCluster(d api.ReadWrite) (*Cluster, error) {
cluster, err := m.readClusterInternal(d)

ctx := mustNewContext(cluster)
Expand Down Expand Up @@ -85,7 +54,7 @@ func (m *Manager) readCluster(d ReadWrite) (*Cluster, error) {
func (m *Manager) readClusterInternal(d api.Resource) (*Cluster, error) {
clusterNamePrefix := d.Get("name").(string)

sess := sdk.AWSSessionFromResourceData(d)
sess := tfsdk.AWSSessionFromResourceData(d)

arns, err := getTargetGroupARNs(sess, clusterNamePrefix)
if err != nil {
Expand All @@ -110,7 +79,7 @@ func (m *Manager) readClusterInternal(d api.Resource) (*Cluster, error) {
return c, err
}

func (m *Manager) planCluster(d *DiffReadWrite) error {
func (m *Manager) planCluster(d *tfsdk.DiffReadWrite) error {
_, err := m.readClusterInternal(d)
if err != nil {
return err
Expand All @@ -123,7 +92,7 @@ func (m *Manager) planCluster(d *DiffReadWrite) error {
return nil
}

func readIAMIdentityMapping(ctx *sdk.Context, d ReadWrite, cluster *Cluster) error {
func readIAMIdentityMapping(ctx *sdk.Context, d api.ReadWrite, cluster *Cluster) error {
iamWithOIDCEnabled, err := cluster.IAMWithOIDCEnabled()
if err != nil {
return fmt.Errorf("reading iam.withOIDC setting from cluster.yaml: %w", err)
Expand Down Expand Up @@ -188,7 +157,7 @@ func runGetIAMIdentityMapping(ctx *sdk.Context, d api.Getter, cluster *Cluster)
return iams, nil
}

func loadOIDCProviderURLAndARN(d ReadWrite, cluster *Cluster) error {
func loadOIDCProviderURLAndARN(d api.ReadWrite, cluster *Cluster) error {
iamWithOIDCEnabled, err := cluster.IAMWithOIDCEnabled()
if err != nil {
return fmt.Errorf("reading iam.withOIDC setting from cluster.yaml: %w", err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/resource/cluster/command_eksctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import (
"fmt"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
"os/exec"
)

func newEksctlCommandFromResourceWithRegionAndProfile(resource api.Getter, args ...string) (*exec.Cmd, error) {
eksctlBin := resource.Get(KeyBin).(string)
eksctlVersion := resource.Get(KeyEksctlVersion).(string)

bin, err := prepareEksctlBinaryInternal(eksctlBin, eksctlVersion)
bin, err := sdk.PrepareExecutable(eksctlBin, "eksctl", eksctlVersion)
if err != nil {
return nil, fmt.Errorf("preparing eksctl binary: %w", err)
}

region, profile := sdk.GetAWSRegionAndProfile(resource)
region, profile := tfsdk.GetAWSRegionAndProfile(resource)

if region != "" {
args = append(args, "--region", region)
Expand Down
4 changes: 2 additions & 2 deletions pkg/resource/cluster/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ResourceCluster() *schema.Resource {
}
}()

if err := m.planCluster(&DiffReadWrite{D: d}); err != nil {
if err := m.planCluster(&tfsdk.DiffReadWrite{D: d}); err != nil {
return fmt.Errorf("diffing cluster: %w", err)
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func ResourceCluster() *schema.Resource {
Optional: true,
Default: "",
},
sdk.KeyAssumeRole: tfsdk.SchemaAssumeRole(),
tfsdk.KeyAssumeRole: tfsdk.SchemaAssumeRole(),
KeyName: {
Type: schema.TypeString,
Required: true,
Expand Down
Loading

0 comments on commit 52d0d0f

Please sign in to comment.