From c90be868977f28ae275a80ce798cf4fe37255516 Mon Sep 17 00:00:00 2001 From: Jason-ZW Date: Thu, 20 Aug 2020 15:58:27 +0800 Subject: [PATCH] feat(autok3s): support multiple region scenario --- cmd/create.go | 4 ++++ cmd/join.go | 4 ++++ pkg/common/common.go | 10 +++++----- pkg/providers/alibaba/alibaba.go | 4 ++++ pkg/providers/provider.go | 2 ++ 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index c834b6f2..da2222ea 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -62,6 +62,10 @@ func CreateCommand() *cobra.Command { logrus.Fatalln(err) } + // generate cluster name. e.g. input: "--name k3s1 --region cn-hangzhou" output: "k3s1.cn-hangzhou" + cp.GenerateClusterName() + + // create k3s cluster with generated cluster name. if err := cp.CreateK3sCluster(cSSH); err != nil { logrus.Errorln(err) if rErr := cp.Rollback(); rErr != nil { diff --git a/cmd/join.go b/cmd/join.go index e763fb5e..0fba84a0 100644 --- a/cmd/join.go +++ b/cmd/join.go @@ -62,6 +62,10 @@ func JoinCommand() *cobra.Command { logrus.Fatalln(err) } + // generate cluster name. e.g. input: "--name k3s1 --region cn-hangzhou" output: "k3s1.cn-hangzhou" + cp.GenerateClusterName() + + // join k3s node to the cluster which named with generated cluster name. if err := jp.JoinK3sNode(jSSH); err != nil { logrus.Errorln(err) if rErr := cp.Rollback(); rErr != nil { diff --git a/pkg/common/common.go b/pkg/common/common.go index 2474146b..2365d08b 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -7,11 +7,11 @@ import ( ) const ( - MasterInstancePrefix = "autok3s.%s.m" // autok3s..m - WorkerInstancePrefix = "autok3s.%s.w" // autok3s..w - MasterInstanceName = MasterInstancePrefix + "[%d,%d]" // autok3s..m - WorkerInstanceName = WorkerInstancePrefix + "[%d,%d]" // autok3s..w - WildcardInstanceName = "autok3s.%s.*" // autok3s..* + MasterInstancePrefix = "autok3s.%s.m" // autok3s..m + WorkerInstancePrefix = "autok3s.%s.w" // autok3s..w + MasterInstanceName = MasterInstancePrefix + "[%d,%d]" // autok3s..m + WorkerInstanceName = WorkerInstancePrefix + "[%d,%d]" // autok3s..w + WildcardInstanceName = "autok3s.%s.*" // autok3s..* BindPrefix = "autok3s.providers.%s.%s" ConfigFile = "config.yaml" StateFile = ".state" diff --git a/pkg/providers/alibaba/alibaba.go b/pkg/providers/alibaba/alibaba.go index bdc62a1b..66157e0a 100644 --- a/pkg/providers/alibaba/alibaba.go +++ b/pkg/providers/alibaba/alibaba.go @@ -67,6 +67,10 @@ func (p *Alibaba) GetProviderName() string { return "alibaba" } +func (p *Alibaba) GenerateClusterName() { + p.Name = fmt.Sprintf("%s.%s", p.Name, p.Region) +} + func (p *Alibaba) CreateK3sCluster(ssh *types.SSH) error { s := utils.NewSpinner("Generating K3s cluster: ") s.Start() diff --git a/pkg/providers/provider.go b/pkg/providers/provider.go index f99ba63d..ccf5fbc5 100644 --- a/pkg/providers/provider.go +++ b/pkg/providers/provider.go @@ -20,6 +20,8 @@ type Provider interface { GetCredentialFlags(cmd *cobra.Command) *pflag.FlagSet // Use this method to bind Viper, although it is somewhat repetitive. BindCredentialFlags() *pflag.FlagSet + // Generate cluster name. + GenerateClusterName() // K3s create cluster interface. CreateK3sCluster(ssh *types.SSH) error // K3s join node interface.