Skip to content

fix: allow recursive dry-run over local sources #5219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025

Conversation

niveau0
Copy link
Contributor

@niveau0 niveau0 commented Feb 27, 2025

This PR tries to fix the issue using flux build with dry-run and recursion.

We have a setup with nested kustomizations and want to render the whole tree
as yaml files, including postBuild substitutions. Afterwards, this should be
checked via kube-linter.

Currently this seems not possible(?) with the flux-cli together with dry-run,
therefore I made some changes.
Since I don't know all the use cases for flux build, it is hard to tell if this
breaks anything.

Maybe someone with deeper knowledge can review the changes.

The command I use is similar to this:

flux build kustomization flux-system \
--recursive \
--path ./clusters/dev/mykustomizations \
-n flux-system \
--local-sources GitRepository/flux-system/flux-system=./ \
--dry-run \
--kustomization-file ./clusters/dev/flux-system/gotk-sync.yaml

@niveau0 niveau0 force-pushed the allow-recursive-dry-run branch 2 times, most recently from 3d1e964 to 04b5ef1 Compare March 5, 2025 07:57
@bkreitch
Copy link
Contributor

Seems reasonable to me. I would just switch branches of if b.dryRun and else in build() and revert blank lines except at line 370. And add following test to cmd/flux/build_kustomization_test.go in TestBuildLocalKustomization:

{
	name:       "build with recursive in dry-run mode",
	args:       "build kustomization podinfo --kustomization-file " + tmpFile + " --path ./testdata/build-kustomization/podinfo-with-my-app --recursive --local-sources GitRepository/default/podinfo=./testdata/build-kustomization --dry-run",
	resultFile: "./testdata/build-kustomization/podinfo-with-my-app-result.yaml",
	assertFunc: "assertGoldenTemplateFile",
},

@niveau0
Copy link
Contributor Author

niveau0 commented Mar 17, 2025

@bkreitch , thanks for the advice, I will add a test. But I'm not sure what you mean with the if and else branches, since the if and else in the PR are not related?

@niveau0 niveau0 force-pushed the allow-recursive-dry-run branch 3 times, most recently from a80f266 to 6d5f7ab Compare March 17, 2025 09:43
@bkreitch
Copy link
Contributor

But I'm not sure what you mean with the if and else branches, since the if and else in the PR are not related?

@niveau0 I mean to reverse if condition so it will be like:

if b.dryRun {
	liveKus = b.kustomization
} else {
	liveKus, err = b.getKustomization(ctx)
	if err != nil {
...

@bkreitch
Copy link
Contributor

@niveau0 and for the test - I think that if you'll put my snippet from above into TestBuildLocalKustomization() instead of TestBuildKustomization() it should be enough and additional files are not needed.

@niveau0 niveau0 force-pushed the allow-recursive-dry-run branch from 6d5f7ab to c0e6b98 Compare March 17, 2025 12:33
@niveau0
Copy link
Contributor Author

niveau0 commented Mar 17, 2025

@niveau0 and for the test - I think that if you'll put my snippet from above into TestBuildLocalKustomization() instead of TestBuildKustomization() it should be enough and additional files are not needed.

@bkreitch ah ok got it now, I think I failed to see the 'Local' part in TestBuildLocalKustomization and got confused.

@stefanprodan
Copy link
Member

@niveau0 can you please rebase with upstream main and force push, this PR should have a single commit. Thanks

@niveau0 niveau0 force-pushed the allow-recursive-dry-run branch 2 times, most recently from f4190ef to 963db06 Compare April 9, 2025 10:48
@niveau0
Copy link
Contributor Author

niveau0 commented Apr 9, 2025

@niveau0 can you please rebase with upstream main and force push, this PR should have a single commit. Thanks

@stefanprodan sure, I think I did now :)

@niveau0 niveau0 force-pushed the allow-recursive-dry-run branch from 963db06 to 2c6ac18 Compare April 15, 2025 06:17
@matheuscscp
Copy link
Member

I'll review this today

Copy link
Member

@matheuscscp matheuscscp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this fix and it correctly rendered all the YAMLS in my Kustomization tree, except for the resources deployed by HelmReleases (as expected)!

This was the command I used:

./flux build kustomization matheus-test-repo \
    --recursive \
    --path ./flux-system \
    -n flux-system \
    --local-sources GitRepository/flux-system/matheus-test-repo=./ \
    --dry-run \
    --kustomization-file ./flux-system/root-kustomization.yaml

This was my Kustomization tree (objects deployed by HelmReleases omitted):

Kustomization/flux-system/matheus-test-repo
├── Kustomization/flux-system/matheus-test-repo-configs
│   ├── ConfigMap/monitoring/flux-grafana-dashboards-dfh45k26mg
│   ├── PodMonitor/monitoring/flux-system
│   └── ServiceMonitor/monitoring/flux-operator
├── Kustomization/flux-system/matheus-test-repo-controllers
│   ├── Namespace/monitoring
│   ├── ServiceAccount/monitoring/flux
│   ├── ClusterRoleBinding/flux-monitoring
│   ├── HelmRelease/flux-system/flux-operator
│   │   └── ...
│   ├── HelmRelease/monitoring/kube-prometheus-stack
│   │   └── ...
│   ├── HelmRelease/monitoring/metrics-server
│   │   └── ...
│   ├── Alert/flux-system/dispatch
│   ├── Provider/flux-system/github-dispatch
│   ├── HelmRepository/monitoring/metrics-server
│   ├── HelmRepository/monitoring/prometheus-community
│   └── OCIRepository/flux-system/flux-operator
├── Kustomization/flux-system/matheus-test-repo-workloads
│   └── ResourceSetInputProvider/flux-system/rsip-gh-app-token-cache-test
└── GitRepository/flux-system/matheus-test-repo

@stefanprodan
Copy link
Member

@niveau0 can you please rebase and force push

@niveau0 niveau0 force-pushed the allow-recursive-dry-run branch from 2c6ac18 to 1b98e16 Compare April 16, 2025 07:27
@niveau0
Copy link
Contributor Author

niveau0 commented Apr 16, 2025

@stefanprodan done

@stefanprodan stefanprodan added the area/kustomization Kustomization related issues and pull requests label Apr 16, 2025
Copy link
Member

@stefanprodan stefanprodan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks @niveau0 🎖️

@stefanprodan stefanprodan merged commit d9b66f6 into fluxcd:main Apr 16, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kustomization Kustomization related issues and pull requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants