Skip to content

Commit

Permalink
resolve suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
jokestax committed Aug 20, 2024
1 parent 9870bdd commit 42b7bc3
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 25 deletions.
32 changes: 15 additions & 17 deletions cmd/k3d/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
)

var (
Expand Down Expand Up @@ -395,7 +396,7 @@ func runK3d(cmd *cobra.Command, args []string) error {
log.Info().Msgf("kubefirst version configs.K1Version: %s ", configs.K1Version)
log.Info().Msgf("cloning gitops-template repo url: %s ", gitopsTemplateURLFlag)
log.Info().Msgf("cloning gitops-template repo branch: %s ", gitopsTemplateBranchFlag)
log.Info().Msgf("branch %s\b", gitopsTemplateBranchFlag)
log.Info().Msgf("branch %s ", gitopsTemplateBranchFlag)
atlantisWebhookSecret := viper.GetString("secrets.atlantis-webhook")
if atlantisWebhookSecret == "" {
atlantisWebhookSecret = utils.Random(20)
Expand Down Expand Up @@ -625,9 +626,8 @@ func runK3d(cmd *cobra.Command, args []string) error {
progressPrinter.IncrementTracker("cloning-and-formatting-git-repositories", 1)
}

prep_err := k3d.TerraformPrep(config)

if prep_err != nil {
log.Info().Msg("Terraform Prep")
if prep_err := k3d.TerraformPrep(config); prep_err != nil {
return prep_err
}

Expand Down Expand Up @@ -1071,30 +1071,28 @@ func runK3d(cmd *cobra.Command, args []string) error {

err = k3d.RestartDeployment(context.Background(), kcfg.Clientset, "argocd", "argocd-applicationset-controller")
if err != nil {
return fmt.Errorf("Error in Restarting argocd controller %w", err)
return fmt.Errorf("error in restarting argocd controller %w", err)
}
retryAttempts := 2
for attempt := 1; attempt <= retryAttempts; attempt++ {
log.Info().Msgf("Attempt #%d to create Argo CD application...\n", attempt)

app, err := argocdClient.ArgoprojV1alpha1().Applications("argocd").Create(context.Background(), registryApplicationObject, metav1.CreateOptions{})
err = wait.PollImmediate(5*time.Second, 20*time.Second, func() (bool, error) {
_, err := argocdClient.ArgoprojV1alpha1().Applications("argocd").Create(context.Background(), registryApplicationObject, metav1.CreateOptions{})
if err != nil {
if attempt == retryAttempts {
return err
}
log.Info().Msgf("Error creating Argo CD application on attempt number #%d: %v\n", attempt, err)
time.Sleep(5 * time.Second)
continue
return false, nil
}
return true, nil
})

log.Info().Msgf("Argo CD application created successfully on attempt #%d: %s\n", attempt, app.Name)
break
if err != nil {
return fmt.Errorf("error creating argocd application : %w", err)
}

log.Info().Msg("Argo CD application created successfully\n")

viper.Set("kubefirst-checks.argocd-create-registry", true)
viper.WriteConfig()
telemetry.SendEvent(segClient, telemetry.CreateRegistryCompleted, "")
progressPrinter.IncrementTracker("installing-argo-cd", 1)

} else {
log.Info().Msg("argocd registry create already done, continuing")
progressPrinter.IncrementTracker("installing-argo-cd", 1)
Expand Down
8 changes: 6 additions & 2 deletions cmd/k3d/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func destroyK3d(cmd *cobra.Command, args []string) error {

// Check for existing port forwards before continuing

gitopsRepoName, metaphorRepoName, _ := common.GetGitmeta(clusterName)
gitopsRepoName, metaphorRepoName, err := common.GetGitmeta(clusterName)

err := k8s.CheckForExistingPortForwards(9000)
if err != nil {
return fmt.Errorf("error in getting repo info: %w", err)
}

err = k8s.CheckForExistingPortForwards(9000)
if err != nil {
log.Error().Msgf("%s - this port is required to tear down your kubefirst environment - please close any existing port forwards before continuing", err.Error())
return fmt.Errorf("%s (maybe the handoff screen is still open in another terminal) - this port is required to tear down your kubefirst environment - please close any existing port forwards before continuing", err.Error())
Expand Down
7 changes: 6 additions & 1 deletion cmd/k3d/mkcert.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ func mkCert(cmd *cobra.Command, args []string) error {
return err
}

gitopsRepoName, metaphorRepoName, _ := common.GetGitmeta(viper.GetString("flags.cluster-name"))
gitopsRepoName, metaphorRepoName, err := common.GetGitmeta(viper.GetString("flags.cluster-name"))

if err != nil {
return fmt.Errorf("error in getting repo info: %w", err)
}

flags := utils.GetClusterStatusFlags()
if !flags.SetupComplete {
return fmt.Errorf("there doesn't appear to be an active k3d cluster")
Expand Down
7 changes: 6 additions & 1 deletion cmd/k3d/root-credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ func getK3dRootCredentials(cmd *cobra.Command, args []string) error {
gitProvider := viper.GetString("flags.git-provider")
gitProtocol := viper.GetString("flags.git-protocol")
gitOwner := viper.GetString(fmt.Sprintf("flags.%s-owner", gitProvider))
gitopsRepoName, metaphorRepoName, _ := common.GetGitmeta(clusterName)
gitopsRepoName, metaphorRepoName, err := common.GetGitmeta(clusterName)

if err != nil {
return fmt.Errorf("error in getting repo info: %w", err)
}

// Parse flags
a, err := cmd.Flags().GetBool("argocd")
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion cmd/k3d/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func unsealVault(cmd *cobra.Command, args []string) error {
if !flags.SetupComplete {
return fmt.Errorf("there doesn't appear to be an active k3d cluster")
}
gitopsRepoName, metaphorRepoName, _ := common.GetGitmeta(viper.GetString("flags.cluster-name"))
gitopsRepoName, metaphorRepoName, err := common.GetGitmeta(viper.GetString("flags.cluster-name"))

if err != nil {
return fmt.Errorf("error in getting repo info: %w", err)
}

config := k3d.GetConfig(
viper.GetString("flags.cluster-name"),
flags.GitProvider,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
go.mongodb.org/mongo-driver v1.10.3
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
k8s.io/api v0.27.1
k8s.io/apimachinery v0.27.1
k8s.io/apimachinery v0.31.0
k8s.io/client-go v11.0.1-0.20190816222228-6d55c1b1f1ca+incompatible
)

Expand Down
9 changes: 7 additions & 2 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func GetGitmeta(clusterName string) (string, string, error) {

homePath, err := os.UserHomeDir()
if err != nil {
return "", "", fmt.Errorf("Error in homePath %w", err)
return "", "", fmt.Errorf("unable to get user's home directory: %w", err)
}

basePath := filepath.Join(homePath, ".k1", clusterName)
Expand All @@ -133,7 +133,12 @@ func GetGitmeta(clusterName string) (string, string, error) {
}

if info.IsDir() {
relPath, _ := filepath.Rel(basePath, path)
relPath, err := filepath.Rel(basePath, path)

if err != nil {
return fmt.Errorf("error while finding repository : %w", err)
}

if relPath == "." || strings.Count(relPath, string(os.PathSeparator)) == 1 {
if info.Name() == "registry" {
if !gitopsFound {
Expand Down

0 comments on commit 42b7bc3

Please sign in to comment.