From 4fe641b4f5bcde6ff939069a00494f5520b3aedb Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Thu, 29 Jun 2023 20:31:58 -0700 Subject: [PATCH] Correctly handle kustomization namespace for flux test (#247) Issue #245 ``` $ flux-local test -v --path tests/testdata/cluster5/ ======================================= test session starts ======================================== collected 1 item tests/testdata/cluster5/clusters/prod::flux-system::kustomization PASSED [100%] ======================================== 1 passed in 0.10s ========================================= ``` ``` $ flux-local test -v -A --path tests/testdata/cluster5/ ======================================= test session starts ======================================== collected 3 items tests/testdata/cluster5/clusters/prod::infra-configs::kustomization PASSED [ 33%] tests/testdata/cluster5/clusters/prod::infra-controllers::kustomization PASSED [ 66%] tests/testdata/cluster5/clusters/prod::flux-system::kustomization PASSED [100%] ======================================== 3 passed in 0.30s ========================================= --- flux_local/tool/test.py | 1 + tests/testdata/cluster5/README.md | 1 + .../clusters/prod/flux-system/gotk-sync.yaml | 26 +++++++++++++++ .../prod/flux-system/kustomization.yaml | 5 +++ .../clusters/prod/infrastructure.yaml | 33 +++++++++++++++++++ .../configs/cluster-policies.yaml | 25 ++++++++++++++ .../configs/helm-repositories.yaml | 31 +++++++++++++++++ .../infrastructure/configs/kustomization.yaml | 6 ++++ .../controllers/kustomization.yaml | 6 ++++ .../controllers/metallb-release.yaml | 27 +++++++++++++++ .../controllers/weave-gitops-release.yaml | 16 +++++++++ 11 files changed, 177 insertions(+) create mode 100644 tests/testdata/cluster5/README.md create mode 100644 tests/testdata/cluster5/clusters/prod/flux-system/gotk-sync.yaml create mode 100644 tests/testdata/cluster5/clusters/prod/flux-system/kustomization.yaml create mode 100644 tests/testdata/cluster5/clusters/prod/infrastructure.yaml create mode 100644 tests/testdata/cluster5/infrastructure/configs/cluster-policies.yaml create mode 100644 tests/testdata/cluster5/infrastructure/configs/helm-repositories.yaml create mode 100644 tests/testdata/cluster5/infrastructure/configs/kustomization.yaml create mode 100644 tests/testdata/cluster5/infrastructure/controllers/kustomization.yaml create mode 100644 tests/testdata/cluster5/infrastructure/controllers/metallb-release.yaml create mode 100644 tests/testdata/cluster5/infrastructure/controllers/weave-gitops-release.yaml diff --git a/flux_local/tool/test.py b/flux_local/tool/test.py index 5c3e11ee..99698740 100644 --- a/flux_local/tool/test.py +++ b/flux_local/tool/test.py @@ -406,6 +406,7 @@ async def run( # type: ignore[no-untyped-def] # If a real file path, then clear so it is not a test nodeid filter if test_path.startswith(".") or test_path.startswith("/"): test_path = None + query.kustomization.namespace = query.cluster.namespace query.kustomization.skip_crds = True query.helm_release.enabled = enable_helm query.helm_release.namespace = None diff --git a/tests/testdata/cluster5/README.md b/tests/testdata/cluster5/README.md new file mode 100644 index 00000000..9a5ca747 --- /dev/null +++ b/tests/testdata/cluster5/README.md @@ -0,0 +1 @@ +This repo has kustomizations that are not in the flux-system namespace. diff --git a/tests/testdata/cluster5/clusters/prod/flux-system/gotk-sync.yaml b/tests/testdata/cluster5/clusters/prod/flux-system/gotk-sync.yaml new file mode 100644 index 00000000..986ca7e8 --- /dev/null +++ b/tests/testdata/cluster5/clusters/prod/flux-system/gotk-sync.yaml @@ -0,0 +1,26 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta1 +kind: GitRepository +metadata: + name: flux-system + namespace: flux-system +spec: + interval: 1m0s + ref: + branch: main + secretRef: + name: flux-system + url: ssh://git@github.com/allenporter/flux-local +--- +apiVersion: kustomize.toolkit.fluxcd.io/v1beta2 +kind: Kustomization +metadata: + name: flux-system + namespace: flux-system +spec: + interval: 10m0s + path: ./tests/testdata/cluster5/clusters/prod + prune: true + sourceRef: + kind: GitRepository + name: flux-system diff --git a/tests/testdata/cluster5/clusters/prod/flux-system/kustomization.yaml b/tests/testdata/cluster5/clusters/prod/flux-system/kustomization.yaml new file mode 100644 index 00000000..197efd8a --- /dev/null +++ b/tests/testdata/cluster5/clusters/prod/flux-system/kustomization.yaml @@ -0,0 +1,5 @@ +--- +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: + - gotk-sync.yaml diff --git a/tests/testdata/cluster5/clusters/prod/infrastructure.yaml b/tests/testdata/cluster5/clusters/prod/infrastructure.yaml new file mode 100644 index 00000000..d8fd81f1 --- /dev/null +++ b/tests/testdata/cluster5/clusters/prod/infrastructure.yaml @@ -0,0 +1,33 @@ +--- +apiVersion: kustomize.toolkit.fluxcd.io/v1beta2 +kind: Kustomization +metadata: + name: infra-controllers + namespace: controllers +spec: + interval: 1h + retryInterval: 1m + timeout: 5m + sourceRef: + kind: GitRepository + name: flux-system + path: ./tests/testdata/cluster5/infrastructure/controllers + prune: true + wait: true +--- +apiVersion: kustomize.toolkit.fluxcd.io/v1beta2 +kind: Kustomization +metadata: + name: infra-configs + namespace: configs +spec: + dependsOn: + - name: infra-controllers + interval: 1h + retryInterval: 1m + timeout: 5m + sourceRef: + kind: GitRepository + name: flux-system + path: ./tests/testdata/cluster5/infrastructure/configs + prune: true diff --git a/tests/testdata/cluster5/infrastructure/configs/cluster-policies.yaml b/tests/testdata/cluster5/infrastructure/configs/cluster-policies.yaml new file mode 100644 index 00000000..fe3bc47a --- /dev/null +++ b/tests/testdata/cluster5/infrastructure/configs/cluster-policies.yaml @@ -0,0 +1,25 @@ +--- +apiVersion: kyverno.io/v1 +kind: ClusterPolicy +metadata: + name: test-allow-policy + annotations: + policies.kyverno.io/title: Test Allow Policy + policies.kyverno.io/description: >- + Policy that is expected to allow resources under test through since no + resources should have this annotation. +spec: + validationFailureAction: audit + background: true + rules: + - name: forbid-test-annotation + match: + resources: + kinds: + - ConfigMap + validate: + message: "Found test-annotation" + pattern: + metadata: + =(annotations): + X(flux-local/test-annotation): "null" diff --git a/tests/testdata/cluster5/infrastructure/configs/helm-repositories.yaml b/tests/testdata/cluster5/infrastructure/configs/helm-repositories.yaml new file mode 100644 index 00000000..4a8d0eee --- /dev/null +++ b/tests/testdata/cluster5/infrastructure/configs/helm-repositories.yaml @@ -0,0 +1,31 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta2 +kind: HelmRepository +metadata: + name: bitnami + namespace: flux-system +spec: + interval: 30m + provider: generic + timeout: 1m0s + url: https://charts.bitnami.com/bitnami +--- +apiVersion: source.toolkit.fluxcd.io/v1beta2 +kind: HelmRepository +metadata: + name: podinfo + namespace: flux-system +spec: + interval: 5m + type: oci + url: oci://ghcr.io/stefanprodan/charts +--- +apiVersion: source.toolkit.fluxcd.io/v1beta2 +kind: HelmRepository +metadata: + name: weave-charts + namespace: flux-system +spec: + interval: 120m + type: oci + url: oci://ghcr.io/weaveworks/charts diff --git a/tests/testdata/cluster5/infrastructure/configs/kustomization.yaml b/tests/testdata/cluster5/infrastructure/configs/kustomization.yaml new file mode 100644 index 00000000..bb6d201c --- /dev/null +++ b/tests/testdata/cluster5/infrastructure/configs/kustomization.yaml @@ -0,0 +1,6 @@ +--- +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: + - cluster-policies.yaml + - helm-repositories.yaml diff --git a/tests/testdata/cluster5/infrastructure/controllers/kustomization.yaml b/tests/testdata/cluster5/infrastructure/controllers/kustomization.yaml new file mode 100644 index 00000000..6bb7dc20 --- /dev/null +++ b/tests/testdata/cluster5/infrastructure/controllers/kustomization.yaml @@ -0,0 +1,6 @@ +--- +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: + - metallb-release.yaml + - weave-gitops-release.yaml diff --git a/tests/testdata/cluster5/infrastructure/controllers/metallb-release.yaml b/tests/testdata/cluster5/infrastructure/controllers/metallb-release.yaml new file mode 100644 index 00000000..f7a9a31e --- /dev/null +++ b/tests/testdata/cluster5/infrastructure/controllers/metallb-release.yaml @@ -0,0 +1,27 @@ +--- +apiVersion: helm.toolkit.fluxcd.io/v2beta1 +kind: HelmRelease +metadata: + name: metallb + namespace: metallb +spec: + chart: + spec: + chart: metallb + reconcileStrategy: ChartVersion + sourceRef: + kind: HelmRepository + name: bitnami + namespace: flux-system + version: 4.1.14 + install: + crds: CreateReplace + remediation: + retries: 3 + interval: 5m + releaseName: metallb + upgrade: + crds: CreateReplace + values: + speaker: + secretName: "metallb-secret" diff --git a/tests/testdata/cluster5/infrastructure/controllers/weave-gitops-release.yaml b/tests/testdata/cluster5/infrastructure/controllers/weave-gitops-release.yaml new file mode 100644 index 00000000..d543f55f --- /dev/null +++ b/tests/testdata/cluster5/infrastructure/controllers/weave-gitops-release.yaml @@ -0,0 +1,16 @@ +--- +apiVersion: helm.toolkit.fluxcd.io/v2beta1 +kind: HelmRelease +metadata: + name: weave-gitops + namespace: flux-system +spec: + interval: 60m + chart: + spec: + chart: weave-gitops + version: "4.0.22" + sourceRef: + kind: HelmRepository + name: weave-charts + interval: 12h