mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-29 09:40:49 +00:00
Compare commits
94 Commits
issue3377
...
release-ku
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f1304194d | ||
|
|
4157933c8d | ||
|
|
a79253e02f | ||
|
|
3b35b121b3 | ||
|
|
3c94d20599 | ||
|
|
1faeb91cc4 | ||
|
|
9f1ef993a1 | ||
|
|
55d8cb3d3a | ||
|
|
a684592639 | ||
|
|
386d10834b | ||
|
|
af32126e80 | ||
|
|
b0cfa15b9c | ||
|
|
62e7df6812 | ||
|
|
c077ed4b58 | ||
|
|
156beb300c | ||
|
|
25b02d2d6c | ||
|
|
781098843e | ||
|
|
af3ffa7059 | ||
|
|
9d7b8952a0 | ||
|
|
cb400c895e | ||
|
|
9a8dcf6a8e | ||
|
|
384b71b5f5 | ||
|
|
4345cd2ade | ||
|
|
1460d13d50 | ||
|
|
97a2b15be6 | ||
|
|
f927cf0b8e | ||
|
|
cbb121e651 | ||
|
|
447b315a61 | ||
|
|
b3cb61b80f | ||
|
|
b9f05dd357 | ||
|
|
1ee16d9f52 | ||
|
|
43157f5d35 | ||
|
|
cd918483f9 | ||
|
|
f71854a0c8 | ||
|
|
6246262965 | ||
|
|
d3ea87220b | ||
|
|
61daea0202 | ||
|
|
a54cd12b39 | ||
|
|
11ce6363b4 | ||
|
|
4de26ccf9d | ||
|
|
7801830152 | ||
|
|
aae2be1a79 | ||
|
|
79e15c05d5 | ||
|
|
507244e6f8 | ||
|
|
3892e3c910 | ||
|
|
dcb26d0901 | ||
|
|
f5f1a15226 | ||
|
|
64644643d4 | ||
|
|
4d1eebbb82 | ||
|
|
b3a9314e27 | ||
|
|
86168cebbc | ||
|
|
57a53797d3 | ||
|
|
f5beffe394 | ||
|
|
4287e28ff4 | ||
|
|
a5cdd98414 | ||
|
|
16a49c50c4 | ||
|
|
a6f29f2bf7 | ||
|
|
22fcf3b3fa | ||
|
|
a31b846fa5 | ||
|
|
22fb23071b | ||
|
|
382cf5c2e0 | ||
|
|
8d4508a041 | ||
|
|
4d5657f037 | ||
|
|
5958edda14 | ||
|
|
48676fe34b | ||
|
|
a8278b6da9 | ||
|
|
31d6e24fa4 | ||
|
|
25e11e9020 | ||
|
|
d8e2a76ef3 | ||
|
|
cff7bd4eb2 | ||
|
|
58db58202c | ||
|
|
fc29d7e108 | ||
|
|
ae060cc225 | ||
|
|
9b4fdcf35a | ||
|
|
bb2d63ab58 | ||
|
|
b5012385c8 | ||
|
|
659a7de8f9 | ||
|
|
866dbf2017 | ||
|
|
ef89df6123 | ||
|
|
97f23966af | ||
|
|
5059033b13 | ||
|
|
d91e8af702 | ||
|
|
1bef8c4cdd | ||
|
|
9b87f78511 | ||
|
|
eda827c317 | ||
|
|
be57e1f6c2 | ||
|
|
021c3ce3fc | ||
|
|
4f184e8ce3 | ||
|
|
2e0d6d42bf | ||
|
|
6cf6eb9f76 | ||
|
|
ffed8f1430 | ||
|
|
7ac37867dc | ||
|
|
d8f2d2256d | ||
|
|
463b776486 |
2
Makefile
2
Makefile
@@ -4,7 +4,7 @@
|
|||||||
# Makefile for kustomize CLI and API.
|
# Makefile for kustomize CLI and API.
|
||||||
|
|
||||||
MYGOBIN := $(shell go env GOPATH)/bin
|
MYGOBIN := $(shell go env GOPATH)/bin
|
||||||
SHELL := /bin/bash
|
SHELL := /usr/bin/env bash
|
||||||
export PATH := $(MYGOBIN):$(PATH)
|
export PATH := $(MYGOBIN):$(PATH)
|
||||||
MODULES := '"cmd/config" "api/" "kustomize/" "kyaml/"'
|
MODULES := '"cmd/config" "api/" "kustomize/" "kyaml/"'
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ func (p *HashTransformerPlugin) Transform(m resmap.ResMap) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
res.SetOriginalName(res.GetName(), false)
|
res.StorePreviousId()
|
||||||
res.SetName(fmt.Sprintf("%s-%s", res.GetName(), h))
|
res.SetName(fmt.Sprintf("%s-%s", res.GetName(), h))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf
|
|||||||
return fmt.Errorf("chartName cannot be empty")
|
return fmt.Errorf("chartName cannot be empty")
|
||||||
}
|
}
|
||||||
if p.ChartHome == "" {
|
if p.ChartHome == "" {
|
||||||
p.ChartHome = path.Join(p.tmpDir, "chart")
|
p.ChartHome = filepath.Join(p.tmpDir, "chart")
|
||||||
}
|
}
|
||||||
if p.ChartRepoName == "" {
|
if p.ChartRepoName == "" {
|
||||||
p.ChartRepoName = "stable"
|
p.ChartRepoName = "stable"
|
||||||
@@ -60,10 +60,10 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf
|
|||||||
p.HelmBin = "helm"
|
p.HelmBin = "helm"
|
||||||
}
|
}
|
||||||
if p.HelmHome == "" {
|
if p.HelmHome == "" {
|
||||||
p.HelmHome = path.Join(p.tmpDir, ".helm")
|
p.HelmHome = filepath.Join(p.tmpDir, ".helm")
|
||||||
}
|
}
|
||||||
if p.Values == "" {
|
if p.Values == "" {
|
||||||
p.Values = path.Join(p.ChartHome, p.ChartName, "values.yaml")
|
p.Values = filepath.Join(p.ChartHome, p.ChartName, "values.yaml")
|
||||||
}
|
}
|
||||||
if p.ValuesMerge == "" {
|
if p.ValuesMerge == "" {
|
||||||
p.ValuesMerge = "override"
|
p.ValuesMerge = "override"
|
||||||
@@ -109,17 +109,16 @@ func (p *HelmChartInflationGeneratorPlugin) EncodeValues(w io.Writer) error {
|
|||||||
|
|
||||||
// useValuesLocal process (merge) inflator config provided values with chart default values.yaml
|
// useValuesLocal process (merge) inflator config provided values with chart default values.yaml
|
||||||
func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error {
|
func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error {
|
||||||
fn := path.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml")
|
// not override, merge, none
|
||||||
vf, err := os.Create(fn)
|
if !(p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false") {
|
||||||
defer vf.Close()
|
var pValues []byte
|
||||||
if err != nil {
|
var err error
|
||||||
return err
|
|
||||||
}
|
if filepath.IsAbs(p.Values) {
|
||||||
// override, merge, none
|
pValues, err = ioutil.ReadFile(p.Values)
|
||||||
if p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false" {
|
} else {
|
||||||
p.Values = fn
|
pValues, err = p.h.Loader().Load(p.Values)
|
||||||
} else {
|
}
|
||||||
pValues, err := ioutil.ReadFile(p.Values)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -141,16 +140,48 @@ func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.ValuesLocal = chValues
|
p.ValuesLocal = chValues
|
||||||
p.Values = fn
|
|
||||||
}
|
}
|
||||||
err = p.EncodeValues(vf)
|
b, err := yaml.Marshal(p.ValuesLocal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
vf.Sync()
|
path, err := p.writeValuesBytes(b)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p.Values = path
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copyValues will copy the relative values file into the temp directory
|
||||||
|
// to avoid messing up with CWD.
|
||||||
|
func (p *HelmChartInflationGeneratorPlugin) copyValues() error {
|
||||||
|
// only copy when the values path is not absolute
|
||||||
|
if filepath.IsAbs(p.Values) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// we must use use loader to read values file
|
||||||
|
b, err := p.h.Loader().Load(p.Values)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
path, err := p.writeValuesBytes(b)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p.Values = path
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *HelmChartInflationGeneratorPlugin) writeValuesBytes(b []byte) (string, error) {
|
||||||
|
path := filepath.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml")
|
||||||
|
err := ioutil.WriteFile(path, b, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return path, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Generate implements generator
|
// Generate implements generator
|
||||||
func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
||||||
// cleanup
|
// cleanup
|
||||||
@@ -174,6 +205,11 @@ func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
err := p.copyValues()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// render the charts
|
// render the charts
|
||||||
@@ -190,7 +226,7 @@ func (p *HelmChartInflationGeneratorPlugin) getTemplateCommandArgs() []string {
|
|||||||
if p.ReleaseName != "" {
|
if p.ReleaseName != "" {
|
||||||
args = append(args, p.ReleaseName)
|
args = append(args, p.ReleaseName)
|
||||||
}
|
}
|
||||||
args = append(args, path.Join(p.ChartHome, p.ChartName))
|
args = append(args, filepath.Join(p.ChartHome, p.ChartName))
|
||||||
if p.ReleaseNamespace != "" {
|
if p.ReleaseNamespace != "" {
|
||||||
args = append(args, "--namespace", p.ReleaseNamespace)
|
args = append(args, "--namespace", p.ReleaseNamespace)
|
||||||
}
|
}
|
||||||
@@ -220,7 +256,7 @@ func (p *HelmChartInflationGeneratorPlugin) getPullCommandArgs() []string {
|
|||||||
// checkLocalChart will return true if the chart does exist in
|
// checkLocalChart will return true if the chart does exist in
|
||||||
// local chart home.
|
// local chart home.
|
||||||
func (p *HelmChartInflationGeneratorPlugin) checkLocalChart() bool {
|
func (p *HelmChartInflationGeneratorPlugin) checkLocalChart() bool {
|
||||||
path := path.Join(p.ChartHome, p.ChartName)
|
path := filepath.Join(p.ChartHome, p.ChartName)
|
||||||
s, err := os.Stat(path)
|
s, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func (p *NamespaceTransformerPlugin) Transform(m resmap.ResMap) error {
|
|||||||
// Don't mutate empty objects?
|
// Don't mutate empty objects?
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
r.SetOriginalNs(r.GetNamespace(), false)
|
r.StorePreviousId()
|
||||||
err := r.ApplyFilter(namespace.Filter{
|
err := r.ApplyFilter(namespace.Filter{
|
||||||
Namespace: p.Namespace,
|
Namespace: p.Namespace,
|
||||||
FsSlice: p.FieldSpecs,
|
FsSlice: p.FieldSpecs,
|
||||||
|
|||||||
@@ -60,11 +60,10 @@ func (p *PatchStrategicMergeTransformerPlugin) Config(
|
|||||||
"patch appears to be empty; files=%v, Patch=%s", p.Paths, p.Patches)
|
"patch appears to be empty; files=%v, Patch=%s", p.Paths, p.Patches)
|
||||||
}
|
}
|
||||||
// Merge the patches, looking for conflicts.
|
// Merge the patches, looking for conflicts.
|
||||||
m, err := h.ResmapFactory().ConflatePatches(p.loadedPatches)
|
_, err = h.ResmapFactory().ConflatePatches(p.loadedPatches)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.loadedPatches = m.Resources()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ func (p *PatchTransformerPlugin) transformJson6902(m resmap.ResMap, patch jsonpa
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, res := range resources {
|
for _, res := range resources {
|
||||||
res.SetOriginalName(res.GetName(), false)
|
res.StorePreviousId()
|
||||||
err = res.ApplyFilter(patchjson6902.Filter{
|
err = res.ApplyFilter(patchjson6902.Filter{
|
||||||
Patch: p.Patch,
|
Patch: p.Patch,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func (p *PrefixSuffixTransformerPlugin) Transform(m resmap.ResMap) error {
|
|||||||
r.AddNamePrefix(p.Prefix)
|
r.AddNamePrefix(p.Prefix)
|
||||||
r.AddNameSuffix(p.Suffix)
|
r.AddNameSuffix(p.Suffix)
|
||||||
if p.Prefix != "" || p.Suffix != "" {
|
if p.Prefix != "" || p.Suffix != "" {
|
||||||
r.SetOriginalName(r.GetName(), false)
|
r.StorePreviousId()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err := r.ApplyFilter(prefixsuffix.Filter{
|
err := r.ApplyFilter(prefixsuffix.Filter{
|
||||||
|
|||||||
@@ -31,9 +31,7 @@ func (p *ReplicaCountTransformerPlugin) Transform(m resmap.ResMap) error {
|
|||||||
found := false
|
found := false
|
||||||
for _, fs := range p.FieldSpecs {
|
for _, fs := range p.FieldSpecs {
|
||||||
matcher := p.createMatcher(fs)
|
matcher := p.createMatcher(fs)
|
||||||
matchOriginal := m.GetMatchingResourcesByOriginalId(matcher)
|
resList := m.GetMatchingResourcesByAnyId(matcher)
|
||||||
resList := append(
|
|
||||||
matchOriginal, m.GetMatchingResourcesByCurrentId(matcher)...)
|
|
||||||
if len(resList) > 0 {
|
if len(resList) > 0 {
|
||||||
found = true
|
found = true
|
||||||
for _, r := range resList {
|
for _, r := range resList {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package filesys_test
|
package filesys_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -98,6 +99,7 @@ func TestNewTempConfirmDir(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(string(tmp))
|
||||||
|
|
||||||
delinked, err := filepath.EvalSymlinks(string(tmp))
|
delinked, err := filepath.EvalSymlinks(string(tmp))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -4,9 +4,11 @@
|
|||||||
package fieldspec
|
package fieldspec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||||
|
"sigs.k8s.io/kustomize/api/internal/utils"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
@@ -14,7 +16,16 @@ import (
|
|||||||
|
|
||||||
var _ yaml.Filter = Filter{}
|
var _ yaml.Filter = Filter{}
|
||||||
|
|
||||||
// Filter applies a single fieldSpec to a single object
|
// Filter possibly mutates its object argument using a FieldSpec.
|
||||||
|
// If the object matches the FieldSpec, and the node found
|
||||||
|
// by following the fieldSpec's path is non-null, this filter calls
|
||||||
|
// the setValue function on the node at the end of the path.
|
||||||
|
// If any part of the path doesn't exist, the filter returns
|
||||||
|
// without doing anything and without error, unless it was set
|
||||||
|
// to create the path. If set to create, it creates a tree of maps
|
||||||
|
// along the path, and the leaf node gets the setValue called on it.
|
||||||
|
// Error on GVK mismatch, empty or poorly formed path.
|
||||||
|
// Filter expect kustomize style paths, not JSON paths.
|
||||||
// Filter stores internal state and should not be reused
|
// Filter stores internal state and should not be reused
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
// FieldSpec contains the path to the value to set.
|
// FieldSpec contains the path to the value to set.
|
||||||
@@ -37,15 +48,17 @@ func (fltr Filter) Filter(obj *yaml.RNode) (*yaml.RNode, error) {
|
|||||||
if match, err := isMatchGVK(fltr.FieldSpec, obj); !match || err != nil {
|
if match, err := isMatchGVK(fltr.FieldSpec, obj); !match || err != nil {
|
||||||
return obj, errors.Wrap(err)
|
return obj, errors.Wrap(err)
|
||||||
}
|
}
|
||||||
fltr.path = splitPath(fltr.FieldSpec.Path)
|
fltr.path = utils.PathSplitter(fltr.FieldSpec.Path)
|
||||||
if err := fltr.filter(obj); err != nil {
|
err := fltr.filter(obj)
|
||||||
|
if err != nil {
|
||||||
s, _ := obj.String()
|
s, _ := obj.String()
|
||||||
return nil, errors.WrapPrefixf(err,
|
return nil, errors.WrapPrefixf(err,
|
||||||
"obj '%s' at path '%v'", s, fltr.FieldSpec.Path)
|
"considering field '%s' of object\n%v", fltr.FieldSpec.Path, s)
|
||||||
}
|
}
|
||||||
return obj, nil
|
return obj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Recursively called.
|
||||||
func (fltr Filter) filter(obj *yaml.RNode) error {
|
func (fltr Filter) filter(obj *yaml.RNode) error {
|
||||||
if len(fltr.path) == 0 {
|
if len(fltr.path) == 0 {
|
||||||
// found the field -- set its value
|
// found the field -- set its value
|
||||||
@@ -56,25 +69,30 @@ func (fltr Filter) filter(obj *yaml.RNode) error {
|
|||||||
}
|
}
|
||||||
switch obj.YNode().Kind {
|
switch obj.YNode().Kind {
|
||||||
case yaml.SequenceNode:
|
case yaml.SequenceNode:
|
||||||
return fltr.seq(obj)
|
return fltr.handleSequence(obj)
|
||||||
case yaml.MappingNode:
|
case yaml.MappingNode:
|
||||||
return fltr.field(obj)
|
return fltr.handleMap(obj)
|
||||||
|
case yaml.AliasNode:
|
||||||
|
return fltr.filter(yaml.NewRNode(obj.YNode().Alias))
|
||||||
default:
|
default:
|
||||||
return errors.Errorf("expected sequence or mapping node")
|
return errors.Errorf("expected sequence or mapping node")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// field calls filter on the field matching the next path element
|
// handleMap calls filter on the map field matching the next path element
|
||||||
func (fltr Filter) field(obj *yaml.RNode) error {
|
func (fltr Filter) handleMap(obj *yaml.RNode) error {
|
||||||
fieldName, isSeq := isSequenceField(fltr.path[0])
|
fieldName, isSeq := isSequenceField(fltr.path[0])
|
||||||
|
if fieldName == "" {
|
||||||
|
return fmt.Errorf("cannot set or create an empty field name")
|
||||||
|
}
|
||||||
// lookup the field matching the next path element
|
// lookup the field matching the next path element
|
||||||
var lookupField yaml.Filter
|
var operation yaml.Filter
|
||||||
var kind yaml.Kind
|
var kind yaml.Kind
|
||||||
tag := yaml.NodeTagEmpty
|
tag := yaml.NodeTagEmpty
|
||||||
switch {
|
switch {
|
||||||
case !fltr.FieldSpec.CreateIfNotPresent || fltr.CreateKind == 0 || isSeq:
|
case !fltr.FieldSpec.CreateIfNotPresent || fltr.CreateKind == 0 || isSeq:
|
||||||
// dont' create the field if we don't find it
|
// don't create the field if we don't find it
|
||||||
lookupField = yaml.Lookup(fieldName)
|
operation = yaml.Lookup(fieldName)
|
||||||
if isSeq {
|
if isSeq {
|
||||||
// The query path thinks this field should be a sequence;
|
// The query path thinks this field should be a sequence;
|
||||||
// accept this hint for use later if the tag is NodeTagNull.
|
// accept this hint for use later if the tag is NodeTagNull.
|
||||||
@@ -82,21 +100,25 @@ func (fltr Filter) field(obj *yaml.RNode) error {
|
|||||||
}
|
}
|
||||||
case len(fltr.path) <= 1:
|
case len(fltr.path) <= 1:
|
||||||
// create the field if it is missing: use the provided node kind
|
// create the field if it is missing: use the provided node kind
|
||||||
lookupField = yaml.LookupCreate(fltr.CreateKind, fieldName)
|
operation = yaml.LookupCreate(fltr.CreateKind, fieldName)
|
||||||
kind = fltr.CreateKind
|
kind = fltr.CreateKind
|
||||||
tag = fltr.CreateTag
|
tag = fltr.CreateTag
|
||||||
default:
|
default:
|
||||||
// create the field if it is missing: must be a mapping node
|
// create the field if it is missing: must be a mapping node
|
||||||
lookupField = yaml.LookupCreate(yaml.MappingNode, fieldName)
|
operation = yaml.LookupCreate(yaml.MappingNode, fieldName)
|
||||||
kind = yaml.MappingNode
|
kind = yaml.MappingNode
|
||||||
tag = yaml.NodeTagMap
|
tag = yaml.NodeTagMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// locate (or maybe create) the field
|
// locate (or maybe create) the field
|
||||||
field, err := obj.Pipe(lookupField)
|
field, err := obj.Pipe(operation)
|
||||||
if err != nil || field == nil {
|
if err != nil {
|
||||||
return errors.WrapPrefixf(err, "fieldName: %s", fieldName)
|
return errors.WrapPrefixf(err, "fieldName: %s", fieldName)
|
||||||
}
|
}
|
||||||
|
if field == nil {
|
||||||
|
// No error if field not found.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// if the value exists, but is null and kind is set,
|
// if the value exists, but is null and kind is set,
|
||||||
// then change it to the creation type
|
// then change it to the creation type
|
||||||
@@ -114,7 +136,7 @@ func (fltr Filter) field(obj *yaml.RNode) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// seq calls filter on all sequence elements
|
// seq calls filter on all sequence elements
|
||||||
func (fltr Filter) seq(obj *yaml.RNode) error {
|
func (fltr Filter) handleSequence(obj *yaml.RNode) error {
|
||||||
if err := obj.VisitElements(func(node *yaml.RNode) error {
|
if err := obj.VisitElements(func(node *yaml.RNode) error {
|
||||||
// recurse on each element -- re-allocating a Filter is
|
// recurse on each element -- re-allocating a Filter is
|
||||||
// not strictly required, but is more consistent with field
|
// not strictly required, but is more consistent with field
|
||||||
@@ -125,16 +147,14 @@ func (fltr Filter) seq(obj *yaml.RNode) error {
|
|||||||
return errors.WrapPrefixf(err,
|
return errors.WrapPrefixf(err,
|
||||||
"visit traversal on path: %v", fltr.path)
|
"visit traversal on path: %v", fltr.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isSequenceField returns true if the path element is for a sequence field.
|
// isSequenceField returns true if the path element is for a sequence field.
|
||||||
// isSequence also returns the path element with the '[]' suffix trimmed
|
// isSequence also returns the path element with the '[]' suffix trimmed
|
||||||
func isSequenceField(name string) (string, bool) {
|
func isSequenceField(name string) (string, bool) {
|
||||||
isSeq := strings.HasSuffix(name, "[]")
|
shorter := strings.TrimSuffix(name, "[]")
|
||||||
name = strings.TrimSuffix(name, "[]")
|
return shorter, shorter != name
|
||||||
return name, isSeq
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// isMatchGVK returns true if the fs.GVK matches the obj GVK.
|
// isMatchGVK returns true if the fs.GVK matches the obj GVK.
|
||||||
@@ -163,18 +183,3 @@ func isMatchGVK(fs types.FieldSpec, obj *yaml.RNode) (bool, error) {
|
|||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitPath(path string) []string {
|
|
||||||
ps := strings.Split(path, "/")
|
|
||||||
var res []string
|
|
||||||
res = append(res, ps[0])
|
|
||||||
for i := 1; i < len(ps); i++ {
|
|
||||||
lastIndex := len(res) - 1
|
|
||||||
if strings.HasSuffix(res[lastIndex], "\\") {
|
|
||||||
res[lastIndex] = strings.TrimSuffix(res[lastIndex], "\\") + "/" + ps[i]
|
|
||||||
} else {
|
|
||||||
res = append(res, ps[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -15,206 +15,243 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TestCase struct {
|
func TestFilter_Filter(t *testing.T) {
|
||||||
name string
|
testCases := map[string]struct {
|
||||||
input string
|
input string
|
||||||
expected string
|
expected string
|
||||||
filter fieldspec.Filter
|
filter fieldspec.Filter
|
||||||
fieldSpec string
|
fieldSpec string
|
||||||
error string
|
error string
|
||||||
}
|
}{
|
||||||
|
"path not found": {
|
||||||
var tests = []TestCase{
|
fieldSpec: `
|
||||||
{
|
|
||||||
name: "update",
|
|
||||||
fieldSpec: `
|
|
||||||
path: a/b
|
path: a/b
|
||||||
group: foo
|
group: foo
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
|
apiVersion: foo
|
||||||
|
kind: Bar
|
||||||
|
xxx:
|
||||||
|
`,
|
||||||
|
expected: `
|
||||||
|
apiVersion: foo
|
||||||
|
kind: Bar
|
||||||
|
xxx:
|
||||||
|
`,
|
||||||
|
filter: fieldspec.Filter{
|
||||||
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"empty path": {
|
||||||
|
fieldSpec: `
|
||||||
|
group: foo
|
||||||
|
kind: Bar
|
||||||
|
`,
|
||||||
|
input: `
|
||||||
|
apiVersion: foo
|
||||||
|
kind: Bar
|
||||||
|
xxx:
|
||||||
|
`,
|
||||||
|
expected: `
|
||||||
|
apiVersion: foo
|
||||||
|
kind: Bar
|
||||||
|
xxx:
|
||||||
|
`,
|
||||||
|
error: `considering field '' of object
|
||||||
|
apiVersion: foo
|
||||||
|
kind: Bar
|
||||||
|
xxx:
|
||||||
|
: cannot set or create an empty field name`,
|
||||||
|
filter: fieldspec.Filter{
|
||||||
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
"update": {
|
||||||
|
fieldSpec: `
|
||||||
|
path: a/b
|
||||||
|
group: foo
|
||||||
|
kind: Bar
|
||||||
|
`,
|
||||||
|
input: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b: c
|
b: c
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b: e
|
b: e
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
{
|
"update-kind-not-match": {
|
||||||
name: "update-kind-not-match",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b
|
path: a/b
|
||||||
group: foo
|
group: foo
|
||||||
kind: Bar1
|
kind: Bar1
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar2
|
kind: Bar2
|
||||||
a:
|
a:
|
||||||
b: c
|
b: c
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar2
|
kind: Bar2
|
||||||
a:
|
a:
|
||||||
b: c
|
b: c
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
{
|
"update-group-not-match": {
|
||||||
name: "update-group-not-match",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b
|
path: a/b
|
||||||
group: foo1
|
group: foo1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: foo2/v1beta1
|
apiVersion: foo2/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b: c
|
b: c
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: foo2/v1beta1
|
apiVersion: foo2/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b: c
|
b: c
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
{
|
"update-version-not-match": {
|
||||||
name: "update-version-not-match",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b
|
path: a/b
|
||||||
group: foo
|
group: foo
|
||||||
version: v1beta1
|
version: v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: foo/v1beta2
|
apiVersion: foo/v1beta2
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b: c
|
b: c
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: foo/v1beta2
|
apiVersion: foo/v1beta2
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b: c
|
b: c
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
{
|
"bad-version": {
|
||||||
name: "bad-version",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b
|
path: a/b
|
||||||
group: foo
|
group: foo
|
||||||
version: v1beta1
|
version: v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: foo/v1beta2/something
|
apiVersion: foo/v1beta2/something
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b: c
|
b: c
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: foo/v1beta2/something
|
apiVersion: foo/v1beta2/something
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b: c
|
b: c
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
{
|
"bad-meta": {
|
||||||
name: "bad-meta",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b
|
path: a/b
|
||||||
group: foo
|
group: foo
|
||||||
version: v1beta1
|
version: v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
a:
|
a:
|
||||||
b: c
|
b: c
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
|
},
|
||||||
|
error: "missing Resource metadata",
|
||||||
},
|
},
|
||||||
error: "missing Resource metadata",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
"miss-match-type": {
|
||||||
name: "miss-match-type",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b/c
|
path: a/b/c
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b: a
|
b: a
|
||||||
`,
|
`,
|
||||||
error: "obj 'kind: Bar\na:\n b: a\n' at path 'a/b/c': " +
|
error: `considering field 'a/b/c' of object
|
||||||
"expected sequence or mapping node",
|
kind: Bar
|
||||||
filter: fieldspec.Filter{
|
a:
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
b: a
|
||||||
|
: expected sequence or mapping node`,
|
||||||
|
filter: fieldspec.Filter{
|
||||||
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
{
|
"add": {
|
||||||
name: "add",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b/c/d
|
path: a/b/c/d
|
||||||
group: foo
|
group: foo
|
||||||
create: true
|
create: true
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a: {}
|
a: {}
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a: {b: {c: {d: e}}}
|
a: {b: {c: {d: e}}}
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
{
|
"update-in-sequence": {
|
||||||
name: "update-in-sequence",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b[]/c/d
|
path: a/b[]/c/d
|
||||||
group: foo
|
group: foo
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
@@ -222,7 +259,7 @@ a:
|
|||||||
- c:
|
- c:
|
||||||
d: a
|
d: a
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
@@ -230,244 +267,237 @@ a:
|
|||||||
- c:
|
- c:
|
||||||
d: e
|
d: e
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
// Don't create a sequence
|
// Don't create a sequence
|
||||||
{
|
"empty-sequence-no-create": {
|
||||||
name: "empty-sequence-no-create",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b[]/c/d
|
path: a/b[]/c/d
|
||||||
group: foo
|
group: foo
|
||||||
create: true
|
create: true
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a: {}
|
a: {}
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a: {}
|
a: {}
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
// Create a new field for an element in a sequence
|
// Create a new field for an element in a sequence
|
||||||
{
|
"empty-sequence-create": {
|
||||||
name: "empty-sequence-create",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b[]/c/d
|
path: a/b[]/c/d
|
||||||
group: foo
|
group: foo
|
||||||
create: true
|
create: true
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b:
|
b:
|
||||||
- c: {}
|
- c: {}
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: foo/v1beta1
|
apiVersion: foo/v1beta1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b:
|
b:
|
||||||
- c: {d: e}
|
- c: {d: e}
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
{
|
"group v1": {
|
||||||
name: "group v1",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b
|
path: a/b
|
||||||
group: v1
|
group: v1
|
||||||
create: true
|
create: true
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
{
|
"version v1": {
|
||||||
name: "version v1",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: a/b
|
path: a/b
|
||||||
version: v1
|
version: v1
|
||||||
create: true
|
create: true
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b: e
|
b: e
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("e"),
|
SetValue: filtersutil.SetScalar("e"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
{
|
"successfully set field on array entry no sequence hint": {
|
||||||
name: "successfully set field on array entry no sequence hint",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: spec/containers/image
|
path: spec/containers/image
|
||||||
version: v1
|
version: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- image: foo
|
- image: foo
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- image: bar
|
- image: bar
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("bar"),
|
SetValue: filtersutil.SetScalar("bar"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
{
|
"successfully set field on array entry with sequence hint": {
|
||||||
name: "successfully set field on array entry with sequence hint",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: spec/containers[]/image
|
path: spec/containers[]/image
|
||||||
version: v1
|
version: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- image: foo
|
- image: foo
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- image: bar
|
- image: bar
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("bar"),
|
SetValue: filtersutil.SetScalar("bar"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
"failure to set field on array entry with sequence hint in path": {
|
||||||
{
|
fieldSpec: `
|
||||||
name: "failure to set field on array entry with sequence hint in path",
|
|
||||||
fieldSpec: `
|
|
||||||
path: spec/containers[]/image
|
path: spec/containers[]/image
|
||||||
version: v1
|
version: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
spec:
|
spec:
|
||||||
containers: []
|
containers: []
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("bar"),
|
SetValue: filtersutil.SetScalar("bar"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
{
|
"failure to set field on array entry, no sequence hint in path": {
|
||||||
name: "failure to set field on array entry, no sequence hint in path",
|
fieldSpec: `
|
||||||
fieldSpec: `
|
|
||||||
path: spec/containers/image
|
path: spec/containers/image
|
||||||
version: v1
|
version: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("bar"),
|
SetValue: filtersutil.SetScalar("bar"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
"fieldname with slash '/'": {
|
||||||
{
|
fieldSpec: `
|
||||||
name: "filedname with slash '/'",
|
|
||||||
fieldSpec: `
|
|
||||||
path: a/b\/c/d
|
path: a/b\/c/d
|
||||||
version: v1
|
version: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b/c:
|
b/c:
|
||||||
d: foo
|
d: foo
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
b/c:
|
b/c:
|
||||||
d: bar
|
d: bar
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("bar"),
|
SetValue: filtersutil.SetScalar("bar"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
"fieldname with multiple '/'": {
|
||||||
{
|
fieldSpec: `
|
||||||
name: "filedname with multiple '/'",
|
|
||||||
fieldSpec: `
|
|
||||||
path: a/b\/c/d\/e/f
|
path: a/b\/c/d\/e/f
|
||||||
version: v1
|
version: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
`,
|
`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
@@ -475,7 +505,7 @@ a:
|
|||||||
d/e:
|
d/e:
|
||||||
f: foo
|
f: foo
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Bar
|
kind: Bar
|
||||||
a:
|
a:
|
||||||
@@ -483,25 +513,24 @@ a:
|
|||||||
d/e:
|
d/e:
|
||||||
f: bar
|
f: bar
|
||||||
`,
|
`,
|
||||||
filter: fieldspec.Filter{
|
filter: fieldspec.Filter{
|
||||||
SetValue: filtersutil.SetScalar("bar"),
|
SetValue: filtersutil.SetScalar("bar"),
|
||||||
CreateKind: yaml.ScalarNode,
|
CreateKind: yaml.ScalarNode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func TestFilter_Filter(t *testing.T) {
|
for n := range testCases {
|
||||||
for i := range tests {
|
tc := testCases[n]
|
||||||
test := tests[i]
|
t.Run(n, func(t *testing.T) {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
err := yaml.Unmarshal([]byte(tc.fieldSpec), &tc.filter.FieldSpec)
|
||||||
err := yaml.Unmarshal([]byte(test.fieldSpec), &test.filter.FieldSpec)
|
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
rw := &kio.ByteReadWriter{
|
rw := &kio.ByteReadWriter{
|
||||||
Reader: bytes.NewBufferString(test.input),
|
Reader: bytes.NewBufferString(tc.input),
|
||||||
Writer: out,
|
Writer: out,
|
||||||
OmitReaderAnnotations: true,
|
OmitReaderAnnotations: true,
|
||||||
}
|
}
|
||||||
@@ -509,11 +538,11 @@ func TestFilter_Filter(t *testing.T) {
|
|||||||
// run the filter
|
// run the filter
|
||||||
err = kio.Pipeline{
|
err = kio.Pipeline{
|
||||||
Inputs: []kio.Reader{rw},
|
Inputs: []kio.Reader{rw},
|
||||||
Filters: []kio.Filter{kio.FilterAll(test.filter)},
|
Filters: []kio.Filter{kio.FilterAll(tc.filter)},
|
||||||
Outputs: []kio.Writer{rw},
|
Outputs: []kio.Writer{rw},
|
||||||
}.Execute()
|
}.Execute()
|
||||||
if test.error != "" {
|
if tc.error != "" {
|
||||||
if !assert.EqualError(t, err, test.error) {
|
if !assert.EqualError(t, err, tc.error) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
// stop rest of test
|
// stop rest of test
|
||||||
@@ -526,7 +555,7 @@ func TestFilter_Filter(t *testing.T) {
|
|||||||
|
|
||||||
// check results
|
// check results
|
||||||
if !assert.Equal(t,
|
if !assert.Equal(t,
|
||||||
strings.TrimSpace(test.expected),
|
strings.TrimSpace(tc.expected),
|
||||||
strings.TrimSpace(out.String())) {
|
strings.TrimSpace(out.String())) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,30 +5,38 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
kyaml_filtersutil "sigs.k8s.io/kustomize/kyaml/filtersutil"
|
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Filter updates a name references.
|
// Filter updates a name references.
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
// Referrer is the object that refers to something else by a name,
|
// Referrer refers to another resource X by X's name.
|
||||||
// a name that this filter seeks to update.
|
// E.g. A Deployment can refer to a ConfigMap.
|
||||||
|
// The Deployment is the Referrer,
|
||||||
|
// the ConfigMap is the ReferralTarget.
|
||||||
|
// This filter seeks to repair the reference in Deployment, given
|
||||||
|
// that the ConfigMap's name may have changed.
|
||||||
Referrer *resource.Resource
|
Referrer *resource.Resource
|
||||||
|
|
||||||
// NameFieldToUpdate is the field in the Referrer that holds the
|
// NameFieldToUpdate is the field in the Referrer
|
||||||
// name requiring an update.
|
// that holds the name requiring an update.
|
||||||
NameFieldToUpdate types.FieldSpec `json:"nameFieldToUpdate,omitempty" yaml:"nameFieldToUpdate,omitempty"`
|
// This is the field to write.
|
||||||
|
NameFieldToUpdate types.FieldSpec
|
||||||
|
|
||||||
// Source of the new value for the name (in its name field).
|
// ReferralTarget is the source of the new value for
|
||||||
|
// the name, always in the 'metadata/name' field.
|
||||||
|
// This is the field to read.
|
||||||
ReferralTarget resid.Gvk
|
ReferralTarget resid.Gvk
|
||||||
|
|
||||||
// Set of resources to hunt through to find the ReferralTarget.
|
// Set of resources to scan to find the ReferralTarget.
|
||||||
ReferralCandidates resmap.ResMap
|
ReferralCandidates resmap.ResMap
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,18 +46,31 @@ func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
|||||||
return kio.FilterAll(yaml.FilterFunc(f.run)).Filter(nodes)
|
return kio.FilterAll(yaml.FilterFunc(f.run)).Filter(nodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The node passed in here is the same node as held in Referrer, and
|
// The node passed in here is the same node as held in Referrer;
|
||||||
// that's how the referrer's name field is updated.
|
// that's how the referrer's name field is updated.
|
||||||
// However, this filter still needs the extra methods on Referrer
|
// Currently, however, this filter still needs the extra methods on Referrer
|
||||||
// to consult things like the resource Id, its namespace, etc.
|
// to consult things like the resource Id, its namespace, etc.
|
||||||
|
// TODO(3455): No filter should use the Resource api; all information
|
||||||
|
// about names should come from annotations, with helper methods
|
||||||
|
// on the RNode object. Resource should get stupider, RNode smarter.
|
||||||
func (f Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
|
func (f Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
|
||||||
err := node.PipeE(fieldspec.Filter{
|
if err := f.confirmNodeMatchesReferrer(node); err != nil {
|
||||||
|
// sanity check.
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := node.PipeE(fieldspec.Filter{
|
||||||
FieldSpec: f.NameFieldToUpdate,
|
FieldSpec: f.NameFieldToUpdate,
|
||||||
SetValue: f.set,
|
SetValue: f.set,
|
||||||
})
|
}); err != nil {
|
||||||
return node, err
|
return nil, errors.Wrapf(
|
||||||
|
err, "updating name reference in '%s' field of '%s'",
|
||||||
|
f.NameFieldToUpdate.Path, f.Referrer.CurId().String())
|
||||||
|
}
|
||||||
|
return node, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function is called on the node found at FieldSpec.Path.
|
||||||
|
// It's some node in the Referrer.
|
||||||
func (f Filter) set(node *yaml.RNode) error {
|
func (f Filter) set(node *yaml.RNode) error {
|
||||||
if yaml.IsMissingOrNull(node) {
|
if yaml.IsMissingOrNull(node) {
|
||||||
return nil
|
return nil
|
||||||
@@ -65,97 +86,108 @@ func (f Filter) set(node *yaml.RNode) error {
|
|||||||
setMappingFn: f.setMapping,
|
setMappingFn: f.setMapping,
|
||||||
}, node)
|
}, node)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf("node must be a scalar, sequence or map")
|
||||||
"node is expected to be either a string or a slice of string or a map of string")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace name field within a map RNode and leverage the namespace field.
|
// This method used when NameFieldToUpdate doesn't lead to
|
||||||
|
// one scalar field (typically called 'name'), but rather
|
||||||
|
// leads to a map field (called anything). In this case we
|
||||||
|
// must complete the field path, looking for both a 'name'
|
||||||
|
// and a 'namespace' field to help select the proper
|
||||||
|
// ReferralTarget to read the name and namespace from.
|
||||||
func (f Filter) setMapping(node *yaml.RNode) error {
|
func (f Filter) setMapping(node *yaml.RNode) error {
|
||||||
if node.YNode().Kind != yaml.MappingNode {
|
if node.YNode().Kind != yaml.MappingNode {
|
||||||
return fmt.Errorf("expect a mapping node")
|
return fmt.Errorf("expect a mapping node")
|
||||||
}
|
}
|
||||||
nameNode, err := node.Pipe(yaml.FieldMatcher{Name: "name"})
|
nameNode, err := node.Pipe(yaml.FieldMatcher{Name: "name"})
|
||||||
if err != nil || nameNode == nil {
|
|
||||||
return fmt.Errorf("cannot find field 'name' in node")
|
|
||||||
}
|
|
||||||
namespaceNode, err := node.Pipe(yaml.FieldMatcher{Name: "namespace"})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error when find field 'namespace'")
|
return errors.Wrap(err, "trying to match 'name' field")
|
||||||
}
|
}
|
||||||
|
if nameNode == nil {
|
||||||
// name will not be updated if the namespace doesn't match
|
// This is a _configuration_ error; the field path
|
||||||
subset := f.ReferralCandidates.Resources()
|
// specified in NameFieldToUpdate.Path doesn't resolve
|
||||||
if namespaceNode != nil {
|
// to a map with a 'name' field, so we have no idea what
|
||||||
namespace := namespaceNode.YNode().Value
|
// field to update with a new name.
|
||||||
bynamespace := f.ReferralCandidates.GroupedByOriginalNamespace()
|
return fmt.Errorf("path config error; no 'name' field in node")
|
||||||
if _, ok := bynamespace[namespace]; !ok {
|
|
||||||
bynamespace = f.ReferralCandidates.GroupedByCurrentNamespace()
|
|
||||||
if _, ok := bynamespace[namespace]; !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
subset = bynamespace[namespace]
|
|
||||||
}
|
}
|
||||||
|
candidates, err := f.filterMapCandidatesByNamespace(node)
|
||||||
oldName := nameNode.YNode().Value
|
if err != nil {
|
||||||
res, err := f.selectReferral(oldName, subset)
|
|
||||||
if err != nil || res == nil {
|
|
||||||
// Nil res means nothing to do.
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f.recordTheReferral(res)
|
oldName := nameNode.YNode().Value
|
||||||
if res.GetName() == oldName && res.GetNamespace() == "" {
|
referral, err := f.selectReferral(oldName, candidates)
|
||||||
|
if err != nil || referral == nil {
|
||||||
|
// Nil referral means nothing to do.
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f.recordTheReferral(referral)
|
||||||
|
if referral.GetName() == oldName && referral.GetNamespace() == "" {
|
||||||
// The name has not changed, nothing to do.
|
// The name has not changed, nothing to do.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = node.PipeE(yaml.FieldSetter{
|
if err = node.PipeE(yaml.FieldSetter{
|
||||||
Name: "name",
|
Name: "name",
|
||||||
StringValue: res.GetName(),
|
StringValue: referral.GetName(),
|
||||||
})
|
}); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if res.GetNamespace() != "" {
|
if referral.GetNamespace() == "" {
|
||||||
// We don't want value "" to replace value "default" since
|
// Don't write an empty string into the namespace field, as
|
||||||
// the empty string is handled as a wild card here not default namespace
|
// it should not replace the value "default". The empty
|
||||||
// by kubernetes.
|
// string is handled as a wild card here, not as an implicit
|
||||||
err = node.PipeE(yaml.FieldSetter{
|
// specification of the "default" k8s namespace.
|
||||||
Name: "namespace",
|
return nil
|
||||||
StringValue: res.GetNamespace(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return err
|
return node.PipeE(yaml.FieldSetter{
|
||||||
|
Name: "namespace",
|
||||||
|
StringValue: referral.GetNamespace(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Filter) filterMapCandidatesByNamespace(
|
||||||
|
node *yaml.RNode) ([]*resource.Resource, error) {
|
||||||
|
namespaceNode, err := node.Pipe(yaml.FieldMatcher{Name: "namespace"})
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "trying to match 'namespace' field")
|
||||||
|
}
|
||||||
|
if namespaceNode == nil {
|
||||||
|
return f.ReferralCandidates.Resources(), nil
|
||||||
|
}
|
||||||
|
namespace := namespaceNode.YNode().Value
|
||||||
|
nsMap := f.ReferralCandidates.GroupedByOriginalNamespace()
|
||||||
|
if candidates, ok := nsMap[namespace]; ok {
|
||||||
|
return candidates, nil
|
||||||
|
}
|
||||||
|
nsMap = f.ReferralCandidates.GroupedByCurrentNamespace()
|
||||||
|
// This could be nil, or an empty list.
|
||||||
|
return nsMap[namespace], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Filter) setScalar(node *yaml.RNode) error {
|
func (f Filter) setScalar(node *yaml.RNode) error {
|
||||||
res, err := f.selectReferral(
|
referral, err := f.selectReferral(
|
||||||
node.YNode().Value, f.ReferralCandidates.Resources())
|
node.YNode().Value, f.ReferralCandidates.Resources())
|
||||||
if err != nil || res == nil {
|
if err != nil || referral == nil {
|
||||||
// Nil res means nothing to do.
|
// Nil referral means nothing to do.
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f.recordTheReferral(res)
|
f.recordTheReferral(referral)
|
||||||
if res.GetName() == node.YNode().Value {
|
if referral.GetName() == node.YNode().Value {
|
||||||
// The name has not changed, nothing to do.
|
// The name has not changed, nothing to do.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return node.PipeE(yaml.FieldSetter{StringValue: res.GetName()})
|
return node.PipeE(yaml.FieldSetter{StringValue: referral.GetName()})
|
||||||
}
|
}
|
||||||
|
|
||||||
// In the resource, make a note that it is referred to by the referrer.
|
// In the resource, make a note that it is referred to by the Referrer.
|
||||||
func (f Filter) recordTheReferral(res *resource.Resource) {
|
func (f Filter) recordTheReferral(referral *resource.Resource) {
|
||||||
res.AppendRefBy(f.Referrer.CurId())
|
referral.AppendRefBy(f.Referrer.CurId())
|
||||||
}
|
|
||||||
|
|
||||||
func (f Filter) isRoleRef() bool {
|
|
||||||
return strings.HasSuffix(f.NameFieldToUpdate.Path, "roleRef/name")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// getRoleRefGvk returns a Gvk in the roleRef field. Return error
|
// getRoleRefGvk returns a Gvk in the roleRef field. Return error
|
||||||
// if the roleRef, roleRef/apiGroup or roleRef/kind is missing.
|
// if the roleRef, roleRef/apiGroup or roleRef/kind is missing.
|
||||||
func getRoleRefGvk(res json.Marshaler) (*resid.Gvk, error) {
|
func getRoleRefGvk(res json.Marshaler) (*resid.Gvk, error) {
|
||||||
n, err := kyaml_filtersutil.GetRNode(res)
|
n, err := filtersutil.GetRNode(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -188,82 +220,183 @@ func getRoleRefGvk(res json.Marshaler) (*resid.Gvk, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Filter) filterReferralCandidates(
|
// sieveFunc returns true if the resource argument satisfies some criteria.
|
||||||
matches []*resource.Resource) []*resource.Resource {
|
type sieveFunc func(*resource.Resource) bool
|
||||||
var ret []*resource.Resource
|
|
||||||
for _, m := range matches {
|
// doSieve uses a function to accept or ignore resources from a list.
|
||||||
// If target kind is not ServiceAccount, we shouldn't consider condidates which
|
// If list is nil, returns immediately.
|
||||||
// doesn't have same namespace.
|
// It's a filter obviously, but that term is overloaded here.
|
||||||
if f.ReferralTarget.Kind != "ServiceAccount" &&
|
func doSieve(list []*resource.Resource, fn sieveFunc) (s []*resource.Resource) {
|
||||||
m.GetNamespace() != f.Referrer.GetNamespace() {
|
for _, r := range list {
|
||||||
continue
|
if fn(r) {
|
||||||
|
s = append(s, r)
|
||||||
}
|
}
|
||||||
if !f.Referrer.PrefixesSuffixesEquals(m) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ret = append(ret, m)
|
|
||||||
}
|
}
|
||||||
return ret
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// selectReferral picks the referral among a subset of candidates.
|
func acceptAll(r *resource.Resource) bool {
|
||||||
// The content of the candidateSubset slice is most of the time
|
return true
|
||||||
// identical to the ReferralCandidates resmap. Still in some cases, such
|
}
|
||||||
// as ClusterRoleBinding, the subset only contains the resources of a specific
|
|
||||||
// namespace.
|
func previousNameMatches(name string) sieveFunc {
|
||||||
|
return func(r *resource.Resource) bool {
|
||||||
|
for _, id := range r.PrevIds() {
|
||||||
|
if id.Name == name {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func previousIdSelectedByGvk(gvk *resid.Gvk) sieveFunc {
|
||||||
|
return func(r *resource.Resource) bool {
|
||||||
|
for _, id := range r.PrevIds() {
|
||||||
|
if id.IsSelected(gvk) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the we are updating a 'roleRef/name' field, the 'apiGroup' and 'kind'
|
||||||
|
// fields in the same 'roleRef' map must be considered.
|
||||||
|
// If either object is cluster-scoped (!IsNamespaceableKind), there
|
||||||
|
// can be a referral.
|
||||||
|
// E.g. a RoleBinding (which exists in a namespace) can refer
|
||||||
|
// to a ClusterRole (cluster-scoped) object.
|
||||||
|
// https://kubernetes.io/docs/reference/access-authn-authz/rbac/#role-and-clusterrole
|
||||||
|
// Likewise, a ClusterRole can refer to a Secret (in a namespace).
|
||||||
|
// Objects in different namespaces generally cannot refer to other
|
||||||
|
// with some exceptions (e.g. RoleBinding and ServiceAccount are both
|
||||||
|
// namespaceable, but the former can refer to accounts in other namespaces).
|
||||||
|
func (f Filter) roleRefFilter() sieveFunc {
|
||||||
|
if !strings.HasSuffix(f.NameFieldToUpdate.Path, "roleRef/name") {
|
||||||
|
return acceptAll
|
||||||
|
}
|
||||||
|
roleRefGvk, err := getRoleRefGvk(f.Referrer)
|
||||||
|
if err != nil {
|
||||||
|
return acceptAll
|
||||||
|
}
|
||||||
|
return previousIdSelectedByGvk(roleRefGvk)
|
||||||
|
}
|
||||||
|
|
||||||
|
func prefixSuffixEquals(other resource.ResCtx) sieveFunc {
|
||||||
|
return func(r *resource.Resource) bool {
|
||||||
|
return r.PrefixesSuffixesEquals(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Filter) sameCurrentNamespaceAsReferrer() sieveFunc {
|
||||||
|
referrerCurId := f.Referrer.CurId()
|
||||||
|
if !referrerCurId.IsNamespaceableKind() {
|
||||||
|
// If the referrer is cluster-scoped, let anything through.
|
||||||
|
return acceptAll
|
||||||
|
}
|
||||||
|
return func(r *resource.Resource) bool {
|
||||||
|
if !r.CurId().IsNamespaceableKind() {
|
||||||
|
// Allow cluster-scoped through.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if r.GetKind() == "ServiceAccount" {
|
||||||
|
// Allow service accounts through, even though they
|
||||||
|
// are in a namespace. A RoleBinding in another namespace
|
||||||
|
// can reference them.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return referrerCurId.IsNsEquals(r.CurId())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectReferral picks the best referral from a list of candidates.
|
||||||
func (f Filter) selectReferral(
|
func (f Filter) selectReferral(
|
||||||
|
// The name referral that may need to be updated.
|
||||||
oldName string,
|
oldName string,
|
||||||
candidateSubset []*resource.Resource) (*resource.Resource, error) {
|
candidates []*resource.Resource) (*resource.Resource, error) {
|
||||||
var roleRefGvk *resid.Gvk
|
candidates = doSieve(candidates, previousNameMatches(oldName))
|
||||||
if f.isRoleRef() {
|
candidates = doSieve(candidates, previousIdSelectedByGvk(&f.ReferralTarget))
|
||||||
var err error
|
candidates = doSieve(candidates, f.roleRefFilter())
|
||||||
roleRefGvk, err = getRoleRefGvk(f.Referrer)
|
candidates = doSieve(candidates, f.sameCurrentNamespaceAsReferrer())
|
||||||
if err != nil {
|
if len(candidates) == 1 {
|
||||||
return nil, err
|
return candidates[0], nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for _, res := range candidateSubset {
|
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer))
|
||||||
if res.GetOriginalName() != oldName {
|
if len(candidates) == 1 {
|
||||||
continue
|
return candidates[0], nil
|
||||||
}
|
|
||||||
id := res.OrgId()
|
|
||||||
if !id.IsSelected(&f.ReferralTarget) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// If the we are processing a roleRef, the apiGroup and Kind in the
|
|
||||||
// roleRef are needed to be considered.
|
|
||||||
if f.isRoleRef() && !id.IsSelected(roleRefGvk) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
matches := f.ReferralCandidates.GetMatchingResourcesByOriginalId(id.Equals)
|
|
||||||
// If there's more than one match,
|
|
||||||
// filter the matches by prefix and suffix
|
|
||||||
if len(matches) > 1 {
|
|
||||||
filteredMatches := f.filterReferralCandidates(matches)
|
|
||||||
if len(filteredMatches) > 1 {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"multiple matches for %s:\n %v",
|
|
||||||
id, getIds(filteredMatches))
|
|
||||||
}
|
|
||||||
// Check is the match the resource we are working on
|
|
||||||
if len(filteredMatches) == 0 || res != filteredMatches[0] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// In the resource, note that it is referenced
|
|
||||||
// by the referrer.
|
|
||||||
res.AppendRefBy(f.Referrer.CurId())
|
|
||||||
// Return transformed name of the object,
|
|
||||||
// complete with prefixes, hashes, etc.
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
return nil, nil
|
if len(candidates) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if allNamesAreTheSame(candidates) {
|
||||||
|
// Just take the first one.
|
||||||
|
return candidates[0], nil
|
||||||
|
}
|
||||||
|
ids := getIds(candidates)
|
||||||
|
f.failureDetails(candidates)
|
||||||
|
return nil, fmt.Errorf(" found multiple possible referrals: %s", ids)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIds(rs []*resource.Resource) []string {
|
func (f Filter) failureDetails(resources []*resource.Resource) {
|
||||||
|
fmt.Printf(
|
||||||
|
"\n**** Too many possible referral targets to referrer:\n%s\n",
|
||||||
|
f.Referrer.MustYaml())
|
||||||
|
for i, r := range resources {
|
||||||
|
fmt.Printf(
|
||||||
|
"--- possible referral %d:\n%s", i, r.MustYaml())
|
||||||
|
fmt.Println("------")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func allNamesAreTheSame(resources []*resource.Resource) bool {
|
||||||
|
name := resources[0].GetName()
|
||||||
|
for i := 1; i < len(resources); i++ {
|
||||||
|
if name != resources[i].GetName() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func getIds(rs []*resource.Resource) string {
|
||||||
var result []string
|
var result []string
|
||||||
for _, r := range rs {
|
for _, r := range rs {
|
||||||
result = append(result, r.CurId().String()+"\n")
|
result = append(result, r.CurId().String())
|
||||||
}
|
}
|
||||||
return result
|
return strings.Join(result, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkEqual(k, a, b string) error {
|
||||||
|
if a != b {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"node-referrerOriginal '%s' mismatch '%s' != '%s'",
|
||||||
|
k, a, b)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Filter) confirmNodeMatchesReferrer(node *yaml.RNode) error {
|
||||||
|
meta, err := node.GetMeta()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
gvk := f.Referrer.GetGvk()
|
||||||
|
if err = checkEqual(
|
||||||
|
"APIVersion", meta.APIVersion, gvk.ApiVersion()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = checkEqual(
|
||||||
|
"Kind", meta.Kind, gvk.Kind); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = checkEqual(
|
||||||
|
"Name", meta.Name, f.Referrer.GetName()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = checkEqual(
|
||||||
|
"Namespace", meta.Namespace, f.Referrer.GetNamespace()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,14 +14,14 @@ import (
|
|||||||
|
|
||||||
func TestNamerefFilter(t *testing.T) {
|
func TestNamerefFilter(t *testing.T) {
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
input string
|
referrerOriginal string
|
||||||
candidates string
|
candidates string
|
||||||
expected string
|
referrerFinal string
|
||||||
filter Filter
|
filter Filter
|
||||||
originalNames []string
|
originalNames []string
|
||||||
}{
|
}{
|
||||||
"simple scalar": {
|
"simple scalar": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -40,8 +40,8 @@ kind: NotSecret
|
|||||||
metadata:
|
metadata:
|
||||||
name: newName2
|
name: newName2
|
||||||
`,
|
`,
|
||||||
originalNames: []string{"oldName", ""},
|
originalNames: []string{"oldName", "newName2"},
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -59,7 +59,7 @@ ref:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"sequence": {
|
"sequence": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -79,8 +79,8 @@ kind: NotSecret
|
|||||||
metadata:
|
metadata:
|
||||||
name: newName2
|
name: newName2
|
||||||
`,
|
`,
|
||||||
originalNames: []string{"oldName1", ""},
|
originalNames: []string{"oldName1", "newName2"},
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -99,7 +99,7 @@ seq:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"mapping": {
|
"mapping": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -118,8 +118,8 @@ kind: NotSecret
|
|||||||
metadata:
|
metadata:
|
||||||
name: newName2
|
name: newName2
|
||||||
`,
|
`,
|
||||||
originalNames: []string{"oldName", ""},
|
originalNames: []string{"oldName", "newName2"},
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -137,36 +137,43 @@ map:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"mapping with namespace": {
|
"mapping with namespace": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: dep
|
name: dep
|
||||||
|
namespace: someNs
|
||||||
map:
|
map:
|
||||||
name: oldName
|
name: oldName
|
||||||
namespace: oldNs
|
namespace: someNs
|
||||||
`,
|
`,
|
||||||
candidates: `
|
candidates: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
name: newName
|
name: newName
|
||||||
namespace: oldNs
|
namespace: someNs
|
||||||
---
|
---
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: NotSecret
|
kind: NotSecret
|
||||||
metadata:
|
metadata:
|
||||||
name: newName2
|
name: newName2
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
name: thirdName
|
||||||
`,
|
`,
|
||||||
originalNames: []string{"oldName", ""},
|
originalNames: []string{"oldName", "oldName", "oldName"},
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: dep
|
name: dep
|
||||||
|
namespace: someNs
|
||||||
map:
|
map:
|
||||||
name: newName
|
name: newName
|
||||||
namespace: oldNs
|
namespace: someNs
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "map"},
|
NameFieldToUpdate: types.FieldSpec{Path: "map"},
|
||||||
@@ -178,7 +185,7 @@ map:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"null value": {
|
"null value": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -197,8 +204,8 @@ kind: NotSecret
|
|||||||
metadata:
|
metadata:
|
||||||
name: newName2
|
name: newName2
|
||||||
`,
|
`,
|
||||||
originalNames: []string{"oldName", ""},
|
originalNames: []string{"oldName", "newName2"},
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -220,7 +227,7 @@ map:
|
|||||||
for tn, tc := range testCases {
|
for tn, tc := range testCases {
|
||||||
t.Run(tn, func(t *testing.T) {
|
t.Run(tn, func(t *testing.T) {
|
||||||
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
||||||
referrer, err := factory.FromBytes([]byte(tc.input))
|
referrer, err := factory.FromBytes([]byte(tc.referrerOriginal))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -236,10 +243,10 @@ map:
|
|||||||
candidates := resMapFactory.FromResourceSlice(candidatesRes)
|
candidates := resMapFactory.FromResourceSlice(candidatesRes)
|
||||||
tc.filter.ReferralCandidates = candidates
|
tc.filter.ReferralCandidates = candidates
|
||||||
|
|
||||||
|
result := filtertest_test.RunFilter(t, tc.referrerOriginal, tc.filter)
|
||||||
if !assert.Equal(t,
|
if !assert.Equal(t,
|
||||||
strings.TrimSpace(tc.expected),
|
strings.TrimSpace(tc.referrerFinal),
|
||||||
strings.TrimSpace(
|
strings.TrimSpace(result)) {
|
||||||
filtertest_test.RunFilter(t, tc.input, tc.filter))) {
|
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -248,14 +255,14 @@ map:
|
|||||||
|
|
||||||
func TestNamerefFilterUnhappy(t *testing.T) {
|
func TestNamerefFilterUnhappy(t *testing.T) {
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
input string
|
referrerOriginal string
|
||||||
candidates string
|
candidates string
|
||||||
expected string
|
referrerFinal string
|
||||||
filter Filter
|
filter Filter
|
||||||
originalNames []string
|
originalNames []string
|
||||||
}{
|
}{
|
||||||
"multiple match": {
|
"multiple match": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -275,7 +282,7 @@ metadata:
|
|||||||
name: newName2
|
name: newName2
|
||||||
`,
|
`,
|
||||||
originalNames: []string{"oldName", "oldName"},
|
originalNames: []string{"oldName", "oldName"},
|
||||||
expected: "",
|
referrerFinal: "",
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
ReferralTarget: resid.Gvk{
|
||||||
@@ -286,7 +293,7 @@ metadata:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"no name": {
|
"no name": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -306,7 +313,7 @@ metadata:
|
|||||||
name: newName2
|
name: newName2
|
||||||
`,
|
`,
|
||||||
originalNames: []string{"oldName", "oldName"},
|
originalNames: []string{"oldName", "oldName"},
|
||||||
expected: "",
|
referrerFinal: "",
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref"},
|
NameFieldToUpdate: types.FieldSpec{Path: "ref"},
|
||||||
ReferralTarget: resid.Gvk{
|
ReferralTarget: resid.Gvk{
|
||||||
@@ -321,7 +328,7 @@ metadata:
|
|||||||
for tn, tc := range testCases {
|
for tn, tc := range testCases {
|
||||||
t.Run(tn, func(t *testing.T) {
|
t.Run(tn, func(t *testing.T) {
|
||||||
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
||||||
referrer, err := factory.FromBytes([]byte(tc.input))
|
referrer, err := factory.FromBytes([]byte(tc.referrerOriginal))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -337,11 +344,11 @@ metadata:
|
|||||||
candidates := resMapFactory.FromResourceSlice(candidatesRes)
|
candidates := resMapFactory.FromResourceSlice(candidatesRes)
|
||||||
tc.filter.ReferralCandidates = candidates
|
tc.filter.ReferralCandidates = candidates
|
||||||
|
|
||||||
_, err = filtertest_test.RunFilterE(t, tc.input, tc.filter)
|
_, err = filtertest_test.RunFilterE(t, tc.referrerOriginal, tc.filter)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expect an error")
|
t.Fatalf("expect an error")
|
||||||
}
|
}
|
||||||
if tc.expected != "" && !assert.EqualError(t, err, tc.expected) {
|
if tc.referrerFinal != "" && !assert.EqualError(t, err, tc.referrerFinal) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -350,19 +357,19 @@ metadata:
|
|||||||
|
|
||||||
func TestCandidatesWithDifferentPrefixSuffix(t *testing.T) {
|
func TestCandidatesWithDifferentPrefixSuffix(t *testing.T) {
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
input string
|
referrerOriginal string
|
||||||
candidates string
|
candidates string
|
||||||
expected string
|
referrerFinal string
|
||||||
filter Filter
|
filter Filter
|
||||||
originalNames []string
|
originalNames []string
|
||||||
prefix []string
|
prefix []string
|
||||||
suffix []string
|
suffix []string
|
||||||
inputPrefix string
|
inputPrefix string
|
||||||
inputSuffix string
|
inputSuffix string
|
||||||
err bool
|
err bool
|
||||||
}{
|
}{
|
||||||
"prefix match": {
|
"prefix match": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -386,7 +393,7 @@ metadata:
|
|||||||
suffix: []string{"", "suffix2"},
|
suffix: []string{"", "suffix2"},
|
||||||
inputPrefix: "prefix1",
|
inputPrefix: "prefix1",
|
||||||
inputSuffix: "",
|
inputSuffix: "",
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -405,7 +412,7 @@ ref:
|
|||||||
err: false,
|
err: false,
|
||||||
},
|
},
|
||||||
"suffix match": {
|
"suffix match": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -429,7 +436,7 @@ metadata:
|
|||||||
suffix: []string{"suffix1", "suffix2"},
|
suffix: []string{"suffix1", "suffix2"},
|
||||||
inputPrefix: "",
|
inputPrefix: "",
|
||||||
inputSuffix: "suffix1",
|
inputSuffix: "suffix1",
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -448,7 +455,7 @@ ref:
|
|||||||
err: false,
|
err: false,
|
||||||
},
|
},
|
||||||
"prefix suffix both match": {
|
"prefix suffix both match": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -472,7 +479,7 @@ metadata:
|
|||||||
suffix: []string{"suffix1", "suffix2"},
|
suffix: []string{"suffix1", "suffix2"},
|
||||||
inputPrefix: "prefix1",
|
inputPrefix: "prefix1",
|
||||||
inputSuffix: "suffix1",
|
inputSuffix: "suffix1",
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -491,7 +498,7 @@ ref:
|
|||||||
err: false,
|
err: false,
|
||||||
},
|
},
|
||||||
"multiple match: both": {
|
"multiple match: both": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -515,7 +522,7 @@ metadata:
|
|||||||
suffix: []string{"suffix", "suffix"},
|
suffix: []string{"suffix", "suffix"},
|
||||||
inputPrefix: "prefix",
|
inputPrefix: "prefix",
|
||||||
inputSuffix: "suffix",
|
inputSuffix: "suffix",
|
||||||
expected: "",
|
referrerFinal: "",
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
ReferralTarget: resid.Gvk{
|
||||||
@@ -527,7 +534,7 @@ metadata:
|
|||||||
err: true,
|
err: true,
|
||||||
},
|
},
|
||||||
"multiple match: only prefix": {
|
"multiple match: only prefix": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -551,7 +558,7 @@ metadata:
|
|||||||
suffix: []string{"", ""},
|
suffix: []string{"", ""},
|
||||||
inputPrefix: "prefix",
|
inputPrefix: "prefix",
|
||||||
inputSuffix: "",
|
inputSuffix: "",
|
||||||
expected: "",
|
referrerFinal: "",
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
ReferralTarget: resid.Gvk{
|
||||||
@@ -563,7 +570,7 @@ metadata:
|
|||||||
err: true,
|
err: true,
|
||||||
},
|
},
|
||||||
"multiple match: only suffix": {
|
"multiple match: only suffix": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -587,7 +594,7 @@ metadata:
|
|||||||
suffix: []string{"suffix", "suffix"},
|
suffix: []string{"suffix", "suffix"},
|
||||||
inputPrefix: "",
|
inputPrefix: "",
|
||||||
inputSuffix: "suffix",
|
inputSuffix: "suffix",
|
||||||
expected: "",
|
referrerFinal: "",
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
ReferralTarget: resid.Gvk{
|
||||||
@@ -599,7 +606,7 @@ metadata:
|
|||||||
err: true,
|
err: true,
|
||||||
},
|
},
|
||||||
"no match: neither match": {
|
"no match: neither match": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -623,7 +630,7 @@ metadata:
|
|||||||
suffix: []string{"suffix1", "suffix2"},
|
suffix: []string{"suffix1", "suffix2"},
|
||||||
inputPrefix: "prefix",
|
inputPrefix: "prefix",
|
||||||
inputSuffix: "suffix",
|
inputSuffix: "suffix",
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -642,7 +649,7 @@ ref:
|
|||||||
err: false,
|
err: false,
|
||||||
},
|
},
|
||||||
"no match: prefix doesn't match": {
|
"no match: prefix doesn't match": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -666,7 +673,7 @@ metadata:
|
|||||||
suffix: []string{"suffix", "suffix"},
|
suffix: []string{"suffix", "suffix"},
|
||||||
inputPrefix: "prefix",
|
inputPrefix: "prefix",
|
||||||
inputSuffix: "suffix",
|
inputSuffix: "suffix",
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -685,7 +692,7 @@ ref:
|
|||||||
err: false,
|
err: false,
|
||||||
},
|
},
|
||||||
"no match: suffix doesn't match": {
|
"no match: suffix doesn't match": {
|
||||||
input: `
|
referrerOriginal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -709,7 +716,7 @@ metadata:
|
|||||||
suffix: []string{"suffix1", "suffix2"},
|
suffix: []string{"suffix1", "suffix2"},
|
||||||
inputPrefix: "prefix",
|
inputPrefix: "prefix",
|
||||||
inputSuffix: "suffix",
|
inputSuffix: "suffix",
|
||||||
expected: `
|
referrerFinal: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -732,7 +739,7 @@ ref:
|
|||||||
for tn, tc := range testCases {
|
for tn, tc := range testCases {
|
||||||
t.Run(tn, func(t *testing.T) {
|
t.Run(tn, func(t *testing.T) {
|
||||||
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
||||||
referrer, err := factory.FromBytes([]byte(tc.input))
|
referrer, err := factory.FromBytes([]byte(tc.referrerOriginal))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -764,13 +771,15 @@ ref:
|
|||||||
|
|
||||||
if !tc.err {
|
if !tc.err {
|
||||||
if !assert.Equal(t,
|
if !assert.Equal(t,
|
||||||
strings.TrimSpace(tc.expected),
|
strings.TrimSpace(tc.referrerFinal),
|
||||||
strings.TrimSpace(
|
strings.TrimSpace(
|
||||||
filtertest_test.RunFilter(t, tc.input, tc.filter))) {
|
filtertest_test.RunFilter(
|
||||||
|
t, tc.referrerOriginal, tc.filter))) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_, err := filtertest_test.RunFilterE(t, tc.input, tc.filter)
|
_, err := filtertest_test.RunFilterE(
|
||||||
|
t, tc.referrerOriginal, tc.filter)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("an error is expected")
|
t.Fatalf("an error is expected")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -672,6 +672,63 @@ spec:
|
|||||||
ports:
|
ports:
|
||||||
- containerPort: 80
|
- containerPort: 80
|
||||||
- containerPort: 8080
|
- containerPort: 8080
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Test for issue #3513
|
||||||
|
// Currently broken; when one port has only containerPort, the output
|
||||||
|
// should not merge containerPort 8301 together
|
||||||
|
// This occurs because when protocol is missing on the first port,
|
||||||
|
// the merge code uses [containerPort] as the merge key rather than
|
||||||
|
// [containerPort, protocol]
|
||||||
|
"list map keys - protocol only present on some ports": {
|
||||||
|
input: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: test-deployment
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: consul
|
||||||
|
image: "hashicorp/consul:1.9.1"
|
||||||
|
ports:
|
||||||
|
- containerPort: 8500
|
||||||
|
name: http
|
||||||
|
- containerPort: 8301
|
||||||
|
protocol: "TCP"
|
||||||
|
- containerPort: 8301
|
||||||
|
protocol: "UDP"
|
||||||
|
`,
|
||||||
|
patch: yaml.MustParse(`
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: test-deployment
|
||||||
|
labels:
|
||||||
|
test: label
|
||||||
|
`),
|
||||||
|
expected: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: test-deployment
|
||||||
|
labels:
|
||||||
|
test: label
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: consul
|
||||||
|
image: "hashicorp/consul:1.9.1"
|
||||||
|
ports:
|
||||||
|
- containerPort: 8500
|
||||||
|
name: http
|
||||||
|
- containerPort: 8301
|
||||||
|
protocol: "UDP"
|
||||||
|
- containerPort: 8301
|
||||||
|
protocol: "UDP"
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -243,7 +243,8 @@ metadata:
|
|||||||
data:
|
data:
|
||||||
slice:
|
slice:
|
||||||
- false`,
|
- false`,
|
||||||
expectedError: `obj 'apiVersion: apps/v1
|
expectedError: `considering field 'data/slice' of object
|
||||||
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: dep
|
name: dep
|
||||||
@@ -252,7 +253,7 @@ metadata:
|
|||||||
data:
|
data:
|
||||||
slice:
|
slice:
|
||||||
- false
|
- false
|
||||||
' at path 'data/slice': invalid value type expect a string`,
|
: invalid value type expect a string`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: makeMf(map[string]interface{}{
|
||||||
"VAR": int64(5),
|
"VAR": int64(5),
|
||||||
@@ -268,7 +269,8 @@ metadata:
|
|||||||
name: dep
|
name: dep
|
||||||
data:
|
data:
|
||||||
1: str`,
|
1: str`,
|
||||||
expectedError: `obj 'apiVersion: apps/v1
|
expectedError: `considering field 'data' of object
|
||||||
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: dep
|
name: dep
|
||||||
@@ -276,7 +278,7 @@ metadata:
|
|||||||
config.kubernetes.io/index: '0'
|
config.kubernetes.io/index: '0'
|
||||||
data:
|
data:
|
||||||
1: str
|
1: str
|
||||||
' at path 'data': invalid map key: value='1', tag='` + yaml.NodeTagInt + `'`,
|
: invalid map key: value='1', tag='` + yaml.NodeTagInt + `'`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: makeMf(map[string]interface{}{
|
||||||
"VAR": int64(5),
|
"VAR": int64(5),
|
||||||
|
|||||||
@@ -21,8 +21,6 @@ require (
|
|||||||
k8s.io/apimachinery v0.17.0
|
k8s.io/apimachinery v0.17.0
|
||||||
k8s.io/client-go v0.17.0
|
k8s.io/client-go v0.17.0
|
||||||
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a
|
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a
|
||||||
sigs.k8s.io/kustomize/kyaml v0.10.6
|
sigs.k8s.io/kustomize/kyaml v0.10.7
|
||||||
sigs.k8s.io/yaml v1.2.0
|
sigs.k8s.io/yaml v1.2.0
|
||||||
)
|
)
|
||||||
|
|
||||||
replace sigs.k8s.io/kustomize/kyaml => ../kyaml
|
|
||||||
|
|||||||
@@ -599,6 +599,8 @@ mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b h1:DxJ5nJdkhDlLok9K6qO+5290kphD
|
|||||||
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4=
|
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4=
|
||||||
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f h1:Cq7MalBHYACRd6EesksG1Q8EoIAKOsiZviGKbOLIej4=
|
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f h1:Cq7MalBHYACRd6EesksG1Q8EoIAKOsiZviGKbOLIej4=
|
||||||
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
||||||
|
sigs.k8s.io/kustomize/kyaml v0.10.7 h1:r0r8UEL0bL7X56HKUmhJZ+TP+nvRNGrDHHSLO7izlcQ=
|
||||||
|
sigs.k8s.io/kustomize/kyaml v0.10.7/go.mod h1:K9yg1k/HB/6xNOf5VH3LhTo1DK9/5ykSZO5uIv+Y/1k=
|
||||||
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
||||||
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||||
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
|
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ func loadCrdIntoConfig(
|
|||||||
err = theConfig.AddNamereferenceFieldSpec(
|
err = theConfig.AddNamereferenceFieldSpec(
|
||||||
builtinconfig.NameBackReferences{
|
builtinconfig.NameBackReferences{
|
||||||
Gvk: resid.Gvk{Kind: kind, Version: version},
|
Gvk: resid.Gvk{Kind: kind, Version: version},
|
||||||
FieldSpecs: []types.FieldSpec{
|
Referrers: []types.FieldSpec{
|
||||||
makeFs(theGvk, append(path, propName, nameKey))},
|
makeFs(theGvk, append(path, propName, nameKey))},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -8,10 +8,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/loader"
|
|
||||||
|
|
||||||
. "sigs.k8s.io/kustomize/api/internal/accumulator"
|
. "sigs.k8s.io/kustomize/api/internal/accumulator"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
||||||
|
"sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
@@ -140,21 +139,19 @@ func TestLoadCRDs(t *testing.T) {
|
|||||||
nbrs := []builtinconfig.NameBackReferences{
|
nbrs := []builtinconfig.NameBackReferences{
|
||||||
{
|
{
|
||||||
Gvk: resid.Gvk{Kind: "Secret", Version: "v1"},
|
Gvk: resid.Gvk{Kind: "Secret", Version: "v1"},
|
||||||
FieldSpecs: []types.FieldSpec{
|
Referrers: []types.FieldSpec{
|
||||||
{
|
{
|
||||||
CreateIfNotPresent: false,
|
Gvk: resid.Gvk{Kind: "MyKind"},
|
||||||
Gvk: resid.Gvk{Kind: "MyKind"},
|
Path: "spec/secretRef/name",
|
||||||
Path: "spec/secretRef/name",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Gvk: resid.Gvk{Kind: "Bee", Version: "v1beta1"},
|
Gvk: resid.Gvk{Kind: "Bee", Version: "v1beta1"},
|
||||||
FieldSpecs: []types.FieldSpec{
|
Referrers: []types.FieldSpec{
|
||||||
{
|
{
|
||||||
CreateIfNotPresent: false,
|
Gvk: resid.Gvk{Kind: "MyKind"},
|
||||||
Gvk: resid.Gvk{Kind: "MyKind"},
|
Path: "spec/beeRef/name",
|
||||||
Path: "spec/beeRef/name",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,19 +4,25 @@
|
|||||||
package accumulator
|
package accumulator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filters/nameref"
|
"sigs.k8s.io/kustomize/api/filters/nameref"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
)
|
)
|
||||||
|
|
||||||
type nameReferenceTransformer struct {
|
type nameReferenceTransformer struct {
|
||||||
backRefs []builtinconfig.NameBackReferences
|
backRefs []builtinconfig.NameBackReferences
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const doDebug = false
|
||||||
|
|
||||||
var _ resmap.Transformer = &nameReferenceTransformer{}
|
var _ resmap.Transformer = &nameReferenceTransformer{}
|
||||||
|
|
||||||
|
type filterMap map[*resource.Resource][]nameref.Filter
|
||||||
|
|
||||||
// newNameReferenceTransformer constructs a nameReferenceTransformer
|
// newNameReferenceTransformer constructs a nameReferenceTransformer
|
||||||
// with a given slice of NameBackReferences.
|
// with a given slice of NameBackReferences.
|
||||||
func newNameReferenceTransformer(
|
func newNameReferenceTransformer(
|
||||||
@@ -29,16 +35,64 @@ func newNameReferenceTransformer(
|
|||||||
|
|
||||||
// Transform updates name references in resource A that
|
// Transform updates name references in resource A that
|
||||||
// refer to resource B, given that B's name may have
|
// refer to resource B, given that B's name may have
|
||||||
// changed. A is the referrer, B is the referralTarget.
|
// changed.
|
||||||
//
|
//
|
||||||
// For example, a HorizontalPodAutoscaler (HPA)
|
// For example, a HorizontalPodAutoscaler (HPA)
|
||||||
// necessarily refers to a Deployment, the thing that
|
// necessarily refers to a Deployment, the thing that
|
||||||
// the HPA scales. The Deployment's name might change
|
// an HPA scales. In this case:
|
||||||
// (e.g. prefix added), and the reference in the HPA
|
|
||||||
// has to be fixed.
|
|
||||||
//
|
//
|
||||||
// In the outer loop over the ResMap below, say we
|
// - the HPA instance is the Referrer,
|
||||||
// encounter a specific HPA. Then, in scanning the set
|
// - the Deployment instance is the ReferralTarget.
|
||||||
|
//
|
||||||
|
// If the Deployment's name changes, e.g. a prefix is added,
|
||||||
|
// then the HPA's reference to the Deployment must be fixed.
|
||||||
|
//
|
||||||
|
func (t *nameReferenceTransformer) Transform(m resmap.ResMap) error {
|
||||||
|
fMap := t.determineFilters(m.Resources())
|
||||||
|
debug(fMap)
|
||||||
|
for r, fList := range fMap {
|
||||||
|
c := m.SubsetThatCouldBeReferencedByResource(r)
|
||||||
|
for _, f := range fList {
|
||||||
|
f.Referrer = r
|
||||||
|
f.ReferralCandidates = c
|
||||||
|
if err := f.Referrer.ApplyFilter(f); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func debug(fMap filterMap) {
|
||||||
|
if !doDebug {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("filterMap has %d entries:\n", len(fMap))
|
||||||
|
rCount := 0
|
||||||
|
for r, fList := range fMap {
|
||||||
|
yml, _ := r.AsYAML()
|
||||||
|
rCount++
|
||||||
|
fmt.Printf(`
|
||||||
|
---- %3d. possible referrer -------------
|
||||||
|
%s
|
||||||
|
---------`, rCount, string(yml),
|
||||||
|
)
|
||||||
|
for i, f := range fList {
|
||||||
|
fmt.Printf(`
|
||||||
|
%3d/%3d update: %s
|
||||||
|
from: %s
|
||||||
|
`, rCount, i+1, f.NameFieldToUpdate.Path, f.ReferralTarget,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Produce a map from referrer resources that might need to be fixed
|
||||||
|
// to filters that might fix them. The keys to this map are potential
|
||||||
|
// referrers, so won't include resources like ConfigMap or Secret.
|
||||||
|
//
|
||||||
|
// In the inner loop over the resources below, say we
|
||||||
|
// encounter an HPA instance. Then, in scanning the set
|
||||||
// of all known backrefs, we encounter an entry like
|
// of all known backrefs, we encounter an entry like
|
||||||
//
|
//
|
||||||
// - kind: Deployment
|
// - kind: Deployment
|
||||||
@@ -48,64 +102,53 @@ func newNameReferenceTransformer(
|
|||||||
//
|
//
|
||||||
// This entry says that an HPA, via its
|
// This entry says that an HPA, via its
|
||||||
// 'spec/scaleTargetRef/name' field, may refer to a
|
// 'spec/scaleTargetRef/name' field, may refer to a
|
||||||
// Deployment. This match to HPA means we may need to
|
// Deployment.
|
||||||
// modify the value in its 'spec/scaleTargetRef/name'
|
|
||||||
// field, by searching for the thing it refers to,
|
|
||||||
// and getting its new name.
|
|
||||||
//
|
//
|
||||||
// As a filter, and search optimization, we compute a
|
// This means that a filter will need to hunt for the right Deployment,
|
||||||
// subset of all resources that the HPA could refer to,
|
// obtain it's new name, and write that name into the HPA's
|
||||||
// by excluding objects from other namespaces, and
|
// 'spec/scaleTargetRef/name' field. Return a filter that can do that.
|
||||||
// excluding objects that don't have the same prefix-
|
func (t *nameReferenceTransformer) determineFilters(
|
||||||
// suffix mods as the HPA.
|
resources []*resource.Resource) (fMap filterMap) {
|
||||||
//
|
fMap = make(filterMap)
|
||||||
// We look in this subset for all Deployment objects
|
for _, backReference := range t.backRefs {
|
||||||
// with a resId that has a Name matching the field value
|
for _, referrerSpec := range backReference.Referrers {
|
||||||
// present in the HPA. If no match do nothing; if more
|
for _, res := range resources {
|
||||||
// than one match, it's an error.
|
if res.OrgId().IsSelected(&referrerSpec.Gvk) {
|
||||||
//
|
// If this is true, the res might be a referrer, and if
|
||||||
// We overwrite the HPA name field with the value found
|
// so, the name reference it holds might need an update.
|
||||||
// in the Deployment's name field (the name in the raw
|
if resHasField(res, referrerSpec.Path) {
|
||||||
// object - the modified name - not the unmodified name
|
// Optimization - the referrer has the field
|
||||||
// in the Deployment's resId).
|
// that might need updating.
|
||||||
//
|
fMap[res] = append(fMap[res], nameref.Filter{
|
||||||
// This process assumes that the name stored in a ResId
|
// Name field to write in the Referrer.
|
||||||
// (the ResMap key) isn't modified by name transformers.
|
// If the path specified here isn't found in
|
||||||
// Name transformers should only modify the name in the
|
// the Referrer, nothing happens (no error,
|
||||||
// body of the resource object (the value in the ResMap).
|
// no field creation).
|
||||||
//
|
NameFieldToUpdate: referrerSpec,
|
||||||
func (t *nameReferenceTransformer) Transform(m resmap.ResMap) error {
|
// Specification of object class to read from.
|
||||||
// TODO: Too much looping, here and in transitive calls.
|
// Always read from metadata/name field.
|
||||||
for _, referrer := range m.Resources() {
|
ReferralTarget: backReference.Gvk,
|
||||||
var candidates resmap.ResMap
|
})
|
||||||
for _, referralTarget := range t.backRefs {
|
|
||||||
for _, fSpec := range referralTarget.FieldSpecs {
|
|
||||||
if referrer.OrgId().IsSelected(&fSpec.Gvk) {
|
|
||||||
if candidates == nil {
|
|
||||||
// This excludes objects from other namespaces.
|
|
||||||
// In most realistic uses, it returns all elements of m,
|
|
||||||
// (since they're all in the same namespace).
|
|
||||||
candidates = m.SubsetThatCouldBeReferencedByResource(referrer)
|
|
||||||
}
|
|
||||||
// One way to get here is with, say, a referrer that's an
|
|
||||||
// HPA, and a target that's a Deployment (one of the
|
|
||||||
// Deployment's fieldSpecs selects an HPA). Now we look
|
|
||||||
// through the candidates to see if one is a Deployment
|
|
||||||
// (the target), and if so, get the Deployment's name and
|
|
||||||
// write it into the referrer, at the field specfied in
|
|
||||||
// fSpec.
|
|
||||||
err := referrer.ApplyFilter(nameref.Filter{
|
|
||||||
Referrer: referrer,
|
|
||||||
NameFieldToUpdate: fSpec,
|
|
||||||
ReferralTarget: referralTarget.Gvk,
|
|
||||||
ReferralCandidates: candidates,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return fMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: check res for field existence here to avoid extra work.
|
||||||
|
// res.GetFieldValue, which uses yaml.Lookup under the hood, doesn't know
|
||||||
|
// how to parse fieldspec-style paths that make no distinction
|
||||||
|
// between maps and sequences. This means it cannot lookup commonly
|
||||||
|
// used "indeterminate" paths like
|
||||||
|
// spec/containers/env/valueFrom/configMapKeyRef/name
|
||||||
|
// ('containers' is a list, not a map).
|
||||||
|
// However, the fieldspec filter does know how to handle this;
|
||||||
|
// extract that code and call it here?
|
||||||
|
func resHasField(res *resource.Resource, path string) bool {
|
||||||
|
return true
|
||||||
|
// fld := strings.Join(utils.PathSplitter(path), ".")
|
||||||
|
// _, e := res.GetFieldValue(fld)
|
||||||
|
// return e == nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,12 +8,15 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
||||||
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
resmaptest_test "sigs.k8s.io/kustomize/api/testutils/resmaptest"
|
resmaptest_test "sigs.k8s.io/kustomize/api/testutils/resmaptest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const notEqualErrFmt = "expected (self) doesn't match actual (other): %v"
|
||||||
|
|
||||||
func TestNameReferenceHappyRun(t *testing.T) {
|
func TestNameReferenceHappyRun(t *testing.T) {
|
||||||
m := resmaptest_test.NewRmBuilderDefault(t).AddWithName(
|
m := resmaptest_test.NewRmBuilderDefault(t).AddWithName(
|
||||||
"cm1",
|
"cm1",
|
||||||
@@ -470,7 +473,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err = expected.ErrorIfNotEqualLists(m); err != nil {
|
if err = expected.ErrorIfNotEqualLists(m); err != nil {
|
||||||
t.Fatalf("actual doesn't match expected: %v", err)
|
t.Fatalf(notEqualErrFmt, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -518,7 +521,27 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).ResMap(),
|
}).ResMap(),
|
||||||
expectedErr: "cannot find field 'name' in node"},
|
// TODO(#3304): DECISION - kyaml better; not a bug.
|
||||||
|
expectedErr: konfig.IfApiMachineryElseKyaml(
|
||||||
|
`updating name reference in 'rules/resourceNames' field of `+
|
||||||
|
`'rbac.authorization.k8s.io_v1_ClusterRole|~X|cr'`+
|
||||||
|
`: considering field 'rules/resourceNames' of object
|
||||||
|
{"apiVersion": "rbac.authorization.k8s.io/v1", "kind": "ClusterRole", "metadata": {
|
||||||
|
"name": "cr"}, "rules": [{"resourceNames": {"foo": "bar"}, "resources": ["secrets"]}]}
|
||||||
|
: visit traversal on path: [resourceNames]: path config error; no 'name' field in node`,
|
||||||
|
`updating name reference in 'rules/resourceNames' field of `+
|
||||||
|
`'rbac.authorization.k8s.io_v1_ClusterRole|~X|cr'`+
|
||||||
|
`: considering field 'rules/resourceNames' of object
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRole
|
||||||
|
metadata:
|
||||||
|
name: cr
|
||||||
|
rules:
|
||||||
|
- resourceNames:
|
||||||
|
foo: bar
|
||||||
|
resources:
|
||||||
|
- secrets
|
||||||
|
: visit traversal on path: [resourceNames]: path config error; no 'name' field in node`)},
|
||||||
}
|
}
|
||||||
|
|
||||||
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference)
|
nrt := newNameReferenceTransformer(builtinconfig.MakeDefaultConfig().NameReference)
|
||||||
@@ -529,7 +552,7 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(err.Error(), test.expectedErr) {
|
if !strings.Contains(err.Error(), test.expectedErr) {
|
||||||
t.Fatalf("Incorrect error.\nExpected: %s, but got %v",
|
t.Fatalf("Incorrect error.\nExpected:\n %s\nGot:\n%v",
|
||||||
test.expectedErr, err)
|
test.expectedErr, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -587,7 +610,7 @@ func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
|
|||||||
v2.AppendRefBy(c2.CurId())
|
v2.AppendRefBy(c2.CurId())
|
||||||
|
|
||||||
if err := m1.ErrorIfNotEqualLists(m2); err != nil {
|
if err := m1.ErrorIfNotEqualLists(m2); err != nil {
|
||||||
t.Fatalf("actual doesn't match expected: %v", err)
|
t.Fatalf(notEqualErrFmt, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -723,7 +746,7 @@ func TestNameReferenceNamespace(t *testing.T) {
|
|||||||
|
|
||||||
m.RemoveBuildAnnotations()
|
m.RemoveBuildAnnotations()
|
||||||
if err = expected.ErrorIfNotEqualLists(m); err != nil {
|
if err = expected.ErrorIfNotEqualLists(m); err != nil {
|
||||||
t.Fatalf("actual doesn't match expected: %v", err)
|
t.Fatalf(notEqualErrFmt, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -885,7 +908,7 @@ func TestNameReferenceClusterWide(t *testing.T) {
|
|||||||
|
|
||||||
m.RemoveBuildAnnotations()
|
m.RemoveBuildAnnotations()
|
||||||
if err = expected.ErrorIfNotEqualLists(m); err != nil {
|
if err = expected.ErrorIfNotEqualLists(m); err != nil {
|
||||||
t.Fatalf("actual doesn't match expected: %v", err)
|
t.Fatalf(notEqualErrFmt, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1012,7 +1035,7 @@ func TestNameReferenceNamespaceTransformation(t *testing.T) {
|
|||||||
|
|
||||||
m.RemoveBuildAnnotations()
|
m.RemoveBuildAnnotations()
|
||||||
if err = expected.ErrorIfNotEqualLists(m); err != nil {
|
if err = expected.ErrorIfNotEqualLists(m); err != nil {
|
||||||
t.Fatalf("actual doesn't match expected: %v", err)
|
t.Fatalf(notEqualErrFmt, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1049,6 +1072,6 @@ func TestNameReferenceCandidateSelection(t *testing.T) {
|
|||||||
|
|
||||||
m.RemoveBuildAnnotations()
|
m.RemoveBuildAnnotations()
|
||||||
if err = expected.ErrorIfNotEqualLists(m); err != nil {
|
if err = expected.ErrorIfNotEqualLists(m); err != nil {
|
||||||
t.Fatalf("actual doesn't match expected: %v", err)
|
t.Fatalf(notEqualErrFmt, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,14 +24,12 @@ func TestRefVarTransformer(t *testing.T) {
|
|||||||
res resmap.ResMap
|
res resmap.ResMap
|
||||||
unused []string
|
unused []string
|
||||||
}
|
}
|
||||||
testCases := []struct {
|
testCases := map[string]struct {
|
||||||
description string
|
given given
|
||||||
given given
|
expected expected
|
||||||
expected expected
|
errMessage string
|
||||||
errMessage string
|
|
||||||
}{
|
}{
|
||||||
{
|
"var replacement in map[string]": {
|
||||||
description: "var replacement in map[string]",
|
|
||||||
given: given{
|
given: given{
|
||||||
varMap: map[string]interface{}{
|
varMap: map[string]interface{}{
|
||||||
"FOO": "replacementForFoo",
|
"FOO": "replacementForFoo",
|
||||||
@@ -106,8 +104,7 @@ func TestRefVarTransformer(t *testing.T) {
|
|||||||
unused: []string{"BAR"},
|
unused: []string{"BAR"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
"var replacement panic in map[string]": {
|
||||||
description: "var replacement panic in map[string]",
|
|
||||||
given: given{
|
given: given{
|
||||||
varMap: map[string]interface{}{},
|
varMap: map[string]interface{}{},
|
||||||
fs: []types.FieldSpec{
|
fs: []types.FieldSpec{
|
||||||
@@ -126,20 +123,21 @@ func TestRefVarTransformer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
// TODO(#3304): DECISION - kyaml better; not a bug.
|
// TODO(#3304): DECISION - kyaml better; not a bug.
|
||||||
errMessage: konfig.IfApiMachineryElseKyaml(
|
errMessage: konfig.IfApiMachineryElseKyaml(
|
||||||
`obj '{"apiVersion": "v1", "data": {"slice": [5]}, "kind": "ConfigMap", "metadata": {"name": "cm1"}}
|
`considering field 'data/slice' of object
|
||||||
' at path 'data/slice': invalid value type expect a string`,
|
{"apiVersion": "v1", "data": {"slice": [5]}, "kind": "ConfigMap", "metadata": {"name": "cm1"}}
|
||||||
`obj 'apiVersion: v1
|
: invalid value type expect a string`,
|
||||||
|
`considering field 'data/slice' of object
|
||||||
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
slice:
|
slice:
|
||||||
- 5
|
- 5
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: cm1
|
name: cm1
|
||||||
' at path 'data/slice': invalid value type expect a string`,
|
: invalid value type expect a string`,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
"var replacement in nil": {
|
||||||
description: "var replacement in nil",
|
|
||||||
given: given{
|
given: given{
|
||||||
varMap: map[string]interface{}{},
|
varMap: map[string]interface{}{},
|
||||||
fs: []types.FieldSpec{
|
fs: []types.FieldSpec{
|
||||||
@@ -171,20 +169,18 @@ metadata:
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for tn, tc := range testCases {
|
||||||
t.Run(tc.description, func(t *testing.T) {
|
t.Run(tn, func(t *testing.T) {
|
||||||
// arrange
|
|
||||||
tr := newRefVarTransformer(tc.given.varMap, tc.given.fs)
|
tr := newRefVarTransformer(tc.given.varMap, tc.given.fs)
|
||||||
|
|
||||||
// act
|
|
||||||
err := tr.Transform(tc.given.res)
|
err := tr.Transform(tc.given.res)
|
||||||
|
|
||||||
// assert
|
|
||||||
if tc.errMessage != "" {
|
if tc.errMessage != "" {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("missing expected error %v", tc.errMessage)
|
t.Fatalf("missing expected error %v", tc.errMessage)
|
||||||
} else if err.Error() != tc.errMessage {
|
} else if err.Error() != tc.errMessage {
|
||||||
t.Fatalf("actual error doesn't match expected error: \nACTUAL: %v\nEXPECTED: %v", err.Error(), tc.errMessage)
|
t.Fatalf(`actual error doesn't match expected error:
|
||||||
|
ACTUAL: %v
|
||||||
|
EXPECTED: %v`,
|
||||||
|
err.Error(), tc.errMessage)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -194,7 +190,13 @@ metadata:
|
|||||||
a, e := tc.given.res, tc.expected.res
|
a, e := tc.given.res, tc.expected.res
|
||||||
if !reflect.DeepEqual(a, e) {
|
if !reflect.DeepEqual(a, e) {
|
||||||
err = e.ErrorIfNotEqualLists(a)
|
err = e.ErrorIfNotEqualLists(a)
|
||||||
t.Fatalf("actual doesn't match expected: \nACTUAL:\n%v\nEXPECTED:\n%v\nERR: %v", a, e, err)
|
t.Fatalf(`actual doesn't match expected:
|
||||||
|
ACTUAL:
|
||||||
|
%v
|
||||||
|
EXPECTED:
|
||||||
|
%v
|
||||||
|
ERR: %v`,
|
||||||
|
a, e, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ func (ra *ResAccumulator) MergeVars(incoming []types.Var) error {
|
|||||||
// wildcard search on the namespace hence we still use GvknEquals
|
// wildcard search on the namespace hence we still use GvknEquals
|
||||||
idMatcher = targetId.Equals
|
idMatcher = targetId.Equals
|
||||||
}
|
}
|
||||||
matched := ra.resMap.GetMatchingResourcesByOriginalId(idMatcher)
|
matched := ra.resMap.GetMatchingResourcesByAnyId(idMatcher)
|
||||||
if len(matched) > 1 {
|
if len(matched) > 1 {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"found %d resId matches for var %s "+
|
"found %d resId matches for var %s "+
|
||||||
|
|||||||
@@ -344,20 +344,20 @@ func TestResolveVarsWithNoambiguation(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}}).
|
}}).
|
||||||
|
// Make it seem like this resource
|
||||||
|
// went through a prefix transformer.
|
||||||
Add(map[string]interface{}{
|
Add(map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
"kind": "Service",
|
"kind": "Service",
|
||||||
"metadata": map[string]interface{}{
|
"metadata": map[string]interface{}{
|
||||||
"name": "backendOne",
|
"name": "sub-backendOne",
|
||||||
|
"annotations": map[string]interface{}{
|
||||||
|
"config.kubernetes.io/previousNames": "backendOne",
|
||||||
|
"config.kubernetes.io/previousNamespaces": "default",
|
||||||
|
"config.kubernetes.io/prefixes": "sub-",
|
||||||
|
},
|
||||||
}}).ResMap()
|
}}).ResMap()
|
||||||
|
|
||||||
// Make it seem like this resource
|
|
||||||
// went through a prefix transformer.
|
|
||||||
r := m.GetByIndex(1)
|
|
||||||
r.AddNamePrefix("sub-")
|
|
||||||
r.SetName("sub-backendOne")
|
|
||||||
r.SetOriginalName("backendOne", true)
|
|
||||||
|
|
||||||
err = ra2.AppendAll(m)
|
err = ra2.AppendAll(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected err: %v", err)
|
t.Fatalf("unexpected err: %v", err)
|
||||||
|
|||||||
@@ -534,8 +534,8 @@ k8s.io/utils v0.0.0-20191114184206-e782cd3c129f/go.mod h1:sZAwmy6armz5eXlNoLmJcl
|
|||||||
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc=
|
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc=
|
||||||
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4=
|
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4=
|
||||||
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
||||||
sigs.k8s.io/kustomize/kyaml v0.10.6 h1:xUJxc/k8JoWqHUahaB8DTqY0KwEPxTbTGStvW8TOcDc=
|
sigs.k8s.io/kustomize/kyaml v0.10.7 h1:r0r8UEL0bL7X56HKUmhJZ+TP+nvRNGrDHHSLO7izlcQ=
|
||||||
sigs.k8s.io/kustomize/kyaml v0.10.6/go.mod h1:K9yg1k/HB/6xNOf5VH3LhTo1DK9/5ykSZO5uIv+Y/1k=
|
sigs.k8s.io/kustomize/kyaml v0.10.7/go.mod h1:K9yg1k/HB/6xNOf5VH3LhTo1DK9/5ykSZO5uIv+Y/1k=
|
||||||
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
||||||
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
||||||
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NameBackReferences is an association between a gvk.GVK and a list
|
// NameBackReferences is an association between a gvk.GVK (a ReferralTarget)
|
||||||
// of FieldSpec instances that could refer to it.
|
// and a list of Referrers that could refer to it.
|
||||||
//
|
//
|
||||||
// It is used to handle name changes, and can be thought of as a
|
// It is used to handle name changes, and can be thought of as a
|
||||||
// a contact list. If you change your own contact info (name,
|
// a contact list. If you change your own contact info (name,
|
||||||
@@ -19,16 +19,20 @@ import (
|
|||||||
// know about the change.
|
// know about the change.
|
||||||
//
|
//
|
||||||
// For example, ConfigMaps can be used by Pods and everything that
|
// For example, ConfigMaps can be used by Pods and everything that
|
||||||
// contains a Pod; Deployment, Job, StatefulSet, etc. To change
|
// contains a Pod; Deployment, Job, StatefulSet, etc.
|
||||||
// the name of a ConfigMap instance from 'alice' to 'bob', one
|
// The ConfigMap is the ReferralTarget, the others are Referrers.
|
||||||
// must visit all objects that could refer to the ConfigMap, see if
|
//
|
||||||
// they mention 'alice', and if so, change the reference to 'bob'.
|
// If the the name of a ConfigMap instance changed from 'alice' to 'bob',
|
||||||
|
// one must
|
||||||
|
// - visit all objects that could refer to the ConfigMap (the Referrers)
|
||||||
|
// - see if they mention 'alice',
|
||||||
|
// - if so, change the Referrer's name reference to 'bob'.
|
||||||
//
|
//
|
||||||
// The NameBackReferences instance to aid in this could look like
|
// The NameBackReferences instance to aid in this could look like
|
||||||
// {
|
// {
|
||||||
// kind: ConfigMap
|
// kind: ConfigMap
|
||||||
// version: v1
|
// version: v1
|
||||||
// FieldSpecs:
|
// fieldSpecs:
|
||||||
// - kind: Pod
|
// - kind: Pod
|
||||||
// version: v1
|
// version: v1
|
||||||
// path: spec/volumes/configMap/name
|
// path: spec/volumes/configMap/name
|
||||||
@@ -39,13 +43,15 @@ import (
|
|||||||
// (etc.)
|
// (etc.)
|
||||||
// }
|
// }
|
||||||
type NameBackReferences struct {
|
type NameBackReferences struct {
|
||||||
resid.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
|
resid.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
|
||||||
FieldSpecs types.FsSlice `json:"FieldSpecs,omitempty" yaml:"FieldSpecs,omitempty"`
|
// TODO: rename json 'fieldSpecs' to 'referrers' for clarity.
|
||||||
|
// This will, however, break anyone using a custom config.
|
||||||
|
Referrers types.FsSlice `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n NameBackReferences) String() string {
|
func (n NameBackReferences) String() string {
|
||||||
var r []string
|
var r []string
|
||||||
for _, f := range n.FieldSpecs {
|
for _, f := range n.Referrers {
|
||||||
r = append(r, f.String())
|
r = append(r, f.String())
|
||||||
}
|
}
|
||||||
return n.Gvk.String() + ": (\n" +
|
return n.Gvk.String() + ": (\n" +
|
||||||
@@ -77,7 +83,7 @@ func (s nbrSlice) mergeOne(other NameBackReferences) (nbrSlice, error) {
|
|||||||
found := false
|
found := false
|
||||||
for _, c := range s {
|
for _, c := range s {
|
||||||
if c.Gvk.Equals(other.Gvk) {
|
if c.Gvk.Equals(other.Gvk) {
|
||||||
c.FieldSpecs, err = c.FieldSpecs.MergeAll(other.FieldSpecs)
|
c.Referrers, err = c.Referrers.MergeAll(other.Referrers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,13 +50,13 @@ func TestMergeAll(t *testing.T) {
|
|||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "ConfigMap",
|
Kind: "ConfigMap",
|
||||||
},
|
},
|
||||||
FieldSpecs: fsSlice1,
|
Referrers: fsSlice1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
},
|
},
|
||||||
FieldSpecs: fsSlice2,
|
Referrers: fsSlice2,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
nbrsSlice2 := nbrSlice{
|
nbrsSlice2 := nbrSlice{
|
||||||
@@ -64,13 +64,13 @@ func TestMergeAll(t *testing.T) {
|
|||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "ConfigMap",
|
Kind: "ConfigMap",
|
||||||
},
|
},
|
||||||
FieldSpecs: fsSlice1,
|
Referrers: fsSlice1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
},
|
},
|
||||||
FieldSpecs: fsSlice2,
|
Referrers: fsSlice2,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
expected := nbrSlice{
|
expected := nbrSlice{
|
||||||
@@ -78,13 +78,13 @@ func TestMergeAll(t *testing.T) {
|
|||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "ConfigMap",
|
Kind: "ConfigMap",
|
||||||
},
|
},
|
||||||
FieldSpecs: fsSlice1,
|
Referrers: fsSlice1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
},
|
},
|
||||||
FieldSpecs: fsSlice2,
|
Referrers: fsSlice2,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := nbrsSlice1.mergeAll(nbrsSlice2)
|
actual, err := nbrsSlice1.mergeAll(nbrsSlice2)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func TestAddNamereferenceFieldSpec(t *testing.T) {
|
|||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "KindA",
|
Kind: "KindA",
|
||||||
},
|
},
|
||||||
FieldSpecs: []types.FieldSpec{
|
Referrers: []types.FieldSpec{
|
||||||
{
|
{
|
||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "KindB",
|
Kind: "KindB",
|
||||||
@@ -89,7 +89,7 @@ func TestMerge(t *testing.T) {
|
|||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "KindA",
|
Kind: "KindA",
|
||||||
},
|
},
|
||||||
FieldSpecs: []types.FieldSpec{
|
Referrers: []types.FieldSpec{
|
||||||
{
|
{
|
||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "KindB",
|
Kind: "KindB",
|
||||||
@@ -103,7 +103,7 @@ func TestMerge(t *testing.T) {
|
|||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "KindA",
|
Kind: "KindA",
|
||||||
},
|
},
|
||||||
FieldSpecs: []types.FieldSpec{
|
Referrers: []types.FieldSpec{
|
||||||
{
|
{
|
||||||
Gvk: resid.Gvk{
|
Gvk: resid.Gvk{
|
||||||
Kind: "KindC",
|
Kind: "KindC",
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -13,8 +14,8 @@ import (
|
|||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
@@ -135,9 +136,6 @@ func GetResMapWithIDAnnotation(rm resmap.ResMap) (resmap.ResMap, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
annotations := r.GetAnnotations()
|
annotations := r.GetAnnotations()
|
||||||
if annotations == nil {
|
|
||||||
annotations = make(map[string]string)
|
|
||||||
}
|
|
||||||
annotations[idAnnotation] = string(idString)
|
annotations[idAnnotation] = string(idString)
|
||||||
r.SetAnnotations(annotations)
|
r.SetAnnotations(annotations)
|
||||||
}
|
}
|
||||||
@@ -147,41 +145,65 @@ func GetResMapWithIDAnnotation(rm resmap.ResMap) (resmap.ResMap, error) {
|
|||||||
// UpdateResMapValues updates the Resource value in the given ResMap
|
// UpdateResMapValues updates the Resource value in the given ResMap
|
||||||
// with the emitted Resource values in output.
|
// with the emitted Resource values in output.
|
||||||
func UpdateResMapValues(pluginName string, h *resmap.PluginHelpers, output []byte, rm resmap.ResMap) error {
|
func UpdateResMapValues(pluginName string, h *resmap.PluginHelpers, output []byte, rm resmap.ResMap) error {
|
||||||
outputRM, err := h.ResmapFactory().NewResMapFromBytes(output)
|
mapFactory := h.ResmapFactory()
|
||||||
|
resFactory := mapFactory.RF()
|
||||||
|
resources, err := resFactory.SliceFromBytes(output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, r := range outputRM.Resources() {
|
// Don't use resources here, or error message will be unfriendly to plugin builders
|
||||||
// for each emitted Resource, find the matching Resource in the original ResMap
|
newMap, err := mapFactory.NewResMapFromBytes([]byte{})
|
||||||
// using its id
|
if err != nil {
|
||||||
annotations := r.GetAnnotations()
|
return err
|
||||||
idString, ok := annotations[idAnnotation]
|
}
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("the transformer %s should not remove annotation %s",
|
for _, r := range resources {
|
||||||
pluginName, idAnnotation)
|
removeIDAnnotation(r) // stale--not manipulated by plugin transformers
|
||||||
|
|
||||||
|
// Add to the new map, checking for duplicates
|
||||||
|
if err := newMap.Append(r); err != nil {
|
||||||
|
prettyID, err := json.Marshal(r.CurId())
|
||||||
|
if err != nil {
|
||||||
|
prettyID = []byte(r.CurId().String())
|
||||||
|
}
|
||||||
|
return fmt.Errorf("plugin %s generated duplicate resource: %s", pluginName, prettyID)
|
||||||
}
|
}
|
||||||
id := resid.ResId{}
|
|
||||||
err := yaml.Unmarshal([]byte(idString), &id)
|
// Add to or update the old map
|
||||||
|
oldIdx, err := rm.GetIndexOfCurrentId(r.CurId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
res, err := rm.GetByCurrentId(id)
|
if oldIdx != -1 {
|
||||||
if err != nil {
|
rm.GetByIndex(oldIdx).ResetPrimaryData(r)
|
||||||
return fmt.Errorf("unable to find unique match to %s", id.String())
|
} else {
|
||||||
|
if err := rm.Append(r); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// remove the annotation set by Kustomize to track the resource
|
|
||||||
delete(annotations, idAnnotation)
|
|
||||||
if len(annotations) == 0 {
|
|
||||||
annotations = nil
|
|
||||||
}
|
|
||||||
r.SetAnnotations(annotations)
|
|
||||||
|
|
||||||
// update the resource value with the transformed object
|
|
||||||
res.ResetPrimaryData(r)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove items the transformer deleted from the old map
|
||||||
|
for _, id := range rm.AllIds() {
|
||||||
|
newIdx, _ := newMap.GetIndexOfCurrentId(id)
|
||||||
|
if newIdx == -1 {
|
||||||
|
rm.Remove(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func removeIDAnnotation(r *resource.Resource) {
|
||||||
|
// remove the annotation set by Kustomize to track the resource
|
||||||
|
annotations := r.GetAnnotations()
|
||||||
|
delete(annotations, idAnnotation)
|
||||||
|
if len(annotations) == 0 {
|
||||||
|
annotations = nil
|
||||||
|
}
|
||||||
|
r.SetAnnotations(annotations)
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateResourceOptions updates the generator options for each resource in the
|
// UpdateResourceOptions updates the generator options for each resource in the
|
||||||
// given ResMap based on plugin provided annotations.
|
// given ResMap based on plugin provided annotations.
|
||||||
func UpdateResourceOptions(rm resmap.ResMap) (resmap.ResMap, error) {
|
func UpdateResourceOptions(rm resmap.ResMap) (resmap.ResMap, error) {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -382,6 +383,11 @@ func (kt *KustTarget) accumulateDirectory(
|
|||||||
return nil, errors.Wrapf(
|
return nil, errors.Wrapf(
|
||||||
err, "couldn't make target for path '%s'", ldr.Root())
|
err, "couldn't make target for path '%s'", ldr.Root())
|
||||||
}
|
}
|
||||||
|
err = openapi.SetSchemaVersion(subKt.Kustomization().OpenAPI, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(
|
||||||
|
err, "couldn't set openapi version for path '%s'", ldr.Root())
|
||||||
|
}
|
||||||
if isComponent && subKt.kustomization.Kind != types.ComponentKind {
|
if isComponent && subKt.kustomization.Kind != types.ComponentKind {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"expected kind '%s' for path '%s' but got '%s'", types.ComponentKind, ldr.Root(), subKt.kustomization.Kind)
|
"expected kind '%s' for path '%s' but got '%s'", types.ComponentKind, ldr.Root(), subKt.kustomization.Kind)
|
||||||
|
|||||||
22
api/internal/utils/pathsplitter.go
Normal file
22
api/internal/utils/pathsplitter.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
// Copyright 2021 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
// PathSplitter splits a slash delimited string, permitting escaped slashes.
|
||||||
|
func PathSplitter(path string) []string {
|
||||||
|
ps := strings.Split(path, "/")
|
||||||
|
var res []string
|
||||||
|
res = append(res, ps[0])
|
||||||
|
for i := 1; i < len(ps); i++ {
|
||||||
|
last := len(res) - 1
|
||||||
|
if strings.HasSuffix(res[last], `\`) {
|
||||||
|
res[last] = strings.TrimSuffix(res[last], `\`) + "/" + ps[i]
|
||||||
|
} else {
|
||||||
|
res = append(res, ps[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
49
api/internal/utils/pathsplitter_test.go
Normal file
49
api/internal/utils/pathsplitter_test.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
// Copyright 2021 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package utils_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
. "sigs.k8s.io/kustomize/api/internal/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPathSplitter(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
exp []string
|
||||||
|
path string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
path: "",
|
||||||
|
exp: []string{""},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "s",
|
||||||
|
exp: []string{"s"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "a/b/c",
|
||||||
|
exp: []string{"a", "b", "c"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: `a/b[]/c`,
|
||||||
|
exp: []string{"a", "b[]", "c"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: `a/b\/c/d\/e/f`,
|
||||||
|
exp: []string{"a", "b/c", "d/e", "f"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// The actual reason for this.
|
||||||
|
path: `metadata/annotations/nginx.ingress.kubernetes.io\/auth-secret`,
|
||||||
|
exp: []string{
|
||||||
|
"metadata",
|
||||||
|
"annotations",
|
||||||
|
"nginx.ingress.kubernetes.io/auth-secret"},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
assert.Equal(t, tc.exp, PathSplitter(tc.path))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,6 +33,38 @@ const (
|
|||||||
"area": "51",
|
"area": "51",
|
||||||
"greeting": "Take me to your leader."
|
"greeting": "Take me to your leader."
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"spec": {
|
||||||
|
"template": {
|
||||||
|
"spec": {
|
||||||
|
"containers": [
|
||||||
|
{
|
||||||
|
"env": [
|
||||||
|
{
|
||||||
|
"name": "CM_FOO",
|
||||||
|
"valueFrom": {
|
||||||
|
"configMapKeyRef": {
|
||||||
|
"key": "somekey",
|
||||||
|
"name": "myCm"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "SECRET_FOO",
|
||||||
|
"valueFrom": {
|
||||||
|
"secretKeyRef": {
|
||||||
|
"key": "someKey",
|
||||||
|
"name": "mySecret"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"image": "nginx:1.7.9",
|
||||||
|
"name": "nginx"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
@@ -358,6 +390,51 @@ func TestGetFieldValueReturnsMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetFieldValueReturnsStuff(t *testing.T) {
|
||||||
|
wn := NewWNode()
|
||||||
|
if err := wn.UnmarshalJSON([]byte(deploymentBiggerJson)); err != nil {
|
||||||
|
t.Fatalf("unexpected unmarshaljson err: %v", err)
|
||||||
|
}
|
||||||
|
expected := []interface{}{
|
||||||
|
map[string]interface{}{
|
||||||
|
"env": []interface{}{
|
||||||
|
map[string]interface{}{
|
||||||
|
"name": "CM_FOO",
|
||||||
|
"valueFrom": map[string]interface{}{
|
||||||
|
"configMapKeyRef": map[string]interface{}{
|
||||||
|
"key": "somekey",
|
||||||
|
"name": "myCm",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"name": "SECRET_FOO",
|
||||||
|
"valueFrom": map[string]interface{}{
|
||||||
|
"secretKeyRef": map[string]interface{}{
|
||||||
|
"key": "someKey",
|
||||||
|
"name": "mySecret",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"image": string("nginx:1.7.9"),
|
||||||
|
"name": string("nginx"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
actual, err := wn.GetFieldValue("spec.template.spec.containers")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error getting field value: %v", err)
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(expected, actual); diff != "" {
|
||||||
|
t.Fatalf("actual map does not deep equal expected map:\n%v", diff)
|
||||||
|
}
|
||||||
|
// Cannot go deeper yet.
|
||||||
|
_, err = wn.GetFieldValue("spec.template.spec.containers.env")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected err %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetFieldValueReturnsSlice(t *testing.T) {
|
func TestGetFieldValueReturnsSlice(t *testing.T) {
|
||||||
bytes, err := yaml.Marshal(makeBigMap())
|
bytes, err := yaml.Marshal(makeBigMap())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
|
|
||||||
package builtinpluginconsts
|
package builtinpluginconsts
|
||||||
|
|
||||||
|
// TODO: rename 'fieldSpecs' to 'referrers' for clarity.
|
||||||
|
// This will, however, break anyone using a custom config.
|
||||||
|
|
||||||
const (
|
const (
|
||||||
nameReferenceFieldSpecs = `
|
nameReferenceFieldSpecs = `
|
||||||
nameReference:
|
nameReference:
|
||||||
|
|||||||
@@ -93,3 +93,70 @@ resources:
|
|||||||
t.Fatalf("unexpected error: %q", err)
|
t.Fatalf("unexpected error: %q", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResourceHasAnchor(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("/app", `
|
||||||
|
resources:
|
||||||
|
- ingress.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/ingress.yaml", `
|
||||||
|
apiVersion: networking.k8s.io/v1
|
||||||
|
kind: Ingress
|
||||||
|
metadata:
|
||||||
|
name: blog
|
||||||
|
spec:
|
||||||
|
tls:
|
||||||
|
- hosts:
|
||||||
|
- xyz.me
|
||||||
|
- www.xyz.me
|
||||||
|
secretName: cert-tls
|
||||||
|
rules:
|
||||||
|
- host: xyz.me
|
||||||
|
http: &xxx_rules
|
||||||
|
paths:
|
||||||
|
- path: /
|
||||||
|
pathType: Prefix
|
||||||
|
backend:
|
||||||
|
service:
|
||||||
|
name: service
|
||||||
|
port:
|
||||||
|
number: 80
|
||||||
|
- host: www.xyz.me
|
||||||
|
http: *xxx_rules
|
||||||
|
`)
|
||||||
|
m := th.Run("/app", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: networking.k8s.io/v1
|
||||||
|
kind: Ingress
|
||||||
|
metadata:
|
||||||
|
name: blog
|
||||||
|
spec:
|
||||||
|
rules:
|
||||||
|
- host: xyz.me
|
||||||
|
http:
|
||||||
|
paths:
|
||||||
|
- backend:
|
||||||
|
service:
|
||||||
|
name: service
|
||||||
|
port:
|
||||||
|
number: 80
|
||||||
|
path: /
|
||||||
|
pathType: Prefix
|
||||||
|
- host: www.xyz.me
|
||||||
|
http:
|
||||||
|
paths:
|
||||||
|
- backend:
|
||||||
|
service:
|
||||||
|
name: service
|
||||||
|
port:
|
||||||
|
number: 80
|
||||||
|
path: /
|
||||||
|
pathType: Prefix
|
||||||
|
tls:
|
||||||
|
- hosts:
|
||||||
|
- xyz.me
|
||||||
|
- www.xyz.me
|
||||||
|
secretName: cert-tls
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|||||||
227
api/krusty/diamondpatchref_test.go
Normal file
227
api/krusty/diamondpatchref_test.go
Normal file
@@ -0,0 +1,227 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package krusty_test
|
||||||
|
|
||||||
|
// In the following structure `top` patches `left-deploy` and `right-deploy` to reference the
|
||||||
|
// configMap in `bottom`. This test verifies `configMapRef.name` in `left-deploy` and
|
||||||
|
// `right-deploy` is modified correctly.
|
||||||
|
//
|
||||||
|
// top
|
||||||
|
// / \
|
||||||
|
// left right
|
||||||
|
// / \ / \
|
||||||
|
// left-service bottom bottom right-service
|
||||||
|
// | \ / |
|
||||||
|
// left-deploy bottom right-deploy
|
||||||
|
// |
|
||||||
|
// configMap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConfMapNameResolutionInDiamondWithPatches(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
|
th.WriteK("bottom", `
|
||||||
|
configMapGenerator:
|
||||||
|
- name: bottom
|
||||||
|
literals:
|
||||||
|
- KEY=value
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("left-service", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("left-service/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: left-deploy
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: left-deploy
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app.kubernetes.io/name: left-pod
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: left-pod
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: left-image:v1.0
|
||||||
|
name: service
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("right-service", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("right-service/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: right-deploy
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: right-deploy
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app.kubernetes.io/name: right-pod
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: right-pod
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: right-image:v1.0
|
||||||
|
name: service
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("top", `
|
||||||
|
resources:
|
||||||
|
- ./left
|
||||||
|
- ./right
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("top/left", `
|
||||||
|
resources:
|
||||||
|
- ../../left-service
|
||||||
|
- ./bottom
|
||||||
|
|
||||||
|
patches:
|
||||||
|
- target:
|
||||||
|
kind: Deployment
|
||||||
|
name: left-deploy
|
||||||
|
patch: |-
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: ignored-by-kustomize
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: service
|
||||||
|
env:
|
||||||
|
- name: MAPPED_CONFIG_ITEM
|
||||||
|
valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
name: left-bottom
|
||||||
|
key: KEY
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("top/left/bottom", `
|
||||||
|
namePrefix: left-
|
||||||
|
|
||||||
|
resources:
|
||||||
|
- ../../../bottom
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("top/right", `
|
||||||
|
resources:
|
||||||
|
- ../../right-service
|
||||||
|
- ./bottom
|
||||||
|
|
||||||
|
patches:
|
||||||
|
- target:
|
||||||
|
kind: Deployment
|
||||||
|
name: right-deploy
|
||||||
|
patch: |-
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: ignored-by-kustomize
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: service
|
||||||
|
env:
|
||||||
|
- name: MAPPED_CONFIG_ITEM
|
||||||
|
valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
name: right-bottom
|
||||||
|
key: KEY
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("top/right/bottom", `
|
||||||
|
namePrefix: right-
|
||||||
|
|
||||||
|
resources:
|
||||||
|
- ../../../bottom
|
||||||
|
`)
|
||||||
|
|
||||||
|
m := th.Run("top", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: left-deploy
|
||||||
|
name: left-deploy
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app.kubernetes.io/name: left-pod
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: left-pod
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- name: MAPPED_CONFIG_ITEM
|
||||||
|
valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
key: KEY
|
||||||
|
name: left-bottom-9f2t6f5h6d
|
||||||
|
image: left-image:v1.0
|
||||||
|
name: service
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
data:
|
||||||
|
KEY: value
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: left-bottom-9f2t6f5h6d
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: right-deploy
|
||||||
|
name: right-deploy
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app.kubernetes.io/name: right-pod
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: right-pod
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- name: MAPPED_CONFIG_ITEM
|
||||||
|
valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
key: KEY
|
||||||
|
name: right-bottom-9f2t6f5h6d
|
||||||
|
image: right-image:v1.0
|
||||||
|
name: service
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
data:
|
||||||
|
KEY: value
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: right-bottom-9f2t6f5h6d
|
||||||
|
`)
|
||||||
|
}
|
||||||
387
api/krusty/intermediateresourceid_test.go
Normal file
387
api/krusty/intermediateresourceid_test.go
Normal file
@@ -0,0 +1,387 @@
|
|||||||
|
package krusty_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Checks that a patch at the top of the stack can refer to resources
|
||||||
|
// by an intermediate name after it has gone through multiple name
|
||||||
|
// transformations.
|
||||||
|
// Ref: Issue #3455
|
||||||
|
func TestIntermediateName(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("/app/gcp", `
|
||||||
|
namePrefix: gcp-
|
||||||
|
resources:
|
||||||
|
- ../emea
|
||||||
|
patchesStrategicMerge:
|
||||||
|
- depPatch.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/gcp/depPatch.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: prod-foo
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
`)
|
||||||
|
th.WriteK("/app/emea", `
|
||||||
|
namePrefix: emea-
|
||||||
|
resources:
|
||||||
|
- ../prod
|
||||||
|
`)
|
||||||
|
th.WriteK("/app/prod", `
|
||||||
|
namePrefix: prod-
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
`)
|
||||||
|
th.WriteK("/app/base", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/base/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
m := th.Run("/app/gcp", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: gcp-emea-prod-foo
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that if resources in different layers (containing name
|
||||||
|
// transformations) have the same name, there is no conflict
|
||||||
|
func TestIntermediateNameSameNameDifferentLayer(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("/app/gcp", `
|
||||||
|
namePrefix: gcp-
|
||||||
|
resources:
|
||||||
|
- ../emea
|
||||||
|
patchesStrategicMerge:
|
||||||
|
- depPatch.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/gcp/depPatch.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: prod-foo
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
`)
|
||||||
|
th.WriteK("/app/emea", `
|
||||||
|
namePrefix: emea-
|
||||||
|
resources:
|
||||||
|
- ../prod
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/emea/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
th.WriteK("/app/prod", `
|
||||||
|
namePrefix: prod-
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
`)
|
||||||
|
th.WriteK("/app/base", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/base/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
m := th.Run("/app/gcp", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: gcp-emea-prod-foo
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: gcp-emea-foo
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Same as above test but tries to refer to the name foo
|
||||||
|
// instead of prod-foo
|
||||||
|
func TestIntermediateNameAmbiguous(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("/app/gcp", `
|
||||||
|
namePrefix: gcp-
|
||||||
|
resources:
|
||||||
|
- ../emea
|
||||||
|
patchesStrategicMerge:
|
||||||
|
- depPatch.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/gcp/depPatch.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
`)
|
||||||
|
th.WriteK("/app/emea", `
|
||||||
|
namePrefix: emea-
|
||||||
|
resources:
|
||||||
|
- ../prod
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/emea/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
th.WriteK("/app/prod", `
|
||||||
|
namePrefix: prod-
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
`)
|
||||||
|
th.WriteK("/app/base", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/base/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
err := th.RunWithErr("/app/gcp", th.MakeDefaultOptions())
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test for issue #3228
|
||||||
|
// References to resources by their intermediate names after multiple
|
||||||
|
// name transformations should be supported
|
||||||
|
func TestIntermediateNameSecretKeyRefDiamond(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
|
th.WriteK(".", `
|
||||||
|
namePrefix: project-
|
||||||
|
resources:
|
||||||
|
- app`)
|
||||||
|
|
||||||
|
th.WriteK("/app", `
|
||||||
|
resources:
|
||||||
|
- resources/deployment.yaml
|
||||||
|
- resources/xql
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("/app/resources/xql", `
|
||||||
|
resources:
|
||||||
|
- xql-zero
|
||||||
|
- xql-one
|
||||||
|
configurations:
|
||||||
|
- ./kustomizeconfig.yaml
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("/app/resources/xql/kustomizeconfig.yaml", `
|
||||||
|
varReference:
|
||||||
|
- path: spec/template/spec/containers/env/valueFrom/secretKeyRef/name
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("/app/resources/xql/xql-one", `
|
||||||
|
namePrefix: xql-one-
|
||||||
|
resources:
|
||||||
|
- ../../../../bases/xql
|
||||||
|
secretGenerator:
|
||||||
|
- name: xql-secret
|
||||||
|
behavior: merge
|
||||||
|
envs:
|
||||||
|
- config/xql-one-secret.env
|
||||||
|
vars:
|
||||||
|
- name: PROJECT_XQL_ONE_SECRET_NAME
|
||||||
|
objref:
|
||||||
|
kind: Secret
|
||||||
|
name: xql-secret
|
||||||
|
apiVersion: v1
|
||||||
|
fieldref:
|
||||||
|
fieldpath: metadata.name
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("/app/resources/xql/xql-one/config/xql-one-secret.env", `
|
||||||
|
arg=1
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("/app/resources/xql/xql-zero", `
|
||||||
|
namePrefix: xql-zero-
|
||||||
|
resources:
|
||||||
|
- ../../../../bases/xql
|
||||||
|
secretGenerator:
|
||||||
|
- name: xql-secret
|
||||||
|
behavior: merge
|
||||||
|
envs:
|
||||||
|
- config/xql-zero-secret.env
|
||||||
|
vars:
|
||||||
|
- name: PROJECT_XQL_ZERO_SECRET_NAME
|
||||||
|
objref:
|
||||||
|
kind: Secret
|
||||||
|
name: xql-secret
|
||||||
|
apiVersion: v1
|
||||||
|
fieldref:
|
||||||
|
fieldpath: metadata.name
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("/app/resources/xql/xql-zero/config/xql-zero-secret.env", `
|
||||||
|
arg=0
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("app/resources/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: app
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: app
|
||||||
|
image: example.com/app:latest
|
||||||
|
imagePullPolicy: Always
|
||||||
|
env:
|
||||||
|
- name: XQL_ZERO_ARG
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: $(PROJECT_XQL_ZERO_SECRET_NAME)
|
||||||
|
key: arg
|
||||||
|
- name: XQL_ZERO_PASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: xql-zero-xql-secret
|
||||||
|
key: password
|
||||||
|
- name: XQL_ONE_ARG
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: $(PROJECT_XQL_ONE_SECRET_NAME)
|
||||||
|
key: arg
|
||||||
|
- name: XQL_ONE_PASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: xql-one-xql-secret
|
||||||
|
key: password
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteK("/bases/xql", `
|
||||||
|
secretGenerator:
|
||||||
|
- name: xql-secret
|
||||||
|
envs:
|
||||||
|
- config/xql-secret.env
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("bases/xql/config/xql-secret.env", `
|
||||||
|
password=SUPER_SECRET_PASSWORD
|
||||||
|
`)
|
||||||
|
|
||||||
|
m := th.Run(".", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: project-app
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- name: XQL_ZERO_ARG
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
key: arg
|
||||||
|
name: project-xql-zero-xql-secret-6khmtc56hm
|
||||||
|
- name: XQL_ZERO_PASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
key: password
|
||||||
|
name: project-xql-zero-xql-secret-6khmtc56hm
|
||||||
|
- name: XQL_ONE_ARG
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
key: arg
|
||||||
|
name: project-xql-one-xql-secret-79mhmf5dgt
|
||||||
|
- name: XQL_ONE_PASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
key: password
|
||||||
|
name: project-xql-one-xql-secret-79mhmf5dgt
|
||||||
|
image: example.com/app:latest
|
||||||
|
imagePullPolicy: Always
|
||||||
|
name: app
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
data:
|
||||||
|
arg: MA==
|
||||||
|
password: U1VQRVJfU0VDUkVUX1BBU1NXT1JE
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
name: project-xql-zero-xql-secret-6khmtc56hm
|
||||||
|
type: Opaque
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
data:
|
||||||
|
arg: MQ==
|
||||||
|
password: U1VQRVJfU0VDUkVUX1BBU1NXT1JE
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
name: project-xql-one-xql-secret-79mhmf5dgt
|
||||||
|
type: Opaque
|
||||||
|
`)
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Kustomizer performs kustomizations. It's meant to behave
|
// Kustomizer performs kustomizations. It's meant to behave
|
||||||
@@ -73,6 +74,10 @@ func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
err = openapi.SetSchemaVersion(kt.Kustomization().OpenAPI, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
var m resmap.ResMap
|
var m resmap.ResMap
|
||||||
m, err = kt.MakeCustomizedResMap()
|
m, err = kt.MakeCustomizedResMap()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ func TestNamePrefixSuffixPatch(t *testing.T) {
|
|||||||
|
|
||||||
th.WriteF("handlers/kustomization.yaml", `
|
th.WriteF("handlers/kustomization.yaml", `
|
||||||
nameSuffix: -suffix
|
nameSuffix: -suffix
|
||||||
|
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
`)
|
`)
|
||||||
|
|||||||
@@ -1,11 +1,517 @@
|
|||||||
package krusty_test
|
package krusty_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestIssue3489Simplified(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK(".", `
|
||||||
|
namespace: kube-system
|
||||||
|
resources:
|
||||||
|
- aa
|
||||||
|
- bb
|
||||||
|
`)
|
||||||
|
th.WriteK("aa", `
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
`)
|
||||||
|
th.WriteK("bb", `
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
nameSuffix: -private
|
||||||
|
`)
|
||||||
|
th.WriteK("base", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
- serviceAccount.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("base/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDep
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
serviceAccountName: mySvcAcct
|
||||||
|
containers:
|
||||||
|
- name: whatever
|
||||||
|
image: k8s.gcr.io/governmentCheese
|
||||||
|
`)
|
||||||
|
th.WriteF("base/serviceAccount.yaml", `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ServiceAccount
|
||||||
|
metadata:
|
||||||
|
name: mySvcAcct
|
||||||
|
`)
|
||||||
|
m := th.Run(".", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDep
|
||||||
|
namespace: kube-system
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: k8s.gcr.io/governmentCheese
|
||||||
|
name: whatever
|
||||||
|
serviceAccountName: mySvcAcct
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ServiceAccount
|
||||||
|
metadata:
|
||||||
|
name: mySvcAcct
|
||||||
|
namespace: kube-system
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDep-private
|
||||||
|
namespace: kube-system
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: k8s.gcr.io/governmentCheese
|
||||||
|
name: whatever
|
||||||
|
serviceAccountName: mySvcAcct-private
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ServiceAccount
|
||||||
|
metadata:
|
||||||
|
name: mySvcAcct-private
|
||||||
|
namespace: kube-system
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIssue3489(t *testing.T) {
|
||||||
|
const assets = `{
|
||||||
|
"tenantId": "XXXXX-XXXXXX-XXXXX-XXXXXX-XXXXXX",
|
||||||
|
"subscriptionId": "XXXXX-XXXXXX-XXXXX-XXXXXX-XXXXXX",
|
||||||
|
"resourceGroup": "DNS-EUW-XXX-RG",
|
||||||
|
"useManagedIdentityExtension": true,
|
||||||
|
"userAssignedIdentityID": "XXXXX-XXXXXX-XXXXX-XXXXXX-XXXXXX"
|
||||||
|
}
|
||||||
|
`
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK(".", `
|
||||||
|
namespace: kube-system
|
||||||
|
resources:
|
||||||
|
- external-dns
|
||||||
|
- external-dns-private
|
||||||
|
`)
|
||||||
|
th.WriteK("external-dns", `
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
commonLabels:
|
||||||
|
app: external-dns
|
||||||
|
instance: public
|
||||||
|
images:
|
||||||
|
- name: k8s.gcr.io/external-dns/external-dns
|
||||||
|
newName: xxx.azurecr.io/external-dns
|
||||||
|
newTag: v0.7.4_sylr.1
|
||||||
|
- name: quay.io/sylr/external-dns
|
||||||
|
newName: xxx.azurecr.io/external-dns
|
||||||
|
newTag: v0.7.4_sylr.1
|
||||||
|
secretGenerator:
|
||||||
|
- name: azure-config-file
|
||||||
|
behavior: replace
|
||||||
|
files:
|
||||||
|
- assets/azure.json
|
||||||
|
patches:
|
||||||
|
- target:
|
||||||
|
group: apps
|
||||||
|
version: v1
|
||||||
|
kind: Deployment
|
||||||
|
name: external-dns
|
||||||
|
patch: |-
|
||||||
|
- op: replace
|
||||||
|
path: /spec/template/spec/containers/0/args
|
||||||
|
value:
|
||||||
|
- --txt-owner-id="aks"
|
||||||
|
- --txt-prefix=external-dns-
|
||||||
|
- --source=service
|
||||||
|
- --provider=azure
|
||||||
|
- --registry=txt
|
||||||
|
- --domain-filter=dev.company.com
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.WriteF("external-dns/assets/azure.json", assets)
|
||||||
|
th.WriteK("external-dns-private", `
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
nameSuffix: -private
|
||||||
|
commonLabels:
|
||||||
|
app: external-dns
|
||||||
|
instance: private
|
||||||
|
images:
|
||||||
|
- name: k8s.gcr.io/external-dns/external-dns
|
||||||
|
newName: xxx.azurecr.io/external-dns
|
||||||
|
newTag: v0.7.4_sylr.1
|
||||||
|
- name: quay.io/sylr/external-dns
|
||||||
|
newName: xxx.azurecr.io/external-dns
|
||||||
|
newTag: v0.7.4_sylr.1
|
||||||
|
secretGenerator:
|
||||||
|
- name: azure-config-file
|
||||||
|
behavior: replace
|
||||||
|
files:
|
||||||
|
- assets/azure.json
|
||||||
|
patches:
|
||||||
|
- target:
|
||||||
|
group: apps
|
||||||
|
version: v1
|
||||||
|
kind: Deployment
|
||||||
|
name: external-dns
|
||||||
|
patch: |-
|
||||||
|
- op: replace
|
||||||
|
path: /spec/template/spec/containers/0/args
|
||||||
|
value:
|
||||||
|
- --txt-owner-id="aks"
|
||||||
|
- --txt-prefix=external-dns-private-
|
||||||
|
- --source=service
|
||||||
|
- --provider=azure-private-dns
|
||||||
|
- --registry=txt
|
||||||
|
- --domain-filter=static.company.az
|
||||||
|
`)
|
||||||
|
th.WriteF("external-dns-private/assets/azure.json", assets)
|
||||||
|
th.WriteK("base", `
|
||||||
|
resources:
|
||||||
|
- clusterrole.yaml
|
||||||
|
- clusterrolebinding.yaml
|
||||||
|
- deployment.yaml
|
||||||
|
- serviceaccount.yaml
|
||||||
|
commonLabels:
|
||||||
|
app: external-dns
|
||||||
|
instance: public
|
||||||
|
images:
|
||||||
|
- name: k8s.gcr.io/external-dns/external-dns
|
||||||
|
newName: quay.io/sylr/external-dns
|
||||||
|
newTag: v0.7.4-73-g00a9a0c7
|
||||||
|
secretGenerator:
|
||||||
|
- name: azure-config-file
|
||||||
|
files:
|
||||||
|
- assets/azure.json
|
||||||
|
`)
|
||||||
|
th.WriteF("base/assets/azure.json", assets)
|
||||||
|
th.WriteF("base/clusterrolebinding.yaml", `
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRoleBinding
|
||||||
|
metadata:
|
||||||
|
name: external-dns-viewer
|
||||||
|
roleRef:
|
||||||
|
apiGroup: rbac.authorization.k8s.io
|
||||||
|
kind: ClusterRole
|
||||||
|
name: external-dns
|
||||||
|
subjects:
|
||||||
|
- kind: ServiceAccount
|
||||||
|
name: external-dns
|
||||||
|
`)
|
||||||
|
th.WriteF("base/clusterrole.yaml", `
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRole
|
||||||
|
metadata:
|
||||||
|
name: external-dns
|
||||||
|
rules:
|
||||||
|
- apiGroups: ['']
|
||||||
|
resources: ['endpoints', 'pods', 'services', 'nodes']
|
||||||
|
verbs: ['get', 'watch', 'list']
|
||||||
|
- apiGroups: ['extensions', 'networking.k8s.io']
|
||||||
|
resources: ['ingresses']
|
||||||
|
verbs: ['get', 'watch', 'list']
|
||||||
|
`)
|
||||||
|
th.WriteF("base/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: external-dns
|
||||||
|
spec:
|
||||||
|
strategy:
|
||||||
|
type: Recreate
|
||||||
|
selector:
|
||||||
|
matchLabels: {}
|
||||||
|
template:
|
||||||
|
metadata: {}
|
||||||
|
spec:
|
||||||
|
serviceAccountName: external-dns
|
||||||
|
containers:
|
||||||
|
- name: external-dns
|
||||||
|
image: k8s.gcr.io/external-dns/external-dns
|
||||||
|
args:
|
||||||
|
- --domain-filter=""
|
||||||
|
- --txt-owner-id=""
|
||||||
|
- --txt-prefix=external-dns-
|
||||||
|
- --source=service
|
||||||
|
- --provider=azure
|
||||||
|
- --registry=txt
|
||||||
|
resources: {}
|
||||||
|
volumeMounts:
|
||||||
|
- name: azure-config-file
|
||||||
|
mountPath: /etc/kubernetes
|
||||||
|
readOnly: true
|
||||||
|
volumes:
|
||||||
|
- name: azure-config-file
|
||||||
|
secret:
|
||||||
|
secretName: azure-config-file
|
||||||
|
`)
|
||||||
|
th.WriteF("base/serviceaccount.yaml", `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ServiceAccount
|
||||||
|
metadata:
|
||||||
|
name: external-dns
|
||||||
|
`)
|
||||||
|
opts := th.MakeDefaultOptions()
|
||||||
|
m := th.Run(".", opts)
|
||||||
|
expFmt := `
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRole
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: public
|
||||||
|
name: external-dns
|
||||||
|
rules:
|
||||||
|
- apiGroups:
|
||||||
|
- ""
|
||||||
|
resources:
|
||||||
|
- endpoints
|
||||||
|
- pods
|
||||||
|
- services
|
||||||
|
- nodes
|
||||||
|
verbs:
|
||||||
|
- get
|
||||||
|
- watch
|
||||||
|
- list
|
||||||
|
- apiGroups:
|
||||||
|
- extensions
|
||||||
|
- networking.k8s.io
|
||||||
|
resources:
|
||||||
|
- ingresses
|
||||||
|
verbs:
|
||||||
|
- get
|
||||||
|
- watch
|
||||||
|
- list
|
||||||
|
---
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRoleBinding
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: public
|
||||||
|
name: external-dns-viewer
|
||||||
|
roleRef:
|
||||||
|
apiGroup: rbac.authorization.k8s.io
|
||||||
|
kind: ClusterRole
|
||||||
|
name: external-dns
|
||||||
|
subjects:
|
||||||
|
- kind: ServiceAccount
|
||||||
|
name: external-dns
|
||||||
|
namespace: kube-system
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: public
|
||||||
|
name: external-dns
|
||||||
|
namespace: kube-system
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: external-dns
|
||||||
|
instance: public
|
||||||
|
strategy:
|
||||||
|
type: Recreate
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: public
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- args:
|
||||||
|
- --txt-owner-id="aks"
|
||||||
|
- --txt-prefix=external-dns-
|
||||||
|
- --source=service
|
||||||
|
- --provider=azure
|
||||||
|
- --registry=txt
|
||||||
|
- --domain-filter=dev.company.com
|
||||||
|
image: xxx.azurecr.io/external-dns:v0.7.4_sylr.1
|
||||||
|
name: external-dns
|
||||||
|
resources: {}
|
||||||
|
volumeMounts:
|
||||||
|
- mountPath: /etc/kubernetes
|
||||||
|
name: azure-config-file
|
||||||
|
readOnly: true
|
||||||
|
serviceAccountName: external-dns
|
||||||
|
volumes:
|
||||||
|
- name: azure-config-file
|
||||||
|
secret:
|
||||||
|
secretName: azure-config-file-%s
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ServiceAccount
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: public
|
||||||
|
name: external-dns
|
||||||
|
namespace: kube-system
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
data:
|
||||||
|
azure.json: %s
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: public
|
||||||
|
name: azure-config-file-%s
|
||||||
|
namespace: kube-system
|
||||||
|
type: Opaque
|
||||||
|
---
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRole
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: private
|
||||||
|
name: external-dns-private
|
||||||
|
rules:
|
||||||
|
- apiGroups:
|
||||||
|
- ""
|
||||||
|
resources:
|
||||||
|
- endpoints
|
||||||
|
- pods
|
||||||
|
- services
|
||||||
|
- nodes
|
||||||
|
verbs:
|
||||||
|
- get
|
||||||
|
- watch
|
||||||
|
- list
|
||||||
|
- apiGroups:
|
||||||
|
- extensions
|
||||||
|
- networking.k8s.io
|
||||||
|
resources:
|
||||||
|
- ingresses
|
||||||
|
verbs:
|
||||||
|
- get
|
||||||
|
- watch
|
||||||
|
- list
|
||||||
|
---
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRoleBinding
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: private
|
||||||
|
name: external-dns-viewer-private
|
||||||
|
roleRef:
|
||||||
|
apiGroup: rbac.authorization.k8s.io
|
||||||
|
kind: ClusterRole
|
||||||
|
name: external-dns-private
|
||||||
|
subjects:
|
||||||
|
- kind: ServiceAccount
|
||||||
|
name: external-dns-private
|
||||||
|
namespace: kube-system
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: private
|
||||||
|
name: external-dns-private
|
||||||
|
namespace: kube-system
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: external-dns
|
||||||
|
instance: private
|
||||||
|
strategy:
|
||||||
|
type: Recreate
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: private
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- args:
|
||||||
|
- --txt-owner-id="aks"
|
||||||
|
- --txt-prefix=external-dns-private-
|
||||||
|
- --source=service
|
||||||
|
- --provider=azure-private-dns
|
||||||
|
- --registry=txt
|
||||||
|
- --domain-filter=static.company.az
|
||||||
|
image: xxx.azurecr.io/external-dns:v0.7.4_sylr.1
|
||||||
|
name: external-dns
|
||||||
|
resources: {}
|
||||||
|
volumeMounts:
|
||||||
|
- mountPath: /etc/kubernetes
|
||||||
|
name: azure-config-file
|
||||||
|
readOnly: true
|
||||||
|
serviceAccountName: external-dns-private
|
||||||
|
volumes:
|
||||||
|
- name: azure-config-file
|
||||||
|
secret:
|
||||||
|
secretName: azure-config-file-private-%s
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ServiceAccount
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: private
|
||||||
|
name: external-dns-private
|
||||||
|
namespace: kube-system
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
data:
|
||||||
|
azure.json: %s
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: external-dns
|
||||||
|
instance: private
|
||||||
|
name: azure-config-file-private-%s
|
||||||
|
namespace: kube-system
|
||||||
|
type: Opaque
|
||||||
|
`
|
||||||
|
const (
|
||||||
|
nameHashKyaml = "66cc4224mm"
|
||||||
|
contentKyaml = `|
|
||||||
|
ewoJInRlbmFudElkIjogIlhYWFhYLVhYWFhYWC1YWFhYWC1YWFhYWFgtWFhYWFhYIiwKCS
|
||||||
|
JzdWJzY3JpcHRpb25JZCI6ICJYWFhYWC1YWFhYWFgtWFhYWFgtWFhYWFhYLVhYWFhYWCIs
|
||||||
|
CgkicmVzb3VyY2VHcm91cCI6ICJETlMtRVVXLVhYWC1SRyIsCgkidXNlTWFuYWdlZElkZW
|
||||||
|
50aXR5RXh0ZW5zaW9uIjogdHJ1ZSwKCSJ1c2VyQXNzaWduZWRJZGVudGl0eUlEIjogIlhY
|
||||||
|
WFhYLVhYWFhYWC1YWFhYWC1YWFhYWFgtWFhYWFhYIgp9Cg==`
|
||||||
|
nameHashApiMach = "g2k4bkgt4d"
|
||||||
|
// nolint: lll
|
||||||
|
contentApiMach = `ewoJInRlbmFudElkIjogIlhYWFhYLVhYWFhYWC1YWFhYWC1YWFhYWFgtWFhYWFhYIiwKCSJzdWJzY3JpcHRpb25JZCI6ICJYWFhYWC1YWFhYWFgtWFhYWFgtWFhYWFhYLVhYWFhYWCIsCgkicmVzb3VyY2VHcm91cCI6ICJETlMtRVVXLVhYWC1SRyIsCgkidXNlTWFuYWdlZElkZW50aXR5RXh0ZW5zaW9uIjogdHJ1ZSwKCSJ1c2VyQXNzaWduZWRJZGVudGl0eUlEIjogIlhYWFhYLVhYWFhYWC1YWFhYWC1YWFhYWFgtWFhYWFhYIgp9Cg==`
|
||||||
|
)
|
||||||
|
th.AssertActualEqualsExpected(
|
||||||
|
m,
|
||||||
|
// TODO(#3304): DECISION - kyaml better; not a bug.
|
||||||
|
opts.IfApiMachineryElseKyaml(
|
||||||
|
fmt.Sprintf(expFmt,
|
||||||
|
nameHashApiMach,
|
||||||
|
contentApiMach, nameHashApiMach,
|
||||||
|
nameHashApiMach,
|
||||||
|
contentApiMach, nameHashApiMach),
|
||||||
|
fmt.Sprintf(expFmt,
|
||||||
|
nameHashKyaml,
|
||||||
|
contentKyaml, nameHashKyaml,
|
||||||
|
nameHashKyaml,
|
||||||
|
contentKyaml, nameHashKyaml)))
|
||||||
|
}
|
||||||
|
|
||||||
func TestEmptyFieldSpecValue(t *testing.T) {
|
func TestEmptyFieldSpecValue(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
th.WriteK("/app", `
|
th.WriteK("/app", `
|
||||||
|
|||||||
@@ -50,10 +50,10 @@ resources:
|
|||||||
- secrets.yaml
|
- secrets.yaml
|
||||||
- role.yaml
|
- role.yaml
|
||||||
`)
|
`)
|
||||||
// This validates Fix #1444. This should not be an error anymore -
|
// This validates fix for Issue #1044. This should not be an error anymore -
|
||||||
// the secrets have the same name but are in different namespaces.
|
// the secrets have the same name but are in different namespaces.
|
||||||
// The ClusterRole (by def) is not in a namespace,
|
// The ClusterRole (by def) is not in a namespace,
|
||||||
// an in this case applies to *any* Secret resource
|
// and in this case applies to *any* Secret resource
|
||||||
// named "dummy"
|
// named "dummy"
|
||||||
m := th.Run("/app", th.MakeDefaultOptions())
|
m := th.Run("/app", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
@@ -91,13 +91,134 @@ rules:
|
|||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNameReferenceDeploymentIssue3489(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("base", `
|
||||||
|
resources:
|
||||||
|
- cm.yaml
|
||||||
|
- dep.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("base/cm.yaml", `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: myMap
|
||||||
|
`)
|
||||||
|
th.WriteF("base/dep.yaml", `
|
||||||
|
apiVersion: v1
|
||||||
|
group: apps
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDep
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- name: CM_FOO
|
||||||
|
valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
key: foo
|
||||||
|
name: myMap
|
||||||
|
`)
|
||||||
|
th.WriteK("ov1", `
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
namePrefix: pp-
|
||||||
|
`)
|
||||||
|
th.WriteK("ov2", `
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
nameSuffix: -ss
|
||||||
|
`)
|
||||||
|
th.WriteK("ov3", `
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
namespace: fred
|
||||||
|
nameSuffix: -xx
|
||||||
|
`)
|
||||||
|
th.WriteK(".", `
|
||||||
|
resources:
|
||||||
|
- ../ov1
|
||||||
|
- ../ov2
|
||||||
|
- ../ov3
|
||||||
|
`)
|
||||||
|
m := th.Run(".", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: pp-myMap
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
group: apps
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: pp-myDep
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- name: CM_FOO
|
||||||
|
valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
key: foo
|
||||||
|
name: pp-myMap
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: myMap-ss
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
group: apps
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDep-ss
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- name: CM_FOO
|
||||||
|
valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
key: foo
|
||||||
|
name: myMap-ss
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: myMap-xx
|
||||||
|
namespace: fred
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
group: apps
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDep-xx
|
||||||
|
namespace: fred
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- name: CM_FOO
|
||||||
|
valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
key: foo
|
||||||
|
name: myMap-xx
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
// TestNameAndNsTransformation validates that NamespaceTransformer,
|
// TestNameAndNsTransformation validates that NamespaceTransformer,
|
||||||
// PrefixSuffixTransformer and namereference transformers are
|
// PrefixSuffixTransformer and namereference transformers are
|
||||||
// able to deal with simultaneous change of namespace and name.
|
// able to deal with simultaneous change of namespace and name.
|
||||||
func TestNameAndNsTransformation(t *testing.T) {
|
func TestNameAndNsTransformation(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
th.WriteK("/nameandns", `
|
th.WriteK(".", `
|
||||||
namePrefix: p1-
|
namePrefix: p1-
|
||||||
nameSuffix: -s1
|
nameSuffix: -s1
|
||||||
namespace: newnamespace
|
namespace: newnamespace
|
||||||
@@ -105,7 +226,7 @@ resources:
|
|||||||
- resources.yaml
|
- resources.yaml
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteF("/nameandns/resources.yaml", `
|
th.WriteF("resources.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
@@ -204,7 +325,7 @@ kind: PersistentVolume
|
|||||||
metadata:
|
metadata:
|
||||||
name: pv1
|
name: pv1
|
||||||
`)
|
`)
|
||||||
m := th.Run("/nameandns", th.MakeDefaultOptions())
|
m := th.Run(".", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
|
|||||||
301
api/krusty/openapiversion_test.go
Normal file
301
api/krusty/openapiversion_test.go
Normal file
@@ -0,0 +1,301 @@
|
|||||||
|
package krusty_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestOpenApiFieldBasicUsage(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("/app", `
|
||||||
|
openapi:
|
||||||
|
version: v1.18.8
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
|
||||||
|
m := th.Run("/app", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
assert.Equal(t, "v1188", openapi.GetSchemaVersion())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenApiFieldNotBuiltin(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("/app", `
|
||||||
|
openapi:
|
||||||
|
version: v1.14.1
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
|
||||||
|
err := th.RunWithErr("/app", th.MakeDefaultOptions())
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenApiFieldDefaultVersion(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("/app", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
|
||||||
|
m := th.Run("/app", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
assert.Equal(t, kubernetesapi.DefaultOpenAPI, openapi.GetSchemaVersion())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenApiFieldFromBase(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("base", `
|
||||||
|
openapi:
|
||||||
|
version: v1.19.0
|
||||||
|
namePrefix: a-
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("base/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
th.WriteK("overlay", `
|
||||||
|
namePrefix: b-
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
patchesStrategicMerge:
|
||||||
|
- depPatch.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("overlay/depPatch.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
`)
|
||||||
|
m := th.Run("overlay", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: b-a-myDeployment
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
assert.Equal(t, "v1190", openapi.GetSchemaVersion())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenApiFieldFromOverlay(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("base", `
|
||||||
|
namePrefix: a-
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("base/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
th.WriteK("overlay", `
|
||||||
|
openapi:
|
||||||
|
version: v1.18.8
|
||||||
|
namePrefix: b-
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
patchesStrategicMerge:
|
||||||
|
- depPatch.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("overlay/depPatch.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
`)
|
||||||
|
m := th.Run("overlay", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: b-a-myDeployment
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
assert.Equal(t, "v1188", openapi.GetSchemaVersion())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenApiFieldOverlayTakesPrecedence(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("base", `
|
||||||
|
openapi:
|
||||||
|
version: v1.19.0
|
||||||
|
namePrefix: a-
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("base/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
th.WriteK("overlay", `
|
||||||
|
openapi:
|
||||||
|
version: v1.18.8
|
||||||
|
namePrefix: b-
|
||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
patchesStrategicMerge:
|
||||||
|
- depPatch.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("overlay/depPatch.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: myDeployment
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
`)
|
||||||
|
m := th.Run("overlay", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: b-a-myDeployment
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: whatever
|
||||||
|
`)
|
||||||
|
assert.Equal(t, "v1188", openapi.GetSchemaVersion())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenAPIFieldFromComponentDefault(t *testing.T) {
|
||||||
|
input := []FileGen{writeTestBase, writeTestComponent, writeOverlayProd}
|
||||||
|
runPath := "/app/prod"
|
||||||
|
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
for _, f := range input {
|
||||||
|
f(th)
|
||||||
|
}
|
||||||
|
th.Run(runPath, th.MakeDefaultOptions())
|
||||||
|
assert.Equal(t, kubernetesapi.DefaultOpenAPI, openapi.GetSchemaVersion())
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeTestComponentWithOlderOpenAPIVersion(th kusttest_test.Harness) {
|
||||||
|
th.WriteC("/app/comp", `
|
||||||
|
openapi:
|
||||||
|
version: v1.18.8
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/comp/stub.yaml", `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: stub
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenAPIFieldFromComponent(t *testing.T) {
|
||||||
|
input := []FileGen{
|
||||||
|
writeTestBase,
|
||||||
|
writeTestComponentWithOlderOpenAPIVersion,
|
||||||
|
writeOverlayProd}
|
||||||
|
runPath := "/app/prod"
|
||||||
|
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
for _, f := range input {
|
||||||
|
f(th)
|
||||||
|
}
|
||||||
|
th.Run(runPath, th.MakeDefaultOptions())
|
||||||
|
assert.Equal(t, "v1188", openapi.GetSchemaVersion())
|
||||||
|
}
|
||||||
@@ -69,28 +69,6 @@ spec:
|
|||||||
image: helloworld
|
image: helloworld
|
||||||
name: whatever
|
name: whatever
|
||||||
`
|
`
|
||||||
// Allow expected variable to be unused
|
|
||||||
_ = expected
|
|
||||||
|
|
||||||
// Currently, kustomize inserts $patch: delete elements into the resulting resources
|
|
||||||
erroneousActual := `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: whatever
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- env:
|
|
||||||
- $patch: delete
|
|
||||||
name: NOT_EXISTING_FOR_REMOVAL
|
|
||||||
- name: EXISTING
|
|
||||||
value: EXISTING_VALUE
|
|
||||||
image: helloworld
|
|
||||||
name: whatever
|
|
||||||
`
|
|
||||||
|
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
m := th.Run(".", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, erroneousActual)
|
th.AssertActualEqualsExpected(m, expected)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -362,7 +362,7 @@ metadata:
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected error")
|
t.Fatalf("expected error")
|
||||||
}
|
}
|
||||||
if !strings.Contains(err.Error(), "multiple matches for ~G_v1_ServiceAccount|~X|serviceaccount") {
|
if !strings.Contains(err.Error(), "found multiple possible referrals") {
|
||||||
t.Fatalf("unexpected error %v", err)
|
t.Fatalf("unexpected error %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ func (m *merginator) ConflatePatches(in []*resource.Resource) (ResMap, error) {
|
|||||||
|
|
||||||
func (m *merginator) appendIfNoMatch(index int) (*resource.Resource, error) {
|
func (m *merginator) appendIfNoMatch(index int) (*resource.Resource, error) {
|
||||||
candidate := m.incoming[index]
|
candidate := m.incoming[index]
|
||||||
matchedResources := m.result.GetMatchingResourcesByOriginalId(
|
matchedResources := m.result.GetMatchingResourcesByAnyId(
|
||||||
candidate.OrgId().Equals)
|
candidate.OrgId().Equals)
|
||||||
if len(matchedResources) == 0 {
|
if len(matchedResources) == 0 {
|
||||||
m.result.Append(candidate)
|
m.result.Append(candidate)
|
||||||
|
|||||||
@@ -142,24 +142,18 @@ type ResMap interface {
|
|||||||
// who's CurId is matched by the argument.
|
// who's CurId is matched by the argument.
|
||||||
GetMatchingResourcesByCurrentId(matches IdMatcher) []*resource.Resource
|
GetMatchingResourcesByCurrentId(matches IdMatcher) []*resource.Resource
|
||||||
|
|
||||||
// GetMatchingResourcesByOriginalId returns the resources
|
// GetMatchingResourcesByAnyId returns the resources
|
||||||
// who's OriginalId is matched by the argument.
|
// who's current or previous IDs is matched by the argument.
|
||||||
GetMatchingResourcesByOriginalId(matches IdMatcher) []*resource.Resource
|
GetMatchingResourcesByAnyId(matches IdMatcher) []*resource.Resource
|
||||||
|
|
||||||
// GetByCurrentId is shorthand for calling
|
// GetByCurrentId is shorthand for calling
|
||||||
// GetMatchingResourcesByCurrentId with a matcher requiring
|
// GetMatchingResourcesByCurrentId with a matcher requiring
|
||||||
// an exact match, returning an error on multiple or no matches.
|
// an exact match, returning an error on multiple or no matches.
|
||||||
GetByCurrentId(resid.ResId) (*resource.Resource, error)
|
GetByCurrentId(resid.ResId) (*resource.Resource, error)
|
||||||
|
|
||||||
// GetByOriginalId is shorthand for calling
|
// GetById is shorthand for calling
|
||||||
// GetMatchingResourcesByOriginalId with a matcher requiring
|
// GetMatchingResourcesByAnyId with a matcher requiring
|
||||||
// an exact match, returning an error on multiple or no matches.
|
// an exact match, returning an error on multiple or no matches.
|
||||||
GetByOriginalId(resid.ResId) (*resource.Resource, error)
|
|
||||||
|
|
||||||
// GetById is a helper function which first
|
|
||||||
// attempts GetByOriginalId, then GetByCurrentId,
|
|
||||||
// returning an error if both fail to find a single
|
|
||||||
// match.
|
|
||||||
GetById(resid.ResId) (*resource.Resource, error)
|
GetById(resid.ResId) (*resource.Resource, error)
|
||||||
|
|
||||||
// GroupedByCurrentNamespace returns a map of namespace
|
// GroupedByCurrentNamespace returns a map of namespace
|
||||||
|
|||||||
@@ -155,8 +155,7 @@ func (m *resWrangler) GetIndexOfCurrentId(id resid.ResId) (int, error) {
|
|||||||
|
|
||||||
type IdFromResource func(r *resource.Resource) resid.ResId
|
type IdFromResource func(r *resource.Resource) resid.ResId
|
||||||
|
|
||||||
func GetOriginalId(r *resource.Resource) resid.ResId { return r.OrgId() }
|
func GetCurrentId(r *resource.Resource) resid.ResId { return r.CurId() }
|
||||||
func GetCurrentId(r *resource.Resource) resid.ResId { return r.CurId() }
|
|
||||||
|
|
||||||
// GetMatchingResourcesByCurrentId implements ResMap.
|
// GetMatchingResourcesByCurrentId implements ResMap.
|
||||||
func (m *resWrangler) GetMatchingResourcesByCurrentId(
|
func (m *resWrangler) GetMatchingResourcesByCurrentId(
|
||||||
@@ -164,10 +163,19 @@ func (m *resWrangler) GetMatchingResourcesByCurrentId(
|
|||||||
return m.filteredById(matches, GetCurrentId)
|
return m.filteredById(matches, GetCurrentId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMatchingResourcesByOriginalId implements ResMap.
|
// GetMatchingResourcesByAnyId implements ResMap.
|
||||||
func (m *resWrangler) GetMatchingResourcesByOriginalId(
|
func (m *resWrangler) GetMatchingResourcesByAnyId(
|
||||||
matches IdMatcher) []*resource.Resource {
|
matches IdMatcher) []*resource.Resource {
|
||||||
return m.filteredById(matches, GetOriginalId)
|
var result []*resource.Resource
|
||||||
|
for _, r := range m.rList {
|
||||||
|
for _, id := range append(r.PrevIds(), r.CurId()) {
|
||||||
|
if matches(id) {
|
||||||
|
result = append(result, r)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *resWrangler) filteredById(
|
func (m *resWrangler) filteredById(
|
||||||
@@ -187,26 +195,16 @@ func (m *resWrangler) GetByCurrentId(
|
|||||||
return demandOneMatch(m.GetMatchingResourcesByCurrentId, id, "Current")
|
return demandOneMatch(m.GetMatchingResourcesByCurrentId, id, "Current")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetByOriginalId implements ResMap.
|
|
||||||
func (m *resWrangler) GetByOriginalId(
|
|
||||||
id resid.ResId) (*resource.Resource, error) {
|
|
||||||
return demandOneMatch(m.GetMatchingResourcesByOriginalId, id, "Original")
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetById implements ResMap.
|
// GetById implements ResMap.
|
||||||
func (m *resWrangler) GetById(
|
func (m *resWrangler) GetById(
|
||||||
id resid.ResId) (*resource.Resource, error) {
|
id resid.ResId) (*resource.Resource, error) {
|
||||||
match, err1 := m.GetByOriginalId(id)
|
r, err := demandOneMatch(m.GetMatchingResourcesByAnyId, id, "Id")
|
||||||
if err1 == nil {
|
if err != nil {
|
||||||
return match, nil
|
return nil, fmt.Errorf(
|
||||||
|
"%s; failed to find unique target for patch %s",
|
||||||
|
err.Error(), id.GvknString())
|
||||||
}
|
}
|
||||||
match, err2 := m.GetByCurrentId(id)
|
return r, nil
|
||||||
if err2 == nil {
|
|
||||||
return match, nil
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"%s; %s; failed to find unique target for patch %s",
|
|
||||||
err1.Error(), err2.Error(), id.GvknString())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type resFinder func(IdMatcher) []*resource.Resource
|
type resFinder func(IdMatcher) []*resource.Resource
|
||||||
@@ -218,7 +216,7 @@ func demandOneMatch(
|
|||||||
return r[0], nil
|
return r[0], nil
|
||||||
}
|
}
|
||||||
if len(r) > 1 {
|
if len(r) > 1 {
|
||||||
return nil, fmt.Errorf("multiple matches for %sId %s", s, id)
|
return nil, fmt.Errorf("multiple matches for %s %s", s, id)
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("no matches for %sId %s", s, id)
|
return nil, fmt.Errorf("no matches for %sId %s", s, id)
|
||||||
}
|
}
|
||||||
@@ -272,9 +270,9 @@ func (m *resWrangler) AsYaml() ([]byte, error) {
|
|||||||
var b []byte
|
var b []byte
|
||||||
buf := bytes.NewBuffer(b)
|
buf := bytes.NewBuffer(b)
|
||||||
for _, res := range m.Resources() {
|
for _, res := range m.Resources() {
|
||||||
out, err := yaml.Marshal(res.Map())
|
out, err := res.AsYAML()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.Wrapf(err, "%#v", res.Map())
|
||||||
}
|
}
|
||||||
if firstObj {
|
if firstObj {
|
||||||
firstObj = false
|
firstObj = false
|
||||||
@@ -377,52 +375,59 @@ func (m *resWrangler) makeCopy(copier resCopier) ResMap {
|
|||||||
|
|
||||||
// SubsetThatCouldBeReferencedByResource implements ResMap.
|
// SubsetThatCouldBeReferencedByResource implements ResMap.
|
||||||
func (m *resWrangler) SubsetThatCouldBeReferencedByResource(
|
func (m *resWrangler) SubsetThatCouldBeReferencedByResource(
|
||||||
inputRes *resource.Resource) ResMap {
|
referrer *resource.Resource) ResMap {
|
||||||
|
referrerId := referrer.CurId()
|
||||||
|
if !referrerId.IsNamespaceableKind() {
|
||||||
|
// A cluster scoped resource can refer to anything.
|
||||||
|
return m
|
||||||
|
}
|
||||||
result := newOne()
|
result := newOne()
|
||||||
inputId := inputRes.CurId()
|
roleBindingNamespaces := getNamespacesForRoleBinding(referrer)
|
||||||
isInputIdNamespaceable := inputId.IsNamespaceableKind()
|
for _, possibleTarget := range m.Resources() {
|
||||||
subjectNamespaces := getNamespacesForRoleBinding(inputRes)
|
id := possibleTarget.CurId()
|
||||||
for _, r := range m.Resources() {
|
if !id.IsNamespaceableKind() {
|
||||||
// Need to match more accuratly both at the time of selection and transformation.
|
// A cluster-scoped resource can be referred to by anything.
|
||||||
// OutmostPrefixSuffixEquals is not accurate enough since it is only using
|
result.append(possibleTarget)
|
||||||
// the outer most suffix and the last prefix. Use PrefixedSuffixesEquals instead.
|
continue
|
||||||
resId := r.CurId()
|
}
|
||||||
if !isInputIdNamespaceable || !resId.IsNamespaceableKind() || resId.IsNsEquals(inputId) ||
|
if id.IsNsEquals(referrerId) {
|
||||||
isRoleBindingNamespace(&subjectNamespaces, r.GetNamespace()) {
|
// The two objects are in the same namespace.
|
||||||
result.append(r)
|
result.append(possibleTarget)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// The two objects are namespaced (not cluster-scoped), AND
|
||||||
|
// are in different namespaces.
|
||||||
|
// There's still a chance they can refer to each other.
|
||||||
|
ns := possibleTarget.GetNamespace()
|
||||||
|
if roleBindingNamespaces[ns] {
|
||||||
|
result.append(possibleTarget)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// isRoleBindingNamespace returns true is the namespace `ns` is in role binding
|
// getNamespacesForRoleBinding returns referenced ServiceAccount namespaces
|
||||||
// namespaces `m`
|
// if the resource is a RoleBinding
|
||||||
func isRoleBindingNamespace(m *map[string]bool, ns string) bool {
|
func getNamespacesForRoleBinding(r *resource.Resource) map[string]bool {
|
||||||
return (*m)[ns]
|
result := make(map[string]bool)
|
||||||
}
|
if r.GetKind() != "RoleBinding" {
|
||||||
|
return result
|
||||||
// getNamespacesForRoleBinding returns referenced ServiceAccount namespaces if the inputRes is
|
|
||||||
// a RoleBinding
|
|
||||||
func getNamespacesForRoleBinding(inputRes *resource.Resource) map[string]bool {
|
|
||||||
res := make(map[string]bool)
|
|
||||||
if inputRes.GetKind() != "RoleBinding" {
|
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
subjects, err := inputRes.GetSlice("subjects")
|
subjects, err := r.GetSlice("subjects")
|
||||||
if err != nil || subjects == nil {
|
if err != nil || subjects == nil {
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, s := range subjects {
|
for _, s := range subjects {
|
||||||
subject := s.(map[string]interface{})
|
subject := s.(map[string]interface{})
|
||||||
if subject["namespace"] == nil || subject["kind"] == nil ||
|
if ns, ok1 := subject["namespace"]; ok1 {
|
||||||
subject["kind"].(string) != "ServiceAccount" {
|
if kind, ok2 := subject["kind"]; ok2 {
|
||||||
continue
|
if kind.(string) == "ServiceAccount" {
|
||||||
|
result[ns.(string)] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
res[subject["namespace"].(string)] = true
|
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *resWrangler) append(res *resource.Resource) {
|
func (m *resWrangler) append(res *resource.Resource) {
|
||||||
@@ -458,10 +463,7 @@ func (m *resWrangler) AbsorbAll(other ResMap) error {
|
|||||||
|
|
||||||
func (m *resWrangler) appendReplaceOrMerge(res *resource.Resource) error {
|
func (m *resWrangler) appendReplaceOrMerge(res *resource.Resource) error {
|
||||||
id := res.CurId()
|
id := res.CurId()
|
||||||
matches := m.GetMatchingResourcesByOriginalId(id.Equals)
|
matches := m.GetMatchingResourcesByAnyId(id.Equals)
|
||||||
if len(matches) == 0 {
|
|
||||||
matches = m.GetMatchingResourcesByCurrentId(id.Equals)
|
|
||||||
}
|
|
||||||
switch len(matches) {
|
switch len(matches) {
|
||||||
case 0:
|
case 0:
|
||||||
switch res.Behavior() {
|
switch res.Behavior() {
|
||||||
@@ -586,10 +588,8 @@ func (m *resWrangler) ApplySmPatch(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
patchCopy := patch.DeepCopy()
|
patchCopy := patch.DeepCopy()
|
||||||
patchCopy.SetName(res.GetName())
|
patchCopy.CopyMergeMetaDataFieldsFrom(patch)
|
||||||
patchCopy.SetNamespace(res.GetNamespace())
|
|
||||||
patchCopy.SetGvk(res.GetGvk())
|
patchCopy.SetGvk(res.GetGvk())
|
||||||
patchCopy.SetOriginalName(res.GetOriginalName(), true)
|
|
||||||
err := res.ApplySmPatch(patchCopy)
|
err := res.ApplySmPatch(patchCopy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Check for an error string from UnmarshalJSON that's indicative
|
// Check for an error string from UnmarshalJSON that's indicative
|
||||||
|
|||||||
@@ -331,6 +331,134 @@ func TestGetMatchingResourcesByCurrentId(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetMatchingResourcesByAnyId(t *testing.T) {
|
||||||
|
r1 := rf.FromMap(
|
||||||
|
map[string]interface{}{
|
||||||
|
"apiVersion": "v1",
|
||||||
|
"kind": "ConfigMap",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"name": "new-alice",
|
||||||
|
"annotations": map[string]interface{}{
|
||||||
|
"config.kubernetes.io/previousNames": "alice",
|
||||||
|
"config.kubernetes.io/previousNamespaces": "default",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
r2 := rf.FromMap(
|
||||||
|
map[string]interface{}{
|
||||||
|
"apiVersion": "v1",
|
||||||
|
"kind": "ConfigMap",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"name": "new-bob",
|
||||||
|
"annotations": map[string]interface{}{
|
||||||
|
"config.kubernetes.io/previousNames": "bob,bob2",
|
||||||
|
"config.kubernetes.io/previousNamespaces": "default,default",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
r3 := rf.FromMap(
|
||||||
|
map[string]interface{}{
|
||||||
|
"apiVersion": "v1",
|
||||||
|
"kind": "ConfigMap",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"name": "new-bob",
|
||||||
|
"namespace": "new-happy",
|
||||||
|
"annotations": map[string]interface{}{
|
||||||
|
"config.kubernetes.io/previousNames": "bob",
|
||||||
|
"config.kubernetes.io/previousNamespaces": "happy",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
r4 := rf.FromMap(
|
||||||
|
map[string]interface{}{
|
||||||
|
"apiVersion": "v1",
|
||||||
|
"kind": "ConfigMap",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"name": "charlie",
|
||||||
|
"namespace": "happy",
|
||||||
|
"annotations": map[string]interface{}{
|
||||||
|
"config.kubernetes.io/previousNames": "charlie",
|
||||||
|
"config.kubernetes.io/previousNamespaces": "default",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
r5 := rf.FromMap(
|
||||||
|
map[string]interface{}{
|
||||||
|
"apiVersion": "v1",
|
||||||
|
"kind": "Deployment",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"name": "charlie",
|
||||||
|
"namespace": "happy",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
m := resmaptest_test.NewRmBuilder(t, rf).
|
||||||
|
AddR(r1).AddR(r2).AddR(r3).AddR(r4).AddR(r5).ResMap()
|
||||||
|
|
||||||
|
// nolint:goconst
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
matcher IdMatcher
|
||||||
|
count int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"match everything",
|
||||||
|
func(resid.ResId) bool { return true },
|
||||||
|
5,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match nothing",
|
||||||
|
func(resid.ResId) bool { return false },
|
||||||
|
0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name is alice",
|
||||||
|
func(x resid.ResId) bool { return x.Name == "alice" },
|
||||||
|
1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name is charlie",
|
||||||
|
func(x resid.ResId) bool { return x.Name == "charlie" },
|
||||||
|
2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name is bob",
|
||||||
|
func(x resid.ResId) bool { return x.Name == "bob" },
|
||||||
|
2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"happy namespace",
|
||||||
|
func(x resid.ResId) bool {
|
||||||
|
return x.Namespace == "happy"
|
||||||
|
},
|
||||||
|
3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"happy deployment",
|
||||||
|
func(x resid.ResId) bool {
|
||||||
|
return x.Namespace == "happy" &&
|
||||||
|
x.Gvk.Kind == "Deployment"
|
||||||
|
},
|
||||||
|
1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"happy ConfigMap",
|
||||||
|
func(x resid.ResId) bool {
|
||||||
|
return x.Namespace == "happy" &&
|
||||||
|
x.Gvk.Kind == "ConfigMap"
|
||||||
|
},
|
||||||
|
2,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tst := range tests {
|
||||||
|
result := m.GetMatchingResourcesByAnyId(tst.matcher)
|
||||||
|
if len(result) != tst.count {
|
||||||
|
t.Fatalf("test '%s'; actual: %d, expected: %d",
|
||||||
|
tst.name, len(result), tst.count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSubsetThatCouldBeReferencedByResource(t *testing.T) {
|
func TestSubsetThatCouldBeReferencedByResource(t *testing.T) {
|
||||||
r1 := rf.FromMap(
|
r1 := rf.FromMap(
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/internal/kusterr"
|
"sigs.k8s.io/kustomize/api/internal/kusterr"
|
||||||
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,17 +36,12 @@ func (rf *Factory) FromMap(m map[string]interface{}) *Resource {
|
|||||||
|
|
||||||
// FromMapWithName returns a new instance with the given "original" name.
|
// FromMapWithName returns a new instance with the given "original" name.
|
||||||
func (rf *Factory) FromMapWithName(n string, m map[string]interface{}) *Resource {
|
func (rf *Factory) FromMapWithName(n string, m map[string]interface{}) *Resource {
|
||||||
return rf.makeOne(rf.kf.FromMap(m), nil).SetOriginalName(n, true)
|
return rf.FromMapWithNamespaceAndName(resid.DefaultNamespace, n, m)
|
||||||
}
|
|
||||||
|
|
||||||
// FromMapWithNamespace returns a new instance with the given "original" namespace.
|
|
||||||
func (rf *Factory) FromMapWithNamespace(n string, m map[string]interface{}) *Resource {
|
|
||||||
return rf.makeOne(rf.kf.FromMap(m), nil).SetOriginalNs(n, true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromMapWithNamespaceAndName returns a new instance with the given "original" namespace.
|
// FromMapWithNamespaceAndName returns a new instance with the given "original" namespace.
|
||||||
func (rf *Factory) FromMapWithNamespaceAndName(ns string, n string, m map[string]interface{}) *Resource {
|
func (rf *Factory) FromMapWithNamespaceAndName(ns string, n string, m map[string]interface{}) *Resource {
|
||||||
return rf.makeOne(rf.kf.FromMap(m), nil).SetOriginalNs(ns, true).SetOriginalName(n, true)
|
return rf.makeOne(rf.kf.FromMap(m), nil).setPreviousNamespaceAndName(ns, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromMapAndOption returns a new instance of Resource with given options.
|
// FromMapAndOption returns a new instance of Resource with given options.
|
||||||
@@ -157,7 +153,7 @@ func (rf *Factory) SliceFromBytesWithNames(names []string, in []byte) ([]*Resour
|
|||||||
return nil, fmt.Errorf("number of names doesn't match number of resources")
|
return nil, fmt.Errorf("number of names doesn't match number of resources")
|
||||||
}
|
}
|
||||||
for i, res := range result {
|
for i, res := range result {
|
||||||
res.SetOriginalName(names[i], true)
|
res.setPreviousNamespaceAndName(resid.DefaultNamespace, names[i])
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,9 @@
|
|||||||
package resource
|
package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -31,18 +33,29 @@ type Resource struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
buildAnnotationOriginalName = konfig.ConfigAnnoDomain + "/originalName"
|
buildAnnotationPreviousNames = konfig.ConfigAnnoDomain + "/previousNames"
|
||||||
buildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
|
buildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
|
||||||
buildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
|
buildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
|
||||||
buildAnnotationOriginalNamespace = konfig.ConfigAnnoDomain + "/originalNs"
|
buildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var buildAnnotations = []string{
|
||||||
|
buildAnnotationPreviousNames,
|
||||||
|
buildAnnotationPrefixes,
|
||||||
|
buildAnnotationSuffixes,
|
||||||
|
buildAnnotationPreviousNamespaces,
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Resource) ResetPrimaryData(incoming *Resource) {
|
func (r *Resource) ResetPrimaryData(incoming *Resource) {
|
||||||
r.kunStr = incoming.Copy()
|
r.kunStr = incoming.Copy()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) GetAnnotations() map[string]string {
|
func (r *Resource) GetAnnotations() map[string]string {
|
||||||
return r.kunStr.GetAnnotations()
|
annotations := r.kunStr.GetAnnotations()
|
||||||
|
if annotations == nil {
|
||||||
|
return make(map[string]string)
|
||||||
|
}
|
||||||
|
return annotations
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) Copy() ifc.Kunstructured {
|
func (r *Resource) Copy() ifc.Kunstructured {
|
||||||
@@ -145,8 +158,6 @@ func (r *Resource) UnmarshalJSON(s []byte) error {
|
|||||||
type ResCtx interface {
|
type ResCtx interface {
|
||||||
AddNamePrefix(p string)
|
AddNamePrefix(p string)
|
||||||
AddNameSuffix(s string)
|
AddNameSuffix(s string)
|
||||||
GetOutermostNamePrefix() string
|
|
||||||
GetOutermostNameSuffix() string
|
|
||||||
GetNamePrefixes() []string
|
GetNamePrefixes() []string
|
||||||
GetNameSuffixes() []string
|
GetNameSuffixes() []string
|
||||||
}
|
}
|
||||||
@@ -195,21 +206,27 @@ func (r *Resource) ErrIfNotEquals(o *Resource) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !r.ReferencesEqual(o) {
|
if !r.ReferencesEqual(o) {
|
||||||
return fmt.Errorf("references unequal")
|
return fmt.Errorf(
|
||||||
|
`unequal references - self:
|
||||||
|
%sreferenced by: %s
|
||||||
|
--- other:
|
||||||
|
%sreferenced by: %s
|
||||||
|
`, meYaml, r.GetRefBy(), otherYaml, o.GetRefBy())
|
||||||
}
|
}
|
||||||
if string(meYaml) != string(otherYaml) {
|
if string(meYaml) != string(otherYaml) {
|
||||||
return fmt.Errorf("--- self:\n"+
|
return fmt.Errorf(`--- self:
|
||||||
"%s\n"+
|
%s
|
||||||
"--- other:\n"+
|
--- other:
|
||||||
"%s\n", meYaml, otherYaml)
|
%s
|
||||||
|
`, meYaml, otherYaml)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) ReferencesEqual(o *Resource) bool {
|
func (r *Resource) ReferencesEqual(other *Resource) bool {
|
||||||
setSelf := make(map[resid.ResId]bool)
|
setSelf := make(map[resid.ResId]bool)
|
||||||
setOther := make(map[resid.ResId]bool)
|
setOther := make(map[resid.ResId]bool)
|
||||||
for _, ref := range o.refBy {
|
for _, ref := range other.refBy {
|
||||||
setOther[ref] = true
|
setOther[ref] = true
|
||||||
}
|
}
|
||||||
for _, ref := range r.refBy {
|
for _, ref := range r.refBy {
|
||||||
@@ -245,22 +262,19 @@ func copyStringSlice(s []string) []string {
|
|||||||
|
|
||||||
// Implements ResCtx AddNamePrefix
|
// Implements ResCtx AddNamePrefix
|
||||||
func (r *Resource) AddNamePrefix(p string) {
|
func (r *Resource) AddNamePrefix(p string) {
|
||||||
r.addAdditiveAnnotation(buildAnnotationPrefixes, p)
|
r.appendCsvAnnotation(buildAnnotationPrefixes, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements ResCtx AddNameSuffix
|
// Implements ResCtx AddNameSuffix
|
||||||
func (r *Resource) AddNameSuffix(s string) {
|
func (r *Resource) AddNameSuffix(s string) {
|
||||||
r.addAdditiveAnnotation(buildAnnotationSuffixes, s)
|
r.appendCsvAnnotation(buildAnnotationSuffixes, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) addAdditiveAnnotation(name, value string) {
|
func (r *Resource) appendCsvAnnotation(name, value string) {
|
||||||
if value == "" {
|
if value == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
annotations := r.GetAnnotations()
|
annotations := r.GetAnnotations()
|
||||||
if annotations == nil {
|
|
||||||
annotations = make(map[string]string)
|
|
||||||
}
|
|
||||||
if existing, ok := annotations[name]; ok {
|
if existing, ok := annotations[name]; ok {
|
||||||
annotations[name] = existing + "," + value
|
annotations[name] = existing + "," + value
|
||||||
} else {
|
} else {
|
||||||
@@ -269,38 +283,16 @@ func (r *Resource) addAdditiveAnnotation(name, value string) {
|
|||||||
r.SetAnnotations(annotations)
|
r.SetAnnotations(annotations)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements ResCtx GetOutermostNamePrefix
|
func SameEndingSubarray(shortest, longest []string) bool {
|
||||||
func (r *Resource) GetOutermostNamePrefix() string {
|
if len(shortest) > len(longest) {
|
||||||
namePrefixes := r.GetNamePrefixes()
|
longest, shortest = shortest, longest
|
||||||
if len(namePrefixes) == 0 {
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
return namePrefixes[len(namePrefixes)-1]
|
diff := len(longest) - len(shortest)
|
||||||
}
|
if len(shortest) == 0 {
|
||||||
|
return diff == 0
|
||||||
// Implements ResCtx GetOutermostNameSuffix
|
|
||||||
func (r *Resource) GetOutermostNameSuffix() string {
|
|
||||||
nameSuffixes := r.GetNameSuffixes()
|
|
||||||
if len(nameSuffixes) == 0 {
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
return nameSuffixes[len(nameSuffixes)-1]
|
for i := len(shortest) - 1; i >= 0; i-- {
|
||||||
}
|
if longest[i+diff] != shortest[i] {
|
||||||
|
|
||||||
func sameEndingSubarray(a, b []string) bool {
|
|
||||||
compareLen := len(b)
|
|
||||||
if len(a) < len(b) {
|
|
||||||
compareLen = len(a)
|
|
||||||
}
|
|
||||||
|
|
||||||
if compareLen == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
alen := len(a) - 1
|
|
||||||
blen := len(b) - 1
|
|
||||||
for i := 0; i <= compareLen-1; i++ {
|
|
||||||
if a[alen-i] != b[blen-i] {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -309,41 +301,27 @@ func sameEndingSubarray(a, b []string) bool {
|
|||||||
|
|
||||||
// Implements ResCtx GetNamePrefixes
|
// Implements ResCtx GetNamePrefixes
|
||||||
func (r *Resource) GetNamePrefixes() []string {
|
func (r *Resource) GetNamePrefixes() []string {
|
||||||
annotations := r.GetAnnotations()
|
return r.getCsvAnnotation(buildAnnotationPrefixes)
|
||||||
if _, ok := annotations[buildAnnotationPrefixes]; !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return strings.Split(annotations[buildAnnotationPrefixes], ",")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements ResCtx GetNameSuffixes
|
// Implements ResCtx GetNameSuffixes
|
||||||
func (r *Resource) GetNameSuffixes() []string {
|
func (r *Resource) GetNameSuffixes() []string {
|
||||||
annotations := r.GetAnnotations()
|
return r.getCsvAnnotation(buildAnnotationSuffixes)
|
||||||
if _, ok := annotations[buildAnnotationSuffixes]; !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return strings.Split(annotations[buildAnnotationSuffixes], ",")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// OutermostPrefixSuffixEquals returns true if both resources
|
func (r *Resource) getCsvAnnotation(name string) []string {
|
||||||
// outer suffix and prefix matches.
|
annotations := r.GetAnnotations()
|
||||||
func (r *Resource) OutermostPrefixSuffixEquals(o ResCtx) bool {
|
if _, ok := annotations[name]; !ok {
|
||||||
return (r.GetOutermostNamePrefix() == o.GetOutermostNamePrefix()) && (r.GetOutermostNameSuffix() == o.GetOutermostNameSuffix())
|
return nil
|
||||||
|
}
|
||||||
|
return strings.Split(annotations[name], ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrefixesSuffixesEquals is conceptually doing the same task
|
// PrefixesSuffixesEquals is conceptually doing the same task
|
||||||
// as OutermostPrefixSuffix but performs a deeper comparison
|
// as OutermostPrefixSuffix but performs a deeper comparison
|
||||||
// of the suffix and prefix slices.
|
// of the suffix and prefix slices.
|
||||||
//
|
|
||||||
// Important note: The PrefixSuffixTransformer is stacking the
|
|
||||||
// prefix values in the reverse order of appearance in
|
|
||||||
// the transformed name. For this reason the sameEndingSubarray
|
|
||||||
// method is used (as opposed to the sameBeginningSubarray)
|
|
||||||
// to compare the prefix slice. In the same spirit, the
|
|
||||||
// GetOutermostNamePrefix is using the last element of the
|
|
||||||
// nameprefix slice and not the first.
|
|
||||||
func (r *Resource) PrefixesSuffixesEquals(o ResCtx) bool {
|
func (r *Resource) PrefixesSuffixesEquals(o ResCtx) bool {
|
||||||
return sameEndingSubarray(r.GetNamePrefixes(), o.GetNamePrefixes()) && sameEndingSubarray(r.GetNameSuffixes(), o.GetNameSuffixes())
|
return SameEndingSubarray(r.GetNamePrefixes(), o.GetNamePrefixes()) && SameEndingSubarray(r.GetNameSuffixes(), o.GetNameSuffixes())
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveBuildAnnotations removes annotations created by the build process.
|
// RemoveBuildAnnotations removes annotations created by the build process.
|
||||||
@@ -354,57 +332,15 @@ func (r *Resource) RemoveBuildAnnotations() {
|
|||||||
if len(annotations) == 0 {
|
if len(annotations) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
delete(annotations, buildAnnotationOriginalName)
|
for _, a := range buildAnnotations {
|
||||||
delete(annotations, buildAnnotationPrefixes)
|
delete(annotations, a)
|
||||||
delete(annotations, buildAnnotationSuffixes)
|
|
||||||
delete(annotations, buildAnnotationOriginalNamespace)
|
|
||||||
r.SetAnnotations(annotations)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) GetOriginalName() string {
|
|
||||||
annotations := r.GetAnnotations()
|
|
||||||
if name, ok := annotations[buildAnnotationOriginalName]; ok {
|
|
||||||
return name
|
|
||||||
}
|
|
||||||
return r.kunStr.GetName()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) SetOriginalName(n string, overwrite bool) *Resource {
|
|
||||||
annotations := r.GetAnnotations()
|
|
||||||
if annotations == nil {
|
|
||||||
annotations = make(map[string]string)
|
|
||||||
}
|
|
||||||
if _, ok := annotations[buildAnnotationOriginalName]; !ok || overwrite {
|
|
||||||
annotations[buildAnnotationOriginalName] = n
|
|
||||||
}
|
|
||||||
r.kunStr.SetAnnotations(annotations)
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) GetOriginalNs() string {
|
|
||||||
annotations := r.GetAnnotations()
|
|
||||||
if ns, ok := annotations[buildAnnotationOriginalNamespace]; ok {
|
|
||||||
return ns
|
|
||||||
}
|
|
||||||
ns := r.GetNamespace()
|
|
||||||
if ns == "default" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return ns
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) SetOriginalNs(n string, overwrite bool) *Resource {
|
|
||||||
if n == "" {
|
|
||||||
n = "default"
|
|
||||||
}
|
|
||||||
annotations := r.GetAnnotations()
|
|
||||||
if annotations == nil {
|
|
||||||
annotations = make(map[string]string)
|
|
||||||
}
|
|
||||||
if _, ok := annotations[buildAnnotationOriginalNamespace]; !ok || overwrite {
|
|
||||||
annotations[buildAnnotationOriginalNamespace] = n
|
|
||||||
}
|
}
|
||||||
r.SetAnnotations(annotations)
|
r.SetAnnotations(annotations)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resource) setPreviousNamespaceAndName(ns string, n string) *Resource {
|
||||||
|
r.appendCsvAnnotation(buildAnnotationPreviousNames, n)
|
||||||
|
r.appendCsvAnnotation(buildAnnotationPreviousNamespaces, ns)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -427,6 +363,15 @@ func (r *Resource) AsYAML() ([]byte, error) {
|
|||||||
return yaml.JSONToYAML(json)
|
return yaml.JSONToYAML(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MustYaml returns YAML or panics.
|
||||||
|
func (r *Resource) MustYaml() string {
|
||||||
|
yml, err := r.AsYAML()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return string(yml)
|
||||||
|
}
|
||||||
|
|
||||||
// SetOptions updates the generator options for the resource.
|
// SetOptions updates the generator options for the resource.
|
||||||
func (r *Resource) SetOptions(o *types.GenArgs) {
|
func (r *Resource) SetOptions(o *types.GenArgs) {
|
||||||
r.options = o
|
r.options = o
|
||||||
@@ -452,10 +397,42 @@ func (r *Resource) GetNamespace() string {
|
|||||||
|
|
||||||
// OrgId returns the original, immutable ResId for the resource.
|
// OrgId returns the original, immutable ResId for the resource.
|
||||||
// This doesn't have to be unique in a ResMap.
|
// This doesn't have to be unique in a ResMap.
|
||||||
// TODO: compute this once and save it in the resource.
|
|
||||||
func (r *Resource) OrgId() resid.ResId {
|
func (r *Resource) OrgId() resid.ResId {
|
||||||
return resid.NewResIdWithNamespace(
|
ids := r.PrevIds()
|
||||||
r.GetGvk(), r.GetOriginalName(), r.GetOriginalNs())
|
if len(ids) > 0 {
|
||||||
|
return ids[0]
|
||||||
|
}
|
||||||
|
return r.CurId()
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrevIds returns a list of ResIds that includes every
|
||||||
|
// previous ResId the resource has had through all of its
|
||||||
|
// GVKN transformations, in the order that it had that ID.
|
||||||
|
// I.e. the oldest ID is first.
|
||||||
|
// The returned array does not include the resource's current
|
||||||
|
// ID. If there are no previous IDs, this will return nil.
|
||||||
|
func (r *Resource) PrevIds() []resid.ResId {
|
||||||
|
var ids []resid.ResId
|
||||||
|
// TODO: merge previous names and namespaces into one list of
|
||||||
|
// pairs on one annotation so there is no chance of error
|
||||||
|
names := r.getCsvAnnotation(buildAnnotationPreviousNames)
|
||||||
|
ns := r.getCsvAnnotation(buildAnnotationPreviousNamespaces)
|
||||||
|
if len(names) != len(ns) {
|
||||||
|
panic(errors.New(
|
||||||
|
"number of previous names not equal to " +
|
||||||
|
"number of previous namespaces"))
|
||||||
|
}
|
||||||
|
for i := range names {
|
||||||
|
ids = append(ids, resid.NewResIdWithNamespace(
|
||||||
|
r.GetGvk(), names[i], ns[i]))
|
||||||
|
}
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
|
||||||
|
// StorePreviousId stores the resource's current ID via build annotations.
|
||||||
|
func (r *Resource) StorePreviousId() {
|
||||||
|
id := r.CurId()
|
||||||
|
r.setPreviousNamespaceAndName(id.EffectiveNamespace(), id.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CurId returns a ResId for the resource using the
|
// CurId returns a ResId for the resource using the
|
||||||
|
|||||||
@@ -695,322 +695,165 @@ spec:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetOriginalNameAndNs(t *testing.T) {
|
func TestResource_StorePreviousId(t *testing.T) {
|
||||||
input := `apiVersion: apps/v1
|
tests := map[string]struct {
|
||||||
|
input string
|
||||||
|
newName string
|
||||||
|
newNs string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
"default namespace, first previous name": {
|
||||||
|
input: `apiVersion: apps/v1
|
||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
name: newName`
|
name: oldName
|
||||||
|
`,
|
||||||
|
newName: "newName",
|
||||||
|
newNs: "",
|
||||||
|
expected: `apiVersion: apps/v1
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/previousNames: oldName
|
||||||
|
config.kubernetes.io/previousNamespaces: default
|
||||||
|
name: newName
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
|
||||||
|
"default namespace, second previous name": {
|
||||||
|
input: `apiVersion: apps/v1
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/previousNames: oldName
|
||||||
|
config.kubernetes.io/previousNamespaces: default
|
||||||
|
name: oldName2
|
||||||
|
`,
|
||||||
|
newName: "newName",
|
||||||
|
newNs: "",
|
||||||
|
expected: `apiVersion: apps/v1
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/previousNames: oldName,oldName2
|
||||||
|
config.kubernetes.io/previousNamespaces: default,default
|
||||||
|
name: newName
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
|
||||||
|
"non-default namespace": {
|
||||||
|
input: `apiVersion: apps/v1
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/previousNames: oldName
|
||||||
|
config.kubernetes.io/previousNamespaces: default
|
||||||
|
name: oldName2
|
||||||
|
namespace: oldNamespace
|
||||||
|
`,
|
||||||
|
newName: "newName",
|
||||||
|
newNs: "newNamespace",
|
||||||
|
expected: `apiVersion: apps/v1
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/previousNames: oldName,oldName2
|
||||||
|
config.kubernetes.io/previousNamespaces: default,oldNamespace
|
||||||
|
name: newName
|
||||||
|
namespace: newNamespace
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
}
|
||||||
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
||||||
resources, err := factory.SliceFromBytes([]byte(input))
|
for i := range tests {
|
||||||
if err != nil {
|
test := tests[i]
|
||||||
t.Fatal(err)
|
t.Run(i, func(t *testing.T) {
|
||||||
|
resources, err := factory.SliceFromBytes([]byte(test.input))
|
||||||
|
if !assert.NoError(t, err) || len(resources) == 0 {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
r := resources[0]
|
||||||
|
r.StorePreviousId()
|
||||||
|
r.SetName(test.newName)
|
||||||
|
if test.newNs != "" {
|
||||||
|
r.SetNamespace(test.newNs)
|
||||||
|
}
|
||||||
|
bytes, err := r.AsYAML()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
assert.Equal(t, test.expected, string(bytes))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
res := resources[0]
|
|
||||||
res.SetOriginalName("oldName", false)
|
|
||||||
res.SetOriginalNs("default", false)
|
|
||||||
|
|
||||||
expected := `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalName: oldName
|
|
||||||
config.kubernetes.io/originalNs: default
|
|
||||||
name: newName
|
|
||||||
`
|
|
||||||
bytes, err := res.AsYAML()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, expected, string(bytes))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetOriginalName(t *testing.T) {
|
func TestResource_PrevIds(t *testing.T) {
|
||||||
tests := []struct {
|
tests := map[string]struct {
|
||||||
input string
|
input string
|
||||||
expected string
|
expected []resid.ResId
|
||||||
}{
|
}{
|
||||||
{
|
"no previous IDs": {
|
||||||
// no name annotation, return the name
|
|
||||||
input: `apiVersion: apps/v1
|
input: `apiVersion: apps/v1
|
||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
name: mySecret`,
|
name: name
|
||||||
expected: "mySecret",
|
`,
|
||||||
|
expected: nil,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
"one previous ID": {
|
||||||
// return name from name annotation
|
|
||||||
input: `apiVersion: apps/v1
|
input: `apiVersion: apps/v1
|
||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/originalName: oldName
|
config.kubernetes.io/previousNames: oldName
|
||||||
name: newName`,
|
config.kubernetes.io/previousNamespaces: default
|
||||||
expected: "oldName",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
|
||||||
resources, err := factory.SliceFromBytes([]byte(test.input))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, test.expected, resources[0].GetOriginalName())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetOriginalName(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
input string
|
|
||||||
originalName string
|
|
||||||
overwrite bool
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
// no original name set, overwrite is false
|
|
||||||
input: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: newName`,
|
|
||||||
originalName: "oldName",
|
|
||||||
overwrite: false,
|
|
||||||
expected: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalName: oldName
|
|
||||||
name: newName
|
name: newName
|
||||||
`,
|
`,
|
||||||
|
expected: []resid.ResId{
|
||||||
|
{
|
||||||
|
Gvk: resid.Gvk{Group: "apps", Version: "v1", Kind: "Secret"},
|
||||||
|
Name: "oldName",
|
||||||
|
Namespace: resid.DefaultNamespace,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
"two ids": {
|
||||||
// no original name set, overwrite is true
|
|
||||||
input: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: newName`,
|
|
||||||
originalName: "oldName",
|
|
||||||
overwrite: true,
|
|
||||||
expected: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalName: oldName
|
|
||||||
name: newName
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
// original name is set, overwrite is false, resource shouldn't change
|
|
||||||
input: `apiVersion: apps/v1
|
input: `apiVersion: apps/v1
|
||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/originalName: oldName
|
config.kubernetes.io/previousNames: oldName,oldName2
|
||||||
name: newName`,
|
config.kubernetes.io/previousNamespaces: default,oldNamespace
|
||||||
originalName: "newOriginalName",
|
|
||||||
overwrite: false,
|
|
||||||
expected: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalName: oldName
|
|
||||||
name: newName
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
// original name is set, overwrite is true, resource should change
|
|
||||||
input: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalName: oldName
|
|
||||||
name: newName`,
|
|
||||||
originalName: "newOriginalName",
|
|
||||||
overwrite: true,
|
|
||||||
expected: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalName: newOriginalName
|
|
||||||
name: newName
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
|
||||||
resources, err := factory.SliceFromBytes([]byte(test.input))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
res := resources[0]
|
|
||||||
res.SetOriginalName(test.originalName, test.overwrite)
|
|
||||||
|
|
||||||
bytes, err := res.AsYAML()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, test.expected, string(bytes))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetOriginalNs(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
input string
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
// no namespace, return default
|
|
||||||
input: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: mySecret`,
|
|
||||||
expected: "",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
// return old namespace
|
|
||||||
input: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalNs: oldNamespace
|
|
||||||
name: mySecret
|
|
||||||
namespace: myNamespace`,
|
|
||||||
expected: "oldNamespace",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
// return namespace
|
|
||||||
input: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: mySecret
|
|
||||||
namespace: myNamespace`,
|
|
||||||
expected: "myNamespace",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
|
||||||
resources, err := factory.SliceFromBytes([]byte(test.input))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, test.expected, resources[0].GetOriginalNs())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetOriginalNs(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
input string
|
|
||||||
originalNs string
|
|
||||||
overwrite bool
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
// no original namespace set, overwrite is false
|
|
||||||
input: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: newName
|
|
||||||
namespace: newNamespace`,
|
|
||||||
originalNs: "oldNamespace",
|
|
||||||
overwrite: false,
|
|
||||||
expected: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalNs: oldNamespace
|
|
||||||
name: newName
|
|
||||||
namespace: newNamespace
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
// no original name set, overwrite is true
|
|
||||||
input: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: newName
|
|
||||||
namespace: newNamespace`,
|
|
||||||
|
|
||||||
originalNs: "oldNamespace",
|
|
||||||
overwrite: true,
|
|
||||||
expected: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalNs: oldNamespace
|
|
||||||
name: newName
|
|
||||||
namespace: newNamespace
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
// original name is set, overwrite is false, resource shouldn't change
|
|
||||||
input: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalNs: oldNamespace
|
|
||||||
name: newName
|
|
||||||
namespace: newNamespace`,
|
|
||||||
originalNs: "newOriginalNamespace",
|
|
||||||
overwrite: false,
|
|
||||||
expected: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalNs: oldNamespace
|
|
||||||
name: newName
|
|
||||||
namespace: newNamespace
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
// original name is set, overwrite is true, resource should change
|
|
||||||
input: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalNs: oldNamespace
|
|
||||||
name: newName
|
|
||||||
namespace: newNamespace`,
|
|
||||||
originalNs: "newOriginalNamespace",
|
|
||||||
overwrite: true,
|
|
||||||
expected: `apiVersion: apps/v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/originalNs: newOriginalNamespace
|
|
||||||
name: newName
|
name: newName
|
||||||
namespace: newNamespace
|
namespace: newNamespace
|
||||||
`,
|
`,
|
||||||
|
expected: []resid.ResId{
|
||||||
|
{
|
||||||
|
Gvk: resid.Gvk{Group: "apps", Version: "v1", Kind: "Secret"},
|
||||||
|
Name: "oldName",
|
||||||
|
Namespace: resid.DefaultNamespace,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Gvk: resid.Gvk{Group: "apps", Version: "v1", Kind: "Secret"},
|
||||||
|
Name: "oldName2",
|
||||||
|
Namespace: "oldNamespace",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
||||||
for _, test := range tests {
|
for i := range tests {
|
||||||
factory := provider.NewDefaultDepProvider().GetResourceFactory()
|
test := tests[i]
|
||||||
resources, err := factory.SliceFromBytes([]byte(test.input))
|
t.Run(i, func(t *testing.T) {
|
||||||
if err != nil {
|
resources, err := factory.SliceFromBytes([]byte(test.input))
|
||||||
t.Fatal(err)
|
if !assert.NoError(t, err) || len(resources) == 0 {
|
||||||
}
|
t.FailNow()
|
||||||
|
}
|
||||||
res := resources[0]
|
r := resources[0]
|
||||||
res.SetOriginalNs(test.originalNs, test.overwrite)
|
assert.Equal(t, test.expected, r.PrevIds())
|
||||||
|
})
|
||||||
bytes, err := res.AsYAML()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, test.expected, string(bytes))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1177,3 +1020,55 @@ spec:
|
|||||||
name: nginx
|
name: nginx
|
||||||
`, imagename)
|
`, imagename)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSameEndingSubarray(t *testing.T) {
|
||||||
|
testCases := map[string]struct {
|
||||||
|
a []string
|
||||||
|
b []string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
"both nil": {
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
"one nil": {
|
||||||
|
b: []string{},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
"both empty": {
|
||||||
|
a: []string{},
|
||||||
|
b: []string{},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
"no1": {
|
||||||
|
a: []string{"a"},
|
||||||
|
b: []string{},
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
"no2": {
|
||||||
|
a: []string{"b", "a"},
|
||||||
|
b: []string{"b"},
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
"yes1": {
|
||||||
|
a: []string{"a", "b"},
|
||||||
|
b: []string{"b"},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
"yes2": {
|
||||||
|
a: []string{"a", "b", "c"},
|
||||||
|
b: []string{"b", "c"},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
"yes3": {
|
||||||
|
a: []string{"a", "b", "c", "d", "e", "f"},
|
||||||
|
b: []string{"f"},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for n := range testCases {
|
||||||
|
tc := testCases[n]
|
||||||
|
t.Run(n, func(t *testing.T) {
|
||||||
|
assert.Equal(t, tc.expected, SameEndingSubarray(tc.a, tc.b))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced {
|
|||||||
pte := newPluginTestEnv(t).set()
|
pte := newPluginTestEnv(t).set()
|
||||||
|
|
||||||
pc, err := konfig.EnabledPluginConfig(types.BploLoadFromFileSys)
|
pc, err := konfig.EnabledPluginConfig(types.BploLoadFromFileSys)
|
||||||
|
pc.FnpLoadingOptions.EnableStar = true
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
)
|
)
|
||||||
@@ -48,15 +49,7 @@ func (rm *rmBuilder) AddR(r *resource.Resource) *rmBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rm *rmBuilder) AddWithName(n string, m map[string]interface{}) *rmBuilder {
|
func (rm *rmBuilder) AddWithName(n string, m map[string]interface{}) *rmBuilder {
|
||||||
err := rm.m.Append(rm.rf.FromMapWithName(n, m))
|
err := rm.m.Append(rm.rf.FromMapWithNamespaceAndName(resid.DefaultNamespace, n, m))
|
||||||
if err != nil {
|
|
||||||
rm.t.Fatalf("test setup failure: %v", err)
|
|
||||||
}
|
|
||||||
return rm
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rm *rmBuilder) AddWithNs(ns string, m map[string]interface{}) *rmBuilder {
|
|
||||||
err := rm.m.Append(rm.rf.FromMapWithNamespace(ns, m))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rm.t.Fatalf("test setup failure: %v", err)
|
rm.t.Fatalf("test setup failure: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ package types
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
)
|
)
|
||||||
@@ -34,11 +33,6 @@ type FieldSpec struct {
|
|||||||
CreateIfNotPresent bool `json:"create,omitempty" yaml:"create,omitempty"`
|
CreateIfNotPresent bool `json:"create,omitempty" yaml:"create,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
escapedForwardSlash = "\\/"
|
|
||||||
tempSlashReplacement = "???"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (fs FieldSpec) String() string {
|
func (fs FieldSpec) String() string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"%s:%v:%s", fs.Gvk.String(), fs.CreateIfNotPresent, fs.Path)
|
"%s:%v:%s", fs.Gvk.String(), fs.CreateIfNotPresent, fs.Path)
|
||||||
@@ -49,34 +43,6 @@ func (fs FieldSpec) effectivelyEquals(other FieldSpec) bool {
|
|||||||
return fs.IsSelected(&other.Gvk) && fs.Path == other.Path
|
return fs.IsSelected(&other.Gvk) && fs.Path == other.Path
|
||||||
}
|
}
|
||||||
|
|
||||||
// PathSlice converts the path string to a slice of strings,
|
|
||||||
// separated by a '/'. Forward slash can be contained in a
|
|
||||||
// fieldname. such as ingress.kubernetes.io/auth-secret in
|
|
||||||
// Ingress annotations. To deal with this special case, the
|
|
||||||
// path to this field should be formatted as
|
|
||||||
//
|
|
||||||
// metadata/annotations/ingress.kubernetes.io\/auth-secret
|
|
||||||
//
|
|
||||||
// Then PathSlice will return
|
|
||||||
//
|
|
||||||
// []string{
|
|
||||||
// "metadata",
|
|
||||||
// "annotations",
|
|
||||||
// "ingress.auth-secretkubernetes.io/auth-secret"
|
|
||||||
// }
|
|
||||||
func (fs FieldSpec) PathSlice() []string {
|
|
||||||
if !strings.Contains(fs.Path, escapedForwardSlash) {
|
|
||||||
return strings.Split(fs.Path, "/")
|
|
||||||
}
|
|
||||||
s := strings.Replace(fs.Path, escapedForwardSlash, tempSlashReplacement, -1)
|
|
||||||
paths := strings.Split(s, "/")
|
|
||||||
var result []string
|
|
||||||
for _, path := range paths {
|
|
||||||
result = append(result, strings.Replace(path, tempSlashReplacement, "/", -1))
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
type FsSlice []FieldSpec
|
type FsSlice []FieldSpec
|
||||||
|
|
||||||
func (s FsSlice) Len() int { return len(s) }
|
func (s FsSlice) Len() int { return len(s) }
|
||||||
|
|||||||
@@ -13,30 +13,6 @@ import (
|
|||||||
. "sigs.k8s.io/kustomize/api/types"
|
. "sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPathSlice(t *testing.T) {
|
|
||||||
type path struct {
|
|
||||||
input string
|
|
||||||
parsed []string
|
|
||||||
}
|
|
||||||
paths := []path{
|
|
||||||
{
|
|
||||||
input: "spec/metadata/annotations",
|
|
||||||
parsed: []string{"spec", "metadata", "annotations"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: `metadata/annotations/nginx.ingress.kubernetes.io\/auth-secret`,
|
|
||||||
parsed: []string{"metadata", "annotations", "nginx.ingress.kubernetes.io/auth-secret"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, p := range paths {
|
|
||||||
fs := FieldSpec{Path: p.input}
|
|
||||||
actual := fs.PathSlice()
|
|
||||||
if !reflect.DeepEqual(actual, p.parsed) {
|
|
||||||
t.Fatalf("expected %v, but got %v", p.parsed, actual)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var mergeTests = []struct {
|
var mergeTests = []struct {
|
||||||
name string
|
name string
|
||||||
original FsSlice
|
original FsSlice
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ type Kustomization struct {
|
|||||||
// MetaData is a pointer to avoid marshalling empty struct
|
// MetaData is a pointer to avoid marshalling empty struct
|
||||||
MetaData *ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
|
MetaData *ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
|
||||||
|
|
||||||
|
// OpenAPI contains information about what kubernetes schema to use.
|
||||||
|
OpenAPI map[string]string `json:"openapi,omitempty" yaml:"openapi,omitempty"`
|
||||||
|
|
||||||
//
|
//
|
||||||
// Operators - what kustomize can do.
|
// Operators - what kustomize can do.
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -15,7 +15,5 @@ require (
|
|||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
|
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
|
||||||
k8s.io/apimachinery v0.18.10
|
k8s.io/apimachinery v0.18.10
|
||||||
sigs.k8s.io/kustomize/kyaml v0.10.6
|
sigs.k8s.io/kustomize/kyaml v0.10.7
|
||||||
)
|
)
|
||||||
|
|
||||||
replace sigs.k8s.io/kustomize/kyaml => ../../kyaml
|
|
||||||
|
|||||||
@@ -388,6 +388,8 @@ k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUc
|
|||||||
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
|
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
|
||||||
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
|
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
|
||||||
k8s.io/kube-openapi v0.0.0-20200410145947-61e04a5be9a6/go.mod h1:GRQhZsXIAJ1xR0C9bd8UpWHZ5plfAS9fzPjJuQ6JL3E=
|
k8s.io/kube-openapi v0.0.0-20200410145947-61e04a5be9a6/go.mod h1:GRQhZsXIAJ1xR0C9bd8UpWHZ5plfAS9fzPjJuQ6JL3E=
|
||||||
|
sigs.k8s.io/kustomize/kyaml v0.10.7 h1:r0r8UEL0bL7X56HKUmhJZ+TP+nvRNGrDHHSLO7izlcQ=
|
||||||
|
sigs.k8s.io/kustomize/kyaml v0.10.7/go.mod h1:K9yg1k/HB/6xNOf5VH3LhTo1DK9/5ykSZO5uIv+Y/1k=
|
||||||
sigs.k8s.io/structured-merge-diff/v3 v3.0.0-20200116222232-67a7b8c61874/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=
|
sigs.k8s.io/structured-merge-diff/v3 v3.0.0-20200116222232-67a7b8c61874/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=
|
||||||
sigs.k8s.io/structured-merge-diff/v3 v3.0.0/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=
|
sigs.k8s.io/structured-merge-diff/v3 v3.0.0/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=
|
||||||
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
||||||
|
|||||||
@@ -6,11 +6,7 @@ require (
|
|||||||
github.com/rakyll/statik v0.1.7
|
github.com/rakyll/statik v0.1.7
|
||||||
github.com/spf13/cobra v1.0.0
|
github.com/spf13/cobra v1.0.0
|
||||||
github.com/stretchr/testify v1.4.0
|
github.com/stretchr/testify v1.4.0
|
||||||
sigs.k8s.io/kustomize/api v0.7.2
|
sigs.k8s.io/kustomize/api v0.6.9
|
||||||
sigs.k8s.io/kustomize/kyaml v0.10.6
|
sigs.k8s.io/kustomize/kyaml v0.10.7
|
||||||
sigs.k8s.io/yaml v1.2.0
|
sigs.k8s.io/yaml v1.2.0
|
||||||
)
|
)
|
||||||
|
|
||||||
replace sigs.k8s.io/kustomize/api => ../../api
|
|
||||||
|
|
||||||
replace sigs.k8s.io/kustomize/kyaml => ../../kyaml
|
|
||||||
|
|||||||
@@ -534,6 +534,10 @@ k8s.io/utils v0.0.0-20191114184206-e782cd3c129f/go.mod h1:sZAwmy6armz5eXlNoLmJcl
|
|||||||
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc=
|
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc=
|
||||||
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4=
|
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4=
|
||||||
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
||||||
|
sigs.k8s.io/kustomize/api v0.6.9 h1:wJK+JujSyRemVxESNvk0bXjBMcDkJk0IbDkQ+8yRKO4=
|
||||||
|
sigs.k8s.io/kustomize/api v0.6.9/go.mod h1:4RLLidX0T3BWrC6gsRqz6uCJ6OXFecXfh/3QDHMauOg=
|
||||||
|
sigs.k8s.io/kustomize/kyaml v0.10.7 h1:r0r8UEL0bL7X56HKUmhJZ+TP+nvRNGrDHHSLO7izlcQ=
|
||||||
|
sigs.k8s.io/kustomize/kyaml v0.10.7/go.mod h1:K9yg1k/HB/6xNOf5VH3LhTo1DK9/5ykSZO5uIv+Y/1k=
|
||||||
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
||||||
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
||||||
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||||
|
|||||||
@@ -103,7 +103,6 @@ the application will pick up an extra `application-<profile>.properties` file. W
|
|||||||
steps. Add an environment variable through the patch and add a file to the configMap.
|
steps. Add an environment variable through the patch and add a file to the configMap.
|
||||||
|
|
||||||
<!-- @customizeConfigMap @testAgainstLatestRelease -->
|
<!-- @customizeConfigMap @testAgainstLatestRelease -->
|
||||||
|
|
||||||
```
|
```
|
||||||
cat <<EOF >$DEMO_HOME/patch.yaml
|
cat <<EOF >$DEMO_HOME/patch.yaml
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
@@ -283,7 +282,6 @@ The output contains
|
|||||||
Add these patches to the kustomization:
|
Add these patches to the kustomization:
|
||||||
|
|
||||||
<!-- @addPatch @testAgainstLatestRelease -->
|
<!-- @addPatch @testAgainstLatestRelease -->
|
||||||
|
|
||||||
```
|
```
|
||||||
cd $DEMO_HOME
|
cd $DEMO_HOME
|
||||||
kustomize edit add patch --path memorylimit_patch.yaml --name sbdemo --kind Deployment --group apps --version v1
|
kustomize edit add patch --path memorylimit_patch.yaml --name sbdemo --kind Deployment --group apps --version v1
|
||||||
@@ -320,5 +318,5 @@ create the production environment.
|
|||||||
|
|
||||||
<!-- @finalBuild @testAgainstLatestRelease -->
|
<!-- @finalBuild @testAgainstLatestRelease -->
|
||||||
```
|
```
|
||||||
kustomize build $DEMO_HOME # | kubectl apply -f -
|
kustomize build $DEMO_HOME # | kubectl apply -f -
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
for f in $(find $1 -name '*.go'); do
|
for f in $(find $1 -name '*.go'); do
|
||||||
echo $f
|
echo $f
|
||||||
# go run go.coder.com/go-tools/cmd/goimports
|
# go get golang.org/x/tools/cmd/goimports
|
||||||
~/gopath/bin/goimports -w $f
|
# {or} go run go.coder.com/go-tools/cmd/goimports
|
||||||
|
goimports -w $f
|
||||||
done
|
done
|
||||||
|
|||||||
@@ -8,19 +8,28 @@
|
|||||||
# kustomize binary to your current working directory.
|
# kustomize binary to your current working directory.
|
||||||
# (e.g. 'install_kustomize.sh 3.8.2')
|
# (e.g. 'install_kustomize.sh 3.8.2')
|
||||||
#
|
#
|
||||||
|
# If two arguments are given -> Downloads the specified version of the
|
||||||
|
# kustomize binary to the specified directory.
|
||||||
|
# (e.g. 'install_kustomize.sh 3.8.2 $(go env GOPATH)/bin')
|
||||||
|
#
|
||||||
# Fails if the file already exists.
|
# Fails if the file already exists.
|
||||||
|
|
||||||
curl_timeout=600
|
curl_timeout=600
|
||||||
release_url=https://api.github.com/repos/kubernetes-sigs/kustomize/releases
|
|
||||||
|
|
||||||
|
version=""
|
||||||
|
release_url=https://api.github.com/repos/kubernetes-sigs/kustomize/releases
|
||||||
if [ -n "$1" ]; then
|
if [ -n "$1" ]; then
|
||||||
version=v$1
|
version=v$1
|
||||||
release_url=${release_url}/tags/kustomize%2F$version
|
release_url=${release_url}/tags/kustomize%2F$version
|
||||||
fi
|
fi
|
||||||
|
|
||||||
where=$PWD
|
where=$PWD
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
where=$2
|
||||||
|
fi
|
||||||
|
|
||||||
if [ -f $where/kustomize ]; then
|
if [ -f $where/kustomize ]; then
|
||||||
echo "A file named kustomize already exists (remove it first)."
|
echo "A file named $where/kustomize already exists (remove it first)."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -63,6 +72,6 @@ cp ./kustomize $where
|
|||||||
|
|
||||||
popd >& /dev/null
|
popd >& /dev/null
|
||||||
|
|
||||||
./kustomize version
|
$where/kustomize version
|
||||||
|
|
||||||
echo kustomize installed to current directory.
|
echo kustomize installed to $where
|
||||||
|
|||||||
@@ -54,10 +54,10 @@ https://github.com/hashicorp/go-getter#url-format
|
|||||||
`
|
`
|
||||||
|
|
||||||
// NewCmdBuild creates a new build command.
|
// NewCmdBuild creates a new build command.
|
||||||
func NewCmdBuild(out io.Writer) *cobra.Command {
|
func NewCmdBuild(cmdName string, out io.Writer) *cobra.Command {
|
||||||
var o Options
|
var o Options
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "build {path}",
|
Use: cmdName + " {path}",
|
||||||
Short: "Print configuration per contents of " +
|
Short: "Print configuration per contents of " +
|
||||||
konfig.DefaultKustomizationFileName(),
|
konfig.DefaultKustomizationFileName(),
|
||||||
Example: examples,
|
Example: examples,
|
||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,11 +14,11 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
"sigs.k8s.io/kustomize/cmd/config/completion"
|
"sigs.k8s.io/kustomize/cmd/config/completion"
|
||||||
"sigs.k8s.io/kustomize/cmd/config/configcobra"
|
"sigs.k8s.io/kustomize/cmd/config/configcobra"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/build"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/build"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/create"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/create"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/edit"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/openapi"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/openapi"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/version"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewDefaultCommand returns the default (aka root) command for kustomize command.
|
// NewDefaultCommand returns the default (aka root) command for kustomize command.
|
||||||
@@ -37,7 +37,7 @@ See https://sigs.k8s.io/kustomize
|
|||||||
pvd := provider.NewDefaultDepProvider()
|
pvd := provider.NewDefaultDepProvider()
|
||||||
c.AddCommand(
|
c.AddCommand(
|
||||||
completion.NewCommand(),
|
completion.NewCommand(),
|
||||||
build.NewCmdBuild(stdOut),
|
build.NewCmdBuild("build", stdOut),
|
||||||
edit.NewCmdEdit(
|
edit.NewCmdEdit(
|
||||||
fSys, pvd.GetFieldValidator(), pvd.GetKunstructuredFactory()),
|
fSys, pvd.GetFieldValidator(), pvd.GetKunstructuredFactory()),
|
||||||
create.NewCmdCreate(fSys, pvd.GetKunstructuredFactory()),
|
create.NewCmdCreate(fSys, pvd.GetKunstructuredFactory()),
|
||||||
@@ -14,8 +14,8 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/loader"
|
"sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type createFlags struct {
|
type createFlags struct {
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
var factory = provider.NewDefaultDepProvider().GetKunstructuredFactory()
|
var factory = provider.NewDefaultDepProvider().GetKunstructuredFactory()
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
type addBaseOptions struct {
|
type addBaseOptions struct {
|
||||||
@@ -8,8 +8,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
|
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/commands/internal/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -10,8 +10,8 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/loader"
|
"sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type addComponentOptions struct {
|
type addComponentOptions struct {
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
|
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/commands/internal/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -10,8 +10,8 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// kindOfAdd is the kind of metadata being added: label or annotation
|
// kindOfAdd is the kind of metadata being added: label or annotation
|
||||||
@@ -10,8 +10,8 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
|
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/commands/internal/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeKustomization(t *testing.T) *types.Kustomization {
|
func makeKustomization(t *testing.T) *types.Kustomization {
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
type addPatchOptions struct {
|
type addPatchOptions struct {
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
|
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/commands/internal/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -10,8 +10,8 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/loader"
|
"sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type addResourceOptions struct {
|
type addResourceOptions struct {
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
|
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/commands/internal/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -9,8 +9,8 @@ import (
|
|||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type addTransformerOptions struct {
|
type addTransformerOptions struct {
|
||||||
@@ -9,8 +9,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
|
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/commands/internal/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
// newCmdAddConfigMap returns a new command.
|
// newCmdAddConfigMap returns a new command.
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// flagsAndArgs encapsulates the options for add secret/configmap commands.
|
// flagsAndArgs encapsulates the options for add secret/configmap commands.
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
// newCmdAddSecret returns a new command.
|
// newCmdAddSecret returns a new command.
|
||||||
@@ -9,11 +9,11 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/kv"
|
"sigs.k8s.io/kustomize/api/kv"
|
||||||
"sigs.k8s.io/kustomize/api/loader"
|
"sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/add"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/edit/add"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/fix"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/edit/fix"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/listbuiltin"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/edit/listbuiltin"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/remove"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/edit/remove"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/set"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/edit/set"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewCmdEdit returns an instance of 'edit' subcommand.
|
// NewCmdEdit returns an instance of 'edit' subcommand.
|
||||||
@@ -6,7 +6,7 @@ package fix
|
|||||||
import (
|
import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewCmdFix returns an instance of 'fix' subcommand.
|
// NewCmdFix returns an instance of 'fix' subcommand.
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
|
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/commands/internal/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFix(t *testing.T) {
|
func TestFix(t *testing.T) {
|
||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v3/commands/internal/kustfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
// kindOfAdd is the kind of metadata being added: label or annotation
|
// kindOfAdd is the kind of metadata being added: label or annotation
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user