Skip to content

Commit

Permalink
feat: google envs
Browse files Browse the repository at this point in the history
  • Loading branch information
CristhianF7 committed Oct 2, 2023
1 parent 2fee32e commit 2db8530
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cmd/google/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions cmd/google/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 1 addition & 4 deletions internal/provision/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions internal/types/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type CliFlags struct {
GitlabGroup string
GitopsTemplateBranch string
GitopsTemplateURL string
GoogleProject string
UseTelemetry bool
Ecr bool
}
10 changes: 10 additions & 0 deletions internal/utilities/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 28 additions & 12 deletions internal/utilities/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand All @@ -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,
Expand All @@ -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"),
},
}
Expand Down Expand Up @@ -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")
Expand All @@ -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"),
},
}
Expand All @@ -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)
Expand Down

0 comments on commit 2db8530

Please sign in to comment.