...

Source file src/sigs.k8s.io/kustomize/api/filters/annotations/annotations.go

Documentation: sigs.k8s.io/kustomize/api/filters/annotations

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package annotations
     5  
     6  import (
     7  	"sigs.k8s.io/kustomize/api/filters/filtersutil"
     8  	"sigs.k8s.io/kustomize/api/filters/fsslice"
     9  	"sigs.k8s.io/kustomize/api/types"
    10  	"sigs.k8s.io/kustomize/kyaml/kio"
    11  	"sigs.k8s.io/kustomize/kyaml/yaml"
    12  )
    13  
    14  type annoMap map[string]string
    15  
    16  type Filter struct {
    17  	// Annotations is the set of annotations to apply to the inputs
    18  	Annotations annoMap `yaml:"annotations,omitempty"`
    19  
    20  	// FsSlice contains the FieldSpecs to locate the namespace field
    21  	FsSlice types.FsSlice
    22  
    23  	trackableSetter filtersutil.TrackableSetter
    24  }
    25  
    26  var _ kio.Filter = Filter{}
    27  var _ kio.TrackableFilter = &Filter{}
    28  
    29  // WithMutationTracker registers a callback which will be invoked each time a field is mutated
    30  func (f *Filter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) {
    31  	f.trackableSetter.WithMutationTracker(callback)
    32  }
    33  
    34  func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
    35  	keys := yaml.SortedMapKeys(f.Annotations)
    36  	_, err := kio.FilterAll(yaml.FilterFunc(
    37  		func(node *yaml.RNode) (*yaml.RNode, error) {
    38  			for _, k := range keys {
    39  				if err := node.PipeE(fsslice.Filter{
    40  					FsSlice: f.FsSlice,
    41  					SetValue: f.trackableSetter.SetEntry(
    42  						k, f.Annotations[k], yaml.NodeTagString),
    43  					CreateKind: yaml.MappingNode, // Annotations are MappingNodes.
    44  					CreateTag:  yaml.NodeTagMap,
    45  				}); err != nil {
    46  					return nil, err
    47  				}
    48  			}
    49  			return node, nil
    50  		})).Filter(nodes)
    51  	return nodes, err
    52  }
    53  

View as plain text