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

Backfill create cluster unittests #5314

Merged
merged 15 commits into from
Jun 1, 2022
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
29 changes: 10 additions & 19 deletions pkg/actions/nodegroup/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,32 @@ import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
cftypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"

"k8s.io/apimachinery/pkg/runtime"
core "k8s.io/client-go/testing"

"k8s.io/client-go/kubernetes/fake"

"github.com/weaveworks/eksctl/pkg/vpc"

"github.com/weaveworks/eksctl/pkg/cfn/manager"

"github.com/aws/aws-sdk-go-v2/service/cloudformation"
awseks "github.com/aws/aws-sdk-go-v2/service/eks"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"

"github.com/aws/aws-sdk-go-v2/aws"
cftypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/stretchr/testify/mock"

"github.com/weaveworks/eksctl/pkg/utils/tasks"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/pkg/errors"
"github.com/stretchr/testify/mock"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
core "k8s.io/client-go/testing"

"github.com/weaveworks/eksctl/pkg/actions/nodegroup"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
utilFakes "github.com/weaveworks/eksctl/pkg/ctl/cmdutils/filter/fakes"
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/eks/fakes"
"github.com/weaveworks/eksctl/pkg/kubernetes"
"github.com/weaveworks/eksctl/pkg/testutils"
"github.com/weaveworks/eksctl/pkg/testutils/mockprovider"
"github.com/weaveworks/eksctl/pkg/utils/tasks"
"github.com/weaveworks/eksctl/pkg/vpc"
)

type ngEntry struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/cfn/manager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func (c *StackCollection) createClusterStack(ctx context.Context, stackName stri
if err != nil {
return err
}

go func() {
defer close(errCh)
troubleshoot := func() {
Expand Down
242 changes: 242 additions & 0 deletions pkg/ctl/create/cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
package create

import (
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
cftypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
awseks "github.com/aws/aws-sdk-go-v2/service/eks"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/aws/aws-sdk-go/aws/credentials"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
kubefake "k8s.io/client-go/kubernetes/fake"
restclient "k8s.io/client-go/rest"

api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils/filter"
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/eks/fakes"
"github.com/weaveworks/eksctl/pkg/kubernetes"
"github.com/weaveworks/eksctl/pkg/testutils"
"github.com/weaveworks/eksctl/pkg/testutils/mockprovider"
)

var _ = Describe("create cluster", func() {
Expand Down Expand Up @@ -181,4 +200,227 @@ var _ = Describe("create cluster", func() {
}),
)
})
Describe("doCreateCluster", func() {
var (
ctl *eks.ClusterProvider
cfg *api.ClusterConfig
)
BeforeEach(func() {
p := mockprovider.NewMockProvider()
defaultProviderMocks(p, defaultOutput)
fk := &fakes.FakeKubeProvider{}
clientset := kubefake.NewSimpleClientset()
client, err := kubernetes.NewRawClient(clientset, &restclient.Config{})
Expect(err).NotTo(HaveOccurred())
fk.NewRawClientReturns(client, nil)
fk.ServerVersionReturns("1.22", nil)
msp := &mockSessionProvider{}
ctl = &eks.ClusterProvider{
AWSProvider: p,
Status: &eks.ProviderStatus{
ClusterInfo: &eks.ClusterInfo{
Cluster: testutils.NewFakeCluster("my-cluster", ""),
},
SessionCreds: msp,
},
KubeProvider: fk,
}

cfg = api.NewClusterConfig()
cfg.Metadata.Name = "my-cluster"
cfg.VPC.ClusterEndpoints = api.ClusterEndpointAccessDefaults()
cfg.Metadata.Version = "1.22"
})
It("successfully creates a cluster", func() {
cmd := &cmdutils.Cmd{
ClusterConfig: cfg,
ProviderConfig: api.ProviderConfig{
WaitTimeout: time.Second * 1,
},
}
filter := filter.NewNodeGroupFilter()
params := &cmdutils.CreateClusterCmdParams{
Subnets: map[api.SubnetTopology]*[]string{
api.SubnetTopologyPrivate: {},
api.SubnetTopologyPublic: {},
},
}
err := doCreateCluster(cmd, filter, params, ctl)
Expect(err).NotTo(HaveOccurred())
})
})
})

