mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-09-15 12:18:57 +00:00
fix nit
This commit is contained in:
@@ -4511,6 +4511,8 @@ func TestValueInlineStructuredData(t *testing.T) {
|
|||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: target-configmap
|
name: target-configmap
|
||||||
|
annotations:
|
||||||
|
hostname: www.example.com
|
||||||
data:
|
data:
|
||||||
config.json: |-
|
config.json: |-
|
||||||
{
|
{
|
||||||
@@ -4524,7 +4526,7 @@ data:
|
|||||||
- source:
|
- source:
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
name: target-configmap
|
name: target-configmap
|
||||||
fieldPath: metadata.name
|
fieldPath: metadata.annotations.hostname
|
||||||
targets:
|
targets:
|
||||||
- select:
|
- select:
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
@@ -4535,11 +4537,13 @@ data:
|
|||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: target-configmap
|
name: target-configmap
|
||||||
|
annotations:
|
||||||
|
hostname: www.example.com
|
||||||
data:
|
data:
|
||||||
config.json: |-
|
config.json: |-
|
||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
"hostname": "target-configmap",
|
"hostname": "www.example.com",
|
||||||
"id": "42"
|
"id": "42"
|
||||||
}
|
}
|
||||||
}`,
|
}`,
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// PathMatcher returns all RNodes matching the path wrapped in a SequenceNode.
|
// PathMatcher returns all RNodes matching the path wrapped in a SequenceNode.
|
||||||
// Lists may have multiple elements matching the path, and each matching element
|
// Lists may have multiple elements matching the pafunc cleanPath(path []string) []string {g element
|
||||||
// is added to the return result.
|
// is added to the return result.
|
||||||
// If Path points to a SequenceNode, the SequenceNode is wrapped in another SequenceNode
|
// If Path points to a SequenceNode, the SequenceNode is wrapped in another SequenceNode
|
||||||
// If Path does not contain any lists, the result is still wrapped in a SequenceNode of len == 1
|
// If Path does not contain any lists, the result is still wrapped in a SequenceNode of len == 1
|
||||||
@@ -138,12 +138,6 @@ func (p *PathMatcher) doField(rn *RNode) (*RNode, error) {
|
|||||||
// lookup the field
|
// lookup the field
|
||||||
field, err := rn.Pipe(Get(p.Path[0]))
|
field, err := rn.Pipe(Get(p.Path[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// check error is an invalid kind error
|
|
||||||
invalidKindErr := &InvalidNodeKindError{}
|
|
||||||
if errors.As(err, &invalidKindErr) {
|
|
||||||
// if the field is valid json or yaml, continue to lookup the next part of the path
|
|
||||||
fmt.Print("-----------------------------\nOUTPUT: ", err)
|
|
||||||
}
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -347,25 +341,6 @@ func cleanPath(path []string) []string {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
// structuredDataNode is a wrapper around RNode that handles serialization back to scalar fields
|
|
||||||
// when the underlying structured data is modified
|
|
||||||
type structuredDataNode struct {
|
|
||||||
RNode
|
|
||||||
structuredRoot *RNode // The root of the structured data
|
|
||||||
scalarField *RNode // The original scalar field containing the structured data
|
|
||||||
}
|
|
||||||
|
|
||||||
// Override SetYNode to handle structured data serialization
|
|
||||||
func (s *structuredDataNode) SetYNode(node *yaml.Node) {
|
|
||||||
// Call the original SetYNode
|
|
||||||
s.RNode.SetYNode(node)
|
|
||||||
|
|
||||||
// Serialize the modified structured data back to the scalar field
|
|
||||||
if modifiedData, err := s.structuredRoot.String(); err == nil {
|
|
||||||
s.scalarField.YNode().Value = strings.TrimSpace(modifiedData)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleStructuredDataInScalar processes a scalar field that contains structured data (JSON/YAML)
|
// handleStructuredDataInScalar processes a scalar field that contains structured data (JSON/YAML)
|
||||||
// and allows path navigation within that structured data
|
// and allows path navigation within that structured data
|
||||||
func (p *PathMatcher) handleStructuredDataInScalar(scalarField *RNode) (*RNode, error) {
|
func (p *PathMatcher) handleStructuredDataInScalar(scalarField *RNode) (*RNode, error) {
|
||||||
@@ -386,29 +361,5 @@ func (p *PathMatcher) handleStructuredDataInScalar(scalarField *RNode) (*RNode,
|
|||||||
}
|
}
|
||||||
p.Matches = pm.Matches
|
p.Matches = pm.Matches
|
||||||
|
|
||||||
// For structured data, we need to create a special result node that knows how to
|
|
||||||
// serialize back to the original scalar when modified
|
|
||||||
if result != nil && len(result.YNode().Content) > 0 {
|
|
||||||
// Wrap the result with structured data context
|
|
||||||
for _, node := range result.YNode().Content {
|
|
||||||
wrappedNode := NewRNode(node)
|
|
||||||
// Add a custom method to handle setting values back to structured data
|
|
||||||
wrappedNode = p.wrapWithStructuredDataHandler(wrappedNode, structuredField, scalarField)
|
|
||||||
result.YNode().Content[0] = wrappedNode.YNode()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// wrapWithStructuredDataHandler creates a wrapper that will serialize structured data back
|
|
||||||
// to the scalar field when the node is modified
|
|
||||||
func (p *PathMatcher) wrapWithStructuredDataHandler(targetNode, structuredRoot, scalarField *RNode) *RNode {
|
|
||||||
// Create a custom node that overrides SetYNode to handle structured data serialization
|
|
||||||
wrapper := &structuredDataNode{
|
|
||||||
RNode: *targetNode,
|
|
||||||
structuredRoot: structuredRoot,
|
|
||||||
scalarField: scalarField,
|
|
||||||
}
|
|
||||||
return &wrapper.RNode
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user