Add PathPattern (directory discovery) to ArtifactGenerator#341
Add PathPattern (directory discovery) to ArtifactGenerator#341vocarista wants to merge 1 commit into
Conversation
|
@vocarista Please edit the PR description with YAML examples explaining the new feature's behavior. |
| - `pathPattern` (optional): Specifies a directory traversal pattern within a source in the format `@<alias>/<pattern>`. | ||
| Named captures in the pattern (e.g., `{app}`, `{env}`) can be used as Go template variables (`{{ .app }}`, `{{ .env }}`) inside the `artifacts` fields. | ||
|
|
||
| When `pathPattern` is used, the generated ExternalArtifacts will automatically have their labels populated with the extracted capture variables. |
There was a problem hiding this comment.
Dir names are not compatible with Kubernetes labels which are restricted to DNS. Also dir names do not qualify for Kubernetes object names. What if the dirs are in Chinese or they are named like .bar.foo?
There was a problem hiding this comment.
@stefanprodan, thanks for catching that, I hadn't taken that into consideration. I see two quick options, we either skip dirs that do not match the regex or fail the reconciliation and ask the user to use compatible dir names. Thoughts?
There was a problem hiding this comment.
I expect most users to not be able to use this feature, the label value restrictions are very tight. What's the reason to use the dirs as labels?
There was a problem hiding this comment.
I guess whoever whats to use this must have a dir structure that complies with Kubernetes label value restrictions which we should document.
After extraction, the controller should validate the values using https://github.com/fluxcd/flux-schema/blob/aa86d0ac25d21f7312366272ea79e50d475be9e8/internal/validator/metadata.go#L79
If validation passes, the controller should apply to lower on the values before setting the labels and artifact name. If the validation fails, we can mark the reconciliation as stalled and return a terminal error. If paths gets fixed, this will trigger a new reconciliation.
There was a problem hiding this comment.
Specifically content.IsLabelValue?
There was a problem hiding this comment.
@stefanprodan the intent behind label propagation was to align with your original comment about letting RSIP select ExternalArtifacts using label. I do agree that the directory name compliance with label values has to be documented. I think another thing that also needs to be documented is what if two directory names result in the same value (MY-APP and My-App would both be sanitised to my-app.) In this case too, we'll stall the reconciliation and throw an error along the lines of duplicate directories found right?
There was a problem hiding this comment.
Yes good catch, we should also validate for duplicates and error out.
Validation workflow could be:
- run ToLower on all extracted values
- validate values with
content.IsLabelValue - validate all EA names with https://github.com/fluxcd/flux-schema/blob/aa86d0ac25d21f7312366272ea79e50d475be9e8/internal/validator/metadata.go#L55
- validate for duplicates
Any validation error should contain the pathPattern verbatim as in the spec, so that people can figure out from where values come from.
Signed-off-by: Kumar Piyush <kr.piyush888@gmail.com>
Part of controlplaneio-fluxcd/flux-operator#421
This PR introduces the
pathPatternfeature to theArtifactGeneratorCRD to support dynamic, directory-basedExternalArtifactgeneration. This resolves the need for users to manually define anOutputArtifactfor every single directory (e.g., every app/environment in a monorepo).Example
Given a monorepo with this directory structure:
Before (static)
Each artifact must be explicitly listed. Adding a new app or environment requires updating this spec:
After (with
pathPattern)A single
pathPatternreplaces all explicit entries. Theartifactsblock becomes a template that is expanded once per matched directory:Both specs produce the same 4
ExternalArtifactobjects in the cluster. WithpathPattern, the captured variables are additionally injected as labels on each generatedExternalArtifact(e.g.,app: auth,env: dev).When a new directory (e.g.,
apps/payments/envs/prod/) is added to the repo, the controller automatically creates the correspondingExternalArtifact— no spec update required. Conversely, when a directory is removed, itsExternalArtifactis garbage collected.Key Features & Implementation Details:
spec.pathPatterntoArtifactGeneratorSpecand enabled Go template syntax validation forOutputArtifact.Name.@monorepo/apps/{app}/envs/{env}into regex with named capture groups to match target directories.pathPatternis defined,spec.outputArtifactsacts as a template. TheName,Copy.From, andCopy.Tofields are rendered using standard text/template (e.g.,{{ .app }}).apps/auth/envs/devchanges, only theauth-devartifact gets a new revision hash, leaving other untouched artifacts intact.Testing:
artifactgenerator_pathpattern_test.go): Covered static paths, single capture patterns, invalid formats, exact-depth boundary files, and Exclude/Strategy field preservation.artifactgenerator_controller_test.go): Covered end-to-end artifact generation, strict label assertions, unpacking and asserting against physical copy paths, garbage collection on directory deletion, and selective revision updates.