Skip to content

Commit

Permalink
Do Fargate setup before nodegroup creation (#2304)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbeaumont committed Jun 9, 2020
1 parent 1273981 commit 0a13d65
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 63 deletions.
9 changes: 0 additions & 9 deletions pkg/ctl/create/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,6 @@ func doCreateCluster(cmd *cmdutils.Cmd, ng *api.NodeGroup, params *cmdutils.Crea
}
}

if cfg.IsFargateEnabled() {
if err := doCreateFargateProfiles(cmd, ctl); err != nil {
return err
}
if err := scheduleCoreDNSOnFargateIfRelevant(cmd, clientSet); err != nil {
return err
}
}

if cfg.HasGitopsRepoConfigured() {
kubernetesClientConfigs, err := ctl.NewClient(cfg)
if err != nil {
Expand Down
56 changes: 2 additions & 54 deletions pkg/ctl/create/fargate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ package create

import (
"fmt"
"time"

"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/fargate"
"github.com/weaveworks/eksctl/pkg/fargate/coredns"
"github.com/weaveworks/eksctl/pkg/utils/retry"
"github.com/weaveworks/eksctl/pkg/utils/strings"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
Expand Down Expand Up @@ -85,14 +79,14 @@ func doCreateFargateProfile(cmd *cmdutils.Cmd) error {
return err
}

if err := doCreateFargateProfiles(cmd, ctl); err != nil {
if err := eks.DoCreateFargateProfiles(cmd.ClusterConfig, ctl); err != nil {
return err
}
clientSet, err := clientSet(cfg, ctl)
if err != nil {
return err
}
return scheduleCoreDNSOnFargateIfRelevant(cmd, clientSet)
return eks.ScheduleCoreDNSOnFargateIfRelevant(cmd.ClusterConfig, ctl, clientSet)
}

func clientSet(cfg *api.ClusterConfig, ctl *eks.ClusterProvider) (kubernetes.Interface, error) {
Expand All @@ -111,49 +105,3 @@ func clientSet(cfg *api.ClusterConfig, ctl *eks.ClusterProvider) (kubernetes.Int
}
return k8sClientSet, nil
}

func doCreateFargateProfiles(cmd *cmdutils.Cmd, ctl *eks.ClusterProvider) error {
clusterName := cmd.ClusterConfig.Metadata.Name
awsClient := fargate.NewClientWithWaitTimeout(clusterName, ctl.Provider.EKS(), cmd.ProviderConfig.WaitTimeout)
for _, profile := range cmd.ClusterConfig.FargateProfiles {
logger.Info("creating Fargate profile %q on EKS cluster %q", profile.Name, clusterName)

// Default the pod execution role ARN to be the same as the cluster
// role defined in CloudFormation:
if profile.PodExecutionRoleARN == "" {
profile.PodExecutionRoleARN = strings.EmptyIfNil(cmd.ClusterConfig.IAM.FargatePodExecutionRoleARN)
}
// Linearise the creation of Fargate profiles by passing
// wait = true, as the API otherwise errors out with:
// ResourceInUseException: Cannot create Fargate Profile
// ${name2} because cluster ${clusterName} currently has
// Fargate profile ${name1} in status CREATING
if err := awsClient.CreateProfile(profile, true); err != nil {
return errors.Wrapf(err, "failed to create Fargate profile %q on EKS cluster %q", profile.Name, clusterName)
}
logger.Info("created Fargate profile %q on EKS cluster %q", profile.Name, clusterName)
}
return nil
}

func scheduleCoreDNSOnFargateIfRelevant(cmd *cmdutils.Cmd, clientSet kubernetes.Interface) error {
if coredns.IsSchedulableOnFargate(cmd.ClusterConfig.FargateProfiles) {
scheduled, err := coredns.IsScheduledOnFargate(clientSet)
if err != nil {
return err
}
if !scheduled {
if err := coredns.ScheduleOnFargate(clientSet); err != nil {
return err
}
retryPolicy := &retry.TimingOutExponentialBackoff{
Timeout: cmd.ProviderConfig.WaitTimeout,
TimeUnit: time.Second,
}
if err := coredns.WaitForScheduleOnFargate(clientSet, retryPolicy); err != nil {
return err
}
}
}
return nil
}
86 changes: 86 additions & 0 deletions pkg/eks/fargate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package eks

import (
"time"

"github.com/kris-nova/logger"
"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"

"github.com/weaveworks/eksctl/pkg/fargate"
"github.com/weaveworks/eksctl/pkg/fargate/coredns"
"github.com/weaveworks/eksctl/pkg/utils/retry"
"github.com/weaveworks/eksctl/pkg/utils/strings"

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

type fargateProfilesTask struct {
info string
clusterProvider *ClusterProvider
spec *api.ClusterConfig
}

func (fpt *fargateProfilesTask) Describe() string { return fpt.info }

func (fpt *fargateProfilesTask) Do(errCh chan error) error {
defer close(errCh)
if err := DoCreateFargateProfiles(fpt.spec, fpt.clusterProvider); err != nil {
return err
}
clientSet, err := fpt.clusterProvider.NewStdClientSet(fpt.spec)
if err != nil {
return err
}
if err := ScheduleCoreDNSOnFargateIfRelevant(fpt.spec, fpt.clusterProvider, clientSet); err != nil {
return err
}
return nil
}

// DoCreateFargateProfiles creates fargate profiles as specified in the config
func DoCreateFargateProfiles(config *api.ClusterConfig, ctl *ClusterProvider) error {
clusterName := config.Metadata.Name
awsClient := fargate.NewClientWithWaitTimeout(clusterName, ctl.Provider.EKS(), ctl.Provider.WaitTimeout())
for _, profile := range config.FargateProfiles {
logger.Info("creating Fargate profile %q on EKS cluster %q", profile.Name, clusterName)

// Default the pod execution role ARN to be the same as the cluster
// role defined in CloudFormation:
if profile.PodExecutionRoleARN == "" {
profile.PodExecutionRoleARN = strings.EmptyIfNil(config.IAM.FargatePodExecutionRoleARN)
}
// Linearise the creation of Fargate profiles by passing
// wait = true, as the API otherwise errors out with:
// ResourceInUseException: Cannot create Fargate Profile
// ${name2} because cluster ${clusterName} currently has
// Fargate profile ${name1} in status CREATING
if err := awsClient.CreateProfile(profile, true); err != nil {
return errors.Wrapf(err, "failed to create Fargate profile %q on EKS cluster %q", profile.Name, clusterName)
}
logger.Info("created Fargate profile %q on EKS cluster %q", profile.Name, clusterName)
}
return nil
}

func ScheduleCoreDNSOnFargateIfRelevant(config *api.ClusterConfig, ctl *ClusterProvider, clientSet kubernetes.Interface) error {
if coredns.IsSchedulableOnFargate(config.FargateProfiles) {
scheduled, err := coredns.IsScheduledOnFargate(clientSet)
if err != nil {
return err
}
if !scheduled {
if err := coredns.ScheduleOnFargate(clientSet); err != nil {
return err
}
retryPolicy := &retry.TimingOutExponentialBackoff{
Timeout: ctl.Provider.WaitTimeout(),
TimeUnit: time.Second,
}
if err := coredns.WaitForScheduleOnFargate(clientSet, retryPolicy); err != nil {
return err
}
}
}
return nil
}
8 changes: 8 additions & 0 deletions pkg/eks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func (c *ClusterProvider) CreateExtraClusterConfigTasks(cfg *api.ClusterConfig,
clusterProvider: c,
})
}

if cfg.IsFargateEnabled() {
newTasks.Append(&fargateProfilesTask{
info: "create fargate profiles",
spec: cfg,
clusterProvider: c,
})
}
return newTasks
}

Expand Down

0 comments on commit 0a13d65

Please sign in to comment.