Skip to content

Commit 395d261

Browse files
authored
[LIB] Fix the behavior of null-preserving visitor, refactor, add comparison tests (#4774)
* add unit test cases, cleanup and splitting of visitors Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * cover the test cases, minor cleanup Assisted-by: Cursor:grok-4.7 Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * add explanatory comments Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * add comparison tests Assisted-by: Cursor:grok-4.7 Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * remove duplicate case, clarify mismatched test case outputs Assisted-by: Cursor:grok-4.7 Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * add missing implicit null test case Assisted-by: Cursor:grok-4.7 Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * fix lint Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * renaming to PreserveNull part 1 Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * fix schema gen Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * AI docs adjustments, regenerate Assisted-by: Cursor:grok-4.7 Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * uncomment replace Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> * fix copilot comments Assisted-by: Cursor:grok-4.7 Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com> --------- Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com>
1 parent 0198a98 commit 395d261

29 files changed

Lines changed: 1306 additions & 400 deletions

File tree

‎Makefile‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ install-golangci-lint:
4141

4242
.PHONY: install-swagger
4343
install-swagger:
44-
go install github.com/go-swagger/go-swagger/cmd/swagger@v0.33.1
44+
go install github.com/go-swagger/go-swagger/cmd/swagger@$(SWAGGER_VERSION)
4545

4646
.PHONY: install-mdtogo
4747
install-mdtogo:
@@ -53,14 +53,15 @@ fix: fix-api
5353
fmt: fmt-api
5454
go fmt ./...
5555

56-
schema: install-swagger
57-
GOBIN=$(GOBIN) scripts/generate-schema.sh
56+
schema:
57+
SWAGGER_VERSION=$(SWAGGER_VERSION) GOBIN=$(GOBIN) scripts/generate-schema.sh
5858

5959
generate: install-mdtogo generate-api
6060
rm -rf internal/docs/generated
6161
mkdir internal/docs/generated
6262
GOBIN=$(GOBIN) YEAR_GEN=$(YEAR_GEN) go generate ./...
6363
go fmt ./internal/docs/generated/...
64+
make schema
6465

6566
tidy: tidy-api
6667
go mod tidy

‎api/generated/applyconfigurations/kptfile/v1/function.go‎

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/generated/applyconfigurations/kptfile/v1/upstream.go‎

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/kptfile/v1/types.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,12 @@ type Upstream struct {
163163
// UpdateStrategy declares how a package will be updated from upstream.
164164
UpdateStrategy UpdateStrategyType `yaml:"updateStrategy,omitempty" json:"updateStrategy,omitempty"`
165165

166-
// PreserveExplicitNull, when true, keeps a field explicitly nulled in the
167-
// local package during resource-merge instead of deleting it. Only applies
168-
// to the resource-merge update strategy.
169-
PreserveExplicitNull bool `yaml:"preserveExplicitNull,omitempty" json:"preserveExplicitNull,omitempty"`
166+
// PreserveNulls, when true, keeps a null field (`null`, `~`, or an empty
167+
// value) in the local package or upstream during resource-merge instead of
168+
// deleting it. Only applies to the resource-merge update strategy.
169+
//
170+
// Experimental: Behavior and naming are subject to change.
171+
PreserveNulls bool `yaml:"preserveNulls,omitempty" json:"preserveNulls,omitempty"`
170172
}
171173

172174
// Git is the user-specified locator for a package on Git.
@@ -381,11 +383,13 @@ type Function struct {
381383
// If omitted or evaluates to true, the function executes normally.
382384
// If evaluates to false, the function is skipped.
383385
//
384-
// Example: Check if a specific ConfigMap exists among the selected resources:
385-
// when: "resources.exists(r, r.kind == 'ConfigMap' && r.metadata.name == 'my-config')"
386+
// Examples:
387+
//
388+
// - Check if a specific ConfigMap exists among the selected resources:
389+
// when: `"resources.exists(r, r.kind == 'ConfigMap' && r.metadata.name == 'my-config')"`
386390
//
387-
// Example: Check resource count among the selected resources:
388-
// when: "resources.filter(r, r.kind == 'Deployment').size() > 0"
391+
// - Check resource count among the selected resources:
392+
// `when: "resources.filter(r, r.kind == 'Deployment').size() > 0"`
389393
CelCondition string `yaml:"when,omitempty" json:"when,omitempty"`
390394
}
391395

‎commands/pkg/update/cmdupdate.go‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ func NewRunner(ctx context.Context, parent string) *Runner {
5656
_ = c.RegisterFlagCompletionFunc("strategy", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
5757
return kptfilev1.UpdateStrategiesAsStrings(), cobra.ShellCompDirectiveDefault
5858
})
59-
c.Flags().BoolVar(&r.preserveExplicitNull, "preserve-explicit-null", false,
60-
"keep fields explicitly set to null in the local package during resource-merge "+
61-
"instead of deleting them. This will change the default for the package.")
59+
c.Flags().BoolVar(&r.preserveNulls, "preserve-nulls", false,
60+
"keep null fields (null, ~, or an empty value) in the local package or upstream "+
61+
"during resource-merge instead of deleting them. This will change the default for the package.\n\n"+
62+
"EXPERIMENTAL: Behavior and naming are subject to change.")
6263
cmdutil.FixDocs("kpt", parent, c)
6364
r.Command = c
6465
return r
@@ -73,8 +74,8 @@ func NewCommand(ctx context.Context, parent string) *cobra.Command {
7374
type Runner struct {
7475
ctx context.Context
7576

76-
strategy string
77-
preserveExplicitNull bool
77+
strategy string
78+
preserveNulls bool
7879

7980
Update update.Command
8081
Command *cobra.Command
@@ -90,8 +91,8 @@ func (r *Runner) preRunE(cmd *cobra.Command, args []string) error {
9091
} else {
9192
r.Update.Strategy = kptfilev1.UpdateStrategyType(r.strategy)
9293
}
93-
if cmd.Flags().Changed("preserve-explicit-null") {
94-
r.Update.PreserveExplicitNull = &r.preserveExplicitNull
94+
if cmd.Flags().Changed("preserve-nulls") {
95+
r.Update.PreserveNulls = &r.preserveNulls
9596
}
9697

9798
parts := strings.Split(args[0], "@")

‎commands/pkg/update/cmdupdate_test.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func TestCmd_Execute_flagAndArgParsing(t *testing.T) {
454454
assert.NoError(t, err)
455455
assert.Equal(t, "", r.Update.Ref)
456456
assert.Equal(t, kptfilev1.ResourceMerge, r.Update.Strategy)
457-
assert.Nil(t, r.Update.PreserveExplicitNull)
457+
assert.Nil(t, r.Update.PreserveNulls)
458458

459459
// verify an error is thrown if multiple paths are specified
460460
r = update.NewRunner(fake.CtxWithDefaultPrinter(), "kpt")
@@ -494,20 +494,20 @@ func TestCmd_Execute_flagAndArgParsing(t *testing.T) {
494494

495495
r = update.NewRunner(fake.CtxWithDefaultPrinter(), "kpt")
496496
r.Command.RunE = NoOpRunE
497-
r.Command.SetArgs([]string{dir, "--preserve-explicit-null"})
497+
r.Command.SetArgs([]string{dir, "--preserve-nulls"})
498498
err = r.Command.Execute()
499499
assert.NoError(t, err)
500-
if assert.NotNil(t, r.Update.PreserveExplicitNull) {
501-
assert.True(t, *r.Update.PreserveExplicitNull)
500+
if assert.NotNil(t, r.Update.PreserveNulls) {
501+
assert.True(t, *r.Update.PreserveNulls)
502502
}
503503

504504
r = update.NewRunner(fake.CtxWithDefaultPrinter(), "kpt")
505505
r.Command.RunE = NoOpRunE
506-
r.Command.SetArgs([]string{dir, "--preserve-explicit-null=false"})
506+
r.Command.SetArgs([]string{dir, "--preserve-nulls=false"})
507507
err = r.Command.Execute()
508508
assert.NoError(t, err)
509-
if assert.NotNil(t, r.Update.PreserveExplicitNull) {
510-
assert.False(t, *r.Update.PreserveExplicitNull)
509+
if assert.NotNil(t, r.Update.PreserveNulls) {
510+
assert.False(t, *r.Update.PreserveNulls)
511511
}
512512
}
513513

‎documentation/content/en/guides/3-way-merge.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ Uses structural comparison of Kubernetes resources to intelligently merge change
5959
- Matches resources by their identity (group, kind, name, namespace)
6060
- Intelligently handles lists using merge keys (e.g., containers matched by name)
6161
- Preserves your customizations while applying upstream improvements
62-
- By default, an explicit local `null` is treated as a field deletion. Pass
63-
`--preserve-explicit-null` (persisted as `upstream.preserveExplicitNull` in
64-
the Kptfile) to keep those nulls instead.
62+
- (Experimental) By default, a null (`null`, `~`, or an empty value) in local
63+
or upstream is treated as a field deletion. Pass `--preserve-nulls`
64+
(persisted as `upstream.preserveNulls` in the Kptfile) to keep those nulls
65+
instead.
6566

66-
**When to use**:
67+
**When to use**:
6768
- **Default choice** - recommended for most situations
6869
- When you have significant local customizations
6970
- When you want to receive upstream improvements

‎documentation/content/en/reference/cli/pkg/update/_index.md‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ VERSION:
6060
* force-delete-replace: Wipe all the local changes to the package and replace
6161
it with the remote version.
6262

63-
--preserve-explicit-null:
64-
When set, fields explicitly nulled in the local package (for example
65-
`field:`, `null`, or `~`) are kept as null during resource-merge instead of
66-
being deleted. This changes the default for the current and future updates
67-
and is persisted as `upstream.preserveExplicitNull` in the Kptfile.
63+
--preserve-nulls:
64+
(Experimental) When set, null fields in the local package or upstream (`field:`, `null`, or
65+
`~`) are kept during resource-merge instead of being deleted. This changes
66+
the default for the current and future updates and is persisted as
67+
`upstream.preserveNulls` in the Kptfile.
6868
Only applies to the resource-merge strategy. Defaults to false.
6969
```
7070
@@ -202,23 +202,24 @@ On the resource level, the rules are:
202202
* A resource in both upstream and local will be merged into local.
203203

204204
On the field level, the rules differ based on the type of field.
205-
When `--preserve-explicit-null` (or `upstream.preserveExplicitNull` in the
206-
Kptfile) is set, a field explicitly nulled in local is kept as null instead of
207-
being removed. This applies to scalars, mappings, and lists.
205+
When `--preserve-nulls` (or `upstream.preserveNulls` in the
206+
Kptfile) is set, a null in local or upstream is kept instead of being
207+
removed. This includes `null`, `~`, and an empty value (`field:`), and
208+
applies to scalars, mappings, and lists.
208209

209210
For scalars and non-associative lists:
210-
* If the field is present in either upstream or local and the value is `null`, remove the field from local.
211+
* Unless `--preserve-nulls` is set, a field present in either upstream or local whose value is `null`, `~`, or empty is removed from local.
211212
* If the field is unchanged between upstream and local, leave the local value unchanged.
212213
* If the field has been changed in both upstream and local, update local with the value from upstream.
213214

214215
For mappings:
215-
* If the field is present in either upstream or local and the value is `null`, remove the field from local.
216+
* Unless `--preserve-nulls` is set, a field present in either upstream or local whose value is `null`, `~`, or empty is removed from local.
216217
* If the field is present only in local, leave the local value unchanged.
217218
* If the field is not present in local, add the delta between origin and upstream as the value in local.
218219
* If the field is present in both upstream and local, recursively merge the values between local, upstream and origin.
219220

220221
For associative lists:
221-
* If the field is present in either upstream or local and the value is `null`, remove the field from local.
222+
* Unless `--preserve-nulls` is set, a field present in either upstream or local whose value is `null`, `~`, or empty is removed from local.
222223
* If the field is present only in local, leave the local value unchanged.
223224
* If the field is not present in local, add the delta between origin and upstream as the value in local.
224225
* If the field is present in both upstream and local, recursively merge the values between local, upstream and origin.

0 commit comments

Comments
 (0)