Skip to content

Commit

Permalink
change flux upgrader to use management components (#7419)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxbrowne1207 authored Jan 31, 2024
1 parent 489d672 commit be30e46
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 183 deletions.
2 changes: 1 addition & 1 deletion cmd/eksctl-anywhere/cmd/upgradeplanmanagementcomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func getManagementComponentsChangeDiffs(ctx context.Context, clientFactory inter
componentChangeDiffs := &types.ChangeDiff{}
newManagementComponents := cluster.ManagementComponentsFromBundles(newClusterSpec.Bundles)
componentChangeDiffs.Append(eksaupgrader.EksaChangeDiff(currentManagementComponents, newManagementComponents))
componentChangeDiffs.Append(fluxupgrader.FluxChangeDiff(currentSpec, newClusterSpec))
componentChangeDiffs.Append(fluxupgrader.ChangeDiff(currentManagementComponents, newManagementComponents, currentSpec, newClusterSpec))
componentChangeDiffs.Append(capiupgrader.CapiChangeDiff(currentSpec, newClusterSpec, provider))

return componentChangeDiffs, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/gitops/flux/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newFluxForCluster(flux *Flux, clusterSpec *cluster.Spec, datacenterConfig p
// If the remote repository does not exist it will initialize a local repository and push it to the configured remote.
// It will generate the kustomization file and marshal the cluster configuration file to the required locations in the repo.
// These will later be used by Flux and our controllers to reconcile the repository contents and the cluster configuration.
func (fc *fluxForCluster) commitFluxAndClusterConfigToGit(ctx context.Context) error {
func (fc *fluxForCluster) commitFluxAndClusterConfigToGit(ctx context.Context, managementComponents *cluster.ManagementComponents) error {
logger.Info("Adding cluster configuration files to Git")
config := fc.clusterSpec.FluxConfig

Expand All @@ -55,7 +55,7 @@ func (fc *fluxForCluster) commitFluxAndClusterConfigToGit(ctx context.Context) e
}

if fc.clusterSpec.Cluster.IsSelfManaged() {
if err := g.WriteFluxSystemFiles(fc.clusterSpec); err != nil {
if err := g.WriteFluxSystemFiles(managementComponents, fc.clusterSpec); err != nil {
return fmt.Errorf("writing flux system files: %v", err)
}
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/gitops/flux/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ func (g *FileGenerator) WriteEksaFiles(clusterSpec *cluster.Spec, datacenterConf
return nil
}

func (g *FileGenerator) WriteFluxSystemFiles(clusterSpec *cluster.Spec) error {
if err := g.WriteFluxKustomization(clusterSpec); err != nil {
// WriteFluxSystemFiles writes the flux-system files into the flux system git directory.
func (g *FileGenerator) WriteFluxSystemFiles(managementComponents *cluster.ManagementComponents, clusterSpec *cluster.Spec) error {
if err := g.WriteFluxKustomization(managementComponents, clusterSpec); err != nil {
return err
}

Expand Down Expand Up @@ -124,14 +125,14 @@ func (g *FileGenerator) WriteEksaKustomization() error {
return nil
}

func (g *FileGenerator) WriteFluxKustomization(clusterSpec *cluster.Spec) error {
versionsBundle := clusterSpec.RootVersionsBundle()
// WriteFluxKustomization writes the flux-system kustomization file into the flux system git directory.
func (g *FileGenerator) WriteFluxKustomization(managementComponents *cluster.ManagementComponents, clusterSpec *cluster.Spec) error {
values := map[string]string{
"Namespace": clusterSpec.FluxConfig.Spec.SystemNamespace,
"SourceControllerImage": versionsBundle.Flux.SourceController.VersionedImage(),
"KustomizeControllerImage": versionsBundle.Flux.KustomizeController.VersionedImage(),
"HelmControllerImage": versionsBundle.Flux.HelmController.VersionedImage(),
"NotificationControllerImage": versionsBundle.Flux.NotificationController.VersionedImage(),
"SourceControllerImage": managementComponents.Flux.SourceController.VersionedImage(),
"KustomizeControllerImage": managementComponents.Flux.KustomizeController.VersionedImage(),
"HelmControllerImage": managementComponents.Flux.HelmController.VersionedImage(),
"NotificationControllerImage": managementComponents.Flux.NotificationController.VersionedImage(),
}

if path, err := g.fluxTemplater.WriteToFile(fluxKustomizeContent, values, kustomizeFileName, filewriter.PersistentFile); err != nil {
Expand Down
41 changes: 23 additions & 18 deletions pkg/gitops/flux/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,29 +152,34 @@ var wantKustomizationValues = map[string]string{

type fileGeneratorTest struct {
*WithT
ctx context.Context
g *flux.FileGenerator
w *writerMocks.MockFileWriter
t *fluxMocks.MockTemplater
clusterSpec *cluster.Spec
datacenterConfig providers.DatacenterConfig
machineConfigs []providers.MachineConfig
ctx context.Context
g *flux.FileGenerator
w *writerMocks.MockFileWriter
t *fluxMocks.MockTemplater
managementComponents *cluster.ManagementComponents
clusterSpec *cluster.Spec
datacenterConfig providers.DatacenterConfig
machineConfigs []providers.MachineConfig
}

func newFileGeneratorTest(t *testing.T) *fileGeneratorTest {
ctrl := gomock.NewController(t)
writer := writerMocks.NewMockFileWriter(ctrl)
templater := fluxMocks.NewMockTemplater(ctrl)
clusterName := "test-cluster"

clusterSpec := newClusterSpec(t, NewCluster(clusterName), "")

return &fileGeneratorTest{
WithT: NewWithT(t),
ctx: context.Background(),
g: flux.NewFileGeneratorWithWriterTemplater(writer, writer, templater, templater),
w: writer,
t: templater,
clusterSpec: newClusterSpec(t, NewCluster(clusterName), ""),
datacenterConfig: datacenterConfig(clusterName),
machineConfigs: []providers.MachineConfig{machineConfig(clusterName)},
WithT: NewWithT(t),
ctx: context.Background(),
g: flux.NewFileGeneratorWithWriterTemplater(writer, writer, templater, templater),
w: writer,
t: templater,
managementComponents: cluster.ManagementComponentsFromBundles(clusterSpec.Bundles),
clusterSpec: clusterSpec,
datacenterConfig: datacenterConfig(clusterName),
machineConfigs: []providers.MachineConfig{machineConfig(clusterName)},
}
}

Expand Down Expand Up @@ -244,15 +249,15 @@ func TestFileGeneratorWriteFluxSystemFilesSuccess(t *testing.T) {
tt.t.EXPECT().WriteToFile(wantFluxKustomization, wantKustomizationValues, "kustomization.yaml", gomock.Any()).Return("", nil)
tt.t.EXPECT().WriteToFile("", nil, "gotk-sync.yaml", gomock.Any()).Return("", nil)

tt.Expect(tt.g.WriteFluxSystemFiles(tt.clusterSpec)).To(Succeed())
tt.Expect(tt.g.WriteFluxSystemFiles(tt.managementComponents, tt.clusterSpec)).To(Succeed())
}

func TestFileGeneratorWriteFluxSystemFilesWriteFluxKustomizationError(t *testing.T) {
tt := newFileGeneratorTest(t)

tt.t.EXPECT().WriteToFile(wantFluxKustomization, wantKustomizationValues, "kustomization.yaml", gomock.Any()).Return("", errors.New("error in write kustomization"))

tt.Expect(tt.g.WriteFluxSystemFiles(tt.clusterSpec)).To(MatchError(ContainSubstring("error in write kustomization")))
tt.Expect(tt.g.WriteFluxSystemFiles(tt.managementComponents, tt.clusterSpec)).To(MatchError(ContainSubstring("error in write kustomization")))
}

func TestFileGeneratorWriteFluxSystemFilesWriteFluxSyncError(t *testing.T) {
Expand All @@ -261,7 +266,7 @@ func TestFileGeneratorWriteFluxSystemFilesWriteFluxSyncError(t *testing.T) {
tt.t.EXPECT().WriteToFile(wantFluxKustomization, wantKustomizationValues, "kustomization.yaml", gomock.Any()).Return("", nil)
tt.t.EXPECT().WriteToFile("", nil, "gotk-sync.yaml", gomock.Any()).Return("", errors.New("error in write sync"))

tt.Expect(tt.g.WriteFluxSystemFiles(tt.clusterSpec)).To(MatchError(ContainSubstring("error in write sync")))
tt.Expect(tt.g.WriteFluxSystemFiles(tt.managementComponents, tt.clusterSpec)).To(MatchError(ContainSubstring("error in write sync")))
}

func NewCluster(clusterName string) *anywherev1.Cluster {
Expand Down
5 changes: 3 additions & 2 deletions pkg/gitops/flux/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func NewFluxFromGitOpsFluxClient(fluxClient GitOpsFluxClient, gitClient GitClien
}
}

func (f *Flux) InstallGitOps(ctx context.Context, cluster *types.Cluster, clusterSpec *cluster.Spec, datacenterConfig providers.DatacenterConfig, machineConfigs []providers.MachineConfig) error {
// InstallGitOps installs the GitOps components for the cluster.
func (f *Flux) InstallGitOps(ctx context.Context, cluster *types.Cluster, managementComponents *cluster.ManagementComponents, clusterSpec *cluster.Spec, datacenterConfig providers.DatacenterConfig, machineConfigs []providers.MachineConfig) error {
if f.shouldSkipFlux() {
logger.Info("GitOps field not specified, bootstrap flux skipped")
return nil
Expand All @@ -93,7 +94,7 @@ func (f *Flux) InstallGitOps(ctx context.Context, cluster *types.Cluster, cluste
return err
}

if err := fc.commitFluxAndClusterConfigToGit(ctx); err != nil {
if err := fc.commitFluxAndClusterConfigToGit(ctx, managementComponents); err != nil {
return err
}

Expand Down
53 changes: 35 additions & 18 deletions pkg/gitops/flux/flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func newClusterSpec(t *testing.T, clusterConfig *v1alpha1.Cluster, fluxPath stri
clusterSpec := test.NewClusterSpec(func(s *cluster.Spec) {
s.Cluster = clusterConfig
s.VersionsBundles["1.19"].Flux = fluxBundle()
s.Bundles.Spec.VersionsBundles[0].Flux = fluxBundle()
s.FluxConfig = &fluxConfig
})
if err := cluster.SetConfigDefaults(clusterSpec.Config); err != nil {
Expand Down Expand Up @@ -237,11 +238,12 @@ func TestInstallGitOpsOnManagementClusterWithPrexistingRepo(t *testing.T) {

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
cluster := &types.Cluster{}
clusterConfig := NewCluster(tt.clusterName)
g := newFluxTest(t)
clusterSpec := newClusterSpec(t, clusterConfig, tt.fluxpath)
managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles)

cluster := &types.Cluster{}
g.flux.EXPECT().BootstrapGithub(g.ctx, cluster, clusterSpec.FluxConfig)

g.git.EXPECT().GetRepo(g.ctx).Return(&git.Repository{Name: clusterSpec.FluxConfig.Spec.Github.Repository}, nil)
Expand All @@ -256,7 +258,7 @@ func TestInstallGitOpsOnManagementClusterWithPrexistingRepo(t *testing.T) {
datacenterConfig := datacenterConfig(tt.clusterName)
machineConfig := machineConfig(tt.clusterName)

g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, clusterSpec, datacenterConfig, []providers.MachineConfig{machineConfig})).To(Succeed())
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, managementComponents, clusterSpec, datacenterConfig, []providers.MachineConfig{machineConfig})).To(Succeed())

expectedEksaClusterConfigPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedEksaConfigFileName)
test.AssertFilesEquals(t, expectedEksaClusterConfigPath, tt.expectedConfigFileContents)
Expand Down Expand Up @@ -303,11 +305,12 @@ func TestInstallGitOpsOnManagementClusterWithoutClusterSpec(t *testing.T) {

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
cluster := &types.Cluster{}
clusterConfig := NewCluster(tt.clusterName)
g := newFluxTest(t)
clusterSpec := newClusterSpec(t, clusterConfig, tt.fluxpath)
managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles)

cluster := &types.Cluster{}
g.flux.EXPECT().BootstrapGithub(g.ctx, cluster, clusterSpec.FluxConfig)

g.git.EXPECT().GetRepo(g.ctx).Return(&git.Repository{Name: clusterSpec.FluxConfig.Spec.Github.Repository}, nil)
Expand All @@ -319,7 +322,7 @@ func TestInstallGitOpsOnManagementClusterWithoutClusterSpec(t *testing.T) {
g.git.EXPECT().Push(g.ctx).Return(nil)
g.git.EXPECT().Pull(g.ctx, clusterSpec.FluxConfig.Spec.Branch).Return(nil)

g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, clusterSpec, nil, nil)).To(Succeed())
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, managementComponents, clusterSpec, nil, nil)).To(Succeed())

expectedEksaClusterConfigPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedEksaConfigFileName)
g.Expect(validations.FileExists(expectedEksaClusterConfigPath)).To(Equal(false))
Expand All @@ -337,13 +340,14 @@ func TestInstallGitOpsOnManagementClusterWithoutClusterSpec(t *testing.T) {
}

func TestInstallGitOpsOnWorkloadClusterWithPrexistingRepo(t *testing.T) {
cluster := &types.Cluster{}
clusterName := "workload-cluster"
clusterConfig := NewCluster(clusterName)
clusterConfig.SetManagedBy("management-cluster")
g := newFluxTest(t)
clusterSpec := newClusterSpec(t, clusterConfig, "")
managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles)

cluster := &types.Cluster{}
g.git.EXPECT().GetRepo(g.ctx).Return(&git.Repository{Name: clusterSpec.FluxConfig.Spec.Github.Repository}, nil)

g.git.EXPECT().Clone(g.ctx).Return(nil)
Expand All @@ -356,7 +360,7 @@ func TestInstallGitOpsOnWorkloadClusterWithPrexistingRepo(t *testing.T) {
datacenterConfig := datacenterConfig(clusterName)
machineConfig := machineConfig(clusterName)

g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, clusterSpec, datacenterConfig, []providers.MachineConfig{machineConfig})).To(Succeed())
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, managementComponents, clusterSpec, datacenterConfig, []providers.MachineConfig{machineConfig})).To(Succeed())

expectedEksaClusterConfigPath := path.Join(g.writer.Dir(), "clusters/management-cluster/workload-cluster/eksa-system", defaultEksaClusterConfigFileName)
test.AssertFilesEquals(t, expectedEksaClusterConfigPath, "./testdata/cluster-config-default-path-workload.yaml")
Expand All @@ -376,26 +380,29 @@ func TestInstallGitOpsOnWorkloadClusterWithPrexistingRepo(t *testing.T) {
}

func TestInstallGitOpsSetupRepoError(t *testing.T) {
cluster := &types.Cluster{}
clusterName := "test-cluster"
clusterConfig := NewCluster(clusterName)
g := newFluxTest(t)
clusterSpec := newClusterSpec(t, clusterConfig, "")
managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles)

cluster := &types.Cluster{}

g.git.EXPECT().GetRepo(g.ctx).Return(&git.Repository{Name: clusterSpec.FluxConfig.Spec.Github.Repository}, nil)
g.git.EXPECT().Clone(g.ctx).Return(nil)
g.git.EXPECT().Branch(clusterSpec.FluxConfig.Spec.Branch).Return(nil)
g.git.EXPECT().Add(path.Dir("clusters/management-cluster")).Return(errors.New("error in add"))

g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, clusterSpec, nil, nil)).To(MatchError(ContainSubstring("error in add")))
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, managementComponents, clusterSpec, nil, nil)).To(MatchError(ContainSubstring("error in add")))
}

func TestInstallGitOpsBootstrapError(t *testing.T) {
cluster := &types.Cluster{}
clusterName := "test-cluster"
clusterConfig := NewCluster(clusterName)
g := newFluxTest(t)
clusterSpec := newClusterSpec(t, clusterConfig, "")
managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles)
cluster := &types.Cluster{}

g.git.EXPECT().GetRepo(g.ctx).Return(&git.Repository{Name: clusterSpec.FluxConfig.Spec.Github.Repository}, nil)
g.git.EXPECT().Clone(g.ctx).Return(nil)
Expand All @@ -406,18 +413,22 @@ func TestInstallGitOpsBootstrapError(t *testing.T) {
g.flux.EXPECT().BootstrapGithub(g.ctx, cluster, clusterSpec.FluxConfig).Return(errors.New("error in bootstrap"))
g.flux.EXPECT().Uninstall(g.ctx, cluster, clusterSpec.FluxConfig).Return(nil)

g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, clusterSpec, nil, nil)).To(MatchError(ContainSubstring("error in bootstrap")))
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, managementComponents, clusterSpec, nil, nil)).To(MatchError(ContainSubstring("error in bootstrap")))
}

