...

Source file src/sigs.k8s.io/kustomize/api/types/replacement.go

Documentation: sigs.k8s.io/kustomize/api/types

     1  // Copyright 2021 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package types
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"sigs.k8s.io/kustomize/kyaml/resid"
    11  )
    12  
    13  const DefaultReplacementFieldPath = "metadata.name"
    14  
    15  // Replacement defines how to perform a substitution
    16  // where it is from and where it is to.
    17  type Replacement struct {
    18  	// The source of the value.
    19  	Source *SourceSelector `json:"source,omitempty" yaml:"source,omitempty"`
    20  
    21  	// The N fields to write the value to.
    22  	Targets []*TargetSelector `json:"targets,omitempty" yaml:"targets,omitempty"`
    23  }
    24  
    25  // SourceSelector is the source of the replacement transformer.
    26  type SourceSelector struct {
    27  	// A specific object to read it from.
    28  	resid.ResId `json:",inline,omitempty" yaml:",inline,omitempty"`
    29  
    30  	// Structured field path expected in the allowed object.
    31  	FieldPath string `json:"fieldPath,omitempty" yaml:"fieldPath,omitempty"`
    32  
    33  	// Used to refine the interpretation of the field.
    34  	Options *FieldOptions `json:"options,omitempty" yaml:"options,omitempty"`
    35  }
    36  
    37  func (s *SourceSelector) String() string {
    38  	if s == nil {
    39  		return ""
    40  	}
    41  	result := []string{s.ResId.String()}
    42  	if s.FieldPath != "" {
    43  		result = append(result, s.FieldPath)
    44  	}
    45  	if opts := s.Options.String(); opts != "" {
    46  		result = append(result, opts)
    47  	}
    48  	return strings.Join(result, ":")
    49  }
    50  
    51  // TargetSelector specifies fields in one or more objects.
    52  type TargetSelector struct {
    53  	// Include objects that match this.
    54  	Select *Selector `json:"select" yaml:"select"`
    55  
    56  	// From the allowed set, remove objects that match this.
    57  	Reject []*Selector `json:"reject,omitempty" yaml:"reject,omitempty"`
    58  
    59  	// Structured field paths expected in each allowed object.
    60  	FieldPaths []string `json:"fieldPaths,omitempty" yaml:"fieldPaths,omitempty"`
    61  
    62  	// Used to refine the interpretation of the field.
    63  	Options *FieldOptions `json:"options,omitempty" yaml:"options,omitempty"`
    64  }
    65  
    66  // FieldOptions refine the interpretation of FieldPaths.
    67  type FieldOptions struct {
    68  	// Used to split/join the field.
    69  	Delimiter string `json:"delimiter,omitempty" yaml:"delimiter,omitempty"`
    70  
    71  	// Which position in the split to consider.
    72  	Index int `json:"index,omitempty" yaml:"index,omitempty"`
    73  
    74  	// TODO (#3492): Implement use of this option
    75  	// None, Base64, URL, Hex, etc
    76  	Encoding string `json:"encoding,omitempty" yaml:"encoding,omitempty"`
    77  
    78  	// If field missing, add it.
    79  	Create bool `json:"create,omitempty" yaml:"create,omitempty"`
    80  }
    81  
    82  func (fo *FieldOptions) String() string {
    83  	if fo == nil || (fo.Delimiter == "" && !fo.Create) {
    84  		return ""
    85  	}
    86  	return fmt.Sprintf("%s(%d), create=%t", fo.Delimiter, fo.Index, fo.Create)
    87  }
    88  

View as plain text