-
Notifications
You must be signed in to change notification settings - Fork 256
Custom logic for 3-way-merging null values #4542
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
Changes from all commits
Commits
Show all changes
5 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
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
32 changes: 32 additions & 0 deletions
32
pkg/lib/update/merge3/testdata/non-assoc-crd/destination.yaml
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,32 @@ | ||
| apiVersion: infer.kpt.dev/v1alpha1 | ||
| kind: MergeTestResources | ||
| metadata: | ||
| name: "test-resources" | ||
| spec: | ||
| packageName: "test-package" | ||
| workspaceName: "v2" | ||
| revision: 2 | ||
| repositoryName: "test-repo" | ||
| resources: | ||
| Kptfile: | | ||
| apiVersion: kpt.dev/v1 | ||
| kind: Kptfile | ||
| metadata: | ||
| name: test-package | ||
| info: | ||
| description: test package | ||
| fruitstore.yaml: | | ||
| apiVersion: test.group/v1 | ||
| kind: FruitStore | ||
| metadata: | ||
| name: test-fruit-store | ||
| spec: | ||
| airConditioned: false | ||
| preferredTemperature: 20 | ||
| fruits: | ||
| - name: grape | ||
| amount: 5 | ||
| - name: apple | ||
| amount: 25 | ||
| - name: banana | ||
| amount: 3 |
30 changes: 30 additions & 0 deletions
30
pkg/lib/update/merge3/testdata/non-assoc-crd/original.yaml
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,30 @@ | ||
| apiVersion: infer.kpt.dev/v1alpha1 | ||
| kind: MergeTestResources | ||
| metadata: | ||
| name: "test-resources" | ||
| spec: | ||
| packageName: "test-package" | ||
| workspaceName: "v1" | ||
| revision: 1 | ||
| repositoryName: "test-repo" | ||
| resources: | ||
| Kptfile: | | ||
| apiVersion: kpt.dev/v1 | ||
| kind: Kptfile | ||
| metadata: | ||
| name: test-package | ||
| info: | ||
| description: test package | ||
| fruitstore.yaml: | | ||
| apiVersion: test.group/v1 | ||
| kind: FruitStore | ||
| metadata: | ||
| name: test-fruit-store | ||
| spec: | ||
| airConditioned: false | ||
| preferredTemperature: 15 | ||
| fruits: | ||
| - name: apple | ||
| amount: 10 | ||
| - name: grape | ||
| amount: 5 |
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,32 @@ | ||
| apiVersion: infer.kpt.dev/v1alpha1 | ||
| kind: MergeTestResources | ||
| metadata: | ||
| name: "test-resources" | ||
| spec: | ||
| packageName: "test-package" | ||
| workspaceName: "v3" | ||
| revision: 3 | ||
| repositoryName: "test-repo" | ||
| resources: | ||
| Kptfile: | | ||
| apiVersion: kpt.dev/v1 | ||
| kind: Kptfile | ||
| metadata: | ||
| name: test-package | ||
| info: | ||
| description: test package | ||
| fruitstore.yaml: | | ||
| apiVersion: test.group/v1 | ||
| kind: FruitStore | ||
| metadata: | ||
| name: test-fruit-store | ||
| spec: | ||
| airConditioned: false | ||
| preferredTemperature: 10 | ||
| fruits: | ||
| - name: pear | ||
| amount: 30 | ||
| - name: apple | ||
| amount: 20 | ||
| - name: grape | ||
| amount: 5 |
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,145 @@ | ||
| // Copyright 2019 The Kubernetes Authors. | ||
| // Copyright 2026 The kpt Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // copied from sigs.k8s.io/kustomize/kyaml@v0.21.1/yaml/merge3/visitor.go with modifications | ||
|
|
||
| package merge3 | ||
|
|
||
| import ( | ||
| "sigs.k8s.io/kustomize/kyaml/openapi" | ||
| "sigs.k8s.io/kustomize/kyaml/yaml" | ||
| "sigs.k8s.io/kustomize/kyaml/yaml/walk" | ||
| ) | ||
|
|
||
| type Visitor struct{} | ||
|
|
||
| func (m *Visitor) VisitMap(nodes walk.Sources, _ *openapi.ResourceSchema) (*yaml.RNode, error) { | ||
| if m.isCleared(nodes.Origin(), nodes.Updated()) || m.isCleared(nodes.Origin(), nodes.Dest()) { // MODIFIED | ||
| // explicitly cleared from either dest or update | ||
| return walk.ClearNode, nil | ||
| } | ||
| if nodes.Dest() == nil && nodes.Updated() == nil { | ||
| // implicitly cleared missing from both dest and update | ||
| return walk.ClearNode, nil | ||
| } | ||
|
|
||
| if nodes.Dest() == nil { | ||
| // not cleared, but missing from the dest | ||
| // initialize a new value that can be recursively merged | ||
| return yaml.NewRNode(&yaml.Node{Kind: yaml.MappingNode}), nil | ||
| } | ||
|
|
||
| // recursively merge the dest with the original and updated | ||
| return nodes.Dest(), nil | ||
| } | ||
|
|
||
| func (m *Visitor) visitAList(nodes walk.Sources, _ *openapi.ResourceSchema) (*yaml.RNode, error) { | ||
| if yaml.IsMissingOrNull(nodes.Updated()) && !yaml.IsMissingOrNull(nodes.Origin()) { | ||
| // implicitly cleared from update -- element was deleted | ||
| return walk.ClearNode, nil | ||
| } | ||
| if yaml.IsMissingOrNull(nodes.Dest()) { | ||
| // not cleared, but missing from the dest | ||
| // initialize a new value that can be recursively merged | ||
| return yaml.NewRNode(&yaml.Node{Kind: yaml.SequenceNode}), nil | ||
| } | ||
|
|
||
| // recursively merge the dest with the original and updated | ||
| return nodes.Dest(), nil | ||
| } | ||
|
|
||
| func (m *Visitor) VisitScalar(nodes walk.Sources, _ *openapi.ResourceSchema) (*yaml.RNode, error) { | ||
| if m.isCleared(nodes.Origin(), nodes.Updated()) || m.isCleared(nodes.Origin(), nodes.Dest()) { // MODIFIED | ||
| // explicitly cleared from either dest or update | ||
| return nil, nil | ||
|
tgzsolt marked this conversation as resolved.
|
||
| } | ||
| if yaml.IsMissingOrNull(nodes.Updated()) != yaml.IsMissingOrNull(nodes.Origin()) { | ||
| // value added or removed in update | ||
| return nodes.Updated(), nil | ||
| } | ||
| if yaml.IsMissingOrNull(nodes.Updated()) && yaml.IsMissingOrNull(nodes.Origin()) { | ||
| // value absent in both origin and update | ||
| return nodes.Dest(), nil | ||
| } | ||
|
tgzsolt marked this conversation as resolved.
tgzsolt marked this conversation as resolved.
|
||
|
|
||
| if nodes.Updated().YNode().Value != nodes.Origin().YNode().Value { | ||
| // value changed in update | ||
| return nodes.Updated(), nil | ||
| } | ||
|
|
||
| // unchanged between origin and update, keep the dest | ||
| return nodes.Dest(), nil | ||
| } | ||
|
|
||
| func (m *Visitor) visitNAList(nodes walk.Sources) (*yaml.RNode, error) { | ||
| if m.isCleared(nodes.Origin(), nodes.Updated()) || m.isCleared(nodes.Origin(), nodes.Dest()) { // MODIFIED | ||
| // explicitly cleared from either dest or update | ||
| return walk.ClearNode, nil | ||
| } | ||
|
|
||
| if yaml.IsMissingOrNull(nodes.Updated()) != yaml.IsMissingOrNull(nodes.Origin()) { | ||
| // value added or removed in update | ||
| return nodes.Updated(), nil | ||
| } | ||
| if yaml.IsMissingOrNull(nodes.Updated()) && yaml.IsMissingOrNull(nodes.Origin()) { | ||
| // value not present in source or dest | ||
| return nodes.Dest(), nil | ||
| } | ||
|
|
||
| if !m.isNodeContentEqual(nodes.Origin().YNode(), nodes.Updated().YNode()) { | ||
| // value changed in update | ||
| return nodes.Updated(), nil | ||
| } | ||
|
|
||
| // unchanged between origin and update, keep the dest | ||
| return nodes.Dest(), nil | ||
| } | ||
|
|
||
| // NEW | ||
| // isCleared returns if the node has not tagged null in `left` but explicitly removed in `right` | ||
| func (*Visitor) isCleared(left, right *yaml.RNode) bool { | ||
| return !left.IsTaggedNull() && right.IsTaggedNull() | ||
| } | ||
|
tgzsolt marked this conversation as resolved.
tgzsolt marked this conversation as resolved.
|
||
|
|
||
| func (m *Visitor) VisitList(nodes walk.Sources, s *openapi.ResourceSchema, kind walk.ListKind) (*yaml.RNode, error) { | ||
| if kind == walk.AssociativeList { | ||
| return m.visitAList(nodes, s) | ||
| } | ||
| // non-associative list | ||
| return m.visitNAList(nodes) | ||
| } | ||
|
|
||
| // SIMPLIFIED | ||
| // isNodeContentEqual compares the nodes structurally (kind, tag, value and children), | ||
| // avoiding YAML serialization. Presentation style and comments are ignored. | ||
| func (m *Visitor) isNodeContentEqual(a, b *yaml.Node) bool { | ||
| if a == nil || b == nil { | ||
| return a == b | ||
| } | ||
| if a.Kind != b.Kind || a.Tag != b.Tag || a.Value != b.Value { | ||
| return false | ||
| } | ||
| if len(a.Content) != len(b.Content) { | ||
| return false | ||
| } | ||
| for i := range a.Content { | ||
| if !m.isNodeContentEqual(a.Content[i], b.Content[i]) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| var _ walk.Visitor = &Visitor{} | ||
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,21 @@ | ||
| apiVersion: kpt.dev/v1 | ||
| kind: Kptfile | ||
| metadata: | ||
| name: test | ||
| annotations: | ||
| config.kubernetes.io/local-config: "true" | ||
| upstream: | ||
| type: git | ||
| git: | ||
| repo: https://github.com/droot/pkg-catalog.git | ||
| directory: basens | ||
| ref: basens/v0 | ||
| upstreamLock: | ||
| type: git | ||
| git: | ||
| repo: https://github.com/droot/pkg-catalog.git | ||
| directory: basens | ||
| ref: basens/v0 | ||
| commit: b3e1d439516a5e8d49adc0c82d3e95578570dbfa | ||
| info: | ||
| description: Test package. |
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.
Uh oh!
There was an error while loading. Please reload this page.