...

Source file src/sigs.k8s.io/kustomize/kyaml/inpututil/inpututil.go

Documentation: sigs.k8s.io/kustomize/kyaml/inpututil

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package inpututil
     5  
     6  import (
     7  	"sigs.k8s.io/kustomize/kyaml/errors"
     8  	"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
     9  	"sigs.k8s.io/kustomize/kyaml/yaml"
    10  )
    11  
    12  type MapInputsEFn func(*yaml.RNode, yaml.ResourceMeta) error
    13  
    14  // MapInputsE runs the function against each input Resource, providing the parsed metadata
    15  func MapInputsE(inputs []*yaml.RNode, fn MapInputsEFn) error {
    16  	for i := range inputs {
    17  		meta, err := inputs[i].GetMeta()
    18  		if err != nil {
    19  			return errors.Wrap(err)
    20  		}
    21  		if err := fn(inputs[i], meta); err != nil {
    22  			return WrapErrorWithFile(err, meta)
    23  		}
    24  	}
    25  	return nil
    26  }
    27  
    28  type MapInputsFn func(*yaml.RNode, yaml.ResourceMeta) ([]*yaml.RNode, error)
    29  
    30  // MapInputs runs the function against each input Resource, providing the parsed metadata
    31  // and returning the aggregated result
    32  func MapInputs(inputs []*yaml.RNode, fn MapInputsFn) ([]*yaml.RNode, error) {
    33  	var outputs []*yaml.RNode
    34  	for i := range inputs {
    35  		meta, err := inputs[i].GetMeta()
    36  		if err != nil {
    37  			return nil, errors.Wrap(err)
    38  		}
    39  		o, err := fn(inputs[i], meta)
    40  		if err != nil {
    41  			return nil, WrapErrorWithFile(err, meta)
    42  		}
    43  		outputs = append(outputs, o...)
    44  	}
    45  	return outputs, nil
    46  }
    47  
    48  // WrapErrorWithFile returns the original error wrapped with information about the file
    49  // that the Resource was parsed from.
    50  func WrapErrorWithFile(err error, meta yaml.ResourceMeta) error {
    51  	if err == nil {
    52  		return err
    53  	}
    54  	path := meta.Annotations[kioutil.PathAnnotation]
    55  	index := meta.Annotations[kioutil.IndexAnnotation]
    56  	if path == "" {
    57  		path = meta.Annotations[kioutil.LegacyPathAnnotation]
    58  	}
    59  	if index == "" {
    60  		index = meta.Annotations[kioutil.LegacyPathAnnotation]
    61  	}
    62  	return errors.WrapPrefixf(err, "%s [%s]",
    63  		meta.Annotations[path],
    64  		meta.Annotations[index])
    65  }
    66  

View as plain text