Skip to content

Commit

Permalink
Replace deprecated patchesStrategicMerge with patches in flux kus…
Browse files Browse the repository at this point in the history
…tomization
  • Loading branch information
jiayiwang7 committed Dec 19, 2023
1 parent 773fcc9 commit 82fb2f8
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 205 deletions.
29 changes: 6 additions & 23 deletions pkg/gitops/flux/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ var fluxKustomizeContent string
//go:embed manifests/flux-system/gotk-sync.yaml
var fluxSyncContent string

//go:embed manifests/flux-system/gotk-patches.yaml
var fluxPatchContent string

type Templater interface {
WriteToFile(templateContent string, data interface{}, fileName string, f ...filewriter.FileOptionsFunc) (filePath string, err error)
}
Expand Down Expand Up @@ -101,10 +98,6 @@ func (g *FileGenerator) WriteFluxSystemFiles(clusterSpec *cluster.Spec) error {
return err
}

if err := g.WriteFluxPatch(clusterSpec); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -132,8 +125,13 @@ func (g *FileGenerator) WriteEksaKustomization() error {
}

func (g *FileGenerator) WriteFluxKustomization(clusterSpec *cluster.Spec) error {
versionsBundle := clusterSpec.RootVersionsBundle()
values := map[string]string{
"Namespace": clusterSpec.FluxConfig.Spec.SystemNamespace,
"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(),
}

if path, err := g.fluxTemplater.WriteToFile(fluxKustomizeContent, values, kustomizeFileName, filewriter.PersistentFile); err != nil {
Expand All @@ -148,18 +146,3 @@ func (g *FileGenerator) WriteFluxSync() error {
}
return nil
}

func (g *FileGenerator) WriteFluxPatch(clusterSpec *cluster.Spec) error {
versionsBundle := clusterSpec.RootVersionsBundle()
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(),
}
if path, err := g.fluxTemplater.WriteToFile(fluxPatchContent, values, fluxPatchFileName, filewriter.PersistentFile); err != nil {
return fmt.Errorf("creating flux-system patch manifest file into %s: %v", path, err)
}
return nil
}
115 changes: 52 additions & 63 deletions pkg/gitops/flux/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,60 +89,60 @@ var wantFluxKustomization = `apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: {{.Namespace}}
resources:
- gotk-components.yaml
- gotk-sync.yaml
patchesStrategicMerge:
- gotk-patches.yaml`

