Skip to content

Commit

Permalink
#29 - refactoring latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
apanasiuk-el committed Sep 26, 2024
1 parent 7da2c5f commit 5318f79
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func Commands() []*cli.Command {
Usage: "Destroy releases",
Aliases: []string{"d"},
Before: readInputSourceWithContext(gitSpec, conf, flags["releaseHelmfile"]),
Flags: flags["releaseHelmfileWithOutput"],
Flags: flags["releaseHelmfile"],
Category: "release",
BashComplete: util.ShellCompleteCustomOutput,
Action: releaseHelmfileAction(conf),
Expand Down
12 changes: 7 additions & 5 deletions cmd/k3d.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"os"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -41,7 +42,7 @@ func (k *K3DCommands) prepareK3D(args ...string) error {

switch {
case k.APICluster:
k.SpecCMD.Envs = append(k.SpecCMD.Envs, "K3D_NAME=capi")
k.SpecCMD.Envs = append(k.SpecCMD.Envs, "K3D_NAME="+util.CAPI)
case k.K3DCluster:
k.SpecCMD.Envs = append(k.SpecCMD.Envs, "K3D_NAME="+k.Conf.Name)
}
Expand All @@ -58,20 +59,21 @@ func (k *K3DCommands) prepareK3D(args ...string) error {

func (k *K3DCommands) createDeleteK3DCluster() error {
switch k.Ctx.Command.Category {
case "capi":
case util.CAPI:
k.APICluster = true
case "k3d":
case util.K3DPrefix:
k.K3DCluster = true
}

if _, _, err := k.getKubeContext(); err != nil {
return err
}

k.SpecCMD = k.prepareHelmfile("-l", "app="+k.Ctx.Command.Category+"-cluster", "template")
k.SpecCMD = k.prepareHelmfile("--log-level", "error", "-l", "cluster="+k.Ctx.Command.Category, "template")
k.SpecCMD.DisableStdOut = true
if err := runner(k).runCMD(); err != nil {
return err
return fmt.Errorf("Helmfile failed to render template by label release: cluster=%s\n%s",
k.Ctx.Command.Category, k.SpecCMD.StderrBuf.String())
}

k3dConfig, err := util.CreateTempYAMLFile("/tmp", k.Ctx.Command.Category+"-config", k.SpecCMD.StdoutBuf.Bytes())
Expand Down
24 changes: 13 additions & 11 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ func (rc *ReleaseCommands) nestedHelmfiles(envs ...string) []string {
}

func (rc *ReleaseCommands) prepareHelmfile(args ...string) *util.SpecCMD {
defaultArgs := []string{"--environment", rc.Conf.Environment}

envs := append([]string{},
"NAME="+rc.Conf.Name,
"TENANT="+rc.Conf.Tenant,
Expand Down Expand Up @@ -193,9 +195,12 @@ func (rc *ReleaseCommands) prepareHelmfile(args ...string) *util.SpecCMD {
envs = append(envs, "K3D_CLUSTER="+strconv.FormatBool(rc.K3DCluster))
}

if len(rc.Ctx.String("helmfile-log-level")) > 0 {
defaultArgs = append(defaultArgs, "--log-level", rc.Ctx.String("helmfile-log-level"))
}

return &util.SpecCMD{
Args: append([]string{"--environment", rc.Conf.Environment, "--log-level",
rc.Ctx.String("helmfile-log-level")}, args...),
Args: append(defaultArgs, args...),
Command: "helmfile",
Ctx: rc.Ctx.Context,
Dir: rc.WorkDir,
Expand Down Expand Up @@ -230,8 +235,11 @@ func (rc *ReleaseCommands) releaseMiddleware() error {
if _, currentContext, err := rc.getKubeContext(); err != nil {
return err
} else {
if strings.Contains(currentContext, util.K3DConfigPrefix) {
switch {
case strings.Contains(currentContext, util.K3DPrefix) && !strings.Contains(currentContext, util.CAPI):
rc.K3DCluster = true
case currentContext == util.K3DPrefix+"-"+util.CAPI:
rc.APICluster = true
}
}

Expand Down Expand Up @@ -288,7 +296,7 @@ func (rc *ReleaseCommands) getKubeContext() (string, string, error) {
contextName = ""
}

if rc.K3DCluster && len(contextName) > 0 && !strings.Contains(contextName, util.K3DConfigPrefix) {
if rc.K3DCluster && len(contextName) > 0 && !strings.Contains(contextName, util.K3DPrefix) {
return "", "", fmt.Errorf("remote Kubernetes context already exists %s for this branch", contextName)
}

Expand All @@ -312,16 +320,10 @@ func (rc *ReleaseCommands) releaseKubeContext() error {
return nil
}

if strings.Contains(contextName, util.K3DConfigPrefix) && rc.UpdateContext {
if strings.Contains(contextName, util.K3DPrefix) && rc.UpdateContext {
return fmt.Errorf("current context %s already used for K3D cluster, --force flag cannot be used", contextName)
}

//cc := &ClusterCommands{
// Conf: rc.Conf,
// Ctx: rc.Ctx,
// WorkDir: util.GetPwdPath(""),
//}

if err := newClusterCommands(rc.Conf, rc.Ctx, util.GetPwdPath("")).awsClusterContext(); err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions util/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package util
const (
AWSClusterProvider = "aws"
AzureClusterProvider = "azure"
CAPI = "capi"
GitSSHPrivateKey = ".ssh/id_rsa"
GlobalsFileName = "globals.yaml.gotmpl"
HelmfileFileName = "helmfile.yaml"
HelmfileGoTmplName = HelmfileFileName + ".gotmpl"
HelpFlagFull = "--help"
K3DConfigPrefix = "k3d"
LocalClusterProvider = K3DConfigPrefix
K3DPrefix = "k3d"
LocalClusterProvider = K3DPrefix
RMKBin = "rmk"
RMKBucketName = "edenlabllc-rmk"
RMKBucketRegion = "eu-north-1"
Expand Down

0 comments on commit 5318f79

Please sign in to comment.