Skip to content

Commit

Permalink
feat(provider): support gce provider
Browse files Browse the repository at this point in the history
  • Loading branch information
JacieChao authored and Jason-ZW committed Nov 22, 2021
1 parent 1d24018 commit 1b1e9ce
Show file tree
Hide file tree
Showing 13 changed files with 1,125 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ vendor/
# Third-party directories
.idea/
.DS_Store
.vscode/

# Compile the resulting file
dist/
Expand Down
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// import custom provider
_ "github.com/cnrancher/autok3s/pkg/providers/alibaba"
_ "github.com/cnrancher/autok3s/pkg/providers/aws"
_ "github.com/cnrancher/autok3s/pkg/providers/google"
_ "github.com/cnrancher/autok3s/pkg/providers/k3d"
_ "github.com/cnrancher/autok3s/pkg/providers/native"
_ "github.com/cnrancher/autok3s/pkg/providers/tencent"
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go v1.0.34
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/api v0.44.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.20.12
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cloud.google.com/go v0.72.0/go.mod h1:M+5Vjvlc2wnp6tjzE102Dw08nGShTscUx2nZMufOKP
cloud.google.com/go v0.74.0/go.mod h1:VV1xSbzvo+9QJOxLDaJfTjx5e+MePCpCWwvftOeQmWk=
cloud.google.com/go v0.78.0/go.mod h1:QjdrLG0uq+YwhjoVOLsS1t7TW8fs36kLs4XO5R5ECHg=
cloud.google.com/go v0.79.0/go.mod h1:3bzgcEeQlzbuEAYu4mrWhKqWjmpprinYgKJLgKHnbb8=
cloud.google.com/go v0.81.0 h1:at8Tk2zUz63cLPR0JPWm5vp77pEZmzxEQBEfRKn1VV8=
cloud.google.com/go v0.81.0/go.mod h1:mk/AM35KwGk/Nm2YSeZbxXdrNK3KZOYHmLkOqC2V6E0=
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE=
Expand Down Expand Up @@ -461,6 +462,7 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/gnostic v0.0.0-20170426233943-68f4ded48ba9/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
github.com/googleapis/gnostic v0.2.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
Expand Down Expand Up @@ -1375,6 +1377,7 @@ google.golang.org/api v0.36.0/go.mod h1:+z5ficQTmoYpPn8LCUNVpK5I7hwkpjbcgqA7I34q
google.golang.org/api v0.40.0/go.mod h1:fYKFpnQN0DsDSKRVRcQSDQNtqWPfM9i+zNPxepjRCQ8=
google.golang.org/api v0.41.0/go.mod h1:RkxM5lITDfTzmyKFPt+wGrCJbVfniCr2ool8kTBzRTU=
google.golang.org/api v0.43.0/go.mod h1:nQsDGjRXMo4lvh5hP0TKqF244gqhGcr/YSIykhUk/94=
google.golang.org/api v0.44.0 h1:URs6qR1lAxDsqWITsQXI4ZkGiYJ5dHtRNiCpfs2OeKA=
google.golang.org/api v0.44.0/go.mod h1:EBOGZqzyhtvMDoxwS97ctnh0zUmYY6CxqXsc1AvkYD8=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down
2 changes: 1 addition & 1 deletion hack/make-rules/autok3s.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CURR_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"

# The root of the autok3s directory
ROOT_DIR="${CURR_DIR}"
UI_VERSION="v0.4.4"
UI_VERSION="v0.4.5"

source "${ROOT_DIR}/hack/lib/init.sh"
source "${CURR_DIR}/hack/lib/constant.sh"
Expand Down
5 changes: 0 additions & 5 deletions pkg/providers/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ func (p *Alibaba) JoinK3sNode() (err error) {
return p.JoinNodes(p.generateInstance, func() error { return nil }, false, p.rollbackInstance)
}

// Rollback rollback operate.
func (p *Alibaba) Rollback() error {
return p.RollbackCluster(p.rollbackInstance)
}

