Skip to content

Commit

Permalink
chore: addressing linting issues
Browse files Browse the repository at this point in the history
Signed-off-by: nathan-nicholson <nathan@konstruct.io>
  • Loading branch information
nathan-nicholson committed Jan 30, 2025
1 parent 1c765b8 commit c31b58a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 130 deletions.
11 changes: 9 additions & 2 deletions cmd/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"

"github.com/konstructio/kubefirst/internal/launch"
"github.com/konstructio/kubefirst/internal/progress"
"github.com/spf13/cobra"
)

Expand All @@ -35,8 +36,14 @@ func launchUp() *cobra.Command {
Use: "up",
Short: "launch new console and api instance",
TraverseChildren: true,
Run: func(_ *cobra.Command, _ []string) {
launch.Up(additionalHelmFlags, false, true)
RunE: func(cmd *cobra.Command, _ []string) error {

Check failure on line 39 in cmd/launch.go

View workflow job for this annotation

GitHub Actions / build

unnecessary leading newline (whitespace)

if err := launch.Up(cmd.Context(), additionalHelmFlags, false, true); err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to launch console and api: %w", err)
}

return nil
},
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func Execute() {
SilenceUsage: true,
}

output := rootCmd.ErrOrStderr()

rootCmd.AddCommand(
aws.NewCommand(),
azure.NewCommand(),
Expand Down Expand Up @@ -74,9 +76,8 @@ func Execute() {
common.CheckForVersionUpdate()
progressPrinter.GetInstance()
if err := rootCmd.Execute(); err != nil {
fmt.Println("Error occurred during command execution:", err)
fmt.Println("If a detailed error message was available, please make the necessary corrections before retrying.")
fmt.Println("You can re-run the last command to try the operation again.")
fmt.Fprintln(output, "If a detailed error message was available, please make the necessary corrections before retrying.")
fmt.Fprintln(output, "You can re-run the last command to try the operation again.")
progress.Progress.Quit()
}
}
102 changes: 39 additions & 63 deletions internal/launch/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ import (
var consoleClusterName = "kubefirst-console"

// Up
func Up(additionalHelmFlags []string, inCluster, useTelemetry bool) {
func Up(ctx context.Context, additionalHelmFlags []string, inCluster, useTelemetry bool) error {
if viper.GetBool("launch.deployed") {
message := `##
Kubefirst console has already been deployed. To start over, run` + "`" + `kubefirst launch down` + "`" + `to completely remove the existing console.`

progress.Success(message)
return
return nil
}

if !inCluster {
Expand All @@ -50,9 +50,10 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi

homeDir, err := os.UserHomeDir()
if err != nil {
progress.Error(fmt.Sprintf("unable to get user's home directory: %s", err))
return
err = fmt.Errorf("unable to get user's home directory: %s", err)

Check failure on line 53 in internal/launch/cmd.go

View workflow job for this annotation

GitHub Actions / build

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
return err
}

dir := fmt.Sprintf("%s/.k1/%s", homeDir, consoleClusterName)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(dir, os.ModePerm)
Expand Down Expand Up @@ -85,13 +86,11 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
)
err = downloadManager.DownloadFile(k3dClient, k3dDownloadURL)
if err != nil {
progress.Error(fmt.Sprintf("error while trying to download k3d: %s", err))
return
return fmt.Errorf("error while trying to download k3d: %w", err)
}
err = os.Chmod(k3dClient, 0o755)
if err != nil {
progress.Error(fmt.Sprintf("error changing permissions of k3d client: %s", err))
return
return fmt.Errorf("error changing permissions of k3d client: %w", err)
}
} else {
log.Info().Msg("k3d is already installed, continuing")
Expand All @@ -113,13 +112,11 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
helmDownloadTarGzPath := fmt.Sprintf("%s/helm.tar.gz", toolsDir)
err = downloadManager.DownloadFile(helmDownloadTarGzPath, helmDownloadURL)
if err != nil {
progress.Error(fmt.Sprintf("error while trying to download helm: %s", err))
return
return fmt.Errorf("error while trying to download helm: %w", err)
}
helmTarDownload, err := os.Open(helmDownloadTarGzPath)
if err != nil {
progress.Error(fmt.Sprintf("could not read helm download content: %s", err))
return
return fmt.Errorf("could not read helm download content: %w", err)
}
downloadManager.ExtractFileFromTarGz(
helmTarDownload,
Expand All @@ -128,8 +125,7 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
)
err = os.Chmod(helmClient, 0o755)
if err != nil {
progress.Error(fmt.Sprintf("error changing permissions of helm client: %s", err))
return
return fmt.Errorf("error changing permissions of helm client: %w", err)
}
os.Remove(helmDownloadTarGzPath)
} else {
Expand All @@ -150,13 +146,11 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
)
err = downloadManager.DownloadFile(mkcertClient, mkcertDownloadURL)
if err != nil {
progress.Error(fmt.Sprintf("error while trying to download mkcert: %s", err))
return
return fmt.Errorf("error while trying to download mkcert: %w", err)
}
err = os.Chmod(mkcertClient, 0o755)
if err != nil {
progress.Error(fmt.Sprintf("error changing permissions of mkcert client: %s", err))
return
return fmt.Errorf("error changing permissions of mkcert client: %w", err)
}
} else {
log.Info().Msg("mkcert is already installed, continuing")
Expand All @@ -182,17 +176,15 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
fmt.Sprintf("%s/kubeconfig", dir),
)
if err != nil {
progress.Error(fmt.Sprintf("error creating k3d cluster: %s", err))
return
return fmt.Errorf("error creating k3d cluster: %w", err)
}

log.Info().Msg("k3d cluster for Kubefirst console and API created successfully")

// Wait for traefik
kcfg, err := k8s.CreateKubeConfig(false, kubeconfigPath)
if err != nil {
progress.Error(fmt.Sprintf("error creating kubernetes client: %s", err))
return
return fmt.Errorf("error creating kubernetes client: %w", err)
}

log.Info().Msg("Waiting for traefik...")
Expand All @@ -204,13 +196,11 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
240,
)
if err != nil {
progress.Error(fmt.Sprintf("error looking for traefik: %s", err))
return
return fmt.Errorf("error looking for traefik: %w", err)
}
_, err = k8s.WaitForDeploymentReady(kcfg.Clientset, traefikDeployment, 120)
if err != nil {
progress.Error(fmt.Sprintf("error waiting for traefik: %s", err))
return
return fmt.Errorf("error waiting for traefik: %w", err)
}
}

Expand All @@ -226,16 +216,14 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
kubeconfigPath,
)
if err != nil {
progress.Error(fmt.Sprintf("error getting kubeconfig: %s", err))
return
return fmt.Errorf("error getting kubeconfig: %w", err)
}
}

