Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat k3d #1846

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 28 additions & 45 deletions cmd/k3d/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
githttps "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/kubefirst/kubefirst-api/pkg/handlers"
"github.com/kubefirst/kubefirst-api/pkg/reports"
runtimetypes "github.com/kubefirst/kubefirst-api/pkg/types"
utils "github.com/kubefirst/kubefirst-api/pkg/utils"
"github.com/kubefirst/kubefirst-api/pkg/wrappers"
"github.com/kubefirst/kubefirst/internal/gitShim"
"github.com/kubefirst/kubefirst/internal/telemetryShim"
Expand Down Expand Up @@ -176,7 +174,7 @@ func runK3d(cmd *cobra.Command, args []string) error {
viper.Set("flags.domain-name", k3d.DomainName)
viper.Set("flags.git-provider", gitProviderFlag)
viper.Set("flags.git-protocol", gitProtocolFlag)

viper.Set("kubefirst.cloud-provider", "k3d")
viper.WriteConfig()

// Switch based on git provider, set params
Expand Down Expand Up @@ -1336,40 +1334,23 @@ func runK3d(cmd *cobra.Command, args []string) error {
progressPrinter.IncrementTracker("wrapping-up", 1)

// Wait for console Deployment Pods to transition to Running
consoleDeployment, err := k8s.ReturnDeploymentObject(
argoDeployment, err := k8s.ReturnDeploymentObject(
kcfg.Clientset,
"app.kubernetes.io/instance",
"kubefirst",
"kubefirst",
"argo",
"argo",
600,
)
if err != nil {
log.Error().Msgf("Error finding console Deployment: %s", err)
log.Error().Msgf("Error finding argo workflows Deployment: %s", err)
return err
}
_, err = k8s.WaitForDeploymentReady(kcfg.Clientset, consoleDeployment, 120)
_, err = k8s.WaitForDeploymentReady(kcfg.Clientset, argoDeployment, 120)
if err != nil {
log.Error().Msgf("Error waiting for console Deployment ready state: %s", err)
log.Error().Msgf("Error waiting for argo workflows Deployment ready state: %s", err)
return err
}

// * console port-forward
consoleStopChannel := make(chan struct{}, 1)
defer func() {
close(consoleStopChannel)
}()
k8s.OpenPortForwardPodWrapper(
kcfg.Clientset,
kcfg.RestConfig,
"kubefirst-console",
"kubefirst",
8080,
9094,
consoleStopChannel,
)

progressPrinter.IncrementTracker("wrapping-up", 1)

// Mark cluster install as complete
telemetryShim.Transmit(useTelemetryFlag, segmentClient, segment.MetricClusterInstallCompleted, "")
viper.Set("kubefirst-checks.cluster-install-complete", true)
Expand All @@ -1378,33 +1359,35 @@ func runK3d(cmd *cobra.Command, args []string) error {
// Set flags used to track status of active options
helpers.SetClusterStatusFlags(k3d.CloudProvider, config.GitProvider)

//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,
}
cluster := utilities.CreateClusterRecordFromRaw(useTelemetryFlag, cGitOwner, cGitUser, cGitToken, cGitlabOwnerGroupID, gitopsTemplateURLFlag, gitopsTemplateBranchFlag)

err = utils.ExportCluster(kubernetesConfig, cl)
err = utilities.ExportCluster(cluster, kcfg)
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)
kubefirstDeployment, err := k8s.ReturnDeploymentObject(
kcfg.Clientset,
"app.kubernetes.io/instance",
"kubefirst",
"kubefirst",
600,
)
if err != nil {
log.Error().Msgf("Error finding kubefirst Deployment: %s", err)
return err
}
_, err = k8s.WaitForDeploymentReady(kcfg.Clientset, kubefirstDeployment, 120)
if err != nil {
log.Error().Msgf("Error waiting for kubefirst Deployment ready state: %s", err)
return err
}
progressPrinter.IncrementTracker("wrapping-up", 1)

err = pkg.OpenBrowser(pkg.KubefirstConsoleLocalURLTLS)
if err != nil {
log.Error().Err(err).Msg("")
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/go-git/go-git/v5 v5.6.1
github.com/hashicorp/vault/api v1.9.0
github.com/kubefirst/kubefirst-api v0.0.2
github.com/kubefirst/runtime v0.3.24
github.com/kubefirst/runtime v0.3.28
github.com/rs/zerolog v1.29.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,8 @@ 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/kubefirst-api v0.0.2 h1:ezvCOiafSJ9arapBAnewDU8a/r3VO6GAJywQrQs9QDU=
github.com/kubefirst/kubefirst-api v0.0.2/go.mod h1:SFnfUf9aY0sLmju1UvDGxLHL3mxGV94RzHwHrf27vsI=
github.com/kubefirst/runtime v0.3.24 h1:qXmQnn3kXmKNenY0DPn/0k6479Ydv3AZr7o7lOu9eTI=
github.com/kubefirst/runtime v0.3.24/go.mod h1:Ms1VyNgJW2thrAA+fj1V6bCN1DcbOyzIzw9Vc3WZkjg=
github.com/kubefirst/runtime v0.3.28 h1:Ac9NzqX+H0fa4tUauOMrjbtpSFnNluLYmSk6v5u++G4=
github.com/kubefirst/runtime v0.3.28/go.mod h1:9egAgF5cF594mIzra5nTB1kfE57Hr/Nvl79tjLsAWCQ=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo=
github.com/libopenstorage/openstorage v1.0.0/go.mod h1:Sp1sIObHjat1BeXhfMqLZ14wnOzEhNx2YQedreMcUyc=
Expand Down
11 changes: 9 additions & 2 deletions internal/gitShim/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ type GitInitParameters struct {

// InitializeGitProvider
func InitializeGitProvider(p *GitInitParameters) error {
progress.AddStep("Validate git environment")
cloudProvider := viper.Get("kubefirst.cloud-provider")
showProgress := cloudProvider != "k3d"

if showProgress {
progress.AddStep("Validate git environment")
}

switch p.GitProvider {
case "github":
Expand Down Expand Up @@ -114,7 +119,9 @@ func InitializeGitProvider(p *GitInitParameters) error {
}
}

progress.CompleteStep("Validate git environment")
if showProgress {
progress.CompleteStep("Validate git environment")
}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/launch/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
helmChartName = "kubefirst"
helmChartRepoName = "kubefirst"
helmChartRepoURL = "https://charts.kubefirst.com"
helmChartVersion = "2.3.0-rc42"
helmChartVersion = "2.3.0-rc46"
namespace = "kubefirst"
secretName = "kubefirst-initial-secrets"
)
36 changes: 24 additions & 12 deletions internal/utilities/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package utilities

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -17,9 +18,12 @@ import (
"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/internal/progress"
"github.com/kubefirst/kubefirst/internal/types"
"github.com/kubefirst/runtime/pkg/k8s"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson/primitive"
v1secret "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// CreateK1ClusterDirectory
Expand Down Expand Up @@ -193,23 +197,31 @@ func CreateClusterDefinitionRecordFromRaw(gitAuth apiTypes.GitAuth, cliFlags typ
return cl
}

func CreateClusterRecordFile(clustername string, cluster apiTypes.Cluster) error {
var localFilePath = fmt.Sprintf("%s/%s.json", exportFilePath, clustername)
func ExportCluster(cluster apiTypes.Cluster, kcfg *k8s.KubernetesClient) error {
cluster.Status = "provisioned"
cluster.InProgress = false

log.Info().Msgf("creating export file %s", localFilePath)
time.Sleep(time.Second * 10)

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
}
payload, err := json.Marshal(cluster)
if err != nil {
log.Error().Msg(err.Error())
return err
}

secret := &v1secret.Secret{
ObjectMeta: metav1.ObjectMeta{Name: "mongodb-state", Namespace: "kubefirst"},
Data: map[string][]byte{
"cluster-0": []byte(payload),
"cluster-name": []byte(cluster.ClusterName),
},
}

file, _ := json.MarshalIndent(cluster, "", " ")
_ = os.WriteFile(localFilePath, file, 0644)
err = k8s.CreateSecretV2(kcfg.Clientset, secret)

log.Info().Msgf("file created %s", localFilePath)
if err != nil {
return errors.New(fmt.Sprintf("unable to save secret to management cluster. %s", err))
}

return nil
}