-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move IAM Service Account Tasks to actions package #7140
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c5aebc
Move IAM Service Account Tasks to actions package
TiberiuGC 2fdcad7
add unit tests
TiberiuGC 127b982
make sure all tasks close error channels
TiberiuGC e10ecfe
fix integration tests
TiberiuGC 23a3c63
Merge branch 'main' into maintenance/move-iam-sa-related-tasks
TiberiuGC 362ad04
remove unuset test export
TiberiuGC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,115 @@ | ||
package irsa | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/kris-nova/logger" | ||
"github.com/pkg/errors" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" | ||
iamoidc "github.com/weaveworks/eksctl/pkg/iam/oidc" | ||
"github.com/weaveworks/eksctl/pkg/kubernetes" | ||
"github.com/weaveworks/eksctl/pkg/utils/tasks" | ||
) | ||
|
||
func (a *Manager) CreateIAMServiceAccount(iamServiceAccounts []*api.ClusterIAMServiceAccount, plan bool) error { | ||
taskTree := a.stackManager.NewTasksToCreateIAMServiceAccounts(iamServiceAccounts, a.oidcManager, kubernetes.NewCachedClientSet(a.clientSet)) | ||
var ( | ||
managedByKubernetesLabelKey = "app.kubernetes.io/managed-by" | ||
managedByKubernetesLabelValue = "eksctl" | ||
maybeCreateServiceAccountOrUpdateMetadata = kubernetes.MaybeCreateServiceAccountOrUpdateMetadata | ||
) | ||
|
||
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate | ||
//counterfeiter:generate -o fakes/fake_create_tasks_builder.go . CreateTasksBuilder | ||
type CreateTasksBuilder interface { | ||
CreateIAMServiceAccountsTasks(ctx context.Context, serviceAccounts []*api.ClusterIAMServiceAccount) *tasks.TaskTree | ||
} | ||
|
||
type Creator struct { | ||
clusterName string | ||
region string | ||
|
||
clientSetGetter kubernetes.ClientSetGetter | ||
oidcManager *iamoidc.OpenIDConnectManager | ||
stackManager StackManager | ||
} | ||
|
||
func NewCreator( | ||
clusterName string, | ||
region string, | ||
clientSetGetter kubernetes.ClientSetGetter, | ||
oidcManager *iamoidc.OpenIDConnectManager, | ||
stackManager StackManager) *Creator { | ||
return &Creator{ | ||
clusterName: clusterName, | ||
region: region, | ||
clientSetGetter: clientSetGetter, | ||
oidcManager: oidcManager, | ||
stackManager: stackManager, | ||
} | ||
} | ||
|
||
func (c *Creator) CreateIAMServiceAccounts(ctx context.Context, serviceAccounts []*api.ClusterIAMServiceAccount, plan bool) error { | ||
taskTree := c.CreateIAMServiceAccountsTasks(ctx, serviceAccounts) | ||
taskTree.PlanMode = plan | ||
|
||
err := doTasks(taskTree, actionCreate) | ||
|
||
logPlanModeWarning(plan && len(iamServiceAccounts) > 0) | ||
logPlanModeWarning(plan && len(serviceAccounts) > 0) | ||
|
||
return err | ||
} | ||
|
||
func (c *Creator) CreateIAMServiceAccountsTasks(ctx context.Context, serviceAccounts []*api.ClusterIAMServiceAccount) *tasks.TaskTree { | ||
taskTree := &tasks.TaskTree{Parallel: true} | ||
|
||
for i := range serviceAccounts { | ||
sa := serviceAccounts[i] | ||
saTasks := &tasks.TaskTree{ | ||
Parallel: false, | ||
IsSubTask: true, | ||
} | ||
|
||
if sa.AttachRoleARN == "" { | ||
saTasks.Append(&createIAMRoleForServiceAccountTask{ | ||
ctx: ctx, | ||
info: fmt.Sprintf("create IAM role for serviceaccount %q", sa.NameString()), | ||
clusterName: c.clusterName, | ||
region: c.region, | ||
stackManager: c.stackManager, | ||
sa: sa, | ||
oidc: c.oidcManager, | ||
}) | ||
} else { | ||
logger.Debug("attachRoleARN was provided, skipping role creation") | ||
sa.Status = &api.ClusterIAMServiceAccountStatus{ | ||
RoleARN: &sa.AttachRoleARN, | ||
} | ||
} | ||
|
||
if sa.Labels == nil { | ||
sa.Labels = make(map[string]string) | ||
} | ||
sa.Labels[managedByKubernetesLabelKey] = managedByKubernetesLabelValue | ||
if !api.IsEnabled(sa.RoleOnly) { | ||
saTasks.Append(&kubernetesTask{ | ||
info: fmt.Sprintf("create serviceaccount %q", sa.NameString()), | ||
kubernetes: c.clientSetGetter, | ||
objectMeta: sa.ClusterIAMMeta.AsObjectMeta(), | ||
call: func(clientSet kubernetes.Interface, objectMeta v1.ObjectMeta) error { | ||
sa.SetAnnotations() | ||
objectMeta.SetAnnotations(sa.AsObjectMeta().Annotations) | ||
objectMeta.SetLabels(sa.AsObjectMeta().Labels) | ||
if err := maybeCreateServiceAccountOrUpdateMetadata(clientSet, objectMeta); err != nil { | ||
return errors.Wrapf(err, "failed to create service account %s/%s", objectMeta.GetNamespace(), objectMeta.GetName()) | ||
} | ||
return nil | ||
}, | ||
}) | ||
} | ||
|
||
taskTree.Append(saTasks) | ||
} | ||
return taskTree | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package irsa_test | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
. "github.com/onsi/ginkgo/v2" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
"github.com/weaveworks/eksctl/pkg/actions/irsa" | ||
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" | ||
) | ||
|
||
var _ = Describe("Create", func() { | ||
|
||
Describe("CreateIAMServiceAccountsTasks", func() { | ||
var ( | ||
clusterName = "test-cluster" | ||
roleName = "test-role-name" | ||
roleARN = "test-role-arn" | ||
region = "us-west-2" | ||
creator *irsa.Creator | ||
) | ||
|
||
BeforeEach(func() { | ||
creator = irsa.NewCreator(clusterName, region, nil, nil, nil) | ||
}) | ||
|
||
When("attachRoleARN is provided and RoleOnly is true", func() { | ||
It("returns a empty tasktree", func() { | ||
serviceAccounts := []*api.ClusterIAMServiceAccount{ | ||
{ | ||
RoleName: roleName, | ||
AttachRoleARN: roleARN, | ||
RoleOnly: aws.Bool(true), | ||
}, | ||
} | ||
taskTree := creator.CreateIAMServiceAccountsTasks(context.Background(), serviceAccounts) | ||
Expect(taskTree.Parallel).To(Equal(true)) | ||
Expect(taskTree.IsSubTask).To(Equal(false)) | ||
Expect(len(taskTree.Tasks)).To(Equal(1)) | ||
Expect(taskTree.Tasks[0].Describe()).To(Equal("no tasks")) | ||
}) | ||
}) | ||
|
||
When("attachRoleARN is provided and RoleOnly is false", func() { | ||
It("returns a tasktree with all expected tasks", func() { | ||
serviceAccounts := []*api.ClusterIAMServiceAccount{ | ||
{ | ||
RoleName: roleName, | ||
AttachRoleARN: roleARN, | ||
}, | ||
} | ||
taskTree := creator.CreateIAMServiceAccountsTasks(context.Background(), serviceAccounts) | ||
Expect(taskTree.Parallel).To(Equal(true)) | ||
Expect(taskTree.IsSubTask).To(Equal(false)) | ||
Expect(len(taskTree.Tasks)).To(Equal(1)) | ||
Expect(taskTree.Tasks[0].Describe()).To(ContainSubstring("create serviceaccount")) | ||
}) | ||
}) | ||
|
||
When("attachRoleARN is not provided and RoleOnly is true", func() { | ||
It("returns a tasktree with all expected tasks", func() { | ||
serviceAccounts := []*api.ClusterIAMServiceAccount{ | ||
{ | ||
RoleName: roleName, | ||
RoleOnly: aws.Bool(true), | ||
}, | ||
} | ||
taskTree := creator.CreateIAMServiceAccountsTasks(context.Background(), serviceAccounts) | ||
Expect(taskTree.Parallel).To(Equal(true)) | ||
Expect(taskTree.IsSubTask).To(Equal(false)) | ||
Expect(len(taskTree.Tasks)).To(Equal(1)) | ||
Expect(taskTree.Tasks[0].Describe()).To(ContainSubstring("create IAM role for serviceaccount")) | ||
}) | ||
}) | ||
|
||
When("attachRoleARN is not provided and RoleOnly is false", func() { | ||
It("returns a tasktree with all expected tasks", func() { | ||
serviceAccounts := []*api.ClusterIAMServiceAccount{ | ||
{ | ||
RoleName: roleName, | ||
RoleOnly: aws.Bool(false), | ||
}, | ||
} | ||
taskTree := creator.CreateIAMServiceAccountsTasks(context.Background(), serviceAccounts) | ||
Expect(taskTree.Parallel).To(Equal(true)) | ||
Expect(taskTree.IsSubTask).To(Equal(false)) | ||
Expect(len(taskTree.Tasks)).To(Equal(1)) | ||
Expect(taskTree.Tasks[0].Describe()).To(ContainSubstring("2 sequential sub-tasks")) | ||
Expect(taskTree.Tasks[0].Describe()).To(ContainSubstring("create IAM role for serviceaccount")) | ||
Expect(taskTree.Tasks[0].Describe()).To(ContainSubstring("create serviceaccount")) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closing error channel to early here was nullifying the effect of the
--wait
flagThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually introduced a bug - #7177. Going to address it in a separate PR.