Skip to content

Commit

Permalink
Workaround some bugs in the toolkit, and make this operation exclusiv…
Browse files Browse the repository at this point in the history
…e using locks
  • Loading branch information
luxas committed Oct 28, 2020
1 parent 0d38dc3 commit c84e18f
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion pkg/gotk/gotk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/cloud-native-nordics/workshopctl/pkg/config"
"github.com/cloud-native-nordics/workshopctl/pkg/util"
)

func SetupGitOps(ctx context.Context, info *config.ClusterInfo) error {
mux, ok := util.GetMutex(ctx)
if !ok || mux == nil {
return fmt.Errorf("SetupGitOps: programmer error, couldn't get mutex for locking: %v", mux)
}

logger := util.Logger(ctx)
logger.Debug("Waiting for mutex unlock in SetupGitOps")
// Lock during this operation, as the git repo is mutually exclusive
mux.Lock()
logger.Infof("Bootstrapping GitOps for cluster %s...", info.Index)
defer mux.Unlock()
defer logger.Infof("Bootstrapping GitOps for cluster %s is done!", info.Index)

// Make sure we have all prereqs
kubeConfigArg := "--kubeconfig=" + info.Index.KubeConfigPath()
_, _, err := util.Command(ctx,
Expand All @@ -32,7 +46,19 @@ func SetupGitOps(ctx context.Context, info *config.ClusterInfo) error {
return fmt.Errorf("git repo %s: unknown provider domain", info.GitRepo)
}

// Forward the env var
envVarName := fmt.Sprintf("%s_TOKEN", strings.ToUpper(provider))
envVarVal := os.Getenv(envVarName)
if envVarVal == "" {
return fmt.Errorf("Need to set env var: %s", envVarName)
}

// TODO: Upstream gotk doesn't support the --kubeconfig flag in install/bootstrap at least
// Instead, we use the KUBECONFIG env var for now
kubeConfigEnv := "KUBECONFIG=" + util.JoinPaths(ctx, info.Index.KubeConfigPath())

// We assume that the repo is already created, hence we can skip some flags related to that
// TODO: That doesn't work in current gotk, rework that maybe upstream too?
// This command installs the toolkit into the target cluster, and starts reconciling our
// given cluster directory for changes.
_, _, err = util.Command(ctx,
Expand All @@ -47,6 +73,10 @@ func SetupGitOps(ctx context.Context, info *config.ClusterInfo) error {
"--components=source-controller,kustomize-controller,helm-controller",
// Use a short interval as this is a highly dynamic env
"--interval=30s",
).WithStdio(nil, os.Stdout, os.Stderr).Run()
// TODO: Assuming personal for now
"--personal",
).WithStdio(nil, os.Stdout, os.Stderr).
WithEnv(fmt.Sprintf("%s=%s", envVarName, envVarVal), kubeConfigEnv, "PATH="+os.Getenv("PATH")).
Run()
return err
}

0 comments on commit c84e18f

Please sign in to comment.