...

Source file src/sigs.k8s.io/kustomize/kyaml/fn/framework/patch_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/fn/framework

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package framework_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/spf13/cobra"
    10  	"sigs.k8s.io/kustomize/kyaml/fn/framework/parser"
    11  
    12  	"sigs.k8s.io/kustomize/kyaml/fn/framework"
    13  	"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
    14  	"sigs.k8s.io/kustomize/kyaml/fn/framework/frameworktestutil"
    15  	"sigs.k8s.io/kustomize/kyaml/kio"
    16  	"sigs.k8s.io/kustomize/kyaml/yaml"
    17  )
    18  
    19  func TestResourcePatchTemplate_ComplexSelectors(t *testing.T) {
    20  	cmdFn := func() *cobra.Command {
    21  		type api struct {
    22  			Selector framework.Selector `json:"selector" yaml:"selector"`
    23  			A        string             `json:"a" yaml:"a"`
    24  			B        string             `json:"b" yaml:"b"`
    25  			Special  string             `json:"special" yaml:"special"`
    26  			LongList bool
    27  		}
    28  		var config api
    29  		filter := framework.Selector{
    30  			// this is a special manual filter for the Selector for when the built-in matchers
    31  			// are insufficient
    32  			ResourceMatcher: func(rn *yaml.RNode) bool {
    33  				m, _ := rn.GetMeta()
    34  				return config.Special != "" && m.Annotations["foo"] == config.Special
    35  			},
    36  		}
    37  		pt1 := framework.ResourcePatchTemplate{
    38  			// Apply these rendered patches
    39  			Templates: parser.TemplateStrings(`
    40  spec:
    41    template:
    42      spec:
    43        containers:
    44        - name: foo
    45          image: example/sidecar:{{ .B }}
    46  ---
    47  metadata:
    48    annotations:
    49      patched: '{{ .A }}'
    50  {{- if .LongList }}
    51      long: 'true'
    52  {{- end }}
    53  `),
    54  			// Use the selector from the input
    55  			Selector: &config.Selector,
    56  		}
    57  
    58  		pt2 := framework.ResourcePatchTemplate{
    59  			// Apply these rendered patches
    60  			Templates: parser.TemplateStrings(`
    61  metadata:
    62    annotations:
    63      filterPatched: '{{ .A }}'
    64  `),
    65  			// Use an explicit selector
    66  			Selector: &filter,
    67  		}
    68  
    69  		fn := framework.TemplateProcessor{
    70  			TemplateData: &config,
    71  			PreProcessFilters: []kio.Filter{kio.FilterFunc(func(items []*yaml.RNode) ([]*yaml.RNode, error) {
    72  				// do some extra processing based on the inputs
    73  				config.LongList = len(items) > 2
    74  				return items, nil
    75  			})},
    76  			PatchTemplates: []framework.PatchTemplate{&pt1, &pt2},
    77  		}
    78  
    79  		return command.Build(fn, command.StandaloneEnabled, false)
    80  	}
    81  
    82  	tc := frameworktestutil.CommandResultsChecker{Command: cmdFn,
    83  		TestDataDirectory: "testdata/patch-selector"}
    84  	tc.Assert(t)
    85  }
    86  

View as plain text