-
Notifications
You must be signed in to change notification settings - Fork 32
✨ Support kustomize fragments in crane transform #886
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
Open
aufi
wants to merge
3
commits into
migtools:main
Choose a base branch
from
aufi:feature/transform-kustomize-fragments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package transform | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseStageKustomize(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| values []string | ||
| wantErr string | ||
| check func(t *testing.T, result map[string]map[string]interface{}) | ||
| }{ | ||
| { | ||
| name: "single stage json", | ||
| values: []string{`KubernetesPlugin={"namespace": "dest-ns", "commonLabels": {"app": "crane"}}`}, | ||
| check: func(t *testing.T, result map[string]map[string]interface{}) { | ||
| frag := result["KubernetesPlugin"] | ||
| if frag["namespace"] != "dest-ns" { | ||
| t.Errorf("namespace: got %v", frag["namespace"]) | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| name: "single stage yaml", | ||
| values: []string{"CustomEdits=namespace: dest-ns\ncommonLabels:\n app: crane\n"}, | ||
| check: func(t *testing.T, result map[string]map[string]interface{}) { | ||
| frag := result["CustomEdits"] | ||
| if frag["namespace"] != "dest-ns" { | ||
| t.Errorf("namespace: got %v", frag["namespace"]) | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| name: "multiple stages", | ||
| values: []string{ | ||
| `KubernetesPlugin={"namespace": "a"}`, | ||
| `RegistryPlugin={"namespace": "b"}`, | ||
| }, | ||
| check: func(t *testing.T, result map[string]map[string]interface{}) { | ||
| if len(result) != 2 { | ||
| t.Errorf("expected 2 stages, got %d", len(result)) | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| name: "missing equals sign", | ||
| values: []string{"KubernetesPlugin"}, | ||
| wantErr: "expected format StageName=YAML", | ||
| }, | ||
| { | ||
| name: "empty stage name", | ||
| values: []string{`={"namespace": "x"}`}, | ||
| wantErr: "stage name is empty", | ||
| }, | ||
| { | ||
| name: "empty fragment", | ||
| values: []string{`KubernetesPlugin=`}, | ||
| wantErr: "empty", | ||
| }, | ||
| { | ||
| name: "list fragment rejected", | ||
| values: []string{`KubernetesPlugin=["a", "b"]`}, | ||
| wantErr: "must be a mapping", | ||
| }, | ||
| { | ||
| name: "duplicate stage", | ||
| values: []string{ | ||
| `KubernetesPlugin={"namespace": "a"}`, | ||
| `KubernetesPlugin={"namespace": "b"}`, | ||
| }, | ||
| wantErr: "duplicate", | ||
| }, | ||
| { | ||
| name: "fragment with commas is not split", | ||
| values: []string{`KubernetesPlugin={"images": [{"name": "a", "newName": "b"}], "namespace": "ns"}`}, | ||
| check: func(t *testing.T, result map[string]map[string]interface{}) { | ||
| frag := result["KubernetesPlugin"] | ||
| if frag["namespace"] != "ns" { | ||
| t.Errorf("namespace: got %v", frag["namespace"]) | ||
| } | ||
| if _, ok := frag["images"].([]interface{}); !ok { | ||
| t.Errorf("images: expected list, got %T", frag["images"]) | ||
| } | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result, err := parseStageKustomize(tt.values) | ||
| if tt.wantErr != "" { | ||
| if err == nil { | ||
| t.Fatalf("expected error containing %q, got nil", tt.wantErr) | ||
| } | ||
| if !strings.Contains(err.Error(), tt.wantErr) { | ||
| t.Fatalf("expected error containing %q, got %v", tt.wantErr, err) | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if tt.check != nil { | ||
| tt.check(t, result) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # Per-stage kustomize fragments | ||
|
|
||
| `crane transform` can merge an inline **kustomize fragment** into the | ||
| `kustomization.yaml` that is generated for a stage. This lets you inject extra | ||
| kustomize configuration (namespace, common labels/annotations, images, name | ||
| prefixes, additional resources/patches, …) without hand-editing the generated | ||
| files after every run. | ||
|
|
||
| The fragment is provided inline — either through a CLI flag or in the | ||
| transform instructions file — following the same per-stage pattern as | ||
| [`--stage-optionals`](./multistage-pipeline.md). | ||
|
|
||
| ## How it works | ||
|
|
||
| For each stage, crane generates a `kustomization.yaml` containing `resources` | ||
| and `patches`. When a fragment is configured for that stage, it is merged into | ||
| the generated file using these rules: | ||
|
|
||
| | Field | Merge behaviour | | ||
| |-------|-----------------| | ||
| | `resources` | Fragment entries are **appended** to the generated ones (de-duplicated by value). | | ||
| | `patches` | Fragment entries are **appended** to the generated ones. | | ||
| | `apiVersion`, `kind` | Kept from the generated file; fragment values are ignored. | | ||
| | any other field | Fragment value **replaces** the generated value. | | ||
|
|
||
| Stages without a fragment are left byte-for-byte unchanged. | ||
|
|
||
| The stage is identified by its **plugin/base name** (e.g. `KubernetesPlugin`, | ||
| `CustomEdits`) — the same key used by `--stage-optionals` — not by the numbered | ||
| directory name (`10_KubernetesPlugin`). | ||
|
|
||
| ## CLI flag | ||
|
|
||
| ``` | ||
| --stage-kustomize 'StageName=<YAML or JSON>' | ||
| ``` | ||
|
|
||
| The flag is **repeatable** (once per stage). The value after `=` is a kustomize | ||
| fragment as a mapping. JSON is valid YAML, so either form works; JSON is usually | ||
| easier to pass on a single command line. | ||
|
|
||
| ### Example: namespace + common labels | ||
|
|
||
| ```sh | ||
| crane transform KubernetesPlugin \ | ||
| --stage-kustomize 'KubernetesPlugin={"namespace":"dest-ns","commonLabels":{"app":"crane"}}' | ||
| ``` | ||
|
|
||
| Resulting `transform/10_KubernetesPlugin/kustomization.yaml`: | ||
|
|
||
| ```yaml | ||
| apiVersion: kustomize.config.k8s.io/v1beta1 | ||
| kind: Kustomization | ||
| commonLabels: | ||
| app: crane | ||
| namespace: dest-ns | ||
| resources: | ||
| - input/ConfigMap__v1_default_demo.yaml | ||
| ``` | ||
|
|
||
| ### Example: image overrides (multi-line YAML value) | ||
|
|
||
| ```sh | ||
| crane transform KubernetesPlugin \ | ||
| --stage-kustomize 'KubernetesPlugin= | ||
| images: | ||
| - name: nginx | ||
| newName: quay.io/mirror/nginx | ||
| newTag: "1.27" | ||
| ' | ||
| ``` | ||
|
|
||
| ### Example: multiple stages | ||
|
|
||
| ```sh | ||
| crane transform \ | ||
| --stage-kustomize 'KubernetesPlugin={"namespace":"dest-ns"}' \ | ||
| --stage-kustomize 'CustomEdits={"commonAnnotations":{"origin":"crane"}}' | ||
| ``` | ||
|
|
||
| ## Instructions file | ||
|
|
||
| Add a `kustomize:` block to a stage entry, alongside the existing `optionals:` | ||
| field: | ||
|
|
||
| ```yaml | ||
| # instructions.yaml | ||
| stages: | ||
| - name: KubernetesPlugin | ||
| optionals: | ||
| registry-replacement: "docker.io=quay.io" | ||
| kustomize: | ||
| namespace: dest-ns | ||
| commonLabels: | ||
| app: crane | ||
| - name: CustomEdits | ||
| kustomize: | ||
| commonAnnotations: | ||
| origin: instructions | ||
| ``` | ||
|
|
||
| Run it with: | ||
|
|
||
| ```sh | ||
| crane transform --instructions-file instructions.yaml | ||
| ``` | ||
|
|
||
| > `--instructions-file` cannot be combined with `--stage-kustomize` (or | ||
| > `--stage-optionals`). When an instructions file is used, its `kustomize:` | ||
| > blocks take precedence. | ||
|
|
||
| ## Validation & errors | ||
|
|
||
| - The fragment must be a **mapping** — a list or scalar is rejected. | ||
| - A fragment referencing a stage that is not part of the run fails with | ||
| `per-stage kustomize fragment references unknown stage "..."`. | ||
| - `resources`/`patches` in a fragment must be lists. | ||
| - Extra `resources` must point to files that exist relative to the stage | ||
| directory, otherwise the subsequent `kustomize build` fails. | ||
|
|
||
| ## See also | ||
|
|
||
| - [Multistage pipeline](./multistage-pipeline.md) — stages, `--stage-optionals`, | ||
| and the instructions file format. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add a language identifier to this fenced code block.
This fence triggers markdownlint rule MD040. Use
textfor the CLI option syntax.🧰 Tools
🪛 markdownlint-cli2 (0.23.2)
[warning] 34-34: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🤖 Prompt for AI Agents
Source: Linters/SAST tools