Skip to content

Commit

Permalink
add controllers configurations with acceptance and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
waleedhammam committed Nov 21, 2023
1 parent 3829d88 commit 2409dc7
Show file tree
Hide file tree
Showing 20 changed files with 1,298 additions and 28 deletions.
11 changes: 11 additions & 0 deletions cmd/gitops/app/bootstrap/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ gitops bootstrap --version=<version> --password=<admin-password> --discovery-url
# Start WGE installation with OIDC and flux bootstrap with ssh
gitops bootstrap --version=<version> --password=<admin-password> --discovery-url=<oidc-discovery-url> --client-id=<oidc-client-id> --private-key-path=<private-key-path> --private-key-password=<private-key-password> --branch=<git-branch> --repo-path=<path-in-repo-for-management-cluster> --repo-url=ssh://<repo-url> --client-secret=<oidc-secret> -s
# Start WGE installation with extra controller like policy agent
gitops bootstrap --extra-controllers policy-agent
# Start WGE installation with more than one extra controller
gitops bootstrap --extra-controllers policy-agent --extra-controllers capi --extra-controllers tf-controller
`
)

Expand Down Expand Up @@ -69,6 +75,9 @@ type bootstrapFlags struct {

// modes flags
silent bool

// extra controllers
extraControllers []string
}

var flags bootstrapFlags
Expand All @@ -85,6 +94,7 @@ func Command(opts *config.Options) *cobra.Command {
cmd.Flags().StringVarP(&flags.domainType, "domain-type", "t", "", "dashboard domain type: could be 'localhost' or 'externaldns'")
cmd.Flags().StringVarP(&flags.domain, "domain", "d", "", "the domain to access the dashboard in case of using externaldns")
cmd.Flags().StringVarP(&flags.version, "version", "v", "", "version of Weave GitOps Enterprise (should be from the latest 3 versions)")
cmd.Flags().StringArrayVarP(&flags.extraControllers, "extra-controllers", "", []string{}, "extra controllers to be installed from (none, policy-agent, tf-controller, capi)")
cmd.PersistentFlags().BoolVarP(&flags.silent, "bootstrap-flux", "s", false, "always choose yes for interactive questions")
cmd.PersistentFlags().StringVarP(&flags.gitUsername, "git-username", "", "", "git username used in https authentication type")
cmd.PersistentFlags().StringVarP(&flags.gitPassword, "git-password", "", "", "git password/token used in https authentication type")
Expand Down Expand Up @@ -123,6 +133,7 @@ func getBootstrapCmdRun(opts *config.Options) func(*cobra.Command, []string) err
).
WithOIDCConfig(flags.discoveryURL, flags.clientID, flags.clientSecret, true).
WithSilentFlag(flags.silent).
WithExtraControllers(flags.extraControllers).
Build()

if err != nil {
Expand Down
44 changes: 36 additions & 8 deletions cmd/gitops/app/bootstrap/cmd_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ func TestBootstrapCmd(t *testing.T) {
privateKeyFile := os.Getenv("GIT_PRIVATEKEY_PATH")
g.Expect(privateKeyFile).NotTo(BeEmpty())

privateKeyPassword := os.Getenv("GIT_PRIVATEKEY_PASSWORD")

repoURLSSH := os.Getenv("GIT_REPO_URL_SSH")
g.Expect(repoURLSSH).NotTo(BeEmpty())
repoURLHTTPS := os.Getenv("GIT_REPO_URL_HTTPS")
Expand All @@ -89,6 +87,10 @@ func TestBootstrapCmd(t *testing.T) {
g.Expect(gitBranch).NotTo(BeEmpty())
gitRepoPath := os.Getenv("GIT_REPO_PATH")
g.Expect(gitRepoPath).NotTo(BeEmpty())
privateKeyPassword := os.Getenv("GIT_PRIVATEKEY_PASSWORD")
g.Expect(privateKeyPassword).NotTo(BeEmpty())
oidcClientSecret := os.Getenv("OIDC_CLIENT_SECRET")
g.Expect(oidcClientSecret).NotTo(BeEmpty())

privateKeyFlag := fmt.Sprintf("--private-key=%s", privateKeyFile)
privateKeyPasswordFlag := fmt.Sprintf("--private-key-password=%s", privateKeyPassword)
Expand All @@ -103,8 +105,6 @@ func TestBootstrapCmd(t *testing.T) {
gitBranchFlag := fmt.Sprintf("--branch=%s", gitBranch)
gitRepoPathFlag := fmt.Sprintf("--repo-path=%s", gitRepoPath)

oidcClientSecret := os.Getenv("OIDC_CLIENT_SECRET")
g.Expect(oidcClientSecret).NotTo(BeEmpty())
oidcClientSecretFlag := fmt.Sprintf("--client-secret=%s", oidcClientSecret)

_ = k8sClient.Create(context.Background(), &fluxSystemNamespace)
Expand All @@ -119,13 +119,15 @@ func TestBootstrapCmd(t *testing.T) {
{
name: "journey flux exists: should bootstrap with valid arguments",
flags: []string{kubeconfigFlag,
"--version=0.35.0",
"--version=0.36.0",
privateKeyFlag, privateKeyPasswordFlag,
"--password=admin123",
"--domain-type=localhost",
"--discovery-url=https://dex-01.wge.dev.weave.works/.well-known/openid-configuration",
"--client-id=weave-gitops-enterprise",
oidcClientSecretFlag,
"--extra-controllers=none",
"-s",
},
setup: func(t *testing.T) {
bootstrapFluxSsh(g, kubeconfigFlag)
Expand All @@ -141,15 +143,15 @@ func TestBootstrapCmd(t *testing.T) {
{
name: "journey flux does not exist: should bootstrap with valid arguments",
flags: []string{kubeconfigFlag,
"--version=0.35.0",
privateKeyFlag, privateKeyPasswordFlag,
"--version=0.36.0",
"--password=admin123",
"--domain-type=localhost", "--domain=localhost",
"--discovery-url=https://dex-01.wge.dev.weave.works/.well-known/openid-configuration",
"--client-id=weave-gitops-enterprise",
gitUsernameFlag, gitPasswordFlag, gitBranchFlag, gitRepoPathFlag,
repoHTTPSURLFlag,
oidcClientSecretFlag, "-s",
"--extra-controllers=none",
},
setup: func(t *testing.T) {
createEntitlements(t, testLog)
Expand All @@ -161,6 +163,31 @@ func TestBootstrapCmd(t *testing.T) {
},
expectedErrorStr: "",
},
{
name: "journey bootstrap with extra controllers policy-agent capi and tf-controller",
flags: []string{kubeconfigFlag,
"--version=0.36.0",
privateKeyFlag, privateKeyPasswordFlag,
"--password=admin123",
"--domain-type=localhost",
"--discovery-url=https://dex-01.wge.dev.weave.works/.well-known/openid-configuration",
"--client-id=weave-gitops-enterprise",
oidcClientSecretFlag, "-s",
"--extra-controllers=policy-agent",
"--extra-controllers=capi",
"--extra-controllers=tf-controller",
},
setup: func(t *testing.T) {
bootstrapFluxSsh(g, kubeconfigFlag)
createEntitlements(t, testLog)
},
reset: func(t *testing.T) {
deleteEntitlements(t, testLog)
deleteClusterUser(t, testLog)
uninstallFlux(g, kubeconfigFlag)
},
expectedErrorStr: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -202,8 +229,9 @@ func bootstrapFluxSsh(g *WithT, kubeconfigFlag string) {
fmt.Println(privateKeyFile)

privateKeyPassword := os.Getenv("GIT_PRIVATEKEY_PASSWORD")
g.Expect(privateKeyFile).NotTo(BeEmpty())

args := []string{"bootstrap", "git", kubeconfigFlag, "-s", fmt.Sprintf("--url=%s", repoUrl), fmt.Sprintf("--password=%s", privateKeyPassword), fmt.Sprintf("--private-key-file=%s", privateKeyFile), "--path=clusters/management"}
args := []string{"bootstrap", "git", kubeconfigFlag, "-s", fmt.Sprintf("--url=%s", repoUrl), fmt.Sprintf("--private-key-file=%s", privateKeyFile), fmt.Sprintf("--password=%s", privateKeyPassword), "--path=clusters/management"}
fmt.Println(args)

s, err := runner.Run("flux", args...)
Expand Down
1 change: 1 addition & 0 deletions pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Bootstrap(config steps.Config) error {
steps.NewInstallOIDCStep(config),
steps.NewOIDCConfigStep(config),
steps.CheckUIDomainStep,
steps.NewInstallExtraControllers(config),
}

for _, step := range steps {
Expand Down
1 change: 0 additions & 1 deletion pkg/bootstrap/steps/admin_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const (

const (
adminSecretName = "cluster-user-auth"
confirmYes = "y"
defaultAdminUsername = "wego-admin"
)

Expand Down
9 changes: 5 additions & 4 deletions pkg/bootstrap/steps/ask_bootstrap_flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const (

var (
bootstrapFLuxQuestion = StepInput{
Name: inBootstrapFlux,
Type: confirmInput,
Msg: bootstrapFluxMsg,
Enabled: canAskForFluxBootstrap,
Name: inBootstrapFlux,
Type: confirmInput,
Msg: bootstrapFluxMsg,
Enabled: canAskForFluxBootstrap,
DefaultValue: confirmYes,
}
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/bootstrap/steps/ask_bootstrap_flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestAskBootstrapFlux(t *testing.T) {
input: []StepInput{
{
Name: inBootstrapFlux,
Value: "n",
Value: confirmNo,
},
},
config: &Config{
Expand Down
81 changes: 81 additions & 0 deletions pkg/bootstrap/steps/capi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package steps

import (
"encoding/json"
"fmt"

"github.com/weaveworks/weave-gitops-enterprise/pkg/bootstrap/utils"
)

const (
capiInstallInfoMsg = "installing Capi Controller ..."
capiInstallConfirmMsg = "Capi Controller is installed successfully"
)

const (
capiCommitMsg = "Add Capi Controller"
defaultNamespace = "default"
)

// NewInstallCapiControllerStep ask for continue installing OIDC
func NewInstallCapiControllerStep(config Config) BootstrapStep {
return BootstrapStep{
Name: "install Capi Controller",
Step: installCapi,
}
}

// installCapi start installing CAPI controller
func installCapi(input []StepInput, c *Config) ([]StepOutput, error) {
c.Logger.Actionf(capiInstallInfoMsg)

capiValues := map[string]interface{}{
"repositoryURL": c.RepoURL,
"repositoryPath": fmt.Sprintf("%s/clusters", c.RepoPath),
"repositoryClustersPath": c.RepoPath,
"baseBranch": c.Branch,
"templates": map[string]interface{}{
"namespace": defaultNamespace,
},
"clusters": map[string]interface{}{
"namespace": defaultNamespace,
},
}

// enable capi in wge chart
valuesBytes, err := utils.GetHelmReleaseValues(c.KubernetesClient, WgeHelmReleaseName, WGEDefaultNamespace)
if err != nil {
return []StepOutput{}, err
}
var wgeValues valuesFile

err = json.Unmarshal(valuesBytes, &wgeValues)
if err != nil {
return []StepOutput{}, err
}

wgeValues.Global.CapiEnabled = true
wgeValues.Config.CAPI = capiValues

wgeHelmRelease, err := constructWGEhelmRelease(wgeValues, c.WGEVersion)
if err != nil {
return []StepOutput{}, err
}
c.Logger.Actionf("rendered WGE HelmRelease file")

c.Logger.Actionf("updating HelmRelease file")
helmreleaseFile := fileContent{
Name: wgeHelmReleaseFileName,
Content: wgeHelmRelease,
CommitMsg: capiCommitMsg,
}

c.Logger.Successf(capiInstallConfirmMsg)
return []StepOutput{
{
Name: wgeHelmReleaseFileName,
Type: typeFile,
Value: helmreleaseFile,
},
}, nil
}
Loading

0 comments on commit 2409dc7

Please sign in to comment.