// Establish Kubernetes client for console cluster
kcfg, err := k8s.CreateKubeConfig(false, kubeconfigPath)
if err != nil {
progress.Error(fmt.Sprintf("error creating kubernetes client: %s", err))
return
return fmt.Errorf("error creating kubernetes client: %w", err)
}

// Determine if helm chart repository has already been added
Expand All @@ -247,17 +235,15 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
"yaml",
)
if err != nil {
progress.Error(fmt.Sprintf("error listing current helm repositories: %s", err))
return
return fmt.Errorf("error listing current helm repositories: %w", err)
}

var existingHelmRepositories []helm.Repo
repoExists := false

err = yaml.Unmarshal([]byte(res), &existingHelmRepositories)
if err != nil {
progress.Error(fmt.Sprintf("could not get existing helm repositories: %s", err))
return
return fmt.Errorf("could not get existing helm repositories: %w", err)
}

for _, repo := range existingHelmRepositories {
Expand All @@ -276,8 +262,7 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
helmChartRepoURL,
)
if err != nil {
progress.Error(fmt.Sprintf("error adding helm chart repository: %s", err))
return
return fmt.Errorf("error adding helm chart repository: %w", err)
}
log.Info().Msg("Added Kubefirst helm chart repository")
} else {
Expand All @@ -291,8 +276,7 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
"update",
)
if err != nil {
progress.Error(fmt.Sprintf("error updating helm chart repository: %s", err))
return
return fmt.Errorf("error updating helm chart repository: %w", err)
}
log.Info().Msg("Kubefirst helm chart repository updated")

Expand All @@ -307,17 +291,15 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
"-A",
)
if err != nil {
progress.Error(fmt.Sprintf("error listing current helm releases: %s", err))
return
return fmt.Errorf("error listing current helm releases: %w", err)
}

var existingHelmReleases []helm.Release
chartInstalled := false

