Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some suggestion to align code to existing one #3727

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions cmd/gitops/app/bootstrap/cmd_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func TestBootstrapCmd(t *testing.T) {
gitRepoPathFlag := fmt.Sprintf("--repo-path=%s", gitRepoPath)

// wge version
wgeVersion := os.Getenv("WGE_VERSION")
g.Expect(wgeVersion).NotTo(BeEmpty())
wgeVersion := "0.37.0"
wgeVersionFlag := fmt.Sprintf("--version=%s", wgeVersion)

// oidc configuration
oidcClientSecret := os.Getenv("OIDC_CLIENT_SECRET")
Expand All @@ -135,7 +135,7 @@ func TestBootstrapCmd(t *testing.T) {
{
name: "journey flux exists: should bootstrap with valid arguments",
flags: []string{kubeconfigFlag,
"--version=0.37.0",
wgeVersionFlag,
privateKeyFlag, privateKeyPasswordFlag,
"--password=admin123",
"--discovery-url=https://dex-01.wge.dev.weave.works/.well-known/openid-configuration",
Expand All @@ -157,7 +157,7 @@ func TestBootstrapCmd(t *testing.T) {
{
name: "journey flux does not exist: should bootstrap with valid arguments",
flags: []string{kubeconfigFlag,
"--version=0.37.0",
wgeVersionFlag,
"--password=admin123",
"--discovery-url=https://dex-01.wge.dev.weave.works/.well-known/openid-configuration",
"--client-id=weave-gitops-enterprise",
Expand All @@ -180,7 +180,6 @@ func TestBootstrapCmd(t *testing.T) {
{
name: "journey flux does not exist interactive",
flags: []string{kubeconfigFlag,
"--version=0.37.0",
"--password=admin123",
"--components-extra=none",
},
Expand Down
7 changes: 6 additions & 1 deletion pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func Bootstrap(config steps.Config) error {
return fmt.Errorf("cannot create check ui: %v", err)
}

installWge, err := steps.NewInstallWGEStep(config.WgeConfig, config.Logger)
if err != nil {
return fmt.Errorf("cannot create install WGE: %v", err)
}

componentsExtra := steps.NewInstallExtraComponentsStep(config.ComponentsExtra, config.ModesConfig.Silent)

// TODO have a single workflow source of truth and documented in https://docs.gitops.weave.works/docs/0.33.0/enterprise/getting-started/install-enterprise/
Expand All @@ -31,7 +36,7 @@ func Bootstrap(config steps.Config) error {
steps.NewBootstrapFlux(config),
steps.CheckEntitlementSecret,
adminCredentials,
steps.NewInstallWGEStep(config),
installWge,
steps.NewInstallOIDCStep(config),
steps.NewOIDCConfigStep(config),
componentsExtra,
Expand Down
50 changes: 24 additions & 26 deletions pkg/bootstrap/steps/common_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,30 @@ func MakeTestConfig(t *testing.T, config Config, objects ...runtime.Object) Conf
cliLogger := utils.CreateLogger()

return Config{
KubernetesClient: fakeClient,
GitClient: fakeGitClient{},
FluxClient: fakeFluxClient{},
InReader: config.InReader,
OutWriter: config.OutWriter,
Logger: cliLogger,
WGEVersion: config.WGEVersion,
IsExistingWgeInstallation: config.IsExistingWgeInstallation,
ChartURL: config.ChartURL,
ModesConfig: config.ModesConfig,
ClusterUserAuth: config.ClusterUserAuth,
GitRepository: config.GitRepository,
FluxInstalled: config.FluxInstalled,
PrivateKeyPath: config.PrivateKeyPath,
PrivateKeyPassword: config.PrivateKeyPassword,
GitUsername: config.GitUsername,
GitToken: config.GitToken,
AuthType: config.AuthType,
InstallOIDC: config.InstallOIDC,
DiscoveryURL: config.DiscoveryURL,
IssuerURL: config.IssuerURL,
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
RedirectURL: config.RedirectURL,
PromptedForDiscoveryURL: config.PromptedForDiscoveryURL,
ComponentsExtra: config.ComponentsExtra,
KubernetesClient: fakeClient,
GitClient: fakeGitClient{},
FluxClient: fakeFluxClient{},
InReader: config.InReader,
OutWriter: config.OutWriter,
Logger: cliLogger,
WgeConfig: config.WgeConfig,
ModesConfig: config.ModesConfig,
ClusterUserAuth: config.ClusterUserAuth,
GitRepository: config.GitRepository,
FluxInstalled: config.FluxInstalled,
PrivateKeyPath: config.PrivateKeyPath,
PrivateKeyPassword: config.PrivateKeyPassword,
GitUsername: config.GitUsername,
GitToken: config.GitToken,
AuthType: config.AuthType,
InstallOIDC: config.InstallOIDC,
DiscoveryURL: config.DiscoveryURL,
IssuerURL: config.IssuerURL,
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
RedirectURL: config.RedirectURL,
PromptedForDiscoveryURL: config.PromptedForDiscoveryURL,
ComponentsExtra: config.ComponentsExtra,
}
}

Expand Down
70 changes: 32 additions & 38 deletions pkg/bootstrap/steps/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/weaveworks/weave-gitops-enterprise/pkg/bootstrap/utils"
"github.com/weaveworks/weave-gitops/pkg/logger"
Expand All @@ -30,23 +29,22 @@ const (

// inputs names
const (
inPassword = "password"
inWGEVersion = "wgeVersion"
inPrivateKeyPath = "privateKeyPath"
inPrivateKeyPassword = "privateKeyPassword"
inDiscoveryURL = "discoveryURL"
inClientID = "clientID"
inClientSecret = "clientSecret"
inOidcInstalled = "oidcInstalled"
inExistingOIDC = "existingOIDC"
inRepoURL = "repoURL"
inBranch = "branch"
inRepoPath = "repoPath"
inGitUserName = "username"
inGitPassword = "gitPassowrd"
inBootstrapFlux = "bootstrapFlux"
inComponentsExtra = "componentsExtra"
inExistingInstallation = "existingInstallation"
inPassword = "password"
inWGEVersion = "wgeVersion"
inPrivateKeyPath = "privateKeyPath"
inPrivateKeyPassword = "privateKeyPassword"
inDiscoveryURL = "discoveryURL"
inClientID = "clientID"
inClientSecret = "clientSecret"
inOidcInstalled = "oidcInstalled"
inExistingOIDC = "existingOIDC"
inRepoURL = "repoURL"
inBranch = "branch"
inRepoPath = "repoPath"
inGitUserName = "username"
inGitPassword = "gitPassowrd"
inBootstrapFlux = "bootstrapFlux"
inComponentsExtra = "componentsExtra"
)

// input/output types
Expand All @@ -65,7 +63,6 @@ type ConfigBuilder struct {
kubeconfig string
password string
wgeVersion string
chartURL string
privateKeyPath string
privateKeyPassword string
silent bool
Expand Down Expand Up @@ -187,11 +184,10 @@ type Config struct {
// OutWriter holds the output to write to
OutWriter io.Writer

IsExistingWgeInstallation bool
WGEVersion string // user want this version in the cluster
ClusterUserAuth ClusterUserAuthConfig
ModesConfig ModesConfig
ChartURL string
WgeConfig WgeConfig

ClusterUserAuth ClusterUserAuthConfig
ModesConfig ModesConfig

FluxInstalled bool
PrivateKeyPath string
Expand Down Expand Up @@ -255,24 +251,22 @@ func (cb *ConfigBuilder) Build() (Config, error) {
return Config{}, fmt.Errorf("cannot create components extra configuration: %v", err)
}

isExistingWgeInstallation, err := NewCheckWGEInstallationConfig(kubeHttp.Client)
if err != nil && !strings.Contains(err.Error(), "not found") {
return Config{}, fmt.Errorf("cannot create check wge installation configuration: %v", err)
wgeConfig, err := NewWgeConfig(cb.wgeVersion, kubeHttp.Client)
if err != nil {
return Config{}, fmt.Errorf("cannot create WGE configuration: %v", err)
}

//TODO we should do validations in case invalid values and throw an error early
return Config{
KubernetesClient: kubeHttp.Client,
GitClient: &utils.GoGitClient{},
FluxClient: &utils.CmdFluxClient{},
InReader: cb.inReader,
OutWriter: cb.outWriter,
IsExistingWgeInstallation: isExistingWgeInstallation,
WGEVersion: cb.wgeVersion,
ClusterUserAuth: clusterUserAuthConfig,
ChartURL: cb.chartURL,
GitRepository: gitRepositoryConfig,
Logger: cb.logger,
KubernetesClient: kubeHttp.Client,
GitClient: &utils.GoGitClient{},
FluxClient: &utils.CmdFluxClient{},
InReader: cb.inReader,
OutWriter: cb.outWriter,
WgeConfig: wgeConfig,
ClusterUserAuth: clusterUserAuthConfig,
GitRepository: gitRepositoryConfig,
Logger: cb.logger,
ModesConfig: ModesConfig{
Silent: cb.silent,
Export: cb.export,
Expand Down
92 changes: 48 additions & 44 deletions pkg/bootstrap/steps/install_wge.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
"github.com/fluxcd/pkg/apis/meta"
sourcev1beta2 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/weaveworks/weave-gitops-enterprise/pkg/bootstrap/utils"
"github.com/weaveworks/weave-gitops/pkg/logger"
"golang.org/x/exp/slices"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
wgeInstallMsg = "installing v%s ... It may take a few minutes."
versionStepName = "select WGE version"
wgeExistsMsg = "Weave GitOps Enterprise is already installed in namespace %s"
versionMsg = "select one of the following"
wgeInstallMsg = "installing v%s ... It may take a few minutes."
wgeExistsMsg = "Weave GitOps Enterprise is already installed in namespace %s"
versionMsg = "select one of the following"
)

const (
Expand All @@ -41,76 +41,80 @@ const (
gitopssetsHealthBindAddress = ":8081"
)

var getVersionInput = StepInput{
Name: inWGEVersion,
Type: multiSelectionChoice,
Msg: versionMsg,
Valuesfn: getWgeVersions,
DefaultValue: "",
// WgeConfig holds configuration about Weave GitOps Enterprise
type WgeConfig struct {
// ExistingVersion is the version found in the cluster
ExistingVersion string
// RequestedVersion is the version requested by the user
RequestedVersion string
// AllowedVersions represent the allowed versions to install
AllowedVersions []string
}

// NewCheckWGEInstallationConfig handles the WGE installation configuration
func NewCheckWGEInstallationConfig(client client.Client) (bool, error) {
// NewWgeConfig creates a WGE configuration out of the user input and discovered state
func NewWgeConfig(requestedWgeVersion string, client client.Client) (WgeConfig, error) {
existingVersion, err := utils.GetHelmReleaseProperty(client, WgeHelmReleaseName, WGEDefaultNamespace, utils.HelmVersionProperty)
if err != nil {
return WgeConfig{}, fmt.Errorf("error getting WGE version: %v", err)
}

_, err := utils.GetHelmReleaseProperty(client, WgeHelmReleaseName, WGEDefaultNamespace, utils.HelmVersionProperty)
allowedWgeVersions, err := getWgeVersions(client)
if err != nil {
return false, err
return WgeConfig{}, fmt.Errorf("error getting valid WGE versions: %v", err)
}

// validate the user introduced version
if requestedWgeVersion != "" {
if !slices.Contains(allowedWgeVersions, requestedWgeVersion) {
return WgeConfig{}, fmt.Errorf("invalid version: %v. available versions: %s", requestedWgeVersion, allowedWgeVersions)
}
}

return true, nil
return WgeConfig{
ExistingVersion: existingVersion,
RequestedVersion: requestedWgeVersion,
AllowedVersions: allowedWgeVersions,
}, nil
}

// NewInstallWGEStep step to install Weave GitOps Enterprise
func NewInstallWGEStep(config Config) BootstrapStep {
// NewInstallWGEStep creates step to install Weave GitOps Enterprise out of the existing configuration
func NewInstallWGEStep(config WgeConfig, logger logger.Logger) (BootstrapStep, error) {

// check if WGE is already installed
if config.IsExistingWgeInstallation {
config.Logger.Waitingf(wgeExistsMsg, WGEDefaultNamespace)
if config.ExistingVersion != "" {
logger.Actionf(wgeExistsMsg, WGEDefaultNamespace)
return BootstrapStep{
Name: "Existing WGE installation found",
Input: []StepInput{},
Step: doNothingStep,
}
}, nil
}

inputs := []StepInput{}

// validate value by user
if config.WGEVersion != "" {
versions, err := getWgeVersions(inputs, &config)
if err != nil {
config.Logger.Failuref("couldn't get WGE helm chart: %v", err)
return BootstrapStep{
Name: "Helm chart not found",
Input: []StepInput{},
Step: doNothingStep,
}
}
if versions, ok := versions.([]string); !ok || !slices.Contains(versions, config.WGEVersion) {
config.Logger.Failuref("invalid version: %v. available versions: %s", config.WGEVersion, versions)
return BootstrapStep{
Name: "invalid WGE version",
Input: []StepInput{},
Step: doNothingStep,
}
if config.RequestedVersion == "" {
var getVersionInput = StepInput{
Name: inWGEVersion,
Type: multiSelectionChoice,
Msg: versionMsg,
Values: config.AllowedVersions,
DefaultValue: "",
}
}

if config.WGEVersion == "" {
inputs = append(inputs, getVersionInput)
}

return BootstrapStep{
Name: "Install Weave GitOps Enterprise",
Input: inputs,
Step: installWge,
}
}, nil
}

// InstallWge installs weave gitops enterprise chart.
func installWge(input []StepInput, c *Config) ([]StepOutput, error) {

c.Logger.Actionf(wgeInstallMsg, c.WGEVersion)
requestedVersion := c.WgeConfig.RequestedVersion
c.Logger.Actionf(wgeInstallMsg, requestedVersion)

wgeHelmRepository, err := constructWgeHelmRepository()
if err != nil {
Expand Down Expand Up @@ -155,7 +159,7 @@ func installWge(input []StepInput, c *Config) ([]StepOutput, error) {
ClusterController: clusterControllerValues,
}

wgeHelmRelease, err := constructWGEhelmRelease(wgeValues, c.WGEVersion)
wgeHelmRelease, err := constructWGEhelmRelease(wgeValues, requestedVersion)
if err != nil {
return []StepOutput{}, err
}
Expand Down
Loading
Loading