Skip to content

Commit 229029e

Browse files
chore : simplifying the KubeAid Bootstrap Script temp directory name | fix : returning aggregated stdout contents and erroring out immediately on error, while command execution
Signed-off-by: Archisman <[email protected]>
1 parent 71f85aa commit 229029e

File tree

10 files changed

+49
-59
lines changed

10 files changed

+49
-59
lines changed

pkg/constants/constants.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import (
55
"time"
66
)
77

8+
var (
9+
TempDirectoryName = "kubeaid-bootstrap-script"
10+
TempDirectory = "/tmp/" + TempDirectoryName
11+
12+
KubeAidDirectory = path.Join(TempDirectory, "KubeAid")
13+
KubeAidConfigDirectory = path.Join(TempDirectory, "kubeaid-config")
14+
)
15+
816
// Environment variable names.
917
const (
1018
EnvNameAWSAccessKey = "AWS_ACCESS_KEY_ID"

pkg/core/create_dev_env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func CreateDevEnv(ctx context.Context, args *CreateDevEnvArgs) {
4343
// Clone the KubeAid config fork locally (if not already cloned).
4444
_ = gitUtils.CloneRepo(ctx,
4545
config.ParsedGeneralConfig.Forks.KubeaidConfigForkURL,
46-
utils.GetKubeAidConfigDir(),
46+
constants.KubeAidConfigDirectory,
4747
gitAuthMethod,
4848
)
4949

pkg/core/setup_cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func SetupCluster(ctx context.Context, args SetupClusterArgs) {
4040
// Clone the KubeAid fork locally (if not already cloned).
4141
kubeAidRepo := gitUtils.CloneRepo(ctx,
4242
config.ParsedGeneralConfig.Forks.KubeaidForkURL,
43-
utils.GetKubeAidDir(),
43+
constants.KubeAidDirectory,
4444
args.GitAuthMethod,
4545
)
4646

pkg/core/setup_kubeaid_config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ It expects the KubeAid Config repository to be already cloned in the temp direct
4343
func SetupKubeAidConfig(ctx context.Context, args SetupKubeAidConfigArgs) {
4444
slog.InfoContext(ctx, "Setting up KubeAid config repo")
4545

46-
repo, err := goGit.PlainOpen(utils.GetKubeAidConfigDir())
46+
repo, err := goGit.PlainOpen(constants.KubeAidConfigDirectory)
4747
assert.AssertErrNil(ctx, err, "Failed opening existing git repo")
4848

4949
workTree, err := repo.Worktree()
@@ -249,8 +249,8 @@ func buildKubePrometheus(ctx context.Context, clusterDir string, templateValues
249249

250250
// Run the KubePrometheus build script.
251251
slog.InfoContext(ctx, "Running KubePrometheus build script...")
252-
kubePrometheusBuildScriptPath := fmt.Sprintf("%s/build/kube-prometheus/build.sh",
253-
utils.GetKubeAidDir(),
252+
kubePrometheusBuildScriptPath := path.Join(
253+
constants.KubeAidDirectory, "build/kube-prometheus/build.sh",
254254
)
255255
utils.ExecuteCommandOrDie(fmt.Sprintf("%s %s", kubePrometheusBuildScriptPath, clusterDir))
256256
}

pkg/core/upgrade_cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func updateCapiClusterValuesFile(ctx context.Context, args *UpgradeClusterArgs)
8989
// Clone the KubeAid config fork locally (if not already cloned).
9090
repo := git.CloneRepo(ctx,
9191
config.ParsedGeneralConfig.Forks.KubeaidConfigForkURL,
92-
utils.GetKubeAidConfigDir(),
92+
constants.KubeAidConfigDirectory,
9393
gitAuthMethod,
9494
)
9595

pkg/globals/globals.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
var (
1212
ConfigsDirectory,
1313

14-
TempDir,
15-
1614
CloudProviderName string
1715
CloudProvider cloud.CloudProvider
1816

pkg/utils/command.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"context"
5+
"errors"
56
"log/slog"
67
"strings"
78

@@ -15,33 +16,39 @@ import (
1516

1617
// Executes the given command.
1718
// The command output is streamed to the standard output.
18-
func ExecuteCommand(command string) (output string, err error) {
19+
func ExecuteCommand(command string) (stdOutOutput string, err error) {
1920
slog.Info("Executing command", slog.String("command", sensorCredentials(command)))
2021

2122
commandExecutionOptions := cmd.Options{
22-
CombinedOutput: true,
23-
Streaming: true,
23+
Streaming: true,
2424
}
2525
commandExecutor := cmd.NewCmdOptions(commandExecutionOptions,
2626
"sh", "-c", command,
2727
)
2828

29-
// Stream the command execution output to the standard output.
29+
// Execute the command,
30+
// while streaming the stdout contents to the user.
3031
for !commandExecutor.Status().Complete {
3132
select {
32-
case outputLine := <-commandExecutor.Stdout:
33-
println(outputLine)
33+
case output := <-commandExecutor.Stdout:
34+
println(output)
3435

35-
case outputLine := <-commandExecutor.Stderr:
36-
println(outputLine)
36+
// Keep aggregating the stdout contents in stdOutOutput.
37+
// We need to return the aggregated result to the invoker.
38+
stdOutOutput += output
39+
40+
case output := <-commandExecutor.Stderr:
41+
// Error occurred, while execution some portion of the command.
42+
// We'll not execute the remaining portion of the command,
43+
// but just return the aggregated stdout contents and the error that occurred.
44+
if len(output) > 0 {
45+
return stdOutOutput, errors.New(output)
46+
}
3747

3848
case <-commandExecutor.Start():
3949
}
4050
}
41-
42-
output = strings.Join(commandExecutor.Status().Stdout, "")
43-
err = commandExecutor.Status().Error
44-
return
51+
return stdOutOutput, nil
4552
}
4653

4754
// Executes the given command. Panics if the command execution fails.

pkg/utils/fs.go

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,45 @@ package utils
22

33
import (
44
"context"
5-
"fmt"
65
"io"
76
"log/slog"
87
"net/url"
98
"os"
109
"path"
1110
"path/filepath"
1211
"strings"
13-
"time"
1412

1513
"github.com/Obmondo/kubeaid-bootstrap-script/pkg/config"
16-
"github.com/Obmondo/kubeaid-bootstrap-script/pkg/globals"
14+
"github.com/Obmondo/kubeaid-bootstrap-script/pkg/constants"
1715
"github.com/Obmondo/kubeaid-bootstrap-script/pkg/utils/assert"
16+
"github.com/Obmondo/kubeaid-bootstrap-script/pkg/utils/logger"
1817
)
1918

2019
// Creates a temp dir inside /tmp, where KubeAid Bootstrap Script will clone repos.
2120
// Then sets the value of constants.TempDir as the temp dir path.
2221
// If the temp dir already exists, then that gets reused.
2322
func InitTempDir(ctx context.Context) {
24-
namePrefix := "kubeaid-bootstrap-script-"
23+
ctx = logger.AppendSlogAttributesToCtx(ctx, []slog.Attr{
24+
slog.String("path", constants.TempDirectory),
25+
})
2526

2627
// Check if a temp dir already exists for KubeAid Bootstrap Script.
2728
// If yes, then reuse that.
2829
filesAndFolders, err := os.ReadDir("/tmp")
2930
assert.AssertErrNil(ctx, err, "Failed listing files and folders in /tmp")
3031
for _, item := range filesAndFolders {
31-
if item.IsDir() && strings.HasPrefix(item.Name(), namePrefix) {
32-
path := "/tmp/" + item.Name()
33-
slog.InfoContext(ctx,
34-
"Skipped creating temp dir, since it already exists",
35-
slog.String("path", path),
36-
)
37-
38-
globals.TempDir = path
39-
32+
if item.IsDir() && (item.Name() == constants.TempDirectory) {
33+
slog.InfoContext(ctx, "Skipped creating temp dir, since it already exists")
4034
return
4135
}
4236
}
4337

4438
// Otherwise, create it.
4539

46-
dirName := fmt.Sprintf("%s%d", namePrefix, time.Now().Unix())
47-
48-
path, err := os.MkdirTemp("/tmp", dirName)
49-
assert.AssertErrNil(ctx, err,
50-
"Failed creating temp dir",
51-
slog.String("path", path),
52-
)
40+
path, err := os.MkdirTemp("/tmp", constants.TempDirectoryName)
41+
assert.AssertErrNil(ctx, err, "Failed creating temp dir")
5342

5443
slog.InfoContext(ctx, "Created temp dir", slog.String("path", path))
55-
56-
globals.TempDir = path
5744
}
5845

5946
// Returns path to the parent dir of the given file.
@@ -76,27 +63,15 @@ func CreateIntermediateDirsForFile(ctx context.Context, filePath string) {
7663
)
7764
}
7865

79-
// Returns path to the directory (in temp directory), where the KubeAid repo is / will be cloned.
80-
func GetKubeAidDir() string {
81-
return path.Join(globals.TempDir, "KubeAid")
82-
}
83-
84-
// Returns path to the directory (in temp directory), where the customer's KubeAid Config repo
85-
// is / will be cloned.
86-
func GetKubeAidConfigDir() string {
87-
return path.Join(globals.TempDir, "kubeaid-config")
88-
}
89-
9066
// Returns path to the directory containing cluster specific config, in the KubeAid Config dir.
9167
func GetClusterDir() string {
92-
clusterDir := path.Join(GetKubeAidConfigDir(), "k8s", config.ParsedGeneralConfig.Cluster.Name)
93-
return clusterDir
68+
return path.Join(constants.KubeAidConfigDirectory, "k8s", config.ParsedGeneralConfig.Cluster.Name)
9469
}
9570

9671
// Returns the path to the local temp directory, where contents of the given blob storage bucket
9772
// will be / is downloaded.
9873
func GetDownloadedStorageBucketContentsDir(bucketName string) string {
99-
return path.Join(globals.TempDir, "buckets", bucketName)
74+
return path.Join(constants.TempDirectory, "buckets", bucketName)
10075
}
10176

10277
// Converts the given relative path to an absolute path.

pkg/utils/kubernetes/argo.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ func InstallAndSetupArgoCD(ctx context.Context, clusterDir string, clusterClient
6262
if err == nil {
6363
break
6464
}
65+
66+
// Retry after 10 seconds.
67+
time.Sleep(10 * time.Second)
6568
}
6669

6770
// Install the ArgoCD Helm chart.
6871
HelmInstall(ctx, &HelmInstallArgs{
69-
ChartPath: path.Join(utils.GetKubeAidDir(), "argocd-helm-charts/argo-cd"),
72+
ChartPath: path.Join(constants.KubeAidDirectory, "argocd-helm-charts/argo-cd"),
7073

7174
Namespace: constants.NamespaceArgoCD,
7275
ReleaseName: constants.ReleaseNameArgoCD,

pkg/utils/kubernetes/sealed_secrets.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ import (
1515
"k8s.io/client-go/tools/clientcmd"
1616

1717
"github.com/Obmondo/kubeaid-bootstrap-script/pkg/constants"
18-
"github.com/Obmondo/kubeaid-bootstrap-script/pkg/utils"
1918
"github.com/Obmondo/kubeaid-bootstrap-script/pkg/utils/assert"
2019
"github.com/Obmondo/kubeaid-bootstrap-script/pkg/utils/logger"
2120
)
2221

2322
// Performs a minimal installation of Sealed Secrets in the underlying Kubernetes cluster.
2423
func InstallSealedSecrets(ctx context.Context) {
2524
HelmInstall(ctx, &HelmInstallArgs{
26-
ChartPath: path.Join(utils.GetKubeAidDir(), "argocd-helm-charts/sealed-secrets"),
25+
ChartPath: path.Join(constants.KubeAidDirectory, "argocd-helm-charts/sealed-secrets"),
2726
Namespace: constants.NamespaceSealedSecrets,
2827
ReleaseName: "sealed-secrets",
2928
Values: &values.Options{

0 commit comments

Comments
 (0)