var wantFluxPatches = `apiVersion: apps/v1
kind: Deployment
metadata:
name: source-controller
namespace: {{.Namespace}}
spec:
template:
- gotk-components.yaml
- gotk-sync.yaml
patches:
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: helm-controller
namespace: {{.Namespace}}
spec:
containers:
- image: {{.SourceControllerImage}}
name: manager
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: kustomize-controller
namespace: {{.Namespace}}
spec:
template:
template:
spec:
containers:
- image: {{.HelmControllerImage}}
name: manager
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: kustomize-controller
namespace: {{.Namespace}}
spec:
containers:
- image: {{.KustomizeControllerImage}}
name: manager
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: helm-controller
namespace: {{.Namespace}}
spec:
template:
template:
spec:
containers:
- image: {{.KustomizeControllerImage}}
name: manager
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: notification-controller
namespace: {{.Namespace}}
spec:
containers:
- image: {{.HelmControllerImage}}
name: manager
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: notification-controller
namespace: {{.Namespace}}
spec:
template:
template:
spec:
containers:
- image: {{.NotificationControllerImage}}
name: manager
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: source-controller
namespace: {{.Namespace}}
spec:
containers:
- image: {{.NotificationControllerImage}}
name: manager`
template:
spec:
containers:
- image: {{.SourceControllerImage}}
name: manager
`

var wantPatchesValues = map[string]string{
var wantKustomizationValues = map[string]string{
"Namespace": "flux-system",
"SourceControllerImage": "public.ecr.aws/l0g8r8j6/fluxcd/source-controller:v0.12.1-8539f509df046a4f567d2182dde824b957136599",
"KustomizeControllerImage": "public.ecr.aws/l0g8r8j6/fluxcd/kustomize-controller:v0.11.1-d82011942ec8a447ba89a70ff9a84bf7b9579492",
Expand Down Expand Up @@ -241,40 +241,29 @@ func TestFileGeneratorWriteEksaFilesWriteToFileError(t *testing.T) {
func TestFileGeneratorWriteFluxSystemFilesSuccess(t *testing.T) {
tt := newFileGeneratorTest(t)

tt.t.EXPECT().WriteToFile(wantFluxKustomization, map[string]string{"Namespace": "flux-system"}, "kustomization.yaml", gomock.Any()).Return("", nil)
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.t.EXPECT().WriteToFile(wantFluxPatches, wantPatchesValues, "gotk-patches.yaml", gomock.Any()).Return("", nil)

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

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

tt.t.EXPECT().WriteToFile(wantFluxKustomization, map[string]string{"Namespace": "flux-system"}, "kustomization.yaml", gomock.Any()).Return("", errors.New("error in write kustomization"))
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")))
}

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

tt.t.EXPECT().WriteToFile(wantFluxKustomization, map[string]string{"Namespace": "flux-system"}, "kustomization.yaml", gomock.Any()).Return("", nil)
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")))
}

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

tt.t.EXPECT().WriteToFile(wantFluxKustomization, map[string]string{"Namespace": "flux-system"}, "kustomization.yaml", gomock.Any()).Return("", nil)
tt.t.EXPECT().WriteToFile("", nil, "gotk-sync.yaml", gomock.Any()).Return("", nil)
tt.t.EXPECT().WriteToFile(wantFluxPatches, wantPatchesValues, "gotk-patches.yaml", gomock.Any()).Return("", errors.New("error in write patches"))

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

func NewCluster(clusterName string) *anywherev1.Cluster {
c := &anywherev1.Cluster{
TypeMeta: metav1.TypeMeta{
Expand Down
40 changes: 20 additions & 20 deletions pkg/gitops/flux/flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ func TestInstallGitOpsOnManagementClusterWithPrexistingRepo(t *testing.T) {
expectedEksaClusterConfigPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedEksaConfigFileName)
test.AssertFilesEquals(t, expectedEksaClusterConfigPath, tt.expectedConfigFileContents)

expectedKustomizationPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedKustomizationFileName)
test.AssertFilesEquals(t, expectedKustomizationPath, "./testdata/kustomization.yaml")
expectedEksaKustomizationPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedKustomizationFileName)
test.AssertFilesEquals(t, expectedEksaKustomizationPath, "./testdata/eksa-kustomization.yaml")

expectedFluxPatchesPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedFluxPatchesFileName)
test.AssertFilesEquals(t, expectedFluxPatchesPath, "./testdata/gotk-patches.yaml")
expectedFluxKustomizationPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedKustomizationFileName)
test.AssertFilesEquals(t, expectedFluxKustomizationPath, "./testdata/flux-kustomization.yaml")

expectedFluxSyncPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedFluxSyncFileName)
test.AssertFilesEquals(t, expectedFluxSyncPath, "./testdata/gotk-sync.yaml")
Expand Down Expand Up @@ -322,11 +322,11 @@ func TestInstallGitOpsOnManagementClusterWithoutClusterSpec(t *testing.T) {
expectedEksaClusterConfigPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedEksaConfigFileName)
g.Expect(validations.FileExists(expectedEksaClusterConfigPath)).To(Equal(false))

expectedKustomizationPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedKustomizationFileName)
g.Expect(validations.FileExists(expectedKustomizationPath)).To(Equal(false))
expectedEksaKustomizationPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedKustomizationFileName)
g.Expect(validations.FileExists(expectedEksaKustomizationPath)).To(Equal(false))

expectedFluxPatchesPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedFluxPatchesFileName)
test.AssertFilesEquals(t, expectedFluxPatchesPath, "./testdata/gotk-patches.yaml")
expectedFluxKustomizationPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedKustomizationFileName)
test.AssertFilesEquals(t, expectedFluxKustomizationPath, "./testdata/flux-kustomization.yaml")

expectedFluxSyncPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedFluxSyncFileName)
test.AssertFilesEquals(t, expectedFluxSyncPath, "./testdata/gotk-sync.yaml")
Expand Down Expand Up @@ -362,11 +362,11 @@ func TestInstallGitOpsOnWorkloadClusterWithPrexistingRepo(t *testing.T) {
test.AssertFilesEquals(t, expectedEksaClusterConfigPath, "./testdata/cluster-config-default-path-workload.yaml")

expectedKustomizationPath := path.Join(g.writer.Dir(), "clusters/management-cluster/workload-cluster/eksa-system", defaultKustomizationManifestFileName)
test.AssertFilesEquals(t, expectedKustomizationPath, "./testdata/kustomization.yaml")
test.AssertFilesEquals(t, expectedKustomizationPath, "./testdata/eksa-kustomization.yaml")

expectedFluxPatchesPath := path.Join(g.writer.Dir(), "clusters/management-cluster/flux-system", defaultFluxPatchesFileName)
if _, err := os.Stat(expectedFluxPatchesPath); errors.Is(err, os.ErrExist) {
t.Errorf("File exists at %s, should not exist", expectedFluxPatchesPath)
expectedFluxKustomizationPath := path.Join(g.writer.Dir(), "clusters/management-cluster/flux-system", defaultKustomizationManifestFileName)
if _, err := os.Stat(expectedFluxKustomizationPath); errors.Is(err, os.ErrExist) {
t.Errorf("File exists at %s, should not exist", expectedFluxKustomizationPath)
}

expectedFluxSyncPath := path.Join(g.writer.Dir(), "clusters/management-cluster/flux-system", defaultFluxSyncFileName)
Expand Down Expand Up @@ -522,11 +522,11 @@ func TestInstallGitOpsNoPrexistingRepo(t *testing.T) {
expectedEksaClusterConfigPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedEksaConfigFileName)
test.AssertFilesEquals(t, expectedEksaClusterConfigPath, tt.expectedConfigFileContents)

expectedKustomizationPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedKustomizationFileName)
test.AssertFilesEquals(t, expectedKustomizationPath, "./testdata/kustomization.yaml")
expectedEksaKustomizationPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedKustomizationFileName)
test.AssertFilesEquals(t, expectedEksaKustomizationPath, "./testdata/eksa-kustomization.yaml")

expectedFluxPatchesPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedFluxPatchesFileName)
test.AssertFilesEquals(t, expectedFluxPatchesPath, "./testdata/gotk-patches.yaml")
expectedFluxKustomizationPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedKustomizationFileName)
test.AssertFilesEquals(t, expectedFluxKustomizationPath, "./testdata/flux-kustomization.yaml")

expectedFluxSyncPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedFluxSyncFileName)
test.AssertFilesEquals(t, expectedFluxSyncPath, "./testdata/gotk-sync.yaml")
Expand Down Expand Up @@ -590,11 +590,11 @@ func TestInstallGitOpsToolkitsBareRepo(t *testing.T) {
expectedEksaClusterConfigPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedEksaConfigFileName)
test.AssertFilesEquals(t, expectedEksaClusterConfigPath, tt.expectedConfigFileContents)

expectedKustomizationPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedKustomizationFileName)
test.AssertFilesEquals(t, expectedKustomizationPath, "./testdata/kustomization.yaml")
expectedEksaKustomizationPath := path.Join(g.writer.Dir(), tt.expectedEksaSystemDirPath, tt.expectedKustomizationFileName)
test.AssertFilesEquals(t, expectedEksaKustomizationPath, "./testdata/eksa-kustomization.yaml")

expectedFluxPatchesPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedFluxPatchesFileName)
test.AssertFilesEquals(t, expectedFluxPatchesPath, "./testdata/gotk-patches.yaml")
expectedFluxKustomizationPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedKustomizationFileName)
test.AssertFilesEquals(t, expectedFluxKustomizationPath, "./testdata/flux-kustomization.yaml")

expectedFluxSyncPath := path.Join(g.writer.Dir(), tt.expectedFluxSystemDirPath, tt.expectedFluxSyncFileName)
test.AssertFilesEquals(t, expectedFluxSyncPath, "./testdata/gotk-sync.yaml")
Expand Down
47 changes: 0 additions & 47 deletions pkg/gitops/flux/manifests/flux-system/gotk-patches.yaml

This file was deleted.

55 changes: 51 additions & 4 deletions pkg/gitops/flux/manifests/flux-system/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,54 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: {{.Namespace}}
resources:
- gotk-components.yaml
- gotk-sync.yaml
patchesStrategicMerge:
- gotk-patches.yaml
- gotk-components.yaml
- gotk-sync.yaml
patches:
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: helm-controller
namespace: {{.Namespace}}
spec:
template:
spec:
containers:
- image: {{.HelmControllerImage}}
name: manager
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: kustomize-controller
namespace: {{.Namespace}}
spec:
template:
spec:
containers:
- image: {{.KustomizeControllerImage}}
name: manager
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: notification-controller
namespace: {{.Namespace}}
spec:
template:
spec:
containers:
- image: {{.NotificationControllerImage}}
name: manager
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: source-controller
namespace: {{.Namespace}}
spec:
template:
spec:
containers:
- image: {{.SourceControllerImage}}
name: manager
File renamed without changes.
Loading

0 comments on commit 82fb2f8

Please sign in to comment.