...

Source file src/sigs.k8s.io/kustomize/api/internal/builtins/ValueAddTransformer.go

Documentation: sigs.k8s.io/kustomize/api/internal/builtins

     1  // Code generated by pluginator on ValueAddTransformer; DO NOT EDIT.
     2  // pluginator {(devel)  unknown   }
     3  
     4  package builtins
     5  
     6  import (
     7  	"fmt"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"sigs.k8s.io/kustomize/api/filters/namespace"
    12  	"sigs.k8s.io/kustomize/api/filters/valueadd"
    13  	"sigs.k8s.io/kustomize/api/resmap"
    14  	"sigs.k8s.io/kustomize/api/resource"
    15  	"sigs.k8s.io/kustomize/api/types"
    16  	"sigs.k8s.io/yaml"
    17  )
    18  
    19  // An 'Add' transformer inspired by the IETF RFC 6902 JSON spec Add operation.
    20  type ValueAddTransformerPlugin struct {
    21  	// Value is the value to add.
    22  	// Defaults to base name of encompassing kustomization root.
    23  	Value string `json:"value,omitempty" yaml:"value,omitempty"`
    24  
    25  	// Targets is a slice of targets that should have the value added.
    26  	Targets []Target `json:"targets,omitempty" yaml:"targets,omitempty"`
    27  
    28  	// TargetFilePath is a file path.  If specified, the file will be parsed into
    29  	// a slice of Target, and appended to anything that was specified in the
    30  	// Targets field.  This is just a means to share common target specifications.
    31  	TargetFilePath string `json:"targetFilePath,omitempty" yaml:"targetFilePath,omitempty"`
    32  }
    33  
    34  // Target describes where to put the value.
    35  type Target struct {
    36  	// Selector selects the resources to modify.
    37  	Selector *types.Selector `json:"selector,omitempty" yaml:"selector,omitempty"`
    38  
    39  	// NotSelector selects the resources to exclude
    40  	// from those included by overly broad selectors.
    41  	// TODO: implement this?
    42  	// NotSelector *types.Selector `json:"notSelector,omitempty" yaml:"notSelector,omitempty"`
    43  
    44  	// FieldPath is a JSON-style path to the field intended to hold the value.
    45  	FieldPath string `json:"fieldPath,omitempty" yaml:"fieldPath,omitempty"`
    46  
    47  	// FilePathPosition is passed to the filter directly.  Look there for doc.
    48  	FilePathPosition int `json:"filePathPosition,omitempty" yaml:"filePathPosition,omitempty"`
    49  }
    50  
    51  func (p *ValueAddTransformerPlugin) Config(h *resmap.PluginHelpers, c []byte) error {
    52  	err := yaml.Unmarshal(c, p)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	p.Value = strings.TrimSpace(p.Value)
    57  	if p.Value == "" {
    58  		p.Value = filepath.Base(h.Loader().Root())
    59  	}
    60  	if p.TargetFilePath != "" {
    61  		bytes, err := h.Loader().Load(p.TargetFilePath)
    62  		if err != nil {
    63  			return err
    64  		}
    65  		var targets struct {
    66  			Targets []Target `json:"targets,omitempty" yaml:"targets,omitempty"`
    67  		}
    68  		err = yaml.Unmarshal(bytes, &targets)
    69  		if err != nil {
    70  			return err
    71  		}
    72  		p.Targets = append(p.Targets, targets.Targets...)
    73  	}
    74  	if len(p.Targets) == 0 {
    75  		return fmt.Errorf("must specify at least one target")
    76  	}
    77  	for _, target := range p.Targets {
    78  		if err = validateSelector(target.Selector); err != nil {
    79  			return err
    80  		}
    81  		// TODO: call validateSelector(target.NotSelector) if field added.
    82  		if err = validateJsonFieldPath(target.FieldPath); err != nil {
    83  			return err
    84  		}
    85  		if target.FilePathPosition < 0 {
    86  			return fmt.Errorf(
    87  				"value of FilePathPosition (%d) cannot be negative",
    88  				target.FilePathPosition)
    89  		}
    90  	}
    91  	return nil
    92  }
    93  
    94  // TODO: implement
    95  func validateSelector(_ *types.Selector) error {
    96  	return nil
    97  }
    98  
    99  // TODO: Enforce RFC 6902?
   100  func validateJsonFieldPath(p string) error {
   101  	if len(p) == 0 {
   102  		return fmt.Errorf("fieldPath cannot be empty")
   103  	}
   104  	return nil
   105  }
   106  
   107  func (p *ValueAddTransformerPlugin) Transform(m resmap.ResMap) (err error) {
   108  	for _, t := range p.Targets {
   109  		var resources []*resource.Resource
   110  		if t.Selector == nil {
   111  			resources = m.Resources()
   112  		} else {
   113  			resources, err = m.Select(*t.Selector)
   114  			if err != nil {
   115  				return err
   116  			}
   117  		}
   118  		// TODO: consider t.NotSelector if implemented
   119  		for _, res := range resources {
   120  			if t.FieldPath == types.MetadataNamespacePath {
   121  				err = res.ApplyFilter(namespace.Filter{
   122  					Namespace: p.Value,
   123  				})
   124  			} else {
   125  				err = res.ApplyFilter(valueadd.Filter{
   126  					Value:            p.Value,
   127  					FieldPath:        t.FieldPath,
   128  					FilePathPosition: t.FilePathPosition,
   129  				})
   130  			}
   131  			if err != nil {
   132  				return err
   133  			}
   134  		}
   135  	}
   136  	return nil
   137  }
   138  
   139  func NewValueAddTransformerPlugin() resmap.TransformerPlugin {
   140  	return &ValueAddTransformerPlugin{}
   141  }
   142  

View as plain text