func (p *Alibaba) rollbackInstance(ids []string) error {
if len(ids) > 0 {
p.releaseEipAddresses(true)
Expand Down
5 changes: 0 additions & 5 deletions pkg/providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,6 @@ func (p *Amazon) SetConfig(config []byte) error {
return nil
}

// Rollback rollback operate.
func (p *Amazon) Rollback() error {
return p.RollbackCluster(p.rollbackInstance)
}

func (p *Amazon) rollbackInstance(ids []string) error {
if err := p.terminateInstance(ids); err != nil {
return err
Expand Down
291 changes: 291 additions & 0 deletions pkg/providers/google/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
package google

import (
"encoding/json"
"reflect"

"github.com/cnrancher/autok3s/pkg/types"
"github.com/cnrancher/autok3s/pkg/types/google"
"github.com/cnrancher/autok3s/pkg/utils"
)

const createUsageExample = ` autok3s -d create \
--provider google \
--name <cluster name> \
--master 1
`

const joinUsageExample = ` autok3s -d join \
--provider google \
--name <cluster name> \
--worker 1
`

const deleteUsageExample = ` autok3s -d delete \
--provider google \
--name <cluster name>
`

const sshUsageExample = ` autok3s ssh \
--provider google \
--name <cluster name> \
--region <region>
`

// GetUsageExample return cli usage example for provider
func (p *Google) GetUsageExample(action string) string {
switch action {
case "create":
return createUsageExample
case "join":
return joinUsageExample
case "delete":
return deleteUsageExample
case "ssh":
return sshUsageExample
default:
return ""
}
}

// GetCreateFlags returns google create flags.
func (p *Google) GetCreateFlags() []types.Flag {
cSSH := p.GetSSHConfig()
p.SSH = *cSSH
fs := p.GetClusterOptions()
fs = append(fs, p.GetCreateOptions()...)
return fs
}

// GetSSHConfig returns google ssh config.
func (p *Google) GetSSHConfig() *types.SSH {
ssh := &types.SSH{
SSHUser: defaultUser,
SSHPort: "22",
}
return ssh
}

// GetOptionFlags returns google option flags.
func (p *Google) GetOptionFlags() []types.Flag {
return p.sharedFlags()
}

// GetDeleteFlags returns google option flags.
func (p *Google) GetDeleteFlags() []types.Flag {
return []types.Flag{
{
Name: "name",
P: &p.Name,
V: p.Name,
Usage: "Set the name of the kubeconfig context",
ShortHand: "n",
Required: true,
},
{
Name: "region",
P: &p.Region,
V: p.Region,
Usage: "GCE region",
EnvVar: "GOOGLE_REGION",
},
}
}

// GetJoinFlags returns google join flags.
func (p *Google) GetJoinFlags() []types.Flag {
fs := p.sharedFlags()
fs = append(fs, p.GetClusterOptions()...)
return fs
}

// GetSSHFlags returns google ssh flags.
func (p *Google) GetSSHFlags() []types.Flag {
fs := []types.Flag{
{
Name: "name",
P: &p.Name,
V: p.Name,
Usage: "Set the name of the kubeconfig context",
ShortHand: "n",
Required: true,
},
{
Name: "region",
P: &p.Region,
V: p.Region,
Usage: "GCE region",
EnvVar: "GOOGLE_REGION",
},
}
fs = append(fs, p.GetSSHOptions()...)

return fs
}

// GetCredentialFlags return google credential flags.
func (p *Google) GetCredentialFlags() []types.Flag {
return []types.Flag{
{
Name: "service-account-file",
P: &p.ServiceAccountFile,
V: p.ServiceAccountFile,
Usage: "GCE service account json file for OAuth2 validation",
EnvVar: "GOOGLE_SERVICE_ACCOUNT_FILE",
Required: true,
},
{
Name: "service-account",
P: &p.ServiceAccount,
V: p.ServiceAccount,
Usage: "GCE service account to attach to VM (email address)",
EnvVar: "GOOGLE_SERVICE_ACCOUNT",
Required: true,
},
}
}

// BindCredential bind google credential.
func (p *Google) BindCredential() error {
secretMap := map[string]string{
"service-account-file": p.ServiceAccountFile,
"service-account": p.ServiceAccount,
}
return p.SaveCredential(secretMap)
}

// MergeClusterOptions merge google cluster options.
func (p *Google) MergeClusterOptions() error {
opt, err := p.MergeConfig()
if err != nil {
return err
}
stateOption, err := p.GetProviderOptions(opt)
if err != nil {
return err
}
option := stateOption.(*google.Options)
p.CloudControllerManager = option.CloudControllerManager

// merge options
source := reflect.ValueOf(&p.Options).Elem()
target := reflect.ValueOf(option).Elem()
utils.MergeConfig(source, target)

return nil
}

// GetProviderOptions return Google Cloud Provider options.
func (p *Google) GetProviderOptions(opt []byte) (interface{}, error) {
options := &google.Options{}
err := json.Unmarshal(opt, options)
return options, err
}

func (p *Google) sharedFlags() []types.Flag {
return []types.Flag{
{
Name: "region",
P: &p.Region,
V: p.Region,
Usage: "GCE region",
EnvVar: "GOOGLE_REGION",
},
{
Name: "zone",
P: &p.Zone,
V: p.Zone,
Usage: "GCE zone",
EnvVar: "GOOGLE_ZONE",
},
{
Name: "machine-type",
P: &p.MachineType,
V: p.MachineType,
Usage: "GCE machine type",
EnvVar: "GOOGLE_MACHINE_TYPE",
},
{
Name: "machine-image",
P: &p.MachineImage,
V: p.MachineImage,
Usage: "GCE machine image url",
EnvVar: "GOOGLE_MACHINE_IMAGE",
},
{
Name: "project",
P: &p.Project,
V: p.Project,
Usage: "GCE Project",
EnvVar: "GOOGLE_PROJECT",
Required: true,
},
{
Name: "scopes",
P: &p.Scopes,
V: p.Scopes,
Usage: "GCE scopes",
EnvVar: "GOOGLE_SCOPES",
},
{
Name: "disk-size",
P: &p.DiskSize,
V: p.DiskSize,
Usage: "GCE instance disk size (in GB)",
EnvVar: "GOOGLE_DISK_SIZE",
},
{
Name: "disk-type",
P: &p.DiskType,
V: p.DiskType,
Usage: "GCE instance disk type",
EnvVar: "GOOGLE_DISK_TYPE",
},
{
Name: "network",
P: &p.Network,
V: p.Network,
Usage: "Specify network in which to provision vm",
EnvVar: "GOOGLE_NETWORK",
},
{
Name: "subnetwork",
P: &p.Subnetwork,
V: p.Subnetwork,
Usage: "Specify subnetwork in which to provision vm",
EnvVar: "GOOGLE_SUBNETWORK",
},
{
Name: "use-internal-ip-only",
P: &p.UseInternalIPOnly,
V: p.UseInternalIPOnly,
Usage: "Configure GCE instance to not have an external IP address",
EnvVar: "GOOGLE_USE_INTERNAL_IP_ONLY",
},
{
Name: "preemptible",
P: &p.Preemptible,
V: p.Preemptible,
Usage: "GCE Instance Preemptibility",
EnvVar: "GOOGLE_PREEMPTIBLE",
},
{
Name: "tags",
P: &p.Tags,
V: p.Tags,
Usage: "Set instance additional tags, i.e.(--tags a=b --tags b=c), see: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html",
},
{
Name: "open-ports",
P: &p.OpenPorts,
V: p.OpenPorts,
Usage: "Make the specified port number accessible from the Internet, e.g, --open-ports 8080/tcp --open-ports 9090/tcp",
},
{
Name: "cloud-controller-manager",
P: &p.CloudControllerManager,
V: p.CloudControllerManager,
Usage: "Enable cloud-controller-manager component",
},
}
}
Loading

0 comments on commit 1b1e9ce

Please sign in to comment.