From 772e984edfa6850d65d8221e7ce5cff88fb6271d Mon Sep 17 00:00:00 2001 From: Eneko Fernandez Date: Fri, 15 Dec 2023 18:16:27 +0100 Subject: [PATCH] refactoring flux config --- pkg/bootstrap/bootstrap.go | 1 - pkg/bootstrap/steps/ask_bootstrap_flux.go | 2 +- .../steps/ask_bootstrap_flux_test.go | 20 ++++++++++++++----- pkg/bootstrap/steps/bootstrap_flux_test.go | 8 ++++++-- .../steps/bootstrap_git_repo_config.go | 2 +- pkg/bootstrap/steps/common_tests.go | 2 +- pkg/bootstrap/steps/config.go | 17 +++++++--------- pkg/bootstrap/steps/flux.go | 12 +++++------ pkg/bootstrap/utils/flux_test.go | 8 ++++---- 9 files changed, 41 insertions(+), 31 deletions(-) diff --git a/pkg/bootstrap/bootstrap.go b/pkg/bootstrap/bootstrap.go index 8d6e0f1b07..853a59e4d7 100644 --- a/pkg/bootstrap/bootstrap.go +++ b/pkg/bootstrap/bootstrap.go @@ -30,7 +30,6 @@ func Bootstrap(config steps.Config) error { // 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/ var workflow = []steps.BootstrapStep{ - steps.VerifyFluxInstallation, steps.NewAskBootstrapFluxStep(config), repositoryConfig, steps.NewBootstrapFlux(config), diff --git a/pkg/bootstrap/steps/ask_bootstrap_flux.go b/pkg/bootstrap/steps/ask_bootstrap_flux.go index 9f7569cf21..a398cf121b 100644 --- a/pkg/bootstrap/steps/ask_bootstrap_flux.go +++ b/pkg/bootstrap/steps/ask_bootstrap_flux.go @@ -59,5 +59,5 @@ func askBootstrapFlux(input []StepInput, c *Config) ([]StepOutput, error) { // canAskForGitConfig if fluxInstallation is false, then can ask for git config func canAskForFluxBootstrap(input []StepInput, c *Config) bool { - return !c.FluxInstalled + return !c.FluxConfig.IsInstalled } diff --git a/pkg/bootstrap/steps/ask_bootstrap_flux_test.go b/pkg/bootstrap/steps/ask_bootstrap_flux_test.go index 0939edc7d8..2e1a1a7392 100644 --- a/pkg/bootstrap/steps/ask_bootstrap_flux_test.go +++ b/pkg/bootstrap/steps/ask_bootstrap_flux_test.go @@ -18,7 +18,9 @@ func TestAskBootstrapFlux(t *testing.T) { name: "check with flux installed", input: []StepInput{}, config: &Config{ - FluxInstalled: true, + FluxConfig: FluxConfig{ + IsInstalled: true, + }, }, err: false, canAsk: false, @@ -32,7 +34,9 @@ func TestAskBootstrapFlux(t *testing.T) { }, }, config: &Config{ - FluxInstalled: false, + FluxConfig: FluxConfig{ + IsInstalled: false, + }, }, err: true, canAsk: true, @@ -46,7 +50,9 @@ func TestAskBootstrapFlux(t *testing.T) { }, }, config: &Config{ - FluxInstalled: false, + FluxConfig: FluxConfig{ + IsInstalled: false, + }, }, err: false, canAsk: true, @@ -55,7 +61,9 @@ func TestAskBootstrapFlux(t *testing.T) { name: "check with silent mode and bootstrap flux flag available", input: []StepInput{}, config: &Config{ - FluxInstalled: false, + FluxConfig: FluxConfig{ + IsInstalled: false, + }, BootstrapFlux: true, ModesConfig: ModesConfig{ Silent: true, @@ -73,7 +81,9 @@ func TestAskBootstrapFlux(t *testing.T) { }, }, config: &Config{ - FluxInstalled: false, + FluxConfig: FluxConfig{ + IsInstalled: false, + }, ModesConfig: ModesConfig{ Export: true, }, diff --git a/pkg/bootstrap/steps/bootstrap_flux_test.go b/pkg/bootstrap/steps/bootstrap_flux_test.go index 8d2f87c6d3..f59f6a5d2e 100644 --- a/pkg/bootstrap/steps/bootstrap_flux_test.go +++ b/pkg/bootstrap/steps/bootstrap_flux_test.go @@ -29,7 +29,9 @@ func TestConfigureFluxCreds(t *testing.T) { }, }, config: &Config{ - FluxInstalled: true, + FluxConfig: FluxConfig{ + IsInstalled: true, + }, GitRepository: GitRepositoryConfig{ Scheme: sshScheme, }, @@ -51,7 +53,9 @@ func TestConfigureFluxCreds(t *testing.T) { }, }, config: &Config{ - FluxInstalled: true, + FluxConfig: FluxConfig{ + IsInstalled: true, + }, GitRepository: GitRepositoryConfig{ Scheme: httpsScheme, }, diff --git a/pkg/bootstrap/steps/bootstrap_git_repo_config.go b/pkg/bootstrap/steps/bootstrap_git_repo_config.go index 03b4fa5dd2..afe557746d 100644 --- a/pkg/bootstrap/steps/bootstrap_git_repo_config.go +++ b/pkg/bootstrap/steps/bootstrap_git_repo_config.go @@ -127,7 +127,7 @@ func NewGitRepositoryConfigStep(config GitRepositoryConfig) BootstrapStep { func createGitRepositoryConfig(input []StepInput, c *Config) ([]StepOutput, error) { - if c.FluxInstalled { + if c.FluxConfig.IsInstalled { return []StepOutput{}, nil } diff --git a/pkg/bootstrap/steps/common_tests.go b/pkg/bootstrap/steps/common_tests.go index e98361cdba..ffeee5a123 100644 --- a/pkg/bootstrap/steps/common_tests.go +++ b/pkg/bootstrap/steps/common_tests.go @@ -52,7 +52,7 @@ func MakeTestConfig(t *testing.T, config Config, objects ...runtime.Object) Conf ModesConfig: config.ModesConfig, ClusterUserAuth: config.ClusterUserAuth, GitRepository: config.GitRepository, - FluxInstalled: config.FluxInstalled, + FluxConfig: config.FluxConfig, PrivateKeyPath: config.PrivateKeyPath, PrivateKeyPassword: config.PrivateKeyPassword, GitUsername: config.GitUsername, diff --git a/pkg/bootstrap/steps/config.go b/pkg/bootstrap/steps/config.go index 6bf05fee0d..420c55f2ed 100644 --- a/pkg/bootstrap/steps/config.go +++ b/pkg/bootstrap/steps/config.go @@ -175,21 +175,17 @@ type Config struct { GitClient utils.GitClient // TODO move me to a better package FluxClient utils.FluxClient - - Logger logger.Logger - + Logger logger.Logger // InReader holds the stream to read input from InReader io.Reader - // OutWriter holds the output to write to - OutWriter io.Writer - - WgeConfig WgeConfig + OutWriter io.Writer + FluxConfig FluxConfig + WgeConfig WgeConfig ClusterUserAuth ClusterUserAuthConfig ModesConfig ModesConfig - FluxInstalled bool PrivateKeyPath string PrivateKeyPassword string @@ -252,12 +248,12 @@ func (cb *ConfigBuilder) Build() (Config, error) { return Config{}, fmt.Errorf("error creating git repository configuration: %v", err) } - wgeConfig, err := NewWgeConfig(cb.wgeVersion, kubeHttp.Client, fluxConfig.FluxInstalled) + wgeConfig, err := NewWgeConfig(cb.wgeVersion, kubeHttp.Client, fluxConfig.IsInstalled) if err != nil { return Config{}, fmt.Errorf("cannot create WGE configuration: %v", err) } - componentsExtraConfig, err := NewInstallExtraComponentsConfig(cb.componentsExtra, kubeHttp.Client, fluxConfig.FluxInstalled) + componentsExtraConfig, err := NewInstallExtraComponentsConfig(cb.componentsExtra, kubeHttp.Client, fluxConfig.IsInstalled) if err != nil { return Config{}, fmt.Errorf("cannot create components extra configuration: %v", err) } @@ -288,6 +284,7 @@ func (cb *ConfigBuilder) Build() (Config, error) { ClientSecret: cb.clientSecret, PromptedForDiscoveryURL: cb.PromptedForDiscoveryURL, ComponentsExtra: componentsExtraConfig, + FluxConfig: fluxConfig, BootstrapFlux: cb.bootstrapFlux, }, nil diff --git a/pkg/bootstrap/steps/flux.go b/pkg/bootstrap/steps/flux.go index 5ec68c85c9..0231d46757 100644 --- a/pkg/bootstrap/steps/flux.go +++ b/pkg/bootstrap/steps/flux.go @@ -25,8 +25,8 @@ type FluxConfig struct { Url string // Scheme flux-system git repository scheme Scheme string - // FluxInstalled indicates whether flux is already installed - FluxInstalled bool + // IsInstalled indicates whether flux is already installed + IsInstalled bool } // NewFluxConfig creates a Flux configuration out of the existing cluster @@ -38,7 +38,7 @@ func NewFluxConfig(logger logger.Logger, client k8s_client.Client) (FluxConfig, if err != nil { if strings.Contains(string(out), "customresourcedefinitions.apiextensions.k8s.io \"gitrepositories.source.toolkit.fluxcd.io\" not found") { return FluxConfig{ - FluxInstalled: false, + IsInstalled: false, }, nil } return FluxConfig{}, fmt.Errorf("flux installed error: %v. %s", string(out), fluxRecoverMsg) @@ -65,9 +65,9 @@ func NewFluxConfig(logger logger.Logger, client k8s_client.Client) (FluxConfig, logger.Successf("detected git scheme: %s", scheme) return FluxConfig{ - Url: repoUrl, - Scheme: scheme, - FluxInstalled: true, + Url: repoUrl, + Scheme: scheme, + IsInstalled: true, }, nil } diff --git a/pkg/bootstrap/utils/flux_test.go b/pkg/bootstrap/utils/flux_test.go index bf04cdb56e..601f8c977f 100644 --- a/pkg/bootstrap/utils/flux_test.go +++ b/pkg/bootstrap/utils/flux_test.go @@ -186,17 +186,16 @@ func TestGetHelmReleaseProperty(t *testing.T) { }, }, { - name: "should return err if helm release CRD does not exist", + name: "should return err if helm release not found", objects: []runtime.Object{}, property: "domain", - expectedOut: "testdomain.com", + expectedOut: "", wantErr: func(t tassert.TestingT, err error, i ...interface{}) bool { tassert.Error(t, err) - tassert.Contains(t, err.Error(), "invalid repository scheme") + tassert.Contains(t, err.Error(), "helmreleases.helm.toolkit.fluxcd.io \"wego\" not found") return true }, }, - { name: "should return value for supported property 'version'", objects: []runtime.Object{ @@ -222,6 +221,7 @@ func TestGetHelmReleaseProperty(t *testing.T) { prop, err := GetHelmReleaseProperty(client, "wego", "flux-system", tt.property) if tt.wantErr != nil { tt.wantErr(t, err, "unexpected error ") + return } assert.Equal(t, tt.expectedOut, prop, "invalid property") })