var defaultOutput = []cftypes.Output{
{
OutputKey: aws.String("ClusterSecurityGroupId"),
OutputValue: aws.String("csg-1234"),
},
{
OutputKey: aws.String("SecurityGroup"),
OutputValue: aws.String("sg-1"),
},
{
OutputKey: aws.String("VPC"),
OutputValue: aws.String("vpc-1"),
},
{
OutputKey: aws.String("SharedNodeSecurityGroup"),
OutputValue: aws.String("sg-1"),
},
{
OutputKey: aws.String("FeatureNATMode"),
OutputValue: aws.String("Single"),
},
{
OutputKey: aws.String("SubnetsPrivate"),
OutputValue: aws.String("sub-priv-1 sub-priv-2 sub-priv-3"),
},
{
OutputKey: aws.String("SubnetsPublic"),
OutputValue: aws.String("sub-pub-1 sub-pub-2 sub-pub-3"),
},
{
OutputKey: aws.String("ServiceRoleARN"),
OutputValue: aws.String("arn:aws:iam::123456:role/amazingrole-1"),
},
{
OutputKey: aws.String("ARN"),
OutputValue: aws.String("arn:aws:iam::123456:role/amazingrole-2"),
},
{
OutputKey: aws.String("CertificateAuthorityData"),
OutputValue: aws.String("dGVzdAo="),
},
{
OutputKey: aws.String("ClusterStackName"),
OutputValue: aws.String("eksctl-my-cluster-cluster"),
},
{
OutputKey: aws.String("Endpoint"),
OutputValue: aws.String("https://endpoint.com"),
},
}

func defaultProviderMocks(p *mockprovider.MockProvider, output []cftypes.Output) {
p.MockEC2().On("DescribeAvailabilityZones", mock.Anything, &ec2.DescribeAvailabilityZonesInput{
Filters: []ec2types.Filter{{
Name: aws.String("region-name"),
Values: []string{"us-west-2"},
}, {
Name: aws.String("state"),
Values: []string{string(ec2types.AvailabilityZoneStateAvailable)},
}, {
Name: aws.String("zone-type"),
Values: []string{string(ec2types.LocationTypeAvailabilityZone)},
}},
}).Return(&ec2.DescribeAvailabilityZonesOutput{
AvailabilityZones: []ec2types.AvailabilityZone{
{
GroupName: aws.String("name"),
ZoneName: aws.String("us-west-2-1b"),
ZoneId: aws.String("id"),
},
{
GroupName: aws.String("name"),
ZoneName: aws.String("us-west-2-1a"),
ZoneId: aws.String("id"),
}},
}, nil)
p.MockCloudFormation().On("ListStacks", mock.Anything, mock.Anything).Return(&cloudformation.ListStacksOutput{
StackSummaries: []cftypes.StackSummary{
{
StackName: aws.String("eksctl-my-cluster-cluster"),
StackStatus: "CREATE_COMPLETE",
},
},
}, nil)
p.MockCloudFormation().On("DescribeStacks", mock.Anything, mock.Anything).Return(&cloudformation.DescribeStacksOutput{
Stacks: []cftypes.Stack{
{
StackName: aws.String("eksctl-my-cluster-cluster"),
StackStatus: "CREATE_COMPLETE",
Tags: []cftypes.Tag{
{
Key: aws.String(api.ClusterNameTag),
Value: aws.String("eksctl-my-cluster-cluster"),
},
},
Outputs: output,
},
},
}, nil)
p.MockEKS().On("DescribeCluster", mock.Anything, mock.Anything).Return(&awseks.DescribeClusterOutput{
Cluster: &ekstypes.Cluster{
CertificateAuthority: &ekstypes.Certificate{
Data: aws.String("dGVzdAo="),
},
Endpoint: aws.String("endpoint"),
Arn: aws.String("arn"),
KubernetesNetworkConfig: nil,
Logging: nil,
Name: aws.String("my-cluster"),
PlatformVersion: aws.String("1.22"),
ResourcesVpcConfig: &ekstypes.VpcConfigResponse{
ClusterSecurityGroupId: aws.String("csg-1234"),
EndpointPublicAccess: true,
PublicAccessCidrs: []string{"1.2.3.4/24", "1.2.3.4/12"},
SecurityGroupIds: []string{"sg-1", "sg-2"},
SubnetIds: []string{"sub-1", "sub-2"},
VpcId: aws.String("vpc-1"),
},
Status: "CREATE_COMPLETE",
Tags: map[string]string{
api.ClusterNameTag: "eksctl-my-cluster-cluster",
},
Version: aws.String("1.22"),
},
}, nil)

p.MockEC2().On("DescribeImages", mock.Anything, mock.Anything).
Return(&ec2.DescribeImagesOutput{
Images: []ec2types.Image{
{
ImageId: aws.String("ami-123"),
State: ec2types.ImageStateAvailable,
OwnerId: aws.String("123"),
RootDeviceType: ec2types.DeviceTypeEbs,
RootDeviceName: aws.String("/dev/sda1"),
BlockDeviceMappings: []ec2types.BlockDeviceMapping{
{
DeviceName: aws.String("/dev/sda1"),
Ebs: &ec2types.EbsBlockDevice{
Encrypted: aws.Bool(false),
},
},
},
},
},
}, nil)
p.MockEC2().On("DescribeSubnets", mock.Anything, mock.Anything).Return(&ec2.DescribeSubnetsOutput{
Subnets: []ec2types.Subnet{},
}, nil)
p.MockEC2().On("DescribeVpcs", mock.Anything, mock.Anything).Return(&ec2.DescribeVpcsOutput{
Vpcs: []ec2types.Vpc{
{
VpcId: aws.String("vpc-1"),
CidrBlock: aws.String("192.168.0.0/16"),
},
},
}, nil)
p.MockCloudFormation().On("CreateStack", mock.Anything, mock.Anything).Return(&cloudformation.CreateStackOutput{
StackId: aws.String("eksctl-my-cluster-cluster"),
}, nil)
}

