File: builtinplugins.go

package info (click to toggle)
golang-k8s-sigs-kustomize-api 0.20.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,768 kB
  • sloc: makefile: 206; sh: 67
file content (152 lines) | stat: -rw-r--r-- 5,231 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2022 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package localizer

import (
	"sigs.k8s.io/kustomize/api/filters/fieldspec"
	"sigs.k8s.io/kustomize/api/filters/filtersutil"
	"sigs.k8s.io/kustomize/api/filters/fsslice"
	"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
	"sigs.k8s.io/kustomize/api/konfig"
	"sigs.k8s.io/kustomize/api/types"
	"sigs.k8s.io/kustomize/kyaml/errors"
	"sigs.k8s.io/kustomize/kyaml/kio"
	"sigs.k8s.io/kustomize/kyaml/resid"
	"sigs.k8s.io/kustomize/kyaml/yaml"
)

// localizeBuiltinPlugins localizes built-in plugins with file paths.
// Note that this excludes helm, which needs a repo.
type localizeBuiltinPlugins struct {
	lc *localizer

	// locPathFn is used by localizeNode to set the localized path on the plugin.
	locPathFn func(string) (string, error)
}

var _ kio.Filter = &localizeBuiltinPlugins{}

// Filter localizes the built-in plugins with file paths.
func (lbp *localizeBuiltinPlugins) Filter(plugins []*yaml.RNode) ([]*yaml.RNode, error) {
	for _, singlePlugin := range plugins {
		err := singlePlugin.PipeE(fsslice.Filter{
			FsSlice: types.FsSlice{
				types.FieldSpec{
					Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.ConfigMapGenerator.String()},
					Path: "env",
				},
				types.FieldSpec{
					Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.ConfigMapGenerator.String()},
					Path: "envs",
				},
				types.FieldSpec{
					Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.SecretGenerator.String()},
					Path: "env",
				},
				types.FieldSpec{
					Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.SecretGenerator.String()},
					Path: "envs",
				},
				types.FieldSpec{
					Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.HelmChartInflationGenerator.String()},
					Path: "valuesFile",
				},
				types.FieldSpec{
					Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.HelmChartInflationGenerator.String()},
					Path: "additionalValuesFiles",
				},
				types.FieldSpec{
					Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchTransformer.String()},
					Path: "path",
				},
				types.FieldSpec{
					Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchJson6902Transformer.String()},
					Path: "path",
				},
				types.FieldSpec{
					Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.ReplacementTransformer.String()},
					Path: "replacements/path",
				},
			},
			SetValue: func(node *yaml.RNode) error {
				lbp.locPathFn = lbp.lc.localizeFile
				return lbp.localizeAll(node)
			},
		},
			fsslice.Filter{
				FsSlice: types.FsSlice{
					types.FieldSpec{
						Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.ConfigMapGenerator.String()},
						Path: "files",
					},
					types.FieldSpec{
						Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.SecretGenerator.String()},
						Path: "files",
					},
				},
				SetValue: func(node *yaml.RNode) error {
					lbp.locPathFn = lbp.lc.localizeFileSource
					return lbp.localizeAll(node)
				},
			},
			yaml.FilterFunc(func(node *yaml.RNode) (*yaml.RNode, error) {
				isHelm := node.GetApiVersion() == konfig.BuiltinPluginApiVersion &&
					node.GetKind() == builtinhelpers.HelmChartInflationGenerator.String()
				if !isHelm {
					return node, nil
				}
				home, err := node.Pipe(yaml.Lookup("chartHome"))
				if err != nil {
					return nil, errors.Wrap(err)
				}
				if home == nil {
					_, err = lbp.lc.copyChartHomeEntry("")
				} else {
					lbp.locPathFn = lbp.lc.copyChartHomeEntry
					err = lbp.localizeScalar(home)
				}
				return node, errors.WrapPrefixf(err, "plugin %s", resid.FromRNode(node))
			}),
			fieldspec.Filter{
				FieldSpec: types.FieldSpec{
					Gvk:  resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchStrategicMergeTransformer.String()},
					Path: "paths",
				},
				SetValue: func(node *yaml.RNode) error {
					lbp.locPathFn = lbp.lc.localizeK8sResource
					return lbp.localizeAll(node)
				},
			})
		if err != nil {
			return nil, errors.Wrap(err)
		}
	}
	return plugins, nil
}

// localizeAll sets each entry in node to its value localized by locPathFn.
// Node is a sequence or scalar value.
func (lbp *localizeBuiltinPlugins) localizeAll(node *yaml.RNode) error {
	// We rely on the build command to throw errors for nodes in
	// built-in plugins that are sequences when expected to be scalar,
	// and vice versa.
	//nolint: exhaustive
	switch node.YNode().Kind {
	case yaml.SequenceNode:
		return errors.Wrap(node.VisitElements(lbp.localizeScalar))
	case yaml.ScalarNode:
		return lbp.localizeScalar(node)
	default:
		return errors.Errorf("expected sequence or scalar node")
	}
}

// localizeScalar sets the scalar node to its value localized by locPathFn.
func (lbp *localizeBuiltinPlugins) localizeScalar(node *yaml.RNode) error {
	localizedPath, err := lbp.locPathFn(node.YNode().Value)
	if err != nil {
		return err
	}
	return filtersutil.SetScalar(localizedPath)(node)
}