From dc9c2054bc95a9e84365e18bc8f193b870dbf4a1 Mon Sep 17 00:00:00 2001
From: Usman <akeju00+github@gmail.com>
Date: Mon, 31 Oct 2022 12:47:03 +0100
Subject: [PATCH] feat(manager/flux): support system manifest inside
 subdirectories (#18605)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 lib/modules/manager/flux/artifacts.spec.ts | 37 ++++++++++++++++++++++
 lib/modules/manager/flux/common.ts         |  4 ++-
 lib/modules/manager/flux/readme.md         |  2 +-
 3 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/lib/modules/manager/flux/artifacts.spec.ts b/lib/modules/manager/flux/artifacts.spec.ts
index 35e7671476..9a7d507ec5 100644
--- a/lib/modules/manager/flux/artifacts.spec.ts
+++ b/lib/modules/manager/flux/artifacts.spec.ts
@@ -48,6 +48,43 @@ describe('modules/manager/flux/artifacts', () => {
     ]);
   });
 
+  it('detects system manifests in subdirectories', async () => {
+    const snapshots = mockExecAll({ stdout: '', stderr: '' });
+    fs.readLocalFile.mockResolvedValueOnce('old');
+    fs.readLocalFile.mockResolvedValueOnce('test');
+
+    const res = await updateArtifacts({
+      packageFileName:
+        'clusters/my-cluster/flux-system/gitops-toolkit/gotk-components.yaml',
+      updatedDeps: [
+        {
+          newVersion: '1.0.1',
+          managerData: {
+            components:
+              'source-controller,kustomize-controller,helm-controller,notification-controller',
+          },
+        },
+      ],
+      newPackageFileContent: '',
+      config: {},
+    });
+
+    expect(res).toEqual([
+      {
+        file: {
+          type: 'addition',
+          path: 'clusters/my-cluster/flux-system/gitops-toolkit/gotk-components.yaml',
+          contents: 'test',
+        },
+      },
+    ]);
+    expect(snapshots).toMatchObject([
+      {
+        cmd: 'flux install --export --components source-controller,kustomize-controller,helm-controller,notification-controller > clusters/my-cluster/flux-system/gitops-toolkit/gotk-components.yaml',
+      },
+    ]);
+  });
+
   it('ignores non-system manifests', async () => {
     const res = await updateArtifacts({
       packageFileName: 'not-a-system-manifest.yaml',
diff --git a/lib/modules/manager/flux/common.ts b/lib/modules/manager/flux/common.ts
index b478f7a75d..320e7b9f31 100644
--- a/lib/modules/manager/flux/common.ts
+++ b/lib/modules/manager/flux/common.ts
@@ -1,6 +1,8 @@
 import { regEx } from '../../../util/regex';
 
-export const systemManifestRegex = '(^|/)flux-system/gotk-components\\.yaml$';
+export const systemManifestRegex =
+  '(^|\\/)flux-system\\/(?:.+\\/)?gotk-components\\.yaml$';
+
 export function isSystemManifest(file: string): boolean {
   return regEx(systemManifestRegex).test(file);
 }
diff --git a/lib/modules/manager/flux/readme.md b/lib/modules/manager/flux/readme.md
index 27a26f909b..ae2e4a33a4 100644
--- a/lib/modules/manager/flux/readme.md
+++ b/lib/modules/manager/flux/readme.md
@@ -31,7 +31,7 @@ Updating system manifests requires that either:
 
 ### Non-configured fileMatch
 
-By default, the `flux` manager will only match `flux-system/gotk-components.yaml` (i.e. system manifest) files.
+By default, the `flux` manager will only match `flux-system/{.,**}/gotk-components.yaml` (i.e. system manifest) files.
 
 This is because there is no commonly accepted file/directory naming convention for Flux manifests and we don't want to check every single `*.yaml` file in repositories just in case some of them have Flux definitions.
 
-- 
GitLab