func TestInstallGitOpsGitProviderSuccess(t *testing.T) {
cluster := &types.Cluster{}
clusterName := "management-cluster"
clusterConfig := NewCluster(clusterName)
g := newFluxTest(t)
clusterSpec := newClusterSpec(t, clusterConfig, "")

clusterSpec.FluxConfig.Spec.Git = &v1alpha1.GitProviderConfig{RepositoryUrl: "git.xyz"}
clusterSpec.FluxConfig.Spec.Github = nil

managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles)

cluster := &types.Cluster{}

g.flux.EXPECT().BootstrapGit(g.ctx, cluster, clusterSpec.FluxConfig, nil)
g.git.EXPECT().Clone(g.ctx).Return(nil)
g.git.EXPECT().Branch(clusterSpec.FluxConfig.Spec.Branch).Return(nil)
Expand All @@ -429,20 +440,22 @@ func TestInstallGitOpsGitProviderSuccess(t *testing.T) {
datacenterConfig := datacenterConfig(clusterName)
machineConfig := machineConfig(clusterName)

g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, clusterSpec, datacenterConfig, []providers.MachineConfig{machineConfig})).To(Succeed())
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, managementComponents, clusterSpec, datacenterConfig, []providers.MachineConfig{machineConfig})).To(Succeed())
}

