Skip to content

Commit

Permalink
refactor get iamserviceaccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
aclevername committed Jan 22, 2021
1 parent a487044 commit 474e468
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 61 deletions.
70 changes: 70 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.

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

import (
"github.com/pkg/errors"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
)

func (m *Manager) Get(namespace, name string) ([]*api.ClusterIAMServiceAccount, error) {
remoteServiceAccounts, err := m.stackManager.GetIAMServiceAccounts()
if err != nil {
return nil, errors.Wrap(err, "getting iamserviceaccounts")
}

if namespace != "" {
remoteServiceAccounts = filterByNamespace(remoteServiceAccounts, namespace)
}

if name != "" {
remoteServiceAccounts = filterByName(remoteServiceAccounts, name)
}

return remoteServiceAccounts, nil
}

func filterByNamespace(serviceAccounts []*api.ClusterIAMServiceAccount, namespace string) []*api.ClusterIAMServiceAccount {
var serviceAccountsMatching []*api.ClusterIAMServiceAccount
for _, sa := range serviceAccounts {
if sa.Namespace == namespace {
serviceAccountsMatching = append(serviceAccountsMatching, sa)
}
}
return serviceAccountsMatching
}

func filterByName(serviceAccounts []*api.ClusterIAMServiceAccount, name string) []*api.ClusterIAMServiceAccount {
var serviceAccountsMatching []*api.ClusterIAMServiceAccount
for _, sa := range serviceAccounts {
if sa.Name == name {
serviceAccountsMatching = append(serviceAccountsMatching, sa)
}
}
return serviceAccountsMatching
}
172 changes: 172 additions & 0 deletions pkg/actions/iam/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package iam_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/weaveworks/eksctl/pkg/eks"

"github.com/weaveworks/eksctl/pkg/actions/iam"
"github.com/weaveworks/eksctl/pkg/actions/iam/fakes"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
)

var _ = Describe("Get", func() {

var (
iamManager *iam.Manager
fakeStackManager *fakes.FakeStackManager
)

BeforeEach(func() {
fakeStackManager = new(fakes.FakeStackManager)

iamManager = iam.New("my-cluster", &eks.ClusterProvider{}, fakeStackManager, nil, nil)
})

When("no options are specified", func() {
It("returns all service accounts", func() {
fakeStackManager.GetIAMServiceAccountsReturns([]*api.ClusterIAMServiceAccount{
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa",
Namespace: "default",
},
AttachPolicyARNs: []string{"arn-123"},
},
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa-2",
Namespace: "not-default",
},
AttachPolicyARNs: []string{"arn-123"},
},
}, nil)

serviceAccounts, err := iamManager.Get("", "")
Expect(err).NotTo(HaveOccurred())

Expect(fakeStackManager.GetIAMServiceAccountsCallCount()).To(Equal(1))
Expect(serviceAccounts).To(Equal([]*api.ClusterIAMServiceAccount{
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa",
Namespace: "default",
},
AttachPolicyARNs: []string{"arn-123"},
},
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa-2",
Namespace: "not-default",
},
AttachPolicyARNs: []string{"arn-123"},
},
}))
})
})

When("name option is specified", func() {
It("returns only the service account matching the name", func() {
fakeStackManager.GetIAMServiceAccountsReturns([]*api.ClusterIAMServiceAccount{
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa",
Namespace: "default",
},
AttachPolicyARNs: []string{"arn-123"},
},
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa-2",
Namespace: "not-default",
},
AttachPolicyARNs: []string{"arn-123"},
},
}, nil)

serviceAccounts, err := iamManager.Get("", "test-sa")
Expect(err).NotTo(HaveOccurred())

Expect(fakeStackManager.GetIAMServiceAccountsCallCount()).To(Equal(1))
Expect(serviceAccounts).To(Equal([]*api.ClusterIAMServiceAccount{
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa",
Namespace: "default",
},
AttachPolicyARNs: []string{"arn-123"},
},
}))
})
})

When("namespace option is specified", func() {
It("returns only the service account matching the name", func() {
fakeStackManager.GetIAMServiceAccountsReturns([]*api.ClusterIAMServiceAccount{
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa",
Namespace: "default",
},
AttachPolicyARNs: []string{"arn-123"},
},
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa-2",
Namespace: "not-default",
},
AttachPolicyARNs: []string{"arn-123"},
},
}, nil)

serviceAccounts, err := iamManager.Get("not-default", "")
Expect(err).NotTo(HaveOccurred())

Expect(fakeStackManager.GetIAMServiceAccountsCallCount()).To(Equal(1))
Expect(serviceAccounts).To(Equal([]*api.ClusterIAMServiceAccount{
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa-2",
Namespace: "not-default",
},
AttachPolicyARNs: []string{"arn-123"},
},
}))
})
})

When("name and namespace option is specified", func() {
It("returns only the service account matching the name", func() {
fakeStackManager.GetIAMServiceAccountsReturns([]*api.ClusterIAMServiceAccount{
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa",
Namespace: "default",
},
AttachPolicyARNs: []string{"arn-123"},
},
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "some-other-sa",
Namespace: "default",
},
AttachPolicyARNs: []string{"arn-123"},
},
}, nil)

serviceAccounts, err := iamManager.Get("default", "test-sa")
Expect(err).NotTo(HaveOccurred())

Expect(fakeStackManager.GetIAMServiceAccountsCallCount()).To(Equal(1))
Expect(serviceAccounts).To(Equal([]*api.ClusterIAMServiceAccount{
{
ClusterIAMMeta: api.ClusterIAMMeta{
Name: "test-sa",
Namespace: "default",
},
AttachPolicyARNs: []string{"arn-123"},
},
}))
})
})
})
1 change: 1 addition & 0 deletions pkg/actions/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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, replaceExistingRole bool) *tasks.TaskTree
GetIAMServiceAccounts() ([]*api.ClusterIAMServiceAccount, error)
}

func New(clusterName string, clusterProvider *eks.ClusterProvider, stackManager StackManager, oidcManager *iamoidc.OpenIDConnectManager, clientSet kubeclient.Interface) *Manager {
Expand Down
12 changes: 1 addition & 11 deletions pkg/ctl/cmdutils/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func NewCreateIAMServiceAccountLoader(cmd *Cmd, saFilter *filter.IAMServiceAccou
}

// NewGetIAMServiceAccountLoader will load config or use flags for 'eksctl get iamserviceaccount'
func NewGetIAMServiceAccountLoader(cmd *Cmd, sa *api.ClusterIAMServiceAccount) ClusterConfigLoader {
func NewGetIAMServiceAccountLoader(cmd *Cmd) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)

l.validateWithConfigFile = func() error {
Expand All @@ -619,20 +619,10 @@ func NewGetIAMServiceAccountLoader(cmd *Cmd, sa *api.ClusterIAMServiceAccount) C
}

l.validateWithoutConfigFile = func() error {
sa.AttachPolicyARNs = []string{""} // force to pass general validation

if l.ClusterConfig.Metadata.Name == "" {
return ErrMustBeSet(ClusterNameFlag(cmd))
}

if l.NameArg != "" {
sa.Name = l.NameArg
}

if sa.Name == "" {
l.ClusterConfig.IAM.ServiceAccounts = nil
}

l.Plan = false

return nil
Expand Down

0 comments on commit 474e468

Please sign in to comment.