diff --git a/cmd/aws/create.go b/cmd/aws/create.go index cac9ebbcd..207f567fa 100644 --- a/cmd/aws/create.go +++ b/cmd/aws/create.go @@ -46,6 +46,8 @@ import ( "github.com/kubefirst/runtime/pkg/services" internalssh "github.com/kubefirst/runtime/pkg/ssh" "github.com/kubefirst/runtime/pkg/terraform" + runtimetypes "github.com/kubefirst/runtime/pkg/types" + utils "github.com/kubefirst/runtime/pkg/utils" "github.com/kubefirst/runtime/pkg/vault" "github.com/rs/zerolog/log" "github.com/spf13/cobra" @@ -166,6 +168,16 @@ func createAws(cmd *cobra.Command, args []string) error { awsClient := &awsinternal.AWSConfiguration{ Config: awsinternal.NewAwsV2(cloudRegionFlag), } + creds, err := awsClient.Config.Credentials.Retrieve(aws.BackgroundContext()) + + if err != nil { + return err + } + + viper.Set("kubefirst.state-store-creds.access-key-id", creds.AccessKeyID) + viper.Set("kubefirst.state-store-creds.secret-access-key-id", creds.SecretAccessKey) + viper.Set("kubefirst.state-store-creds.token", creds.SessionToken) + viper.WriteConfig() _, err = awsClient.CheckAvailabilityZones(cloudRegionFlag) if err != nil { @@ -461,7 +473,9 @@ func createAws(cmd *cobra.Command, args []string) error { log.Info().Msgf("state store bucket is %s", *kubefirstStateStoreBucket.Location) log.Info().Msgf("artifacts bucket is %s", *kubefirstArtifactsBucket.Location) - viper.Set("kubefirst.state-store-bucket", strings.ReplaceAll(*kubefirstStateStoreBucket.Location, "/", "")) + viper.Set("kubefirst.state-store-bucket", kubefirstStateStoreBucketName) + viper.Set("kubefirst.state-store.name", kubefirstStateStoreBucketName) + viper.Set("kubefirst.state-store.hostname", "s3.amazonaws.com") viper.Set("kubefirst.artifacts-bucket", strings.ReplaceAll(*kubefirstArtifactsBucket.Location, "/", "")) viper.Set("kubefirst-checks.state-store-create", true) viper.WriteConfig() @@ -1432,7 +1446,7 @@ func createAws(cmd *cobra.Command, args []string) error { consoleDeployment, err := k8s.ReturnDeploymentObject( clientset, "app.kubernetes.io/instance", - "kubefirst-console", + "kubefirst", "kubefirst", 1200, ) @@ -1462,20 +1476,11 @@ func createAws(cmd *cobra.Command, args []string) error { consoleStopChannel, ) - log.Info().Msg("kubefirst installation complete") - log.Info().Msg("welcome to your new kubefirst platform powered by AWS") - time.Sleep(time.Second * 1) // allows progress bars to finish - err = pkg.IsConsoleUIAvailable(pkg.KubefirstConsoleLocalURLCloud) if err != nil { log.Error().Err(err).Msg("") } - err = pkg.OpenBrowser(pkg.KubefirstConsoleLocalURLCloud) - if err != nil { - log.Error().Err(err).Msg("") - } - // Mark cluster install as complete telemetryShim.Transmit(useTelemetryFlag, segmentClient, segment.MetricClusterInstallCompleted, "") viper.Set("kubefirst-checks.cluster-install-complete", true) @@ -1484,8 +1489,50 @@ func createAws(cmd *cobra.Command, args []string) error { // Set flags used to track status of active options helpers.SetClusterStatusFlags(awsinternal.CloudProvider, config.GitProvider) - if !ciFlag { - reports.AwsHandoffScreen(viper.GetString("components.argocd.password"), clusterNameFlag, domainNameFlag, cGitOwner, config, false) + //Export and Import Cluster + cl := utilities.CreateClusterRecordFromRaw(useTelemetryFlag, cGitOwner, cGitUser, cGitToken, cGitlabOwnerGroupID, gitopsTemplateURLFlag, gitopsTemplateBranchFlag) + + var localFilePath = fmt.Sprintf("%s/%s.json", "/tmp/api/cluster/export", clusterNameFlag) + var remoteFilePath = fmt.Sprintf("%s.json", clusterNameFlag) + utilities.CreateClusterRecordFile(clusterNameFlag, cl) + + pushObject := runtimetypes.PushBucketObject{ + LocalFilePath: localFilePath, + RemoteFilePath: remoteFilePath, + ContentType: "application/json", + } + + err = utils.PutClusterObject(&cl.StateStoreCredentials, &cl.StateStoreDetails, &pushObject) + if err != nil { + log.Error().Err(err).Msgf("error pushing cluster object, %s", cl.StateStoreDetails.Hostname) + return err + } + + kubernetesConfig := runtimetypes.KubernetesClient{ + Clientset: clientset, + KubeConfigPath: config.Kubeconfig, + RestConfig: restConfig, + } + err = utils.ExportCluster(kubernetesConfig, cl) + if err != nil { + log.Error().Err(err).Msg("error exporting cluster object") + viper.Set("kubefirst.setup-complete", false) + viper.Set("kubefirst-checks.cluster-install-complete", false) + viper.WriteConfig() + return err + } else { + err = pkg.OpenBrowser(pkg.KubefirstConsoleLocalURLCloud) + if err != nil { + log.Error().Err(err).Msg("") + } + + log.Info().Msg("kubefirst installation complete") + log.Info().Msg("welcome to your new kubefirst platform running in K3d") + time.Sleep(time.Second * 1) // allows progress bars to finish + + if !ciFlag { + reports.AwsHandoffScreen(viper.GetString("components.argocd.password"), clusterNameFlag, domainNameFlag, cGitOwner, config, false) + } } defer func(c segment.SegmentClient) { diff --git a/cmd/civo/create.go b/cmd/civo/create.go index 9caa938e1..9b38298c2 100644 --- a/cmd/civo/create.go +++ b/cmd/civo/create.go @@ -43,6 +43,8 @@ import ( internalssh "github.com/kubefirst/runtime/pkg/ssh" "github.com/kubefirst/runtime/pkg/ssl" "github.com/kubefirst/runtime/pkg/terraform" + runtimetypes "github.com/kubefirst/runtime/pkg/types" + utils "github.com/kubefirst/runtime/pkg/utils" "github.com/spf13/cobra" "github.com/spf13/viper" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -471,6 +473,7 @@ func createCivo(cmd *cobra.Command, args []string) error { log.Info().Msg(err.Error()) } + // StateStoreCredentials viper.Set("kubefirst.state-store-creds.access-key-id", creds.AccessKeyID) viper.Set("kubefirst.state-store-creds.secret-access-key-id", creds.SecretAccessKeyID) viper.Set("kubefirst.state-store-creds.name", creds.Name) @@ -549,6 +552,7 @@ func createCivo(cmd *cobra.Command, args []string) error { viper.Set("kubefirst.state-store.id", bucket.ID) viper.Set("kubefirst.state-store.name", bucket.Name) + viper.Set("kubefirst.state-store.hostname", bucket.BucketURL) viper.Set("kubefirst-checks.state-store-create", true) viper.WriteConfig() telemetryShim.Transmit(useTelemetryFlag, segmentClient, segment.MetricStateStoreCreateCompleted, "") @@ -1294,7 +1298,7 @@ func createCivo(cmd *cobra.Command, args []string) error { consoleDeployment, err := k8s.ReturnDeploymentObject( kcfg.Clientset, "app.kubernetes.io/instance", - "kubefirst-console", + "kubefirst", "kubefirst", 1200, ) @@ -1324,20 +1328,11 @@ func createCivo(cmd *cobra.Command, args []string) error { consoleStopChannel, ) - log.Info().Msg("kubefirst installation complete") - log.Info().Msg("welcome to your new kubefirst platform powered by Civo cloud") - time.Sleep(time.Second * 1) // allows progress bars to finish - err = pkg.IsConsoleUIAvailable(pkg.KubefirstConsoleLocalURLCloud) if err != nil { log.Error().Err(err).Msg("") } - err = pkg.OpenBrowser(pkg.KubefirstConsoleLocalURLCloud) - if err != nil { - log.Error().Err(err).Msg("") - } - // Mark cluster install as complete telemetryShim.Transmit(useTelemetryFlag, segmentClient, segment.MetricClusterInstallCompleted, "") viper.Set("kubefirst-checks.cluster-install-complete", true) @@ -1346,8 +1341,51 @@ func createCivo(cmd *cobra.Command, args []string) error { // Set flags used to track status of active options helpers.SetClusterStatusFlags(civo.CloudProvider, config.GitProvider) - if !ciFlag { - reports.CivoHandoffScreen(viper.GetString("components.argocd.password"), clusterNameFlag, domainNameFlag, cGitOwner, config, false) + //Export and Import Cluster + cl := utilities.CreateClusterRecordFromRaw(useTelemetryFlag, cGitOwner, cGitUser, cGitToken, cGitlabOwnerGroupID, gitopsTemplateURLFlag, gitopsTemplateBranchFlag) + + var localFilePath = fmt.Sprintf("%s/%s.json", "/tmp/api/cluster/export", clusterNameFlag) + var remoteFilePath = fmt.Sprintf("%s.json", clusterNameFlag) + utilities.CreateClusterRecordFile(clusterNameFlag, cl) + + pushObject := runtimetypes.PushBucketObject{ + LocalFilePath: localFilePath, + RemoteFilePath: remoteFilePath, + ContentType: "application/json", + } + + err = utils.PutClusterObject(&cl.StateStoreCredentials, &cl.StateStoreDetails, &pushObject) + if err != nil { + log.Error().Err(err).Msgf("error pushing cluster object, %s", cl.StateStoreDetails.Hostname) + return err + } + + kubernetesConfig := runtimetypes.KubernetesClient{ + Clientset: kcfg.Clientset, + KubeConfigPath: kcfg.KubeConfigPath, + RestConfig: kcfg.RestConfig, + } + + err = utils.ExportCluster(kubernetesConfig, cl) + if err != nil { + log.Error().Err(err).Msg("error exporting cluster object") + viper.Set("kubefirst.setup-complete", false) + viper.Set("kubefirst-checks.cluster-install-complete", false) + viper.WriteConfig() + return err + } else { + err = pkg.OpenBrowser(pkg.KubefirstConsoleLocalURLCloud) + if err != nil { + log.Error().Err(err).Msg("") + } + + log.Info().Msg("kubefirst installation complete") + log.Info().Msg("welcome to your new kubefirst platform running in K3d") + time.Sleep(time.Second * 1) // allows progress bars to finish + + if !ciFlag { + reports.CivoHandoffScreen(viper.GetString("components.argocd.password"), clusterNameFlag, domainNameFlag, cGitOwner, config, false) + } } defer func(c segment.SegmentClient) { @@ -1358,4 +1396,5 @@ func createCivo(cmd *cobra.Command, args []string) error { }(*segmentClient) return nil + } diff --git a/cmd/k3d/create.go b/cmd/k3d/create.go index 778147114..8cf92430e 100644 --- a/cmd/k3d/create.go +++ b/cmd/k3d/create.go @@ -43,6 +43,8 @@ import ( "github.com/kubefirst/runtime/pkg/services" internalssh "github.com/kubefirst/runtime/pkg/ssh" "github.com/kubefirst/runtime/pkg/terraform" + runtimetypes "github.com/kubefirst/runtime/pkg/types" + utils "github.com/kubefirst/runtime/pkg/utils" "github.com/kubefirst/runtime/pkg/wrappers" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" @@ -1136,6 +1138,11 @@ func runK3d(cmd *cobra.Command, args []string) error { bucketName := "kubefirst-state-store" log.Info().Msgf("BucketName: %s", bucketName) + viper.Set("kubefirst.state-store.name", bucketName) + viper.Set("kubefirst.state-store.hostname", "minio-console.kubefirst.dev") + viper.Set("kubefirst.state-store-creds.access-key-id", pkg.MinioDefaultUsername) + viper.Set("kubefirst.state-store-creds.secret-access-key-id", pkg.MinioDefaultPassword) + // Upload the zip file with FPutObject info, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType}) if err != nil { @@ -1327,7 +1334,7 @@ func runK3d(cmd *cobra.Command, args []string) error { consoleDeployment, err := k8s.ReturnDeploymentObject( kcfg.Clientset, "app.kubernetes.io/instance", - "kubefirst-console", + "kubefirst", "kubefirst", 600, ) @@ -1341,7 +1348,7 @@ func runK3d(cmd *cobra.Command, args []string) error { return err } - //* console port-forward + // * console port-forward consoleStopChannel := make(chan struct{}, 1) defer func() { close(consoleStopChannel) @@ -1358,15 +1365,6 @@ func runK3d(cmd *cobra.Command, args []string) error { progressPrinter.IncrementTracker("wrapping-up", 1) - log.Info().Msg("kubefirst installation complete") - log.Info().Msg("welcome to your new kubefirst platform running in K3d") - time.Sleep(time.Second * 1) // allows progress bars to finish - - err = pkg.OpenBrowser(pkg.KubefirstConsoleLocalURLTLS) - if err != nil { - log.Error().Err(err).Msg("") - } - // Mark cluster install as complete telemetryShim.Transmit(useTelemetryFlag, segmentClient, segment.MetricClusterInstallCompleted, "") viper.Set("kubefirst-checks.cluster-install-complete", true) @@ -1375,8 +1373,44 @@ func runK3d(cmd *cobra.Command, args []string) error { // Set flags used to track status of active options helpers.SetClusterStatusFlags(k3d.CloudProvider, config.GitProvider) - if !ciFlag { - reports.LocalHandoffScreenV2(viper.GetString("components.argocd.password"), clusterNameFlag, gitDestDescriptor, cGitOwner, config, false) + //Export and Import Cluster + cl := utilities.CreateClusterRecordFromRaw(useTelemetryFlag, cGitOwner, cGitUser, cGitToken, cGitlabOwnerGroupID, gitopsTemplateURLFlag, gitopsTemplateBranchFlag) + + var localFilePath = fmt.Sprintf("%s/%s.json", "/tmp/api/cluster/export", clusterNameFlag) + utilities.CreateClusterRecordFile(clusterNameFlag, cl) + + // Upload the zip file with FPutObject + info, err = minioClient.FPutObject(ctx, bucketName, fmt.Sprintf("%s.json", clusterNameFlag), localFilePath, minio.PutObjectOptions{ContentType: "application/json"}) + if err != nil { + log.Info().Msgf("Error uploading to Minio bucket: %s", err) + } + + kubernetesConfig := runtimetypes.KubernetesClient{ + Clientset: kcfg.Clientset, + KubeConfigPath: kcfg.KubeConfigPath, + RestConfig: kcfg.RestConfig, + } + + err = utils.ExportCluster(kubernetesConfig, cl) + if err != nil { + log.Error().Err(err).Msg("error exporting cluster object") + viper.Set("kubefirst.setup-complete", false) + viper.Set("kubefirst-checks.cluster-install-complete", false) + viper.WriteConfig() + return err + } else { + err = pkg.OpenBrowser(pkg.KubefirstConsoleLocalURLCloud) + if err != nil { + log.Error().Err(err).Msg("") + } + + log.Info().Msg("kubefirst installation complete") + log.Info().Msg("welcome to your new kubefirst platform running in K3d") + time.Sleep(time.Second * 1) // allows progress bars to finish + + if !ciFlag { + reports.LocalHandoffScreenV2(viper.GetString("components.argocd.password"), clusterNameFlag, gitDestDescriptor, cGitOwner, config, false) + } } defer func(c segment.SegmentClient) { diff --git a/cmd/vultr/create.go b/cmd/vultr/create.go index 4e67bf9d0..5ce933dce 100644 --- a/cmd/vultr/create.go +++ b/cmd/vultr/create.go @@ -742,6 +742,11 @@ func createVultr(cmd *cobra.Command, args []string) error { tfEntrypoint := config.GitopsDir + "/terraform/github" tfEnvs := map[string]string{} tfEnvs = vultr.GetGithubTerraformEnvs(config, tfEnvs) + // Erase public key to prevent it from being created if the git protocol argument is set to htps + switch config.GitProtocol { + case "https": + tfEnvs["TF_VAR_kbot_ssh_public_key"] = "" + } err := terraform.InitApplyAutoApprove(config.TerraformClient, tfEntrypoint, tfEnvs) if err != nil { msg := fmt.Sprintf("error creating github resources with terraform %s: %s", tfEntrypoint, err) diff --git a/go.mod b/go.mod index 59bd9c5dd..2368f8b8f 100644 --- a/go.mod +++ b/go.mod @@ -14,11 +14,12 @@ require ( github.com/dustin/go-humanize v1.0.1 github.com/go-git/go-git/v5 v5.6.1 github.com/hashicorp/vault/api v1.9.0 - github.com/kubefirst/runtime v0.3.9 + github.com/kubefirst/runtime v0.3.12 github.com/rs/zerolog v1.29.0 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.15.0 + go.mongodb.org/mongo-driver v1.10.0 k8s.io/api v0.26.2 k8s.io/apimachinery v0.27.1 k8s.io/client-go v11.0.1-0.20190816222228-6d55c1b1f1ca+incompatible @@ -107,7 +108,6 @@ require ( github.com/golang/snappy v0.0.4 // indirect github.com/google/btree v1.0.1 // indirect github.com/google/gnostic v0.6.9 // indirect - github.com/google/go-github v17.0.0+incompatible // indirect github.com/google/go-github/v45 v45.2.0 // indirect github.com/google/s2a-go v0.1.4 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect @@ -115,7 +115,6 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect github.com/googleapis/gax-go/v2 v2.11.0 // indirect github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect - github.com/hashicorp/go-version v1.2.1 // indirect github.com/jedib0t/go-pretty/v6 v6.4.6 // indirect github.com/jonboulle/clockwork v0.2.2 // indirect github.com/josharian/intern v1.0.0 // indirect @@ -166,7 +165,6 @@ require ( github.com/xlab/treeprint v1.1.0 // indirect github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect - go.mongodb.org/mongo-driver v1.10.0 // indirect go.opencensus.io v0.24.0 // indirect go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect golang.org/x/crypto v0.11.0 // indirect diff --git a/go.sum b/go.sum index a7d4275ed..db9ae3c5d 100644 --- a/go.sum +++ b/go.sum @@ -531,8 +531,6 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= -github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-github/v45 v45.2.0 h1:5oRLszbrkvxDDqBCNj2hjDZMKmvexaZ1xw/FCD+K3FI= github.com/google/go-github/v45 v45.2.0/go.mod h1:FObaZJEDSTa/WGCzZ2Z3eoCDXWJKMenWWTrd8jrta28= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= @@ -629,8 +627,6 @@ github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdv github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= -github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= -github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= @@ -721,8 +717,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/kubefirst/runtime v0.3.9 h1:Wl+gxradufEEyUAIDbQxp+4bLrnMxmQS+X6SlSJLQWw= -github.com/kubefirst/runtime v0.3.9/go.mod h1:NUa8VcB99UKEwVUvhdCfiuEYQI44XfGFV2pBtX8YnVo= +github.com/kubefirst/runtime v0.3.12 h1:C1k81WNQt59teLN6/CgQgaITz/Js2flOTkcyF8pYB1Y= +github.com/kubefirst/runtime v0.3.12/go.mod h1:NUa8VcB99UKEwVUvhdCfiuEYQI44XfGFV2pBtX8YnVo= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo= diff --git a/internal/launch/constants.go b/internal/launch/constants.go index 9b90d5c3f..26e7dd744 100644 --- a/internal/launch/constants.go +++ b/internal/launch/constants.go @@ -11,7 +11,7 @@ const ( helmChartName = "kubefirst" helmChartRepoName = "kubefirst" helmChartRepoURL = "https://charts.kubefirst.com" - helmChartVersion = "2.2.10" + helmChartVersion = "2.2.11" namespace = "kubefirst" secretName = "kubefirst-initial-secrets" ) diff --git a/internal/utilities/utilities.go b/internal/utilities/utilities.go index 02f11ce66..35010447c 100644 --- a/internal/utilities/utilities.go +++ b/internal/utilities/utilities.go @@ -7,10 +7,15 @@ See the LICENSE file for more details. package utilities import ( + "encoding/json" "fmt" "os" + "time" + "github.com/kubefirst/runtime/pkg/types" "github.com/rs/zerolog/log" + "github.com/spf13/viper" + "go.mongodb.org/mongo-driver/bson/primitive" ) // CreateK1ClusterDirectory @@ -28,3 +33,108 @@ func CreateK1ClusterDirectory(clusterName string) { } } } + +const ( + exportFilePath = "/tmp/api/cluster/export" +) + +func CreateClusterRecordFromRaw(useTelemetry bool, gitOwner string, gitUser string, gitToken string, gitlabOwnerGroupID int, gitopsTemplateURL string, gitopsTemplateBranch string) types.Cluster { + // viper.Set("flags.dns-provider", dnsProviderFlag) + // viper.Set("flags.git-protocol", gitProtocolFlag) + + cloudProvider := viper.GetString("kubefirst.cloud-provider") + domainName := viper.GetString("flags.domain-name") + gitProvider := viper.GetString("flags.git-provider") + + kubefirstTeam := os.Getenv("KUBEFIRST_TEAM") + if kubefirstTeam == "" { + kubefirstTeam = "false" + } + + cl := types.Cluster{ + ID: primitive.NewObjectID(), + CreationTimestamp: fmt.Sprintf("%v", time.Now().UTC()), + UseTelemetry: useTelemetry, + Status: "provisioned", + AlertsEmail: viper.GetString("flags.alerts-email"), + ClusterName: viper.GetString("flags.cluster-name"), + CloudProvider: cloudProvider, + CloudRegion: viper.GetString("flags.cloud-region"), + DomainName: domainName, + ClusterID: viper.GetString("kubefirst.cluster-id"), + ClusterType: "mgmt", + GitopsTemplateURL: gitopsTemplateURL, + GitopsTemplateBranch: gitopsTemplateBranch, + GitProvider: gitProvider, + GitHost: fmt.Sprintf("%s.com", gitProvider), + GitProtocol: viper.GetString("flags.git-protocol"), + DnsProvider: viper.GetString("flags.dns-provider"), + GitlabOwnerGroupID: gitlabOwnerGroupID, + AtlantisWebhookSecret: viper.GetString("secrets.atlantis-webhook"), + AtlantisWebhookURL: fmt.Sprintf("https://atlantis.%s/events", domainName), + KubefirstTeam: kubefirstTeam, + ArgoCDAuthToken: viper.GetString("components.argocd.auth-token"), + ArgoCDPassword: viper.GetString("components.argocd.password"), + GitAuth: types.GitAuth{ + Token: gitToken, + User: gitUser, + Owner: gitOwner, + PublicKey: viper.GetString("kbot.public-key"), + PrivateKey: viper.GetString("kbot.private-key"), + }, + CloudflareAuth: types.CloudflareAuth{ + Token: os.Getenv("CF_API_TOKEN"), + }, + } + + switch cloudProvider { + case "civo": + cl.CivoAuth.Token = os.Getenv("CIVO_TOKEN") + case "aws": + //ToDo: where to get credentials? + 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") + 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") + } + + cl.StateStoreCredentials.AccessKeyID = viper.GetString("kubefirst.state-store-creds.access-key-id") + cl.StateStoreCredentials.SecretAccessKey = viper.GetString("kubefirst.state-store-creds.secret-access-key-id") + cl.StateStoreCredentials.SessionToken = viper.GetString("kubefirst.state-store-creds.token") + cl.StateStoreCredentials.Name = viper.GetString("kubefirst.state-store-creds.name") + cl.StateStoreCredentials.ID = viper.GetString("kubefirst.state-store-creds.id") + + cl.StateStoreDetails.ID = viper.GetString("kubefirst.state-store.id") + cl.StateStoreDetails.Name = viper.GetString("kubefirst.state-store.name") + cl.StateStoreDetails.Hostname = viper.GetString("kubefirst.state-store.hostname") + cl.StateStoreDetails.AWSArtifactsBucket = viper.GetString("kubefirst.artifacts-bucket") + cl.StateStoreDetails.AWSStateStoreBucket = viper.GetString("kubefirst.state-store-bucket") + + return cl +} + +func CreateClusterRecordFile(clustername string, cluster types.Cluster) error { + var localFilePath = fmt.Sprintf("%s/%s.json", exportFilePath, clustername) + + log.Info().Msgf("creating export file %s", localFilePath) + + if _, err := os.Stat(exportFilePath); os.IsNotExist(err) { + log.Info().Msgf("cluster exports directory does not exist, creating") + err := os.MkdirAll(exportFilePath, 0777) + if err != nil { + return err + } + } + + file, _ := json.MarshalIndent(cluster, "", " ") + _ = os.WriteFile(localFilePath, file, 0644) + + log.Info().Msgf("file created %s", localFilePath) + + return nil +}