func TestInstallGitOpsCommitFilesError(t *testing.T) {
cluster := &types.Cluster{}
clusterName := "test-cluster"
clusterConfig := NewCluster(clusterName)
g := newFluxTest(t)
clusterSpec := newClusterSpec(t, clusterConfig, "")
managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles)

cluster := &types.Cluster{}

g.git.EXPECT().GetRepo(g.ctx).Return(&git.Repository{Name: clusterSpec.FluxConfig.Spec.Github.Repository}, nil)
g.git.EXPECT().Clone(g.ctx).Return(errors.New("error in clone"))

g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, clusterSpec, nil, nil)).To(MatchError(ContainSubstring("error in clone")))
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, managementComponents, clusterSpec, nil, nil)).To(MatchError(ContainSubstring("error in clone")))
}

func TestInstallGitOpsNoPrexistingRepo(t *testing.T) {
Expand Down Expand Up @@ -490,10 +503,12 @@ func TestInstallGitOpsNoPrexistingRepo(t *testing.T) {

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
cluster := &types.Cluster{}
clusterConfig := NewCluster(tt.clusterName)
g := newFluxTest(t)
clusterSpec := newClusterSpec(t, clusterConfig, tt.fluxpath)
managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles)

cluster := &types.Cluster{}

g.flux.EXPECT().BootstrapGithub(g.ctx, cluster, clusterSpec.FluxConfig)

Expand All @@ -517,7 +532,7 @@ func TestInstallGitOpsNoPrexistingRepo(t *testing.T) {

datacenterConfig := datacenterConfig(tt.clusterName)
machineConfig := machineConfig(tt.clusterName)
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, clusterSpec, datacenterConfig, []providers.MachineConfig{machineConfig})).To(Succeed())
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, managementComponents, clusterSpec, datacenterConfig, []providers.MachineConfig{machineConfig})).To(Succeed())

expectedEksaClusterConfigPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedEksaConfigFileName)
test.AssertFilesEquals(t, expectedEksaClusterConfigPath, tt.expectedConfigFileContents)
Expand Down Expand Up @@ -565,10 +580,12 @@ func TestInstallGitOpsToolkitsBareRepo(t *testing.T) {

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
cluster := &types.Cluster{}
clusterConfig := NewCluster(tt.clusterName)
g := newFluxTest(t)
clusterSpec := newClusterSpec(t, clusterConfig, tt.fluxpath)
managementComponents := cluster.ManagementComponentsFromBundles(clusterSpec.Bundles)

cluster := &types.Cluster{}

g.flux.EXPECT().BootstrapGithub(g.ctx, cluster, clusterSpec.FluxConfig)

Expand All @@ -585,7 +602,7 @@ func TestInstallGitOpsToolkitsBareRepo(t *testing.T) {

datacenterConfig := datacenterConfig(tt.clusterName)
machineConfig := machineConfig(tt.clusterName)
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, clusterSpec, datacenterConfig, []providers.MachineConfig{machineConfig})).To(Succeed())
g.Expect(g.gitOpsFlux.InstallGitOps(g.ctx, cluster, managementComponents, clusterSpec, datacenterConfig, []providers.MachineConfig{machineConfig})).To(Succeed())

expectedEksaClusterConfigPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedEksaConfigFileName)
test.AssertFilesEquals(t, expectedEksaClusterConfigPath, tt.expectedConfigFileContents)
Expand Down
Loading

0 comments on commit be30e46

Please sign in to comment.