type mockSessionProvider struct {
}

func (m *mockSessionProvider) Get() (credentials.Value, error) {
return credentials.Value{
AccessKeyID: "key-id",
SecretAccessKey: "secret-access-key",
SessionToken: "token",
ProviderName: "aws",
}, nil
}
12 changes: 9 additions & 3 deletions pkg/eks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type KubeProvider interface {
NewRawClient(spec *api.ClusterConfig) (*kubewrapper.RawClient, error)
NewStdClientSet(spec *api.ClusterConfig) (*k8sclient.Clientset, error)
ServerVersion(rawClient *kubernetes.RawClient) (string, error)
WaitForControlPlane(meta *api.ClusterMeta, clientSet *kubewrapper.RawClient, waitTimeout time.Duration) error
}

// ProviderServices stores the used APIs
Expand Down Expand Up @@ -123,11 +124,16 @@ type ClusterInfo struct {
Cluster *ekstypes.Cluster
}

// SessionProvider abstracts an aws credentials.Value provider.
type SessionProvider interface {
Get() (credentials.Value, error)
}

// ProviderStatus stores information about the used IAM role and the resulting session
type ProviderStatus struct {
IAMRoleARN string
sessionCreds *credentials.Credentials
ClusterInfo *ClusterInfo
SessionCreds SessionProvider
}

// New creates a new setup of the used AWS APIs
Expand Down Expand Up @@ -177,7 +183,7 @@ func New(ctx context.Context, spec *api.ProviderConfig, clusterSpec *api.Cluster
}

c.Status = &ProviderStatus{
sessionCreds: s.Config.Credentials,
SessionCreds: s.Config.Credentials,
}

provider.asg = autoscaling.NewFromConfig(cfg)
Expand Down Expand Up @@ -271,7 +277,7 @@ func (c *ClusterProvider) IsSupportedRegion() bool {

// GetCredentialsEnv returns the AWS credentials for env usage
func (c *ClusterProvider) GetCredentialsEnv() ([]string, error) {
creds, err := c.Status.sessionCreds.Get()
creds, err := c.Status.SessionCreds.Get()
if err != nil {
return nil, errors.Wrap(err, "getting effective credentials")
}
Expand Down
Loading