From 2db8530bea3e98be1401caaaedf7b1894205a0c8 Mon Sep 17 00:00:00 2001 From: CristhianF7 Date: Mon, 2 Oct 2023 15:06:17 -0500 Subject: [PATCH] feat: google envs --- cmd/google/command.go | 4 ++-- cmd/google/create.go | 5 +++++ internal/provision/provision.go | 5 +---- internal/types/flags.go | 1 + internal/utilities/flags.go | 10 +++++++++ internal/utilities/utilities.go | 40 +++++++++++++++++++++++---------- 6 files changed, 47 insertions(+), 18 deletions(-) diff --git a/cmd/google/command.go b/cmd/google/command.go index ad0828a68..19191cba7 100644 --- a/cmd/google/command.go +++ b/cmd/google/command.go @@ -80,8 +80,8 @@ func Create() *cobra.Command { createCmd.Flags().StringVar(&dnsProviderFlag, "dns-provider", "google", fmt.Sprintf("the dns provider - one of: %s", supportedDNSProviders)) createCmd.Flags().StringVar(&domainNameFlag, "domain-name", "", "the GCP DNS Name to use for DNS records (i.e. your-domain.com|subdomain.your-domain.com) (required)") createCmd.MarkFlagRequired("domain-name") - createCmd.Flags().StringVar(&gcpProjectFlag, "gcp-project", "", "gcp project id (required)") - createCmd.MarkFlagRequired("gcp-project") + createCmd.Flags().StringVar(&gcpProjectFlag, "google-project", "", "gcp project id (required)") + createCmd.MarkFlagRequired("google-project") createCmd.Flags().StringVar(&gitProviderFlag, "git-provider", "github", fmt.Sprintf("the git provider - one of: %s", supportedGitProviders)) createCmd.Flags().StringVar(&gitProtocolFlag, "git-protocol", "ssh", fmt.Sprintf("the git protocol - one of: %s", supportedGitProtocolOverride)) createCmd.Flags().StringVar(&githubOrgFlag, "github-org", "", "the GitHub organization for the new gitops and metaphor repositories - required if using github") diff --git a/cmd/google/create.go b/cmd/google/create.go index 2011928a1..c979db231 100644 --- a/cmd/google/create.go +++ b/cmd/google/create.go @@ -99,6 +99,11 @@ func ValidateProvidedFlags(gitProvider string) error { return fmt.Errorf("your GOOGLE_APPLICATION_CREDENTIALS is not set - please set and re-run your last command") } + _, err := os.Open(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")) + if err != nil { + progress.Error("Unable to read GOOGLE_APPLICATION_CREDENTIALS file") + } + switch gitProvider { case "github": key, err := internalssh.GetHostKey("github.com") diff --git a/internal/provision/provision.go b/internal/provision/provision.go index 3975d2297..e4a715320 100644 --- a/internal/provision/provision.go +++ b/internal/provision/provision.go @@ -18,12 +18,9 @@ import ( func CreateMgmtCluster(gitAuth runtimeTypes.GitAuth, cliFlags types.CliFlags) { clusterRecord := utilities.CreateClusterDefinitionRecordFromRaw( gitAuth, - cliFlags.GitopsTemplateURL, - cliFlags.GitopsTemplateBranch, + cliFlags, ) - // clusterRecord.ECR = ecrFlag - clusterCreated, err := cluster.GetCluster(clusterRecord.ClusterName) if err != nil { log.Info().Msg("cluster not found") diff --git a/internal/types/flags.go b/internal/types/flags.go index d4cceecae..743001cae 100644 --- a/internal/types/flags.go +++ b/internal/types/flags.go @@ -21,6 +21,7 @@ type CliFlags struct { GitlabGroup string GitopsTemplateBranch string GitopsTemplateURL string + GoogleProject string UseTelemetry bool Ecr bool } diff --git a/internal/utilities/flags.go b/internal/utilities/flags.go index bb6c2fb83..1d5bca5e4 100644 --- a/internal/utilities/flags.go +++ b/internal/utilities/flags.go @@ -102,6 +102,16 @@ func GetFlags(cmd *cobra.Command, cloudProvider string) (types.CliFlags, error) cliFlags.Ecr = ecrFlag } + if cloudProvider == "google" { + googleProject, err := cmd.Flags().GetString("google-project") + if err != nil { + progress.Error(err.Error()) + return cliFlags, err + } + + cliFlags.GoogleProject = googleProject + } + cliFlags.AlertsEmail = alertsEmailFlag cliFlags.CloudRegion = cloudRegionFlag cliFlags.ClusterName = clusterNameFlag diff --git a/internal/utilities/utilities.go b/internal/utilities/utilities.go index d5e59ce85..0148a22a7 100644 --- a/internal/utilities/utilities.go +++ b/internal/utilities/utilities.go @@ -9,11 +9,14 @@ package utilities import ( "encoding/json" "fmt" + "io/ioutil" "os" "time" - "github.com/kubefirst/kubefirst-api/pkg/types" + apiTypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/kubefirst/configs" + "github.com/kubefirst/kubefirst/internal/progress" + "github.com/kubefirst/kubefirst/internal/types" "github.com/rs/zerolog/log" "github.com/spf13/viper" "go.mongodb.org/mongo-driver/bson/primitive" @@ -39,7 +42,7 @@ const ( exportFilePath = "/tmp/api/cluster/export" ) -func CreateClusterRecordFromRaw(useTelemetry bool, gitOwner string, gitUser string, gitToken string, gitlabOwnerGroupID int, gitopsTemplateURL string, gitopsTemplateBranch string) types.Cluster { +func CreateClusterRecordFromRaw(useTelemetry bool, gitOwner string, gitUser string, gitToken string, gitlabOwnerGroupID int, gitopsTemplateURL string, gitopsTemplateBranch string) apiTypes.Cluster { cloudProvider := viper.GetString("kubefirst.cloud-provider") domainName := viper.GetString("flags.domain-name") gitProvider := viper.GetString("flags.git-provider") @@ -49,7 +52,7 @@ func CreateClusterRecordFromRaw(useTelemetry bool, gitOwner string, gitUser stri kubefirstTeam = "false" } - cl := types.Cluster{ + cl := apiTypes.Cluster{ ID: primitive.NewObjectID(), CreationTimestamp: fmt.Sprintf("%v", time.Now().UTC()), UseTelemetry: useTelemetry, @@ -73,14 +76,14 @@ func CreateClusterRecordFromRaw(useTelemetry bool, gitOwner string, gitUser stri KubefirstTeam: kubefirstTeam, ArgoCDAuthToken: viper.GetString("components.argocd.auth-token"), ArgoCDPassword: viper.GetString("components.argocd.password"), - GitAuth: types.GitAuth{ + GitAuth: apiTypes.GitAuth{ Token: gitToken, User: gitUser, Owner: gitOwner, PublicKey: viper.GetString("kbot.public-key"), PrivateKey: viper.GetString("kbot.private-key"), }, - CloudflareAuth: types.CloudflareAuth{ + CloudflareAuth: apiTypes.CloudflareAuth{ Token: os.Getenv("CF_API_TOKEN"), }, } @@ -116,7 +119,7 @@ func CreateClusterRecordFromRaw(useTelemetry bool, gitOwner string, gitUser stri return cl } -func CreateClusterDefinitionRecordFromRaw(gitAuth types.GitAuth, gitopsTemplateURL string, gitopsTemplateBranch string) types.ClusterDefinition { +func CreateClusterDefinitionRecordFromRaw(gitAuth apiTypes.GitAuth, cliFlags types.CliFlags) apiTypes.ClusterDefinition { cloudProvider := viper.GetString("kubefirst.cloud-provider") domainName := viper.GetString("flags.domain-name") gitProvider := viper.GetString("flags.git-provider") @@ -126,26 +129,26 @@ func CreateClusterDefinitionRecordFromRaw(gitAuth types.GitAuth, gitopsTemplateU kubefirstTeam = "false" } - cl := types.ClusterDefinition{ + cl := apiTypes.ClusterDefinition{ AdminEmail: viper.GetString("flags.alerts-email"), ClusterName: viper.GetString("flags.cluster-name"), CloudProvider: cloudProvider, CloudRegion: viper.GetString("flags.cloud-region"), DomainName: domainName, Type: "mgmt", - GitopsTemplateURL: gitopsTemplateURL, - GitopsTemplateBranch: gitopsTemplateBranch, + GitopsTemplateURL: cliFlags.GitopsTemplateURL, + GitopsTemplateBranch: cliFlags.GitopsTemplateBranch, GitProvider: gitProvider, GitProtocol: viper.GetString("flags.git-protocol"), DnsProvider: viper.GetString("flags.dns-provider"), - GitAuth: types.GitAuth{ + GitAuth: apiTypes.GitAuth{ Token: gitAuth.Token, User: gitAuth.User, Owner: gitAuth.Owner, PublicKey: viper.GetString("kbot.public-key"), PrivateKey: viper.GetString("kbot.private-key"), }, - CloudflareAuth: types.CloudflareAuth{ + CloudflareAuth: apiTypes.CloudflareAuth{ Token: os.Getenv("CF_API_TOKEN"), }, } @@ -166,18 +169,31 @@ func CreateClusterDefinitionRecordFromRaw(gitAuth types.GitAuth, gitopsTemplateU cl.AWSAuth.AccessKeyID = viper.GetString("kubefirst.state-store-creds.access-key-id") cl.AWSAuth.SecretAccessKey = viper.GetString("kubefirst.state-store-creds.secret-access-key-id") cl.AWSAuth.SessionToken = viper.GetString("kubefirst.state-store-creds.token") + cl.ECR = cliFlags.Ecr case "digitalocean": cl.DigitaloceanAuth.Token = os.Getenv("DO_TOKEN") cl.DigitaloceanAuth.SpacesKey = os.Getenv("DO_SPACES_KEY") cl.DigitaloceanAuth.SpacesSecret = os.Getenv("DO_SPACES_SECRET") case "vultr": cl.VultrAuth.Token = os.Getenv("VULTR_API_KEY") + case "google": + jsonFilePath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") + + jsonFile, err := os.Open(jsonFilePath) + if err != nil { + progress.Error("Unable to read GOOGLE_APPLICATION_CREDENTIALS file") + } + + jsonContent, _ := ioutil.ReadAll(jsonFile) + + cl.GoogleAuth.KeyFile = string(jsonContent) + cl.GoogleAuth.ProjectId = cliFlags.GoogleProject } return cl } -func CreateClusterRecordFile(clustername string, cluster types.Cluster) error { +func CreateClusterRecordFile(clustername string, cluster apiTypes.Cluster) error { var localFilePath = fmt.Sprintf("%s/%s.json", exportFilePath, clustername) log.Info().Msgf("creating export file %s", localFilePath)