Skip to content
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

Update iamserviceaccount role policies #3064

Merged
merged 19 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions integration/tests/crud/creategetdelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ var _ = Describe("(Integration) Create, Get, Scale & Delete", func() {
})
})

Context("create and delete iamserviceaccounts", func() {
Context("create, update and delete iamserviceaccounts", func() {
var (
cfg *api.ClusterConfig
ctl *eks.ClusterProvider
Expand All @@ -346,7 +346,7 @@ var _ = Describe("(Integration) Create, Get, Scale & Delete", func() {
Expect(err).ShouldNot(HaveOccurred())
})

It("should enable OIDC and create two iamserviceaccounts", func() {
It("should enable OIDC, create two iamserviceaccounts and update the policies", func() {
{
exists, err := oidc.CheckProviderExists()
Expect(err).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -414,6 +414,19 @@ var _ = Describe("(Integration) Create, Get, Scale & Delete", func() {
Expect(sa.Annotations).To(HaveKey(api.AnnotationEKSRoleARN))
Expect(sa.Annotations[api.AnnotationEKSRoleARN]).To(MatchRegexp("^arn:aws:iam::.*:role/eksctl-" + truncate(params.ClusterName) + ".*$"))
}

cmds = []Cmd{
params.EksctlUpdateCmd.WithArgs(
"iamserviceaccount",
"--cluster", params.ClusterName,
"--name", "app-cache-access",
"--namespace", "app1",
"--attach-policy-arn", "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess",
"--approve",
),
}

Expect(cmds).To(RunSuccessfully())
})

It("should list both iamserviceaccounts", func() {
Expand Down
18 changes: 18 additions & 0 deletions pkg/actions/iam/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package iam

import (
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/kubernetes"
)

func (a *Manager) CreateIAMServiceAccount(iamServiceAccounts []*api.ClusterIAMServiceAccount, plan bool) error {
taskTree := a.stackManager.NewTasksToCreateIAMServiceAccounts(iamServiceAccounts, a.oidcManager, kubernetes.NewCachedClientSet(a.clientSet))
taskTree.PlanMode = plan

err := doTasks(taskTree)

cmdutils.LogPlanModeWarning(plan && len(iamServiceAccounts) > 0)

return err
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left this here for now, if folks want me to remove this small refactor and put it into the refactor PR let me know.

289 changes: 289 additions & 0 deletions pkg/actions/iam/fakes/fake_stack_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions pkg/actions/iam/iam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package iam

import (
"fmt"

"github.com/kris-nova/logger"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
"github.com/weaveworks/eksctl/pkg/eks"
iamoidc "github.com/weaveworks/eksctl/pkg/iam/oidc"
"github.com/weaveworks/eksctl/pkg/kubernetes"
"github.com/weaveworks/eksctl/pkg/utils/tasks"
kubeclient "k8s.io/client-go/kubernetes"
)

type Manager struct {
clusterName string
clusterProvider *eks.ClusterProvider
oidcManager *iamoidc.OpenIDConnectManager
stackManager StackManager
clientSet kubeclient.Interface
}

//go:generate counterfeiter -o fakes/fake_stack_manager.go . StackManager
type StackManager interface {
ListStacksMatching(nameRegex string, statusFilters ...string) ([]*manager.Stack, error)
UpdateStack(stackName, changeSetName, description string, templateData manager.TemplateData, parameters map[string]string) error
NewTasksToCreateIAMServiceAccounts(serviceAccounts []*api.ClusterIAMServiceAccount, oidc *iamoidc.OpenIDConnectManager, clientSetGetter kubernetes.ClientSetGetter) *tasks.TaskTree
}

func New(clusterName string, clusterProvider *eks.ClusterProvider, stackManager StackManager, oidcManager *iamoidc.OpenIDConnectManager, clientSet kubeclient.Interface) *Manager {
return &Manager{
clusterName: clusterName,
clusterProvider: clusterProvider,
oidcManager: oidcManager,
stackManager: stackManager,
clientSet: clientSet,
}
}

func doTasks(taskTree *tasks.TaskTree) error {
logger.Info(taskTree.Describe())
if errs := taskTree.DoAllSync(); len(errs) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm doesn't this exist in some form in the tasks package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't see anything 🤔

logger.Info("%d error(s) occurred and IAM Role stacks haven't been updated properly, you may wish to check CloudFormation console", len(errs))
for _, err := range errs {
logger.Critical("%s\n", err.Error())
}
return fmt.Errorf("failed to create iamserviceaccount(s)")
}
return nil
}