Skip to content

Commit

Permalink
refactoring flux config
Browse files Browse the repository at this point in the history
  • Loading branch information
enekofb committed Dec 15, 2023
1 parent 829e876 commit 772e984
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 31 deletions.
1 change: 0 additions & 1 deletion pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion pkg/bootstrap/steps/ask_bootstrap_flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 15 additions & 5 deletions pkg/bootstrap/steps/ask_bootstrap_flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,7 +34,9 @@ func TestAskBootstrapFlux(t *testing.T) {
},
},
config: &Config{
FluxInstalled: false,
FluxConfig: FluxConfig{
IsInstalled: false,
},
},
err: true,
canAsk: true,
Expand All @@ -46,7 +50,9 @@ func TestAskBootstrapFlux(t *testing.T) {
},
},
config: &Config{
FluxInstalled: false,
FluxConfig: FluxConfig{
IsInstalled: false,
},
},
err: false,
canAsk: true,
Expand All @@ -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,
Expand All @@ -73,7 +81,9 @@ func TestAskBootstrapFlux(t *testing.T) {
},
},
config: &Config{
FluxInstalled: false,
FluxConfig: FluxConfig{
IsInstalled: false,
},
ModesConfig: ModesConfig{
Export: true,
},
Expand Down
8 changes: 6 additions & 2 deletions pkg/bootstrap/steps/bootstrap_flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func TestConfigureFluxCreds(t *testing.T) {
},
},
config: &Config{
FluxInstalled: true,
FluxConfig: FluxConfig{
IsInstalled: true,
},
GitRepository: GitRepositoryConfig{
Scheme: sshScheme,
},
Expand All @@ -51,7 +53,9 @@ func TestConfigureFluxCreds(t *testing.T) {
},
},
config: &Config{
FluxInstalled: true,
FluxConfig: FluxConfig{
IsInstalled: true,
},
GitRepository: GitRepositoryConfig{
Scheme: httpsScheme,
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/bootstrap/steps/bootstrap_git_repo_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/bootstrap/steps/common_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 7 additions & 10 deletions pkg/bootstrap/steps/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -288,6 +284,7 @@ func (cb *ConfigBuilder) Build() (Config, error) {
ClientSecret: cb.clientSecret,
PromptedForDiscoveryURL: cb.PromptedForDiscoveryURL,
ComponentsExtra: componentsExtraConfig,
FluxConfig: fluxConfig,
BootstrapFlux: cb.bootstrapFlux,
}, nil

Expand Down
12 changes: 6 additions & 6 deletions pkg/bootstrap/steps/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/bootstrap/utils/flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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")
})
Expand Down

0 comments on commit 772e984

Please sign in to comment.