err = yaml.Unmarshal([]byte(res), &existingHelmReleases)
if err != nil {
progress.Error(fmt.Sprintf("could not get existing helm releases: %s", err))
return
return fmt.Errorf("could not get existing helm releases: %w", err)
}
for _, release := range existingHelmReleases {
if release.Name == helmChartName {
Expand Down Expand Up @@ -384,10 +366,9 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
installFlags = append(installFlags, "--create-namespace")

// Install helm chart
a, b, err := shell.ExecShellReturnStrings(helmClient, installFlags...)
_, _, err := shell.ExecShellReturnStrings(helmClient, installFlags...)
if err != nil {
progress.Error(fmt.Sprintf("error installing helm chart: %s %s %s", err, a, b))
return
return fmt.Errorf("error installing helm chart: %w", err)
}

log.Info().Msg("Kubefirst console helm chart installed successfully")
Expand All @@ -409,23 +390,20 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
240,
)
if err != nil {
progress.Error(fmt.Sprintf("error looking for kubefirst api: %s", err))
return
return fmt.Errorf("error looking for kubefirst api: %w", err)
}

_, err = k8s.WaitForDeploymentReady(kcfg.Clientset, apiDeployment, 300)
if err != nil {
progress.Error(fmt.Sprintf("error waiting for kubefirst api: %s", err))
return
return fmt.Errorf("error waiting for kubefirst api: %w", err)
}

// Generate certificate for console
sslPemDir := fmt.Sprintf("%s/ssl", dir)
if _, err := os.Stat(sslPemDir); os.IsNotExist(err) {
err := os.MkdirAll(sslPemDir, os.ModePerm)
if err != nil {
progress.Error(fmt.Sprintf("error creating directory for certificates: %s", err))
return
return fmt.Errorf("error creating directory for certificates: %w", err)
}
}
log.Info().Msg("Certificate directory created")
Expand All @@ -434,8 +412,7 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
if _, err := os.Stat(mkcertPemDir); os.IsNotExist(err) {
err := os.MkdirAll(mkcertPemDir, os.ModePerm)
if err != nil {
progress.Error(fmt.Sprintf("error creating directory for certificates: %s", err))
return
return fmt.Errorf("error creating directory for certificates: %w", err)
}
}

Expand All @@ -453,27 +430,24 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
fullAppAddress,
)
if err != nil {
progress.Error(fmt.Sprintf("error generating certificate for console: %s", err))
return
return fmt.Errorf("error generating certificate for console: %w", err)
}

// * read certificate files
certPem, err := os.ReadFile(fmt.Sprintf("%s/%s-cert.pem", mkcertPemDir, "kubefirst-console"))
if err != nil {
progress.Error(fmt.Sprintf("error reading certificate for console: %s", err))
return
return fmt.Errorf("error reading certificate for console: %w", err)
}
keyPem, err := os.ReadFile(fmt.Sprintf("%s/%s-key.pem", mkcertPemDir, "kubefirst-console"))
if err != nil {
progress.Error(fmt.Sprintf("error reading key for console: %s", err))
return
return fmt.Errorf("error reading key for console: %w", err)
}

_, err = kcfg.Clientset.CoreV1().Secrets(namespace).Get(context.Background(), "kubefirst-console-tls", metav1.GetOptions{})
_, err = kcfg.Clientset.CoreV1().Secrets(namespace).Get(ctx, "kubefirst-console-tls", metav1.GetOptions{})
if err == nil {
log.Info().Msg(fmt.Sprintf("kubernetes secret %q already created - skipping", "kubefirst-console"))
} else if strings.Contains(err.Error(), "not found") {
_, err = kcfg.Clientset.CoreV1().Secrets(namespace).Create(context.Background(), &v1.Secret{
_, err = kcfg.Clientset.CoreV1().Secrets(namespace).Create(ctx, &v1.Secret{
Type: "kubernetes.io/tls",
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-tls", "kubefirst-console"),
Expand All @@ -485,8 +459,8 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
},
}, metav1.CreateOptions{})
if err != nil {
progress.Error(fmt.Sprintf("error creating kubernetes secret for cert: %s", err))
return
return fmt.Errorf("error creating kubernetes secret for cert: %w", err)

}

Check failure on line 464 in internal/launch/cmd.go

View workflow job for this annotation

GitHub Actions / build

unnecessary trailing newline (whitespace)
time.Sleep(5 * time.Second)
log.Info().Msg("Created Kubernetes Secret for certificate")
Expand Down Expand Up @@ -519,6 +493,8 @@ Kubefirst console has already been deployed. To start over, run` + "`" + `kubefi
###
#### :tada: Success` + "`Your kubefirst platform provisioner is ready`")
}

return nil
}

// Down destroys a k3d cluster for Kubefirst console and API
Expand Down
Loading

0 comments on commit c31b58a

Please sign in to comment.