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
46 changes: 39 additions & 7 deletions internal/workflows/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,64 @@ import (
"fmt"

"github.com/Amertz08/gitops-example/internal/activities"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
)

const eksTrustPolicy = `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"eks.amazonaws.com"},"Action":"sts:AssumeRole"}]}`

const ec2TrustPolicy = `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}`

type SpinUpIAMInput struct {
type SpinUpEKSIAMInput struct {
Region string
ClusterName string
Environment string
Team string
}

type SpinUpIAMOutput struct {
func (i SpinUpEKSIAMInput) validate() error {
switch {
case i.ClusterName == "":
return fmt.Errorf("ClusterName is required")
case i.Environment == "":
return fmt.Errorf("Environment is required")
case i.Team == "":
return fmt.Errorf("Team is required")
}
return nil
}

type SpinUpEKSIAMOutput struct {
ClusterRoleARN string
ClusterRoleName string
NodeRoleARN string
NodeRoleName string
}

type SpinDownIAMInput struct {
type SpinDownEKSIAMInput struct {
ClusterRoleName string
NodeRoleName string
}

func (i SpinDownEKSIAMInput) validate() error {
switch {
case i.ClusterRoleName == "":
return fmt.Errorf("ClusterRoleName is required")
case i.NodeRoleName == "":
return fmt.Errorf("NodeRoleName is required")
}
return nil
}

func SpinUpIAMWorkflow(
ctx workflow.Context,
input SpinUpIAMInput,
) (output SpinUpIAMOutput, err error) {
input SpinUpEKSIAMInput,
) (output SpinUpEKSIAMOutput, err error) {
if valErr := input.validate(); valErr != nil {
err = temporal.NewNonRetryableApplicationError(valErr.Error(), "InvalidInput", valErr)
return
}

ctx = workflow.WithActivityOptions(ctx, activityOptions)
aws := &activities.AWSActivities{}
logger := workflow.GetLogger(ctx)
Expand Down Expand Up @@ -103,7 +131,7 @@ func SpinUpIAMWorkflow(
}
})

output = SpinUpIAMOutput{
output = SpinUpEKSIAMOutput{
ClusterRoleARN: clusterRoleARN,
ClusterRoleName: clusterRoleName,
NodeRoleARN: nodeRoleARN,
Expand All @@ -112,7 +140,11 @@ func SpinUpIAMWorkflow(
return
}

func SpinDownIAMWorkflow(ctx workflow.Context, input SpinDownIAMInput) error {
func SpinDownIAMWorkflow(ctx workflow.Context, input SpinDownEKSIAMInput) error {
if err := input.validate(); err != nil {
return temporal.NewNonRetryableApplicationError(err.Error(), "InvalidInput", err)
}

ctx = workflow.WithActivityOptions(ctx, activityOptions)
aws := &activities.AWSActivities{}
logger := workflow.GetLogger(ctx)
Expand Down
12 changes: 7 additions & 5 deletions internal/workflows/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (i SpinUpInput) validate() error {
return fmt.Errorf("Team is required")
case i.NodeCount <= 0:
return fmt.Errorf("NodeCount must be greater than 0")
case (i.ClusterRoleARN == "") != (i.NodeRoleARN == ""):
return fmt.Errorf("ClusterRoleARN and NodeRoleARN must both be provided or both be empty")
}
for idx, sc := range i.Subnets {
if strings.TrimSpace(sc.CIDR) == "" {
Expand Down Expand Up @@ -90,9 +92,9 @@ func SpinUpWorkflow(ctx workflow.Context, input SpinUpInput) (err error) {
clusterRoleARN := input.ClusterRoleARN
nodeRoleARN := input.NodeRoleARN

if clusterRoleARN == "" || nodeRoleARN == "" {
var iamOut SpinUpIAMOutput
if err = workflow.ExecuteChildWorkflow(ctx, SpinUpIAMWorkflow, SpinUpIAMInput{
if clusterRoleARN == "" {
var iamOut SpinUpEKSIAMOutput
if err = workflow.ExecuteChildWorkflow(ctx, SpinUpIAMWorkflow, SpinUpEKSIAMInput{
Region: input.Region,
ClusterName: input.ClusterName,
Environment: input.Environment,
Expand All @@ -111,7 +113,7 @@ func SpinUpWorkflow(ctx workflow.Context, input SpinUpInput) (err error) {
if err := workflow.ExecuteChildWorkflow(
cctx,
SpinDownIAMWorkflow,
SpinDownIAMInput{
SpinDownEKSIAMInput{
ClusterRoleName: iamOut.ClusterRoleName,
NodeRoleName: iamOut.NodeRoleName,
},
Expand Down Expand Up @@ -185,7 +187,7 @@ func SpinDownWorkflow(ctx workflow.Context, input SpinDownInput) error {
logger.Info("network torn down", "vpcID", input.VpcID)

if input.ClusterRoleName != "" || input.NodeRoleName != "" {
if err := workflow.ExecuteChildWorkflow(ctx, SpinDownIAMWorkflow, SpinDownIAMInput{
if err := workflow.ExecuteChildWorkflow(ctx, SpinDownIAMWorkflow, SpinDownEKSIAMInput{
ClusterRoleName: input.ClusterRoleName,
NodeRoleName: input.NodeRoleName,
}).Get(ctx, nil); err != nil {
Expand Down
Loading