mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 01:46:23 +00:00
Compare commits
1 Commits
pinToCmdCo
...
kyaml/v0.9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7432392a51 |
61
Makefile
61
Makefile
@@ -26,21 +26,22 @@ verify-kustomize: \
|
|||||||
lint-kustomize \
|
lint-kustomize \
|
||||||
test-unit-kustomize-all \
|
test-unit-kustomize-all \
|
||||||
test-examples-kustomize-against-HEAD \
|
test-examples-kustomize-against-HEAD \
|
||||||
test-examples-kustomize-against-3.9.0 \
|
test-examples-kustomize-against-3.8.6
|
||||||
test-examples-kustomize-against-3.8.8
|
|
||||||
|
|
||||||
# The following target referenced by a file in
|
# The following target referenced by a file in
|
||||||
# https://github.com/kubernetes/test-infra/tree/master/config/jobs/kubernetes-sigs/kustomize
|
# https://github.com/kubernetes/test-infra/tree/master/config/jobs/kubernetes-sigs/kustomize
|
||||||
.PHONY: prow-presubmit-check
|
.PHONY: prow-presubmit-check
|
||||||
prow-presubmit-check: \
|
prow-presubmit-check: \
|
||||||
lint-kustomize \
|
lint-kustomize \
|
||||||
test-multi-module \
|
|
||||||
test-unit-kustomize-all \
|
test-unit-kustomize-all \
|
||||||
test-unit-cmd-all \
|
test-unit-cmd-all \
|
||||||
test-go-mod \
|
test-go-mod \
|
||||||
test-examples-kustomize-against-HEAD \
|
test-examples-kustomize-against-HEAD \
|
||||||
test-examples-kustomize-against-3.9.0 \
|
test-examples-kustomize-against-3.8.6
|
||||||
test-examples-kustomize-against-3.8.8
|
|
||||||
|
# test-multi-module \
|
||||||
|
# Temporarily removed from prow-presubmit-check
|
||||||
|
# See https://github.com/kubernetes-sigs/kustomize/issues/3191
|
||||||
|
|
||||||
.PHONY: verify-kustomize-e2e
|
.PHONY: verify-kustomize-e2e
|
||||||
verify-kustomize-e2e: test-examples-e2e-kustomize
|
verify-kustomize-e2e: test-examples-e2e-kustomize
|
||||||
@@ -192,27 +193,24 @@ $(pGen)/%.go: $(MYGOBIN)/pluginator
|
|||||||
.PHONY: generate-kustomize-builtin-plugins
|
.PHONY: generate-kustomize-builtin-plugins
|
||||||
generate-kustomize-builtin-plugins: $(builtinplugins)
|
generate-kustomize-builtin-plugins: $(builtinplugins)
|
||||||
|
|
||||||
.PHONY: build-kustomize-external-go-plugin
|
.PHONY: kustomize-external-go-plugin-build
|
||||||
build-kustomize-external-go-plugin:
|
kustomize-external-go-plugin-build:
|
||||||
./hack/buildExternalGoPlugins.sh ./plugin
|
./hack/buildExternalGoPlugins.sh ./plugin
|
||||||
|
|
||||||
.PHONY: clean-kustomize-external-go-plugin
|
.PHONY: kustomize-external-go-plugin-clean
|
||||||
clean-kustomize-external-go-plugin:
|
kustomize-external-go-plugin-clean:
|
||||||
./hack/buildExternalGoPlugins.sh ./plugin clean
|
./hack/buildExternalGoPlugins.sh ./plugin clean
|
||||||
|
|
||||||
### End kustomize plugin rules.
|
### End kustomize plugin rules.
|
||||||
|
|
||||||
.PHONY: lint-kustomize
|
.PHONY: lint-kustomize
|
||||||
lint-kustomize: install-tools $(builtinplugins)
|
lint-kustomize: install-tools $(builtinplugins)
|
||||||
cd api; $(MYGOBIN)/golangci-lint-kustomize \
|
cd api; \
|
||||||
-c ../.golangci-kustomize.yml \
|
$(MYGOBIN)/golangci-lint-kustomize -c ../.golangci-kustomize.yml run ./...
|
||||||
run ./...
|
cd kustomize; \
|
||||||
cd kustomize; $(MYGOBIN)/golangci-lint-kustomize \
|
$(MYGOBIN)/golangci-lint-kustomize -c ../.golangci-kustomize.yml run ./...
|
||||||
-c ../.golangci-kustomize.yml \
|
cd cmd/pluginator; \
|
||||||
run ./...
|
$(MYGOBIN)/golangci-lint-kustomize -c ../../.golangci-kustomize.yml run ./...
|
||||||
cd cmd/pluginator; $(MYGOBIN)/golangci-lint-kustomize \
|
|
||||||
-c ../../.golangci-kustomize.yml \
|
|
||||||
run ./...
|
|
||||||
|
|
||||||
# Used to add non-default compilation flags when experimenting with
|
# Used to add non-default compilation flags when experimenting with
|
||||||
# plugin-to-api compatibility checks.
|
# plugin-to-api compatibility checks.
|
||||||
@@ -220,10 +218,6 @@ lint-kustomize: install-tools $(builtinplugins)
|
|||||||
build-kustomize-api: $(builtinplugins)
|
build-kustomize-api: $(builtinplugins)
|
||||||
cd api; go build ./...
|
cd api; go build ./...
|
||||||
|
|
||||||
.PHONY: generate-kustomize-api
|
|
||||||
generate-kustomize-api:
|
|
||||||
cd api; go generate ./...
|
|
||||||
|
|
||||||
.PHONY: test-unit-kustomize-api
|
.PHONY: test-unit-kustomize-api
|
||||||
test-unit-kustomize-api: build-kustomize-api
|
test-unit-kustomize-api: build-kustomize-api
|
||||||
cd api; go test ./... -ldflags "-X sigs.k8s.io/kustomize/api/provenance.version=v444.333.222"
|
cd api; go test ./... -ldflags "-X sigs.k8s.io/kustomize/api/provenance.version=v444.333.222"
|
||||||
@@ -276,12 +270,17 @@ test-examples-kustomize-against-HEAD: $(MYGOBIN)/kustomize $(MYGOBIN)/mdrip
|
|||||||
./hack/testExamplesAgainstKustomize.sh HEAD
|
./hack/testExamplesAgainstKustomize.sh HEAD
|
||||||
|
|
||||||
.PHONY:
|
.PHONY:
|
||||||
test-examples-kustomize-against-3.9.0: $(MYGOBIN)/mdrip
|
test-examples-kustomize-against-3.8.6: $(MYGOBIN)/mdrip
|
||||||
./hack/testExamplesAgainstKustomize.sh v3.9.0
|
( \
|
||||||
|
set -e; \
|
||||||
.PHONY:
|
tag=v3.8.6; \
|
||||||
test-examples-kustomize-against-3.8.8: $(MYGOBIN)/mdrip
|
/bin/rm -f $(MYGOBIN)/kustomize; \
|
||||||
./hack/testExamplesAgainstKustomize.sh v3.8.8
|
echo "Installing kustomize $$tag."; \
|
||||||
|
GO111MODULE=on go get sigs.k8s.io/kustomize/kustomize/v3@$${tag}; \
|
||||||
|
./hack/testExamplesAgainstKustomize.sh $$tag; \
|
||||||
|
echo "Reinstalling kustomize from HEAD."; \
|
||||||
|
cd kustomize; go install .; \
|
||||||
|
)
|
||||||
|
|
||||||
# linux only.
|
# linux only.
|
||||||
# This is for testing an example plugin that
|
# This is for testing an example plugin that
|
||||||
@@ -355,15 +354,13 @@ $(MYGOBIN)/gh:
|
|||||||
)
|
)
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean: clean-kustomize-external-go-plugin
|
clean: kustomize-external-go-plugin-clean
|
||||||
go clean --cache
|
go clean --cache
|
||||||
rm -f $(builtinplugins)
|
rm -f $(builtinplugins)
|
||||||
|
rm -f $(MYGOBIN)/pluginator
|
||||||
rm -f $(MYGOBIN)/kustomize
|
rm -f $(MYGOBIN)/kustomize
|
||||||
rm -f $(MYGOBIN)/golangci-lint-kustomize
|
rm -f $(MYGOBIN)/golangci-lint-kustomize
|
||||||
|
|
||||||
# Handle pluginator manually.
|
|
||||||
# rm -f $(MYGOBIN)/pluginator
|
|
||||||
|
|
||||||
# Nuke the site from orbit. It's the only way to be sure.
|
# Nuke the site from orbit. It's the only way to be sure.
|
||||||
.PHONY: nuke
|
.PHONY: nuke
|
||||||
nuke: clean
|
nuke: clean
|
||||||
|
|||||||
21
README.md
21
README.md
@@ -20,20 +20,15 @@ This tool is sponsored by [sig-cli] ([KEP]).
|
|||||||
|
|
||||||
## kubectl integration
|
## kubectl integration
|
||||||
|
|
||||||
The kustomize build flow at [v2.0.3] was added
|
Since [v1.14][kubectl announcement] the kustomize build system has been included in kubectl.
|
||||||
to [kubectl v1.14][kubectl announcement]. The kustomize
|
|
||||||
flow in kubectl has remained frozen at v2.0.3 while work
|
|
||||||
to extract kubectl from the k/k repo, and work to remove
|
|
||||||
kustomize's dependence on core k/k code ([#2506]) has proceeded.
|
|
||||||
The reintegration effort is tracked in [#1500] (and its blocking
|
|
||||||
issues).
|
|
||||||
|
|
||||||
[v2.0.3]: /../../tree/v2.0.3
|
| kubectl version | kustomize version |
|
||||||
[#2506]: https://github.com/kubernetes-sigs/kustomize/issues/2506
|
|---------|--------|
|
||||||
[#1500]: https://github.com/kubernetes-sigs/kustomize/issues/1500
|
| v1.16.0 | [v2.0.3](/../../tree/v2.0.3) |
|
||||||
|
| v1.15.x | [v2.0.3](/../../tree/v2.0.3) |
|
||||||
|
| v1.14.x | [v2.0.3](/../../tree/v2.0.3) |
|
||||||
|
|
||||||
For examples and guides for using the kubectl integration please
|
For examples and guides for using the kubectl integration please see the [kubectl book] or the [kubernetes documentation].
|
||||||
see the [kubectl book] or the [kubernetes documentation].
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@@ -153,7 +148,7 @@ is governed by the [Kubernetes Code of Conduct].
|
|||||||
[imageBase]: docs/images/base.jpg
|
[imageBase]: docs/images/base.jpg
|
||||||
[imageOverlay]: docs/images/overlay.jpg
|
[imageOverlay]: docs/images/overlay.jpg
|
||||||
[kubectl announcement]: https://kubernetes.io/blog/2019/03/25/kubernetes-1-14-release-announcement
|
[kubectl announcement]: https://kubernetes.io/blog/2019/03/25/kubernetes-1-14-release-announcement
|
||||||
[kubectl book]: https://kubectl.docs.kubernetes.io/guides/introduction/kustomize/
|
[kubectl book]: https://kubectl.docs.kubernetes.io/pages/app_customization/introduction.html
|
||||||
[kubernetes documentation]: https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/
|
[kubernetes documentation]: https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/
|
||||||
[kubernetes style]: https://kubernetes-sigs.github.io/kustomize/api-reference/glossary#kubernetes-style-object
|
[kubernetes style]: https://kubernetes-sigs.github.io/kustomize/api-reference/glossary#kubernetes-style-object
|
||||||
[kustomization]: https://kubernetes-sigs.github.io/kustomize/api-reference/glossary#kustomization
|
[kustomization]: https://kubernetes-sigs.github.io/kustomize/api-reference/glossary#kustomization
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filters/annotations"
|
"sigs.k8s.io/kustomize/api/filters/annotations"
|
||||||
"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/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,14 +25,11 @@ func (p *AnnotationsTransformerPlugin) Config(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error {
|
func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||||
if len(p.Annotations) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for _, r := range m.Resources() {
|
for _, r := range m.Resources() {
|
||||||
err := r.ApplyFilter(annotations.Filter{
|
err := filtersutil.ApplyToJSON(annotations.Filter{
|
||||||
Annotations: p.Annotations,
|
Annotations: p.Annotations,
|
||||||
FsSlice: p.FieldSpecs,
|
FsSlice: p.FieldSpecs,
|
||||||
})
|
}, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ func (p *HashTransformerPlugin) Transform(m resmap.ResMap) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
res.SetOriginalName(res.GetName(), false)
|
|
||||||
res.SetName(fmt.Sprintf("%s-%s", res.GetName(), h))
|
res.SetName(fmt.Sprintf("%s-%s", res.GetName(), h))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,15 +6,12 @@ package builtins
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/imdario/mergo"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
@@ -65,9 +62,6 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf
|
|||||||
if p.Values == "" {
|
if p.Values == "" {
|
||||||
p.Values = path.Join(p.ChartHome, p.ChartName, "values.yaml")
|
p.Values = path.Join(p.ChartHome, p.ChartName, "values.yaml")
|
||||||
}
|
}
|
||||||
if p.ValuesMerge == "" {
|
|
||||||
p.ValuesMerge = "override"
|
|
||||||
}
|
|
||||||
// runHelmCommand will run `helm` command with args provided. Return stdout
|
// runHelmCommand will run `helm` command with args provided. Return stdout
|
||||||
// and error if there is any.
|
// and error if there is any.
|
||||||
p.runHelmCommand = func(args []string) ([]byte, error) {
|
p.runHelmCommand = func(args []string) ([]byte, error) {
|
||||||
@@ -94,63 +88,6 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeValues for writing
|
|
||||||
func (p *HelmChartInflationGeneratorPlugin) EncodeValues(w io.Writer) error {
|
|
||||||
d, err := yaml.Marshal(p.ValuesLocal)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = w.Write(d)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// useValuesLocal process (merge) inflator config provided values with chart default values.yaml
|
|
||||||
func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error {
|
|
||||||
fn := path.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml")
|
|
||||||
vf, err := os.Create(fn)
|
|
||||||
defer vf.Close()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// override, merge, none
|
|
||||||
if p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false" {
|
|
||||||
p.Values = fn
|
|
||||||
} else {
|
|
||||||
pValues, err := ioutil.ReadFile(p.Values)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
chValues := make(map[string]interface{})
|
|
||||||
err = yaml.Unmarshal(pValues, &chValues)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if p.ValuesMerge == "override" {
|
|
||||||
err = mergo.Merge(&chValues, p.ValuesLocal, mergo.WithOverride)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if p.ValuesMerge == "merge" {
|
|
||||||
err = mergo.Merge(&chValues, p.ValuesLocal)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p.ValuesLocal = chValues
|
|
||||||
p.Values = fn
|
|
||||||
}
|
|
||||||
err = p.EncodeValues(vf)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
vf.Sync()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate implements generator
|
// Generate implements generator
|
||||||
func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
||||||
// cleanup
|
// cleanup
|
||||||
@@ -167,15 +104,6 @@ func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// inflator config valuesLocal
|
|
||||||
if len(p.ValuesLocal) > 0 {
|
|
||||||
err := p.useValuesLocal()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// render the charts
|
// render the charts
|
||||||
stdout, err := p.runHelmCommand(p.getTemplateCommandArgs())
|
stdout, err := p.runHelmCommand(p.getTemplateCommandArgs())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -197,7 +125,6 @@ func (p *HelmChartInflationGeneratorPlugin) getTemplateCommandArgs() []string {
|
|||||||
if p.Values != "" {
|
if p.Values != "" {
|
||||||
args = append(args, "--values", p.Values)
|
args = append(args, "--values", p.Values)
|
||||||
}
|
}
|
||||||
args = append(args, p.ExtraArgs...)
|
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filters/imagetag"
|
"sigs.k8s.io/kustomize/api/filters/imagetag"
|
||||||
"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/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -31,17 +32,17 @@ func (p *ImageTagTransformerPlugin) Config(
|
|||||||
func (p *ImageTagTransformerPlugin) Transform(m resmap.ResMap) error {
|
func (p *ImageTagTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||||
for _, r := range m.Resources() {
|
for _, r := range m.Resources() {
|
||||||
// traverse all fields at first
|
// traverse all fields at first
|
||||||
err := r.ApplyFilter(imagetag.LegacyFilter{
|
err := filtersutil.ApplyToJSON(imagetag.LegacyFilter{
|
||||||
ImageTag: p.ImageTag,
|
ImageTag: p.ImageTag,
|
||||||
})
|
}, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// then use user specified field specs
|
// then use user specified field specs
|
||||||
err = r.ApplyFilter(imagetag.Filter{
|
err = filtersutil.ApplyToJSON(imagetag.Filter{
|
||||||
ImageTag: p.ImageTag,
|
ImageTag: p.ImageTag,
|
||||||
FsSlice: p.FieldSpecs,
|
FsSlice: p.FieldSpecs,
|
||||||
})
|
}, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filters/labels"
|
"sigs.k8s.io/kustomize/api/filters/labels"
|
||||||
"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/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,14 +25,11 @@ func (p *LabelTransformerPlugin) Config(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *LabelTransformerPlugin) Transform(m resmap.ResMap) error {
|
func (p *LabelTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||||
if len(p.Labels) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for _, r := range m.Resources() {
|
for _, r := range m.Resources() {
|
||||||
err := r.ApplyFilter(labels.Filter{
|
err := filtersutil.ApplyToJSON(labels.Filter{
|
||||||
Labels: p.Labels,
|
Labels: p.Labels,
|
||||||
FsSlice: p.FieldSpecs,
|
FsSlice: p.FieldSpecs,
|
||||||
})
|
}, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filters/namespace"
|
"sigs.k8s.io/kustomize/api/filters/namespace"
|
||||||
"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/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,11 +35,10 @@ 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)
|
err := filtersutil.ApplyToJSON(namespace.Filter{
|
||||||
err := r.ApplyFilter(namespace.Filter{
|
|
||||||
Namespace: p.Namespace,
|
Namespace: p.Namespace,
|
||||||
FsSlice: p.FieldSpecs,
|
FsSlice: p.FieldSpecs,
|
||||||
})
|
}, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"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/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -78,9 +79,9 @@ func (p *PatchJson6902TransformerPlugin) Transform(m resmap.ResMap) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, res := range resources {
|
for _, res := range resources {
|
||||||
err = res.ApplyFilter(patchjson6902.Filter{
|
err = filtersutil.ApplyToJSON(patchjson6902.Filter{
|
||||||
Patch: p.JsonOp,
|
Patch: p.JsonOp,
|
||||||
})
|
}, res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,14 +5,19 @@ package builtins
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"sigs.k8s.io/kustomize/api/filters/patchstrategicmerge"
|
||||||
"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"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PatchStrategicMergeTransformerPlugin struct {
|
type PatchStrategicMergeTransformerPlugin struct {
|
||||||
|
h *resmap.PluginHelpers
|
||||||
loadedPatches []*resource.Resource
|
loadedPatches []*resource.Resource
|
||||||
Paths []types.PatchStrategicMerge `json:"paths,omitempty" yaml:"paths,omitempty"`
|
Paths []types.PatchStrategicMerge `json:"paths,omitempty" yaml:"paths,omitempty"`
|
||||||
Patches string `json:"patches,omitempty" yaml:"patches,omitempty"`
|
Patches string `json:"patches,omitempty" yaml:"patches,omitempty"`
|
||||||
@@ -20,6 +25,7 @@ type PatchStrategicMergeTransformerPlugin struct {
|
|||||||
|
|
||||||
func (p *PatchStrategicMergeTransformerPlugin) Config(
|
func (p *PatchStrategicMergeTransformerPlugin) Config(
|
||||||
h *resmap.PluginHelpers, c []byte) (err error) {
|
h *resmap.PluginHelpers, c []byte) (err error) {
|
||||||
|
p.h = h
|
||||||
err = yaml.Unmarshal(c, p)
|
err = yaml.Unmarshal(c, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -29,18 +35,13 @@ func (p *PatchStrategicMergeTransformerPlugin) Config(
|
|||||||
}
|
}
|
||||||
if len(p.Paths) != 0 {
|
if len(p.Paths) != 0 {
|
||||||
for _, onePath := range p.Paths {
|
for _, onePath := range p.Paths {
|
||||||
// The following oddly attempts to interpret a path string as an
|
res, err := p.h.ResmapFactory().RF().SliceFromBytes([]byte(onePath))
|
||||||
// actual patch (instead of as a path to a file containing a patch).
|
|
||||||
// All tests pass if this code is commented out. This code should
|
|
||||||
// be deleted; the user should use the Patches field which
|
|
||||||
// exists for this purpose (inline patch declaration).
|
|
||||||
res, err := h.ResmapFactory().RF().SliceFromBytes([]byte(onePath))
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
p.loadedPatches = append(p.loadedPatches, res...)
|
p.loadedPatches = append(p.loadedPatches, res...)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
res, err = h.ResmapFactory().RF().SliceFromPatches(
|
res, err = p.h.ResmapFactory().RF().SliceFromPatches(
|
||||||
h.Loader(), []types.PatchStrategicMerge{onePath})
|
p.h.Loader(), []types.PatchStrategicMerge{onePath})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -48,7 +49,7 @@ func (p *PatchStrategicMergeTransformerPlugin) Config(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if p.Patches != "" {
|
if p.Patches != "" {
|
||||||
res, err := h.ResmapFactory().RF().SliceFromBytes([]byte(p.Patches))
|
res, err := p.h.ResmapFactory().RF().SliceFromBytes([]byte(p.Patches))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -59,25 +60,57 @@ func (p *PatchStrategicMergeTransformerPlugin) Config(
|
|||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"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.
|
return err
|
||||||
m, err := h.ResmapFactory().ConflatePatches(p.loadedPatches)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
p.loadedPatches = m.Resources()
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PatchStrategicMergeTransformerPlugin) Transform(m resmap.ResMap) error {
|
func (p *PatchStrategicMergeTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||||
for _, patch := range p.loadedPatches {
|
patches, err := p.h.ResmapFactory().Merge(p.loadedPatches)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, patch := range patches.Resources() {
|
||||||
target, err := m.GetById(patch.OrgId())
|
target, err := m.GetById(patch.OrgId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = m.ApplySmPatch(
|
patchCopy := patch.DeepCopy()
|
||||||
resource.MakeIdSet([]*resource.Resource{target}), patch); err != nil {
|
patchCopy.SetName(target.GetName())
|
||||||
|
patchCopy.SetNamespace(target.GetNamespace())
|
||||||
|
patchCopy.SetGvk(target.GetGvk())
|
||||||
|
node, err := filtersutil.GetRNode(patchCopy)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
err = filtersutil.ApplyToJSON(patchstrategicmerge.Filter{
|
||||||
|
Patch: node,
|
||||||
|
}, target)
|
||||||
|
if err != nil {
|
||||||
|
// Check for an error string from UnmarshalJSON that's indicative
|
||||||
|
// of an object that's missing basic KRM fields, and thus may have been
|
||||||
|
// entirely deleted (an acceptable outcome). This error handling should
|
||||||
|
// be deleted along with use of ResMap and apimachinery functions like
|
||||||
|
// UnmarshalJSON.
|
||||||
|
if !strings.Contains(err.Error(), "Object 'Kind' is missing") {
|
||||||
|
// Some unknown error, let it through.
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !target.IsEmpty() {
|
||||||
|
return errors.Wrapf(
|
||||||
|
err, "with unexpectedly non-empty object map of size %d",
|
||||||
|
len(target.Map()))
|
||||||
|
}
|
||||||
|
// Fall through to handle deleted object.
|
||||||
|
}
|
||||||
|
if target.IsEmpty() {
|
||||||
|
// This means all fields have been removed from the object.
|
||||||
|
// This can happen if a patch required deletion of the
|
||||||
|
// entire resource (not just a part of it). This means
|
||||||
|
// the overall resmap must shrink by one.
|
||||||
|
err = m.Remove(target.CurId())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,10 +8,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
jsonpatch "github.com/evanphx/json-patch"
|
jsonpatch "github.com/evanphx/json-patch"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/api/filters/patchjson6902"
|
"sigs.k8s.io/kustomize/api/filters/patchjson6902"
|
||||||
|
"sigs.k8s.io/kustomize/api/filters/patchstrategicmerge"
|
||||||
"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"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,6 +41,7 @@ func (p *PatchTransformerPlugin) Config(
|
|||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"patch and path can't be set at the same time\n%s", string(c))
|
"patch and path can't be set at the same time\n%s", string(c))
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Path != "" {
|
if p.Path != "" {
|
||||||
loaded, loadErr := h.Loader().Load(p.Path)
|
loaded, loadErr := h.Loader().Load(p.Path)
|
||||||
if loadErr != nil {
|
if loadErr != nil {
|
||||||
@@ -84,13 +88,66 @@ func (p *PatchTransformerPlugin) transformStrategicMerge(m resmap.ResMap, patch
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return target.ApplySmPatch(patch)
|
return p.applySMPatch(target, patch)
|
||||||
}
|
}
|
||||||
selected, err := m.Select(*p.Target)
|
|
||||||
|
resources, err := m.Select(*p.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return m.ApplySmPatch(resource.MakeIdSet(selected), patch)
|
for _, res := range resources {
|
||||||
|
patchCopy := patch.DeepCopy()
|
||||||
|
patchCopy.SetName(res.GetName())
|
||||||
|
patchCopy.SetNamespace(res.GetNamespace())
|
||||||
|
patchCopy.SetGvk(res.GetGvk())
|
||||||
|
err := p.applySMPatch(res, patchCopy)
|
||||||
|
if err != nil {
|
||||||
|
// Check for an error string from UnmarshalJSON that's indicative
|
||||||
|
// of an object that's missing basic KRM fields, and thus may have been
|
||||||
|
// entirely deleted (an acceptable outcome). This error handling should
|
||||||
|
// be deleted along with use of ResMap and apimachinery functions like
|
||||||
|
// UnmarshalJSON.
|
||||||
|
if !strings.Contains(err.Error(), "Object 'Kind' is missing") {
|
||||||
|
// Some unknown error, let it through.
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !res.IsEmpty() {
|
||||||
|
return errors.Wrapf(
|
||||||
|
err, "with unexpectedly non-empty object map of size %d",
|
||||||
|
len(res.Map()))
|
||||||
|
}
|
||||||
|
// Fall through to handle deleted object.
|
||||||
|
}
|
||||||
|
if res.IsEmpty() {
|
||||||
|
// This means all fields have been removed from the object.
|
||||||
|
// This can happen if a patch required deletion of the
|
||||||
|
// entire resource (not just a part of it). This means
|
||||||
|
// the overall resmap must shrink by one.
|
||||||
|
err = m.Remove(res.CurId())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// applySMPatch applies the provided strategic merge patch to the
|
||||||
|
// given resource.
|
||||||
|
func (p *PatchTransformerPlugin) applySMPatch(resource, patch *resource.Resource) error {
|
||||||
|
node, err := filtersutil.GetRNode(patch)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n, ns := resource.GetName(), resource.GetNamespace()
|
||||||
|
err = filtersutil.ApplyToJSON(patchstrategicmerge.Filter{
|
||||||
|
Patch: node,
|
||||||
|
}, resource)
|
||||||
|
if !resource.IsEmpty() {
|
||||||
|
resource.SetName(n)
|
||||||
|
resource.SetNamespace(ns)
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// transformJson6902 applies the provided json6902 patch
|
// transformJson6902 applies the provided json6902 patch
|
||||||
@@ -104,10 +161,9 @@ 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)
|
err = filtersutil.ApplyToJSON(patchjson6902.Filter{
|
||||||
err = res.ApplyFilter(patchjson6902.Filter{
|
|
||||||
Patch: p.Patch,
|
Patch: p.Patch,
|
||||||
})
|
}, res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"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/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -66,18 +67,14 @@ func (p *PrefixSuffixTransformerPlugin) Transform(m resmap.ResMap) error {
|
|||||||
// this will add a prefix and a suffix
|
// this will add a prefix and a suffix
|
||||||
// to the resource even if those are
|
// to the resource even if those are
|
||||||
// empty
|
// empty
|
||||||
|
|
||||||
r.AddNamePrefix(p.Prefix)
|
r.AddNamePrefix(p.Prefix)
|
||||||
r.AddNameSuffix(p.Suffix)
|
r.AddNameSuffix(p.Suffix)
|
||||||
if p.Prefix != "" || p.Suffix != "" {
|
|
||||||
r.SetOriginalName(r.GetName(), false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
err := r.ApplyFilter(prefixsuffix.Filter{
|
err := filtersutil.ApplyToJSON(prefixsuffix.Filter{
|
||||||
Prefix: p.Prefix,
|
Prefix: p.Prefix,
|
||||||
Suffix: p.Suffix,
|
Suffix: p.Suffix,
|
||||||
FieldSpec: fs,
|
FieldSpec: fs,
|
||||||
})
|
}, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filters/replicacount"
|
"sigs.k8s.io/kustomize/api/filters/replicacount"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||||
|
|
||||||
"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/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
@@ -40,10 +42,10 @@ func (p *ReplicaCountTransformerPlugin) Transform(m resmap.ResMap) error {
|
|||||||
// There are redundant checks in the filter
|
// There are redundant checks in the filter
|
||||||
// that we'll live with until resolution of
|
// that we'll live with until resolution of
|
||||||
// https://github.com/kubernetes-sigs/kustomize/issues/2506
|
// https://github.com/kubernetes-sigs/kustomize/issues/2506
|
||||||
err := r.ApplyFilter(replicacount.Filter{
|
err := filtersutil.ApplyToJSON(replicacount.Filter{
|
||||||
Replica: p.Replica,
|
Replica: p.Replica,
|
||||||
FieldSpec: fs,
|
FieldSpec: fs,
|
||||||
})
|
}, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"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"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -118,15 +119,15 @@ func (p *ValueAddTransformerPlugin) Transform(m resmap.ResMap) (err error) {
|
|||||||
// TODO: consider t.NotSelector if implemented
|
// TODO: consider t.NotSelector if implemented
|
||||||
for _, res := range resources {
|
for _, res := range resources {
|
||||||
if t.FieldPath == types.MetadataNamespacePath {
|
if t.FieldPath == types.MetadataNamespacePath {
|
||||||
err = res.ApplyFilter(namespace.Filter{
|
err = filtersutil.ApplyToJSON(namespace.Filter{
|
||||||
Namespace: p.Value,
|
Namespace: p.Value,
|
||||||
})
|
}, res)
|
||||||
} else {
|
} else {
|
||||||
err = res.ApplyFilter(valueadd.Filter{
|
err = filtersutil.ApplyToJSON(valueadd.Filter{
|
||||||
Value: p.Value,
|
Value: p.Value,
|
||||||
FieldPath: t.FieldPath,
|
FieldPath: t.FieldPath,
|
||||||
FilePathPosition: t.FilePathPosition,
|
FilePathPosition: t.FilePathPosition,
|
||||||
})
|
}, res)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type Filter struct {
|
|||||||
var _ kio.Filter = Filter{}
|
var _ kio.Filter = Filter{}
|
||||||
|
|
||||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||||
keys := yaml.SortedMapKeys(f.Annotations)
|
keys := filtersutil.SortedMapKeys(f.Annotations)
|
||||||
_, err := kio.FilterAll(yaml.FilterFunc(
|
_, err := kio.FilterAll(yaml.FilterFunc(
|
||||||
func(node *yaml.RNode) (*yaml.RNode, error) {
|
func(node *yaml.RNode) (*yaml.RNode, error) {
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
|
|||||||
@@ -28,9 +28,7 @@ metadata:
|
|||||||
`)}},
|
`)}},
|
||||||
Filters: []kio.Filter{Filter{
|
Filters: []kio.Filter{Filter{
|
||||||
Annotations: map[string]string{
|
Annotations: map[string]string{
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
"booleanValue": "true",
|
|
||||||
"numberValue": "42",
|
|
||||||
},
|
},
|
||||||
FsSlice: fss,
|
FsSlice: fss,
|
||||||
}},
|
}},
|
||||||
@@ -46,16 +44,12 @@ metadata:
|
|||||||
// metadata:
|
// metadata:
|
||||||
// name: instance
|
// name: instance
|
||||||
// annotations:
|
// annotations:
|
||||||
// booleanValue: "true"
|
|
||||||
// foo: bar
|
// foo: bar
|
||||||
// numberValue: "42"
|
|
||||||
// ---
|
// ---
|
||||||
// apiVersion: example.com/v1
|
// apiVersion: example.com/v1
|
||||||
// kind: Bar
|
// kind: Bar
|
||||||
// metadata:
|
// metadata:
|
||||||
// name: instance
|
// name: instance
|
||||||
// annotations:
|
// annotations:
|
||||||
// booleanValue: "true"
|
|
||||||
// foo: bar
|
// foo: bar
|
||||||
// numberValue: "42"
|
|
||||||
}
|
}
|
||||||
|
|||||||
21
api/filters/filtersutil/filtersutil.go
Normal file
21
api/filters/filtersutil/filtersutil.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package filtersutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SortedMapKeys returns a sorted slice of keys to the given map.
|
||||||
|
// Writing this function never gets old.
|
||||||
|
func SortedMapKeys(m map[string]string) []string {
|
||||||
|
keys := make([]string, len(m))
|
||||||
|
i := 0
|
||||||
|
for k := range m {
|
||||||
|
keys[i] = k
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
return keys
|
||||||
|
}
|
||||||
@@ -1,13 +1,10 @@
|
|||||||
// Copyright 2021 The Kubernetes Authors.
|
package filtersutil_test
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package yaml_test
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSortedKeys(t *testing.T) {
|
func TestSortedKeys(t *testing.T) {
|
||||||
@@ -26,10 +23,9 @@ func TestSortedKeys(t *testing.T) {
|
|||||||
expected: []string{"a", "b", "c"}},
|
expected: []string{"a", "b", "c"}},
|
||||||
}
|
}
|
||||||
for tn, tc := range testCases {
|
for tn, tc := range testCases {
|
||||||
tc := tc
|
|
||||||
t.Run(tn, func(t *testing.T) {
|
t.Run(tn, func(t *testing.T) {
|
||||||
if !assert.Equal(t,
|
if !assert.Equal(t,
|
||||||
yaml.SortedMapKeys(tc.input),
|
filtersutil.SortedMapKeys(tc.input),
|
||||||
tc.expected) {
|
tc.expected) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,7 @@ func TestImageTagUpdater_Filter(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
"ignore CustomResourceDefinition": {
|
"ignore CustomResourceDefinition": {
|
||||||
input: `
|
input: `
|
||||||
apiVersion: apiextensions.k8s.io/v1
|
apiVersion: apiextensions.k8s.io/v1beta1
|
||||||
kind: CustomResourceDefinition
|
kind: CustomResourceDefinition
|
||||||
metadata:
|
metadata:
|
||||||
name: whatever
|
name: whatever
|
||||||
@@ -30,7 +30,7 @@ spec:
|
|||||||
- image: whatever
|
- image: whatever
|
||||||
`,
|
`,
|
||||||
expectedOutput: `
|
expectedOutput: `
|
||||||
apiVersion: apiextensions.k8s.io/v1
|
apiVersion: apiextensions.k8s.io/v1beta1
|
||||||
kind: CustomResourceDefinition
|
kind: CustomResourceDefinition
|
||||||
metadata:
|
metadata:
|
||||||
name: whatever
|
name: whatever
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type Filter struct {
|
|||||||
var _ kio.Filter = Filter{}
|
var _ kio.Filter = Filter{}
|
||||||
|
|
||||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||||
keys := yaml.SortedMapKeys(f.Labels)
|
keys := filtersutil.SortedMapKeys(f.Labels)
|
||||||
_, err := kio.FilterAll(yaml.FilterFunc(
|
_, err := kio.FilterAll(yaml.FilterFunc(
|
||||||
func(node *yaml.RNode) (*yaml.RNode, error) {
|
func(node *yaml.RNode) (*yaml.RNode, error) {
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
||||||
|
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||||
"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"
|
||||||
@@ -15,36 +16,22 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Filter updates a name references.
|
// Filter will update the name reference
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
// Referrer is the object that refers to something else by a name,
|
FieldSpec types.FieldSpec `json:"fieldSpec,omitempty" yaml:"fieldSpec,omitempty"`
|
||||||
// a name that this filter seeks to update.
|
Referrer *resource.Resource
|
||||||
Referrer *resource.Resource
|
Target resid.Gvk
|
||||||
|
|
||||||
// NameFieldToUpdate is the field in the Referrer that holds the
|
|
||||||
// name requiring an update.
|
|
||||||
NameFieldToUpdate types.FieldSpec `json:"nameFieldToUpdate,omitempty" yaml:"nameFieldToUpdate,omitempty"`
|
|
||||||
|
|
||||||
// Source of the new value for the name (in its name field).
|
|
||||||
ReferralTarget resid.Gvk
|
|
||||||
|
|
||||||
// Set of resources to hunt through to find the ReferralTarget.
|
|
||||||
ReferralCandidates resmap.ResMap
|
ReferralCandidates resmap.ResMap
|
||||||
|
isRoleRef bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// At time of writing, in practice this is called with a slice with only
|
|
||||||
// one entry, the node also referred to be the resource in the Referrer field.
|
|
||||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
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
|
|
||||||
// that's how the referrer's name field is updated.
|
|
||||||
// However, this filter still needs the extra methods on Referrer
|
|
||||||
// to consult things like the resource Id, its namespace, etc.
|
|
||||||
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{
|
err := node.PipeE(fieldspec.Filter{
|
||||||
FieldSpec: f.NameFieldToUpdate,
|
FieldSpec: f.FieldSpec,
|
||||||
SetValue: f.set,
|
SetValue: f.set,
|
||||||
})
|
})
|
||||||
return node, err
|
return node, err
|
||||||
@@ -54,102 +41,58 @@ func (f Filter) set(node *yaml.RNode) error {
|
|||||||
if yaml.IsMissingOrNull(node) {
|
if yaml.IsMissingOrNull(node) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if strings.HasSuffix(f.FieldSpec.Path, "roleRef/name") {
|
||||||
|
f.isRoleRef = true
|
||||||
|
}
|
||||||
switch node.YNode().Kind {
|
switch node.YNode().Kind {
|
||||||
case yaml.ScalarNode:
|
case yaml.ScalarNode:
|
||||||
return f.setScalar(node)
|
return f.setScalar(node)
|
||||||
case yaml.MappingNode:
|
case yaml.MappingNode:
|
||||||
|
// Kind: ValidatingWebhookConfiguration
|
||||||
|
// FieldSpec is webhooks/clientConfig/service
|
||||||
return f.setMapping(node)
|
return f.setMapping(node)
|
||||||
case yaml.SequenceNode:
|
case yaml.SequenceNode:
|
||||||
return applyFilterToSeq(seqFilter{
|
return f.setSequence(node)
|
||||||
setScalarFn: f.setScalar,
|
|
||||||
setMappingFn: f.setMapping,
|
|
||||||
}, node)
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"node is expected to be either a string or a slice of string or a map of string")
|
"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.
|
func (f Filter) setSequence(node *yaml.RNode) error {
|
||||||
|
return applyFilterToSeq(seqFilter{
|
||||||
|
setScalarFn: f.setScalar,
|
||||||
|
setMappingFn: f.setMapping,
|
||||||
|
}, node)
|
||||||
|
}
|
||||||
|
|
||||||
func (f Filter) setMapping(node *yaml.RNode) error {
|
func (f Filter) setMapping(node *yaml.RNode) error {
|
||||||
if node.YNode().Kind != yaml.MappingNode {
|
return setNameAndNs(
|
||||||
return fmt.Errorf("expect a mapping node")
|
node,
|
||||||
}
|
f.Referrer,
|
||||||
nameNode, err := node.Pipe(yaml.FieldMatcher{Name: "name"})
|
f.Target,
|
||||||
if err != nil || nameNode == nil {
|
f.ReferralCandidates,
|
||||||
return fmt.Errorf("cannot find field 'name' in node")
|
f.isRoleRef,
|
||||||
}
|
)
|
||||||
namespaceNode, err := node.Pipe(yaml.FieldMatcher{Name: "namespace"})
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error when find field 'namespace'")
|
|
||||||
}
|
|
||||||
|
|
||||||
// name will not be updated if the namespace doesn't match
|
|
||||||
subset := f.ReferralCandidates.Resources()
|
|
||||||
if namespaceNode != nil {
|
|
||||||
namespace := namespaceNode.YNode().Value
|
|
||||||
bynamespace := f.ReferralCandidates.GroupedByOriginalNamespace()
|
|
||||||
if _, ok := bynamespace[namespace]; !ok {
|
|
||||||
bynamespace = f.ReferralCandidates.GroupedByCurrentNamespace()
|
|
||||||
if _, ok := bynamespace[namespace]; !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
subset = bynamespace[namespace]
|
|
||||||
}
|
|
||||||
|
|
||||||
oldName := nameNode.YNode().Value
|
|
||||||
res, err := f.selectReferral(oldName, subset)
|
|
||||||
if err != nil || res == nil {
|
|
||||||
// Nil res means nothing to do.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
f.recordTheReferral(res)
|
|
||||||
if res.GetName() == oldName && res.GetNamespace() == "" {
|
|
||||||
// The name has not changed, nothing to do.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
err = node.PipeE(yaml.FieldSetter{
|
|
||||||
Name: "name",
|
|
||||||
StringValue: res.GetName(),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if res.GetNamespace() != "" {
|
|
||||||
// We don't want value "" to replace value "default" since
|
|
||||||
// the empty string is handled as a wild card here not default namespace
|
|
||||||
// by kubernetes.
|
|
||||||
err = node.PipeE(yaml.FieldSetter{
|
|
||||||
Name: "namespace",
|
|
||||||
StringValue: res.GetNamespace(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Filter) setScalar(node *yaml.RNode) error {
|
func (f Filter) setScalar(node *yaml.RNode) error {
|
||||||
res, err := f.selectReferral(
|
newValue, err := getSimpleNameField(
|
||||||
node.YNode().Value, f.ReferralCandidates.Resources())
|
node.YNode().Value,
|
||||||
if err != nil || res == nil {
|
f.Referrer,
|
||||||
// Nil res means nothing to do.
|
f.Target,
|
||||||
|
f.ReferralCandidates,
|
||||||
|
f.ReferralCandidates.Resources(),
|
||||||
|
f.isRoleRef,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f.recordTheReferral(res)
|
err = filtersutil.SetScalar(newValue)(node)
|
||||||
if res.GetName() == node.YNode().Value {
|
if err != nil {
|
||||||
// The name has not changed, nothing to do.
|
return err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
return node.PipeE(yaml.FieldSetter{StringValue: res.GetName()})
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
// In the resource, make a note that it is referred to by the referrer.
|
|
||||||
func (f Filter) recordTheReferral(res *resource.Resource) {
|
|
||||||
res.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
|
||||||
@@ -171,16 +114,14 @@ func getRoleRefGvk(res json.Marshaler) (*resid.Gvk, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if apiGroup.IsNil() {
|
if apiGroup.IsNil() {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf("apiGroup cannot be found in roleRef %s", roleRef.MustString())
|
||||||
"apiGroup cannot be found in roleRef %s", roleRef.MustString())
|
|
||||||
}
|
}
|
||||||
kind, err := roleRef.Pipe(yaml.Lookup("kind"))
|
kind, err := roleRef.Pipe(yaml.Lookup("kind"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if kind.IsNil() {
|
if kind.IsNil() {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf("kind cannot be found in roleRef %s", roleRef.MustString())
|
||||||
"kind cannot be found in roleRef %s", roleRef.MustString())
|
|
||||||
}
|
}
|
||||||
return &resid.Gvk{
|
return &resid.Gvk{
|
||||||
Group: apiGroup.YNode().Value,
|
Group: apiGroup.YNode().Value,
|
||||||
@@ -188,17 +129,19 @@ func getRoleRefGvk(res json.Marshaler) (*resid.Gvk, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Filter) filterReferralCandidates(
|
func filterReferralCandidates(
|
||||||
matches []*resource.Resource) []*resource.Resource {
|
referrer *resource.Resource,
|
||||||
|
matches []*resource.Resource,
|
||||||
|
target resid.Gvk,
|
||||||
|
) []*resource.Resource {
|
||||||
var ret []*resource.Resource
|
var ret []*resource.Resource
|
||||||
for _, m := range matches {
|
for _, m := range matches {
|
||||||
// If target kind is not ServiceAccount, we shouldn't consider condidates which
|
// If target kind is not ServiceAccount, we shouldn't consider condidates which
|
||||||
// doesn't have same namespace.
|
// doesn't have same namespace.
|
||||||
if f.ReferralTarget.Kind != "ServiceAccount" &&
|
if target.Kind != "ServiceAccount" && m.GetNamespace() != referrer.GetNamespace() {
|
||||||
m.GetNamespace() != f.Referrer.GetNamespace() {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !f.Referrer.PrefixesSuffixesEquals(m) {
|
if !referrer.PrefixesSuffixesEquals(m) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ret = append(ret, m)
|
ret = append(ret, m)
|
||||||
@@ -207,57 +150,72 @@ func (f Filter) filterReferralCandidates(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// selectReferral picks the referral among a subset of candidates.
|
// selectReferral picks the referral among a subset of candidates.
|
||||||
// The content of the candidateSubset slice is most of the time
|
// It returns the current name and namespace of the selected candidate.
|
||||||
// identical to the ReferralCandidates resmap. Still in some cases, such
|
// Note that the content of the referricalCandidateSubset slice is most of the time
|
||||||
|
// identical to the referralCandidates resmap. Still in some cases, such
|
||||||
// as ClusterRoleBinding, the subset only contains the resources of a specific
|
// as ClusterRoleBinding, the subset only contains the resources of a specific
|
||||||
// namespace.
|
// namespace.
|
||||||
func (f Filter) selectReferral(
|
func selectReferral(
|
||||||
oldName string,
|
oldName string,
|
||||||
candidateSubset []*resource.Resource) (*resource.Resource, error) {
|
referrer *resource.Resource,
|
||||||
|
target resid.Gvk,
|
||||||
|
referralCandidates resmap.ResMap,
|
||||||
|
referralCandidateSubset []*resource.Resource,
|
||||||
|
isRoleRef bool) (string, string, error) {
|
||||||
var roleRefGvk *resid.Gvk
|
var roleRefGvk *resid.Gvk
|
||||||
if f.isRoleRef() {
|
if isRoleRef {
|
||||||
var err error
|
var err error
|
||||||
roleRefGvk, err = getRoleRefGvk(f.Referrer)
|
roleRefGvk, err = getRoleRefGvk(referrer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, res := range candidateSubset {
|
for _, res := range referralCandidateSubset {
|
||||||
if res.GetOriginalName() != oldName {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
id := res.OrgId()
|
id := res.OrgId()
|
||||||
if !id.IsSelected(&f.ReferralTarget) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// If the we are processing a roleRef, the apiGroup and Kind in the
|
// If the we are processing a roleRef, the apiGroup and Kind in the
|
||||||
// roleRef are needed to be considered.
|
// roleRef are needed to be considered.
|
||||||
if f.isRoleRef() && !id.IsSelected(roleRefGvk) {
|
if (!isRoleRef || id.IsSelected(roleRefGvk)) &&
|
||||||
continue
|
id.IsSelected(&target) && res.GetOriginalName() == oldName {
|
||||||
}
|
matches := referralCandidates.GetMatchingResourcesByOriginalId(id.Equals)
|
||||||
matches := f.ReferralCandidates.GetMatchingResourcesByOriginalId(id.Equals)
|
// If there's more than one match,
|
||||||
// If there's more than one match,
|
// filter the matches by prefix and suffix
|
||||||
// filter the matches by prefix and suffix
|
if len(matches) > 1 {
|
||||||
if len(matches) > 1 {
|
filteredMatches := filterReferralCandidates(referrer, matches, target)
|
||||||
filteredMatches := f.filterReferralCandidates(matches)
|
if len(filteredMatches) > 1 {
|
||||||
if len(filteredMatches) > 1 {
|
return "", "", fmt.Errorf(
|
||||||
return nil, fmt.Errorf(
|
"multiple matches for %s:\n %v",
|
||||||
"multiple matches for %s:\n %v",
|
id, getIds(filteredMatches))
|
||||||
id, getIds(filteredMatches))
|
}
|
||||||
}
|
// Check is the match the resource we are working on
|
||||||
// Check is the match the resource we are working on
|
if len(filteredMatches) == 0 || res != filteredMatches[0] {
|
||||||
if len(filteredMatches) == 0 || res != filteredMatches[0] {
|
continue
|
||||||
continue
|
}
|
||||||
}
|
}
|
||||||
|
// In the resource, note that it is referenced
|
||||||
|
// by the referrer.
|
||||||
|
res.AppendRefBy(referrer.CurId())
|
||||||
|
// Return transformed name of the object,
|
||||||
|
// complete with prefixes, hashes, etc.
|
||||||
|
return res.GetName(), res.GetNamespace(), nil
|
||||||
}
|
}
|
||||||
// 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
|
|
||||||
|
return oldName, "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// utility function to replace a simple string by the new name
|
||||||
|
func getSimpleNameField(
|
||||||
|
oldName string,
|
||||||
|
referrer *resource.Resource,
|
||||||
|
target resid.Gvk,
|
||||||
|
referralCandidates resmap.ResMap,
|
||||||
|
referralCandidateSubset []*resource.Resource,
|
||||||
|
isRoleRef bool) (string, error) {
|
||||||
|
|
||||||
|
newName, _, err := selectReferral(oldName, referrer, target,
|
||||||
|
referralCandidates, referralCandidateSubset, isRoleRef)
|
||||||
|
|
||||||
|
return newName, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIds(rs []*resource.Resource) []string {
|
func getIds(rs []*resource.Resource) []string {
|
||||||
@@ -267,3 +225,73 @@ func getIds(rs []*resource.Resource) []string {
|
|||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// utility function to replace name field within a map RNode
|
||||||
|
// and leverage the namespace field.
|
||||||
|
func setNameAndNs(
|
||||||
|
in *yaml.RNode,
|
||||||
|
referrer *resource.Resource,
|
||||||
|
target resid.Gvk,
|
||||||
|
referralCandidates resmap.ResMap,
|
||||||
|
isRoleRef bool) error {
|
||||||
|
|
||||||
|
if in.YNode().Kind != yaml.MappingNode {
|
||||||
|
return fmt.Errorf("expect a mapping node")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get name field
|
||||||
|
nameNode, err := in.Pipe(yaml.FieldMatcher{
|
||||||
|
Name: "name",
|
||||||
|
})
|
||||||
|
if err != nil || nameNode == nil {
|
||||||
|
return fmt.Errorf("cannot find field 'name' in node")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get namespace field
|
||||||
|
namespaceNode, err := in.Pipe(yaml.FieldMatcher{
|
||||||
|
Name: "namespace",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error when find field 'namespace'")
|
||||||
|
}
|
||||||
|
|
||||||
|
// check is namespace matched
|
||||||
|
// name will bot be updated if the namespace doesn't match
|
||||||
|
subset := referralCandidates.Resources()
|
||||||
|
if namespaceNode != nil {
|
||||||
|
namespace := namespaceNode.YNode().Value
|
||||||
|
bynamespace := referralCandidates.GroupedByOriginalNamespace()
|
||||||
|
if _, ok := bynamespace[namespace]; !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
subset = bynamespace[namespace]
|
||||||
|
}
|
||||||
|
|
||||||
|
oldName := nameNode.YNode().Value
|
||||||
|
newname, newnamespace, err := selectReferral(oldName, referrer, target,
|
||||||
|
referralCandidates, subset, isRoleRef)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newname == oldName) && (newnamespace == "") {
|
||||||
|
// no candidate found.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// set name
|
||||||
|
in.Pipe(yaml.FieldSetter{
|
||||||
|
Name: "name",
|
||||||
|
StringValue: newname,
|
||||||
|
})
|
||||||
|
if newnamespace != "" {
|
||||||
|
// We don't want value "" to replace value "default" since
|
||||||
|
// the empty string is handled as a wild card here not default namespace
|
||||||
|
// by kubernetes.
|
||||||
|
in.Pipe(yaml.FieldSetter{
|
||||||
|
Name: "namespace",
|
||||||
|
StringValue: newnamespace,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,9 +5,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"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"
|
||||||
filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest"
|
filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
@@ -50,8 +51,8 @@ ref:
|
|||||||
name: newName
|
name: newName
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -90,8 +91,8 @@ seq:
|
|||||||
- oldName2
|
- oldName2
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "seq"},
|
FieldSpec: types.FieldSpec{Path: "seq"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -128,8 +129,8 @@ map:
|
|||||||
name: newName
|
name: newName
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "map"},
|
FieldSpec: types.FieldSpec{Path: "map"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -169,8 +170,8 @@ map:
|
|||||||
namespace: oldNs
|
namespace: oldNs
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "map"},
|
FieldSpec: types.FieldSpec{Path: "map"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -207,8 +208,8 @@ map:
|
|||||||
name: null
|
name: null
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "map"},
|
FieldSpec: types.FieldSpec{Path: "map"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -219,7 +220,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 := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
||||||
referrer, err := factory.FromBytes([]byte(tc.input))
|
referrer, err := factory.FromBytes([]byte(tc.input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -277,8 +278,8 @@ metadata:
|
|||||||
originalNames: []string{"oldName", "oldName"},
|
originalNames: []string{"oldName", "oldName"},
|
||||||
expected: "",
|
expected: "",
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -308,8 +309,8 @@ metadata:
|
|||||||
originalNames: []string{"oldName", "oldName"},
|
originalNames: []string{"oldName", "oldName"},
|
||||||
expected: "",
|
expected: "",
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref"},
|
FieldSpec: types.FieldSpec{Path: "ref"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -320,7 +321,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 := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
||||||
referrer, err := factory.FromBytes([]byte(tc.input))
|
referrer, err := factory.FromBytes([]byte(tc.input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -395,8 +396,8 @@ ref:
|
|||||||
name: newName
|
name: newName
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -438,8 +439,8 @@ ref:
|
|||||||
name: newName
|
name: newName
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -481,8 +482,8 @@ ref:
|
|||||||
name: newName
|
name: newName
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -517,8 +518,8 @@ metadata:
|
|||||||
inputSuffix: "suffix",
|
inputSuffix: "suffix",
|
||||||
expected: "",
|
expected: "",
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -553,8 +554,8 @@ metadata:
|
|||||||
inputSuffix: "",
|
inputSuffix: "",
|
||||||
expected: "",
|
expected: "",
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -589,8 +590,8 @@ metadata:
|
|||||||
inputSuffix: "suffix",
|
inputSuffix: "suffix",
|
||||||
expected: "",
|
expected: "",
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -632,8 +633,8 @@ ref:
|
|||||||
name: oldName
|
name: oldName
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -675,8 +676,8 @@ ref:
|
|||||||
name: oldName
|
name: oldName
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -718,8 +719,8 @@ ref:
|
|||||||
name: oldName
|
name: oldName
|
||||||
`,
|
`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
NameFieldToUpdate: types.FieldSpec{Path: "ref/name"},
|
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||||
ReferralTarget: resid.Gvk{
|
Target: resid.Gvk{
|
||||||
Group: "apps",
|
Group: "apps",
|
||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Secret",
|
Kind: "Secret",
|
||||||
@@ -731,7 +732,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 := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
||||||
referrer, err := factory.FromBytes([]byte(tc.input))
|
referrer, err := factory.FromBytes([]byte(tc.input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
package patchstrategicmerge
|
package patchstrategicmerge
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml/merge2"
|
"sigs.k8s.io/kustomize/kyaml/yaml/merge2"
|
||||||
@@ -16,7 +15,6 @@ type Filter struct {
|
|||||||
|
|
||||||
var _ kio.Filter = Filter{}
|
var _ kio.Filter = Filter{}
|
||||||
|
|
||||||
// Filter does a strategic merge patch, which can delete nodes.
|
|
||||||
func (pf Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
func (pf Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||||
var result []*yaml.RNode
|
var result []*yaml.RNode
|
||||||
for i := range nodes {
|
for i := range nodes {
|
||||||
@@ -29,9 +27,7 @@ func (pf Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !konfig.FlagEnableKyamlDefaultValue || r != nil {
|
result = append(result, r)
|
||||||
result = append(result, r)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,101 +15,6 @@ func TestFilter(t *testing.T) {
|
|||||||
patch *yaml.RNode
|
patch *yaml.RNode
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
"simple": {
|
|
||||||
input: `apiVersion: v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: clown
|
|
||||||
spec:
|
|
||||||
numReplicas: 1
|
|
||||||
`,
|
|
||||||
patch: yaml.MustParse(`apiVersion: v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: clown
|
|
||||||
spec:
|
|
||||||
numReplicas: 999
|
|
||||||
`),
|
|
||||||
expected: `apiVersion: v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: clown
|
|
||||||
spec:
|
|
||||||
numReplicas: 999
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
"nullMapEntry1": {
|
|
||||||
input: `
|
|
||||||
apiVersion: example.com/v1
|
|
||||||
kind: Foo
|
|
||||||
metadata:
|
|
||||||
name: my-foo
|
|
||||||
spec:
|
|
||||||
bar:
|
|
||||||
B:
|
|
||||||
C: Z
|
|
||||||
`,
|
|
||||||
patch: yaml.MustParse(`
|
|
||||||
apiVersion: example.com/v1
|
|
||||||
kind: Foo
|
|
||||||
metadata:
|
|
||||||
name: my-foo
|
|
||||||
spec:
|
|
||||||
bar:
|
|
||||||
C: Z
|
|
||||||
D: W
|
|
||||||
baz:
|
|
||||||
hello: world
|
|
||||||
`),
|
|
||||||
expected: `
|
|
||||||
apiVersion: example.com/v1
|
|
||||||
kind: Foo
|
|
||||||
metadata:
|
|
||||||
name: my-foo
|
|
||||||
spec:
|
|
||||||
bar:
|
|
||||||
C: Z
|
|
||||||
D: W
|
|
||||||
baz:
|
|
||||||
hello: world
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
"nullMapEntry2": {
|
|
||||||
input: `
|
|
||||||
apiVersion: example.com/v1
|
|
||||||
kind: Foo
|
|
||||||
metadata:
|
|
||||||
name: my-foo
|
|
||||||
spec:
|
|
||||||
bar:
|
|
||||||
C: Z
|
|
||||||
D: W
|
|
||||||
baz:
|
|
||||||
hello: world
|
|
||||||
`,
|
|
||||||
patch: yaml.MustParse(`
|
|
||||||
apiVersion: example.com/v1
|
|
||||||
kind: Foo
|
|
||||||
metadata:
|
|
||||||
name: my-foo
|
|
||||||
spec:
|
|
||||||
bar:
|
|
||||||
B:
|
|
||||||
C: Z
|
|
||||||
`),
|
|
||||||
expected: `
|
|
||||||
apiVersion: example.com/v1
|
|
||||||
kind: Foo
|
|
||||||
metadata:
|
|
||||||
name: my-foo
|
|
||||||
spec:
|
|
||||||
bar:
|
|
||||||
C: Z
|
|
||||||
D: W
|
|
||||||
baz:
|
|
||||||
hello: world
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
"simple patch": {
|
"simple patch": {
|
||||||
input: `
|
input: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
// Package refvar contains a kio.Filter implementation of the kustomize
|
// Package refvar contains a kio.Filter implementation of the kustomize
|
||||||
// refvar transformer (find and replace $(FOO) style variables in strings).
|
// refvar transformer.
|
||||||
package refvar
|
package refvar
|
||||||
|
|||||||
@@ -8,13 +8,15 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
|
|
||||||
|
expansion2 "sigs.k8s.io/kustomize/api/internal/accumulator/expansion"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Filter updates $(VAR) style variables with values.
|
// Filter updates $(VAR) style variables with values.
|
||||||
// The fieldSpecs are the places to look for occurrences of $(VAR).
|
// The fieldSpecs are the places to look for occurrences of $(VAR).
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
MappingFunc MappingFunc `json:"mappingFunc,omitempty" yaml:"mappingFunc,omitempty"`
|
MappingFunc func(string) interface{} `json:"mappingFunc,omitempty" yaml:"mappingFunc,omitempty"`
|
||||||
FieldSpec types.FieldSpec `json:"fieldSpec,omitempty" yaml:"fieldSpec,omitempty"`
|
FieldSpec types.FieldSpec `json:"fieldSpec,omitempty" yaml:"fieldSpec,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||||
@@ -47,21 +49,12 @@ func (f Filter) set(node *yaml.RNode) error {
|
|||||||
|
|
||||||
func updateNodeValue(node *yaml.Node, newValue interface{}) {
|
func updateNodeValue(node *yaml.Node, newValue interface{}) {
|
||||||
switch newValue := newValue.(type) {
|
switch newValue := newValue.(type) {
|
||||||
case int:
|
|
||||||
node.Value = strconv.FormatInt(int64(newValue), 10)
|
|
||||||
node.Tag = yaml.NodeTagInt
|
|
||||||
case int32:
|
|
||||||
node.Value = strconv.FormatInt(int64(newValue), 10)
|
|
||||||
node.Tag = yaml.NodeTagInt
|
|
||||||
case int64:
|
case int64:
|
||||||
node.Value = strconv.FormatInt(newValue, 10)
|
node.Value = strconv.FormatInt(newValue, 10)
|
||||||
node.Tag = yaml.NodeTagInt
|
node.Tag = yaml.NodeTagInt
|
||||||
case bool:
|
case bool:
|
||||||
node.SetString(strconv.FormatBool(newValue))
|
node.SetString(strconv.FormatBool(newValue))
|
||||||
node.Tag = yaml.NodeTagBool
|
node.Tag = yaml.NodeTagBool
|
||||||
case float32:
|
|
||||||
node.SetString(strconv.FormatFloat(float64(newValue), 'f', -1, 32))
|
|
||||||
node.Tag = yaml.NodeTagFloat
|
|
||||||
case float64:
|
case float64:
|
||||||
node.SetString(strconv.FormatFloat(newValue, 'f', -1, 64))
|
node.SetString(strconv.FormatFloat(newValue, 'f', -1, 64))
|
||||||
node.Tag = yaml.NodeTagFloat
|
node.Tag = yaml.NodeTagFloat
|
||||||
@@ -76,7 +69,7 @@ func (f Filter) setScalar(node *yaml.RNode) error {
|
|||||||
if !yaml.IsYNodeString(node.YNode()) {
|
if !yaml.IsYNodeString(node.YNode()) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
v := DoReplacements(node.YNode().Value, f.MappingFunc)
|
v := expansion2.Expand(node.YNode().Value, f.MappingFunc)
|
||||||
updateNodeValue(node.YNode(), v)
|
updateNodeValue(node.YNode(), v)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -85,14 +78,12 @@ func (f Filter) setMap(node *yaml.RNode) error {
|
|||||||
contents := node.YNode().Content
|
contents := node.YNode().Content
|
||||||
for i := 0; i < len(contents); i += 2 {
|
for i := 0; i < len(contents); i += 2 {
|
||||||
if !yaml.IsYNodeString(contents[i]) {
|
if !yaml.IsYNodeString(contents[i]) {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf("invalid map key: %s, type: %s", contents[i].Value, contents[i].Tag)
|
||||||
"invalid map key: value='%s', tag='%s'",
|
|
||||||
contents[i].Value, contents[i].Tag)
|
|
||||||
}
|
}
|
||||||
if !yaml.IsYNodeString(contents[i+1]) {
|
if !yaml.IsYNodeString(contents[i+1]) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
newValue := DoReplacements(contents[i+1].Value, f.MappingFunc)
|
newValue := expansion2.Expand(contents[i+1].Value, f.MappingFunc)
|
||||||
updateNodeValue(contents[i+1], newValue)
|
updateNodeValue(contents[i+1], newValue)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -103,7 +94,7 @@ func (f Filter) setSeq(node *yaml.RNode) error {
|
|||||||
if !yaml.IsYNodeString(item) {
|
if !yaml.IsYNodeString(item) {
|
||||||
return fmt.Errorf("invalid value type expect a string")
|
return fmt.Errorf("invalid value type expect a string")
|
||||||
}
|
}
|
||||||
newValue := DoReplacements(item.Value, f.MappingFunc)
|
newValue := expansion2.Expand(item.Value, f.MappingFunc)
|
||||||
updateNodeValue(item, newValue)
|
updateNodeValue(item, newValue)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -1,22 +1,18 @@
|
|||||||
package refvar_test
|
package refvar
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
. "sigs.k8s.io/kustomize/api/filters/refvar"
|
expansion2 "sigs.k8s.io/kustomize/api/internal/accumulator/expansion"
|
||||||
filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest"
|
filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
var makeMf = func(theMap map[string]interface{}) MappingFunc {
|
|
||||||
ignored := make(map[string]int)
|
|
||||||
return MakePrimitiveReplacer(ignored, theMap)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFilter(t *testing.T) {
|
func TestFilter(t *testing.T) {
|
||||||
|
replacementCounts := make(map[string]int)
|
||||||
|
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
input string
|
input string
|
||||||
@@ -39,7 +35,7 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
replicas: 5`,
|
replicas: 5`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{
|
||||||
"VAR": int64(5),
|
"VAR": int64(5),
|
||||||
}),
|
}),
|
||||||
FieldSpec: types.FieldSpec{Path: "spec/replicas"},
|
FieldSpec: types.FieldSpec{Path: "spec/replicas"},
|
||||||
@@ -61,7 +57,7 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
replicas: 1`,
|
replicas: 1`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{
|
||||||
"VAR": int64(5),
|
"VAR": int64(5),
|
||||||
}),
|
}),
|
||||||
FieldSpec: types.FieldSpec{Path: "spec/replicas"},
|
FieldSpec: types.FieldSpec{Path: "spec/replicas"},
|
||||||
@@ -83,7 +79,7 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
replicas: 1`,
|
replicas: 1`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{
|
||||||
"VAR": int64(5),
|
"VAR": int64(5),
|
||||||
}),
|
}),
|
||||||
FieldSpec: types.FieldSpec{Path: "a/b/c"},
|
FieldSpec: types.FieldSpec{Path: "a/b/c"},
|
||||||
@@ -115,7 +111,7 @@ data:
|
|||||||
- false
|
- false
|
||||||
- 1.23`,
|
- 1.23`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{
|
||||||
"FOO": "foo",
|
"FOO": "foo",
|
||||||
"BAR": "bar",
|
"BAR": "bar",
|
||||||
"BOOL": false,
|
"BOOL": false,
|
||||||
@@ -146,7 +142,7 @@ data:
|
|||||||
BAZ: $(BAZ)
|
BAZ: $(BAZ)
|
||||||
PLUS: foo+bar`,
|
PLUS: foo+bar`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{
|
||||||
"FOO": "foo",
|
"FOO": "foo",
|
||||||
"BAR": "bar",
|
"BAR": "bar",
|
||||||
}),
|
}),
|
||||||
@@ -185,7 +181,7 @@ data:
|
|||||||
SLICE:
|
SLICE:
|
||||||
- $(FOO)`,
|
- $(FOO)`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{
|
||||||
"FOO": "foo",
|
"FOO": "foo",
|
||||||
"BAR": "bar",
|
"BAR": "bar",
|
||||||
}),
|
}),
|
||||||
@@ -208,10 +204,8 @@ metadata:
|
|||||||
data:
|
data:
|
||||||
FOO: null`,
|
FOO: null`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{}),
|
||||||
// no replacements!
|
FieldSpec: types.FieldSpec{Path: "data/FOO"},
|
||||||
}),
|
|
||||||
FieldSpec: types.FieldSpec{Path: "data/FOO"},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -229,6 +223,8 @@ data:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFilterUnhappy(t *testing.T) {
|
func TestFilterUnhappy(t *testing.T) {
|
||||||
|
replacementCounts := make(map[string]int)
|
||||||
|
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
input string
|
input string
|
||||||
expectedError string
|
expectedError string
|
||||||
@@ -254,7 +250,7 @@ data:
|
|||||||
- false
|
- false
|
||||||
' at path 'data/slice': invalid value type expect a string`,
|
' at path 'data/slice': invalid value type expect a string`,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{
|
||||||
"VAR": int64(5),
|
"VAR": int64(5),
|
||||||
}),
|
}),
|
||||||
FieldSpec: types.FieldSpec{Path: "data/slice"},
|
FieldSpec: types.FieldSpec{Path: "data/slice"},
|
||||||
@@ -276,9 +272,9 @@ 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 + `'`,
|
' at path 'data': invalid map key: 1, type: ` + yaml.NodeTagInt,
|
||||||
filter: Filter{
|
filter: Filter{
|
||||||
MappingFunc: makeMf(map[string]interface{}{
|
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{
|
||||||
"VAR": int64(5),
|
"VAR": int64(5),
|
||||||
}),
|
}),
|
||||||
FieldSpec: types.FieldSpec{Path: "data"},
|
FieldSpec: types.FieldSpec{Path: "data"},
|
||||||
|
|||||||
12
api/go.mod
12
api/go.mod
@@ -1,26 +1,26 @@
|
|||||||
module sigs.k8s.io/kustomize/api
|
module sigs.k8s.io/kustomize/api
|
||||||
|
|
||||||
go 1.15
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/evanphx/json-patch v4.5.0+incompatible
|
github.com/evanphx/json-patch v4.5.0+incompatible
|
||||||
github.com/go-errors/errors v1.0.1
|
|
||||||
github.com/go-openapi/spec v0.19.5
|
github.com/go-openapi/spec v0.19.5
|
||||||
github.com/golangci/golangci-lint v1.21.0
|
github.com/golangci/golangci-lint v1.21.0
|
||||||
github.com/google/go-cmp v0.3.0
|
github.com/google/go-cmp v0.3.0
|
||||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
|
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
|
||||||
github.com/hashicorp/go-multierror v1.1.0
|
github.com/hashicorp/go-multierror v1.1.0
|
||||||
github.com/imdario/mergo v0.3.5
|
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/stretchr/testify v1.4.0
|
github.com/stretchr/testify v1.4.0
|
||||||
github.com/yujunz/go-getter v1.5.1-lite.0.20201201013212-6d9c071adddf
|
github.com/yujunz/go-getter v1.4.1-lite
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e
|
||||||
gopkg.in/yaml.v2 v2.3.0
|
gopkg.in/yaml.v2 v2.3.0
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
|
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71
|
||||||
k8s.io/api v0.17.0
|
k8s.io/api v0.17.0
|
||||||
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.9.3
|
||||||
sigs.k8s.io/yaml v1.2.0
|
sigs.k8s.io/yaml v1.2.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
replace sigs.k8s.io/kustomize/kyaml v0.9.3 => ../kyaml
|
||||||
|
|||||||
21
api/go.sum
21
api/go.sum
@@ -158,7 +158,6 @@ github.com/go-toolsmith/strparse v1.0.0 h1:Vcw78DnpCAKlM20kSbAyO4mPfJn/lyYA4BJUD
|
|||||||
github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8=
|
github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8=
|
||||||
github.com/go-toolsmith/typep v1.0.0 h1:zKymWyA1TRYvqYrYDrfEMZULyrhcnGY3x7LDKU2XQaA=
|
github.com/go-toolsmith/typep v1.0.0 h1:zKymWyA1TRYvqYrYDrfEMZULyrhcnGY3x7LDKU2XQaA=
|
||||||
github.com/go-toolsmith/typep v1.0.0/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
|
github.com/go-toolsmith/typep v1.0.0/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
|
||||||
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
|
|
||||||
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
|
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
|
||||||
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
|
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
|
||||||
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b h1:ekuhfTjngPhisSjOJ0QWKpPQE8/rbknHaes6WVJj5Hw=
|
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b h1:ekuhfTjngPhisSjOJ0QWKpPQE8/rbknHaes6WVJj5Hw=
|
||||||
@@ -167,7 +166,6 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a
|
|||||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||||
github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d h1:3PaI8p3seN09VjbTYC/QWlUZdZ1qS1zGjy7LH2Wt07I=
|
github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d h1:3PaI8p3seN09VjbTYC/QWlUZdZ1qS1zGjy7LH2Wt07I=
|
||||||
github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
|
github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
|
||||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
|
|
||||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||||
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||||
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||||
@@ -227,7 +225,6 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
|
|||||||
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d h1:7XGaL1e6bYS1yIonGp9761ExpPPV1ui0SAC59Yube9k=
|
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d h1:7XGaL1e6bYS1yIonGp9761ExpPPV1ui0SAC59Yube9k=
|
||||||
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
||||||
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
|
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
|
||||||
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
|
||||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||||
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3 h1:JVnpOZS+qxli+rgVl98ILOXVNbW+kb5wcxeGx8ShUIw=
|
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3 h1:JVnpOZS+qxli+rgVl98ILOXVNbW+kb5wcxeGx8ShUIw=
|
||||||
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3/go.mod h1:eEOZF4jCKGi+aprrirO9e7WKB3beBRtWgqGunKl6pKE=
|
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3/go.mod h1:eEOZF4jCKGi+aprrirO9e7WKB3beBRtWgqGunKl6pKE=
|
||||||
@@ -252,7 +249,6 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
|||||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
github.com/imdario/mergo v0.3.5 h1:JboBksRwiiAJWvIYJVo46AfV+IAIKZpfrSzVKj42R4Q=
|
|
||||||
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
@@ -291,7 +287,6 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN
|
|||||||
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||||
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
|
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
|
||||||
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
|
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
|
||||||
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
|
|
||||||
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb h1:RHba4YImhrUVQDHUCe2BNSOz4tVy2yGyXhvYDvxGgeE=
|
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb h1:RHba4YImhrUVQDHUCe2BNSOz4tVy2yGyXhvYDvxGgeE=
|
||||||
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
|
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
|
||||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
||||||
@@ -370,7 +365,6 @@ github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e h1:MZM7FHLqUHYI0Y/mQAt
|
|||||||
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
|
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
|
||||||
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=
|
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=
|
||||||
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
|
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
|
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||||
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
||||||
@@ -412,8 +406,8 @@ github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e/go.mod h1:Qimiff
|
|||||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||||
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
|
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
|
||||||
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
|
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
|
||||||
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
|
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
|
||||||
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
|
||||||
github.com/ultraware/funlen v0.0.2 h1:Av96YVBwwNSe4MLR7iI/BIa3VyI7/djnto/pK3Uxbdo=
|
github.com/ultraware/funlen v0.0.2 h1:Av96YVBwwNSe4MLR7iI/BIa3VyI7/djnto/pK3Uxbdo=
|
||||||
github.com/ultraware/funlen v0.0.2/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
|
github.com/ultraware/funlen v0.0.2/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
|
||||||
github.com/ultraware/whitespace v0.0.4 h1:If7Va4cM03mpgrNH9k49/VOicWpGoG70XPBFFODYDsg=
|
github.com/ultraware/whitespace v0.0.4 h1:If7Va4cM03mpgrNH9k49/VOicWpGoG70XPBFFODYDsg=
|
||||||
@@ -429,8 +423,8 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q
|
|||||||
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca h1:1CFlNzQhALwjS9mBAUkycX616GzgsuYUOCHA5+HSlXI=
|
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca h1:1CFlNzQhALwjS9mBAUkycX616GzgsuYUOCHA5+HSlXI=
|
||||||
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
|
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
|
||||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
||||||
github.com/yujunz/go-getter v1.5.1-lite.0.20201201013212-6d9c071adddf h1:gvEmqF83GB8R5XtrMseJb6A6R0OCtNAS8f4TmZg2dGc=
|
github.com/yujunz/go-getter v1.4.1-lite h1:FhvNc94AXMZkfqUwfMKhnQEC9phkphSGdPTL7tIdhOM=
|
||||||
github.com/yujunz/go-getter v1.5.1-lite.0.20201201013212-6d9c071adddf/go.mod h1:bL0Pr07HEdsMZ1WBqZIxXj96r5LnFsY4LgPaPEGkw1k=
|
github.com/yujunz/go-getter v1.4.1-lite/go.mod h1:sbmqxXjyLunH1PkF3n7zSlnVeMvmYUuIl9ZVs/7NyCc=
|
||||||
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||||
go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
|
go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
|
||||||
go.mongodb.org/mongo-driver v1.1.1/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
|
go.mongodb.org/mongo-driver v1.1.1/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
|
||||||
@@ -567,13 +561,12 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
|
|||||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 h1:Xe2gvTZUJpsvOWUnvmL/tmhVBZUmHSvLbMjRj6NUUKo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
|
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
|
||||||
@@ -599,8 +592,6 @@ 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.6 h1:xUJxc/k8JoWqHUahaB8DTqY0KwEPxTbTGStvW8TOcDc=
|
|
||||||
sigs.k8s.io/kustomize/kyaml v0.10.6/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=
|
||||||
|
|||||||
@@ -43,12 +43,12 @@ type Kunstructured interface {
|
|||||||
// Several uses.
|
// Several uses.
|
||||||
Copy() Kunstructured
|
Copy() Kunstructured
|
||||||
|
|
||||||
// GetAnnotations returns the k8s annotations.
|
// Used by Resource.Replace, which in turn is used in many places, e.g.
|
||||||
|
// - resource.Resource.Merge
|
||||||
|
// - resWrangler.appendReplaceOrMerge (AbsorbAll)
|
||||||
|
// - api.internal.k8sdeps.transformer.patch.conflictdetector
|
||||||
GetAnnotations() map[string]string
|
GetAnnotations() map[string]string
|
||||||
|
|
||||||
// GetData returns a top-level "data" field, as in a ConfigMap.
|
|
||||||
GetDataMap() map[string]string
|
|
||||||
|
|
||||||
// Used by ResAccumulator and ReplacementTransformer.
|
// Used by ResAccumulator and ReplacementTransformer.
|
||||||
GetFieldValue(string) (interface{}, error)
|
GetFieldValue(string) (interface{}, error)
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ type Kunstructured interface {
|
|||||||
// Used by resource.Factory.SliceFromBytes
|
// Used by resource.Factory.SliceFromBytes
|
||||||
GetKind() string
|
GetKind() string
|
||||||
|
|
||||||
// GetLabels returns the k8s labels.
|
// Used by Resource.Replace
|
||||||
GetLabels() map[string]string
|
GetLabels() map[string]string
|
||||||
|
|
||||||
// Used by Resource.CurId and resource factory.
|
// Used by Resource.CurId and resource factory.
|
||||||
@@ -84,22 +84,19 @@ type Kunstructured interface {
|
|||||||
// Used by resWrangler.Select
|
// Used by resWrangler.Select
|
||||||
MatchesLabelSelector(selector string) (bool, error)
|
MatchesLabelSelector(selector string) (bool, error)
|
||||||
|
|
||||||
// SetAnnotations replaces the k8s annotations.
|
// Used by Resource.Replace.
|
||||||
SetAnnotations(map[string]string)
|
SetAnnotations(map[string]string)
|
||||||
|
|
||||||
// SetDataMap sets a top-level "data" field, as in a ConfigMap.
|
|
||||||
SetDataMap(map[string]string)
|
|
||||||
|
|
||||||
// Used by PatchStrategicMergeTransformer.
|
// Used by PatchStrategicMergeTransformer.
|
||||||
SetGvk(resid.Gvk)
|
SetGvk(resid.Gvk)
|
||||||
|
|
||||||
// SetLabels replaces the k8s labels.
|
// Used by Resource.Replace and used to remove "validated by" labels.
|
||||||
SetLabels(map[string]string)
|
SetLabels(map[string]string)
|
||||||
|
|
||||||
// SetName changes the name.
|
// Used by Resource.Replace.
|
||||||
SetName(string)
|
SetName(string)
|
||||||
|
|
||||||
// SetNamespace changes the namespace.
|
// Used by Resource.Replace.
|
||||||
SetNamespace(string)
|
SetNamespace(string)
|
||||||
|
|
||||||
// Needed, for now, by kyaml/filtersutil.ApplyToJSON.
|
// Needed, for now, by kyaml/filtersutil.ApplyToJSON.
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package refvar
|
// Package expansion provides functions find and replace $(FOO) style variables in strings.
|
||||||
|
package expansion
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -17,64 +17,38 @@ const (
|
|||||||
|
|
||||||
// syntaxWrap returns the input string wrapped by the expansion syntax.
|
// syntaxWrap returns the input string wrapped by the expansion syntax.
|
||||||
func syntaxWrap(input string) string {
|
func syntaxWrap(input string) string {
|
||||||
var sb strings.Builder
|
return string(operator) + string(referenceOpener) + input + string(referenceCloser)
|
||||||
sb.WriteByte(operator)
|
|
||||||
sb.WriteByte(referenceOpener)
|
|
||||||
sb.WriteString(input)
|
|
||||||
sb.WriteByte(referenceCloser)
|
|
||||||
return sb.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MappingFunc maps a string to anything.
|
// MappingFuncFor returns a mapping function for use with Expand that
|
||||||
type MappingFunc func(string) interface{}
|
// implements the expansion semantics defined in the expansion spec; it
|
||||||
|
// returns the input string wrapped in the expansion syntax if no mapping
|
||||||
// MakePrimitiveReplacer returns a MappingFunc that uses a map to do
|
// for the input is found.
|
||||||
// replacements, and a histogram to count map hits.
|
func MappingFuncFor(
|
||||||
//
|
counts map[string]int,
|
||||||
// Func behavior:
|
context ...map[string]interface{}) func(string) interface{} {
|
||||||
//
|
return func(input string) interface{} {
|
||||||
// If the input key is NOT found in the map, the key is wrapped up as
|
for _, vars := range context {
|
||||||
// as a variable declaration string and returned, e.g. key FOO becomes $(FOO).
|
val, ok := vars[input]
|
||||||
// This string is presumably put back where it was found, and might get replaced
|
if ok {
|
||||||
// later.
|
counts[input]++
|
||||||
//
|
switch typedV := val.(type) {
|
||||||
// If the key is found in the map, the value is returned if it is a primitive
|
case string, int64, float64, bool:
|
||||||
// type (string, bool, number), and the hit is counted.
|
return typedV
|
||||||
//
|
default:
|
||||||
// If it's not a primitive type (e.g. a map, struct, func, etc.) then this
|
return syntaxWrap(input)
|
||||||
// function doesn't know what to do with it and it returns the key wrapped up
|
}
|
||||||
// again as if it had not been replaced. This should probably be an error.
|
|
||||||
func MakePrimitiveReplacer(
|
|
||||||
counts map[string]int, someMap map[string]interface{}) MappingFunc {
|
|
||||||
return func(key string) interface{} {
|
|
||||||
if value, ok := someMap[key]; ok {
|
|
||||||
switch typedV := value.(type) {
|
|
||||||
case string, int, int32, int64, float32, float64, bool:
|
|
||||||
counts[key]++
|
|
||||||
return typedV
|
|
||||||
default:
|
|
||||||
// If the value is some complicated type (e.g. a map or struct),
|
|
||||||
// this function doesn't know how to jam it into a string,
|
|
||||||
// so just pretend it was a cache miss.
|
|
||||||
// Likely this should be an error instead of a silent failure,
|
|
||||||
// since the programmer passed an impossible value.
|
|
||||||
log.Printf(
|
|
||||||
"MakePrimitiveReplacer: bad replacement type=%T val=%v",
|
|
||||||
typedV, typedV)
|
|
||||||
return syntaxWrap(key)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If unable to return the mapped variable, return it
|
return syntaxWrap(input)
|
||||||
// as it was found, and a later mapping might be able to
|
|
||||||
// replace it.
|
|
||||||
return syntaxWrap(key)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoReplacements replaces variable references in the input string
|
// Expand replaces variable references in the input string according to
|
||||||
// using the mapping function.
|
// the expansion spec using the given mapping function to resolve the
|
||||||
func DoReplacements(input string, mapping MappingFunc) interface{} {
|
// values of variables.
|
||||||
var buf strings.Builder
|
func Expand(input string, mapping func(string) interface{}) interface{} {
|
||||||
|
var buf bytes.Buffer
|
||||||
checkpoint := 0
|
checkpoint := 0
|
||||||
for cursor := 0; cursor < len(input); cursor++ {
|
for cursor := 0; cursor < len(input); cursor++ {
|
||||||
if input[cursor] == operator && cursor+1 < len(input) {
|
if input[cursor] == operator && cursor+1 < len(input) {
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package refvar_test
|
package expansion_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
. "sigs.k8s.io/kustomize/api/internal/accumulator/expansion"
|
||||||
. "sigs.k8s.io/kustomize/api/filters/refvar"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type expected struct {
|
type expected struct {
|
||||||
@@ -16,48 +15,6 @@ type expected struct {
|
|||||||
edited string
|
edited string
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrimitiveReplacer(t *testing.T) {
|
|
||||||
varCounts := make(map[string]int)
|
|
||||||
f := MakePrimitiveReplacer(
|
|
||||||
varCounts,
|
|
||||||
map[string]interface{}{
|
|
||||||
"FOO": "bar",
|
|
||||||
"ZOO": "$(FOO)-1",
|
|
||||||
"BLU": "$(ZOO)-2",
|
|
||||||
"EIGHT": 8,
|
|
||||||
"PI": 3.14159,
|
|
||||||
"ZINT": "$(INT)",
|
|
||||||
"BOOL": "true",
|
|
||||||
"HUGENUMBER": int64(9223372036854775807),
|
|
||||||
"CRAZYMAP": map[string]int{"crazy": 200},
|
|
||||||
"ZBOOL": "$(BOOL)",
|
|
||||||
})
|
|
||||||
assert.Equal(t, "$()", f(""))
|
|
||||||
assert.Equal(t, "$( )", f(" "))
|
|
||||||
assert.Equal(t, "$(florida)", f("florida"))
|
|
||||||
assert.Equal(t, "$(0)", f("0"))
|
|
||||||
assert.Equal(t, "bar", f("FOO"))
|
|
||||||
assert.Equal(t, "bar", f("FOO"))
|
|
||||||
assert.Equal(t, "bar", f("FOO"))
|
|
||||||
assert.Equal(t, 8, f("EIGHT"))
|
|
||||||
assert.Equal(t, 8, f("EIGHT"))
|
|
||||||
assert.Equal(t, 3.14159, f("PI"))
|
|
||||||
assert.Equal(t, "true", f("BOOL"))
|
|
||||||
assert.Equal(t, int64(9223372036854775807), f("HUGENUMBER"))
|
|
||||||
assert.Equal(t, "$(FOO)-1", f("ZOO"))
|
|
||||||
assert.Equal(t, "$(CRAZYMAP)", f("CRAZYMAP"))
|
|
||||||
assert.Equal(t,
|
|
||||||
map[string]int{
|
|
||||||
"FOO": 3,
|
|
||||||
"EIGHT": 2,
|
|
||||||
"BOOL": 1,
|
|
||||||
"PI": 1,
|
|
||||||
"ZOO": 1,
|
|
||||||
"HUGENUMBER": 1,
|
|
||||||
},
|
|
||||||
varCounts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMapReference(t *testing.T) {
|
func TestMapReference(t *testing.T) {
|
||||||
type env struct {
|
type env struct {
|
||||||
Name string
|
Name string
|
||||||
@@ -94,7 +51,7 @@ func TestMapReference(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
varMap := map[string]interface{}{
|
declaredEnv := map[string]interface{}{
|
||||||
"FOO": "bar",
|
"FOO": "bar",
|
||||||
"ZOO": "$(FOO)-1",
|
"ZOO": "$(FOO)-1",
|
||||||
"BLU": "$(ZOO)-2",
|
"BLU": "$(ZOO)-2",
|
||||||
@@ -104,11 +61,11 @@ func TestMapReference(t *testing.T) {
|
|||||||
"ZBOOL": "$(BOOL)",
|
"ZBOOL": "$(BOOL)",
|
||||||
}
|
}
|
||||||
|
|
||||||
varCounts := make(map[string]int)
|
counts := make(map[string]int)
|
||||||
|
mapping := MappingFuncFor(counts, declaredEnv)
|
||||||
|
|
||||||
for _, env := range envs {
|
for _, env := range envs {
|
||||||
varMap[env.Name] = DoReplacements(
|
declaredEnv[env.Name] = Expand(fmt.Sprintf("%v", env.Value), mapping)
|
||||||
fmt.Sprintf("%v", env.Value),
|
|
||||||
MakePrimitiveReplacer(varCounts, varMap))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedEnv := map[string]expected{
|
expectedEnv := map[string]expected{
|
||||||
@@ -122,20 +79,45 @@ func TestMapReference(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range expectedEnv {
|
for k, v := range expectedEnv {
|
||||||
if e, a := v, varMap[k]; e.edited != a || e.count != varCounts[k] {
|
if e, a := v, declaredEnv[k]; e.edited != a || e.count != counts[k] {
|
||||||
t.Errorf("Expected %v count=%d, got %v count=%d",
|
t.Errorf("Expected %v count=%d, got %v count=%d",
|
||||||
e.edited, e.count, a, varCounts[k])
|
e.edited, e.count, a, counts[k])
|
||||||
} else {
|
} else {
|
||||||
delete(varMap, k)
|
delete(declaredEnv, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(varMap) != 0 {
|
if len(declaredEnv) != 0 {
|
||||||
t.Errorf("Unexpected keys in declared env: %v", varMap)
|
t.Errorf("Unexpected keys in declared env: %v", declaredEnv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMapping(t *testing.T) {
|
func TestMapping(t *testing.T) {
|
||||||
|
context := map[string]interface{}{
|
||||||
|
"VAR_A": "A",
|
||||||
|
"VAR_B": "B",
|
||||||
|
"VAR_C": "C",
|
||||||
|
"VAR_REF": "$(VAR_A)",
|
||||||
|
"VAR_EMPTY": "",
|
||||||
|
}
|
||||||
|
doExpansionTest(t, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMappingDual(t *testing.T) {
|
||||||
|
context := map[string]interface{}{
|
||||||
|
"VAR_A": "A",
|
||||||
|
"VAR_EMPTY": "",
|
||||||
|
}
|
||||||
|
context2 := map[string]interface{}{
|
||||||
|
"VAR_B": "B",
|
||||||
|
"VAR_C": "C",
|
||||||
|
"VAR_REF": "$(VAR_A)",
|
||||||
|
}
|
||||||
|
|
||||||
|
doExpansionTest(t, context, context2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func doExpansionTest(t *testing.T, context ...map[string]interface{}) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
name string
|
name string
|
||||||
input string
|
input string
|
||||||
@@ -351,17 +333,11 @@ func TestMapping(t *testing.T) {
|
|||||||
expected: "\n",
|
expected: "\n",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
counts := make(map[string]int)
|
counts := make(map[string]int)
|
||||||
expanded := DoReplacements(
|
mapping := MappingFuncFor(counts, context...)
|
||||||
fmt.Sprintf("%v", tc.input),
|
expanded := Expand(fmt.Sprintf("%v", tc.input), mapping)
|
||||||
MakePrimitiveReplacer(counts, map[string]interface{}{
|
|
||||||
"VAR_A": "A",
|
|
||||||
"VAR_B": "B",
|
|
||||||
"VAR_C": "C",
|
|
||||||
"VAR_REF": "$(VAR_A)",
|
|
||||||
"VAR_EMPTY": "",
|
|
||||||
}))
|
|
||||||
if e, a := tc.expected, expanded; e != a {
|
if e, a := tc.expected, expanded; e != a {
|
||||||
t.Errorf("%v: expected %q, got %q", tc.name, e, a)
|
t.Errorf("%v: expected %q, got %q", tc.name, e, a)
|
||||||
}
|
}
|
||||||
@@ -371,7 +347,8 @@ func TestMapping(t *testing.T) {
|
|||||||
}
|
}
|
||||||
if len(tc.counts) > 0 {
|
if len(tc.counts) > 0 {
|
||||||
for k, expectedCount := range tc.counts {
|
for k, expectedCount := range tc.counts {
|
||||||
if c, ok := counts[k]; ok {
|
c, ok := counts[k]
|
||||||
|
if ok {
|
||||||
if c != expectedCount {
|
if c != expectedCount {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
"%v: k=%s, expected count %d, got %d",
|
"%v: k=%s, expected count %d, got %d",
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"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/kyaml/filtersutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
type nameReferenceTransformer struct {
|
type nameReferenceTransformer struct {
|
||||||
@@ -19,8 +20,7 @@ var _ resmap.Transformer = &nameReferenceTransformer{}
|
|||||||
|
|
||||||
// newNameReferenceTransformer constructs a nameReferenceTransformer
|
// newNameReferenceTransformer constructs a nameReferenceTransformer
|
||||||
// with a given slice of NameBackReferences.
|
// with a given slice of NameBackReferences.
|
||||||
func newNameReferenceTransformer(
|
func newNameReferenceTransformer(br []builtinconfig.NameBackReferences) resmap.Transformer {
|
||||||
br []builtinconfig.NameBackReferences) resmap.Transformer {
|
|
||||||
if br == nil {
|
if br == nil {
|
||||||
log.Fatal("backrefs not expected to be nil")
|
log.Fatal("backrefs not expected to be nil")
|
||||||
}
|
}
|
||||||
@@ -29,17 +29,17 @@ 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
|
// the HPA scales. The Deployment name might change
|
||||||
// (e.g. prefix added), and the reference in the HPA
|
// (e.g. prefix added), and the reference in the HPA
|
||||||
// has to be fixed.
|
// has to be fixed.
|
||||||
//
|
//
|
||||||
// In the outer loop over the ResMap below, say we
|
// In the outer loop over the ResMap below, say we
|
||||||
// encounter a specific HPA. Then, in scanning the set
|
// encounter a specific HPA. Then, in scanning backrefs,
|
||||||
// of all known backrefs, we encounter an entry like
|
// we encounter an entry like
|
||||||
//
|
//
|
||||||
// - kind: Deployment
|
// - kind: Deployment
|
||||||
// fieldSpecs:
|
// fieldSpecs:
|
||||||
@@ -74,32 +74,22 @@ func newNameReferenceTransformer(
|
|||||||
// Name transformers should only modify the name in the
|
// Name transformers should only modify the name in the
|
||||||
// body of the resource object (the value in the ResMap).
|
// body of the resource object (the value in the ResMap).
|
||||||
//
|
//
|
||||||
func (t *nameReferenceTransformer) Transform(m resmap.ResMap) error {
|
func (o *nameReferenceTransformer) Transform(m resmap.ResMap) error {
|
||||||
// TODO: Too much looping, here and in transitive calls.
|
// TODO: Too much looping, here and in transitive calls.
|
||||||
for _, referrer := range m.Resources() {
|
for _, referrer := range m.Resources() {
|
||||||
var candidates resmap.ResMap
|
var candidates resmap.ResMap
|
||||||
for _, referralTarget := range t.backRefs {
|
for _, target := range o.backRefs {
|
||||||
for _, fSpec := range referralTarget.FieldSpecs {
|
for _, fSpec := range target.FieldSpecs {
|
||||||
if referrer.OrgId().IsSelected(&fSpec.Gvk) {
|
if referrer.OrgId().IsSelected(&fSpec.Gvk) {
|
||||||
if candidates == nil {
|
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)
|
candidates = m.SubsetThatCouldBeReferencedByResource(referrer)
|
||||||
}
|
}
|
||||||
// One way to get here is with, say, a referrer that's an
|
err := filtersutil.ApplyToJSON(nameref.Filter{
|
||||||
// HPA, and a target that's a Deployment (one of the
|
FieldSpec: fSpec,
|
||||||
// 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,
|
Referrer: referrer,
|
||||||
NameFieldToUpdate: fSpec,
|
Target: target.Gvk,
|
||||||
ReferralTarget: referralTarget.Gvk,
|
|
||||||
ReferralCandidates: candidates,
|
ReferralCandidates: candidates,
|
||||||
})
|
}, referrer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,17 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"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"
|
||||||
resmaptest_test "sigs.k8s.io/kustomize/api/testutils/resmaptest"
|
resmaptest_test "sigs.k8s.io/kustomize/api/testutils/resmaptest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNameReferenceHappyRun(t *testing.T) {
|
func TestNameReferenceHappyRun(t *testing.T) {
|
||||||
m := resmaptest_test.NewRmBuilderDefault(t).AddWithName(
|
rf := resource.NewFactory(
|
||||||
|
kunstruct.NewKunstructuredFactoryImpl())
|
||||||
|
m := resmaptest_test.NewRmBuilder(t, rf).AddWithName(
|
||||||
"cm1",
|
"cm1",
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
@@ -217,7 +220,6 @@ func TestNameReferenceHappyRun(t *testing.T) {
|
|||||||
"secret1",
|
"secret1",
|
||||||
"secret1",
|
"secret1",
|
||||||
"secret2",
|
"secret2",
|
||||||
"cm1",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -259,8 +261,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}).ResMap()
|
}).ResMap()
|
||||||
|
|
||||||
expected := resmaptest_test.NewSeededRmBuilderDefault(
|
expected := resmaptest_test.NewSeededRmBuilder(t, rf, m.ShallowCopy()).ReplaceResource(
|
||||||
t, m.ShallowCopy()).ReplaceResource(
|
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"group": "apps",
|
"group": "apps",
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
@@ -421,7 +422,6 @@ func TestNameReferenceHappyRun(t *testing.T) {
|
|||||||
"someprefix-secret1-somehash",
|
"someprefix-secret1-somehash",
|
||||||
"someprefix-secret1-somehash",
|
"someprefix-secret1-somehash",
|
||||||
"secret2",
|
"secret2",
|
||||||
"someprefix-cm1-somehash",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -475,12 +475,14 @@ func TestNameReferenceHappyRun(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNameReferenceUnhappyRun(t *testing.T) {
|
func TestNameReferenceUnhappyRun(t *testing.T) {
|
||||||
|
rf := resource.NewFactory(
|
||||||
|
kunstruct.NewKunstructuredFactoryImpl())
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
resMap resmap.ResMap
|
resMap resmap.ResMap
|
||||||
expectedErr string
|
expectedErr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
resMap: resmaptest_test.NewRmBuilderDefault(t).Add(
|
resMap: resmaptest_test.NewRmBuilder(t, rf).Add(
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"apiVersion": "rbac.authorization.k8s.io/v1",
|
"apiVersion": "rbac.authorization.k8s.io/v1",
|
||||||
"kind": "ClusterRole",
|
"kind": "ClusterRole",
|
||||||
@@ -500,7 +502,7 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
|
|||||||
}).ResMap(),
|
}).ResMap(),
|
||||||
expectedErr: "is expected to be"},
|
expectedErr: "is expected to be"},
|
||||||
{
|
{
|
||||||
resMap: resmaptest_test.NewRmBuilderDefault(t).Add(
|
resMap: resmaptest_test.NewRmBuilder(t, rf).Add(
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"apiVersion": "rbac.authorization.k8s.io/v1",
|
"apiVersion": "rbac.authorization.k8s.io/v1",
|
||||||
"kind": "ClusterRole",
|
"kind": "ClusterRole",
|
||||||
@@ -536,7 +538,8 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
|
func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
|
||||||
rf := provider.NewDefaultDepProvider().GetResourceFactory()
|
rf := resource.NewFactory(
|
||||||
|
kunstruct.NewKunstructuredFactoryImpl())
|
||||||
|
|
||||||
v1 := rf.FromMapWithName(
|
v1 := rf.FromMapWithName(
|
||||||
"volume1",
|
"volume1",
|
||||||
@@ -661,7 +664,9 @@ const (
|
|||||||
// object with the same original names (uniquename) in different namespaces
|
// object with the same original names (uniquename) in different namespaces
|
||||||
// and with different current Id.
|
// and with different current Id.
|
||||||
func TestNameReferenceNamespace(t *testing.T) {
|
func TestNameReferenceNamespace(t *testing.T) {
|
||||||
m := resmaptest_test.NewRmBuilderDefault(t).
|
rf := resource.NewFactory(
|
||||||
|
kunstruct.NewKunstructuredFactoryImpl())
|
||||||
|
m := resmaptest_test.NewRmBuilder(t, rf).
|
||||||
// Add ConfigMap with the same org name in noNs, "ns1" and "ns2" namespaces
|
// Add ConfigMap with the same org name in noNs, "ns1" and "ns2" namespaces
|
||||||
AddWithName(orgname, map[string]interface{}{
|
AddWithName(orgname, map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
@@ -710,7 +715,7 @@ func TestNameReferenceNamespace(t *testing.T) {
|
|||||||
AddWithNsAndName(ns1, orgname, deploymentMap(ns1, prefixedname, orgname, orgname)).
|
AddWithNsAndName(ns1, orgname, deploymentMap(ns1, prefixedname, orgname, orgname)).
|
||||||
AddWithNsAndName(ns2, orgname, deploymentMap(ns2, suffixedname, orgname, orgname)).ResMap()
|
AddWithNsAndName(ns2, orgname, deploymentMap(ns2, suffixedname, orgname, orgname)).ResMap()
|
||||||
|
|
||||||
expected := resmaptest_test.NewSeededRmBuilderDefault(t, m.ShallowCopy()).
|
expected := resmaptest_test.NewSeededRmBuilder(t, rf, m.ShallowCopy()).
|
||||||
ReplaceResource(deploymentMap(defaultNs, modifiedname, modifiedname, modifiedname)).
|
ReplaceResource(deploymentMap(defaultNs, modifiedname, modifiedname, modifiedname)).
|
||||||
ReplaceResource(deploymentMap(ns1, prefixedname, prefixedname, prefixedname)).
|
ReplaceResource(deploymentMap(ns1, prefixedname, prefixedname, prefixedname)).
|
||||||
ReplaceResource(deploymentMap(ns2, suffixedname, suffixedname, suffixedname)).ResMap()
|
ReplaceResource(deploymentMap(ns2, suffixedname, suffixedname, suffixedname)).ResMap()
|
||||||
@@ -721,7 +726,6 @@ func TestNameReferenceNamespace(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
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("actual doesn't match expected: %v", err)
|
||||||
}
|
}
|
||||||
@@ -731,7 +735,9 @@ func TestNameReferenceNamespace(t *testing.T) {
|
|||||||
// object with the same original names (uniquename) in different namespaces
|
// object with the same original names (uniquename) in different namespaces
|
||||||
// and with different current Id.
|
// and with different current Id.
|
||||||
func TestNameReferenceClusterWide(t *testing.T) {
|
func TestNameReferenceClusterWide(t *testing.T) {
|
||||||
m := resmaptest_test.NewRmBuilderDefault(t).
|
rf := resource.NewFactory(
|
||||||
|
kunstruct.NewKunstructuredFactoryImpl())
|
||||||
|
m := resmaptest_test.NewRmBuilder(t, rf).
|
||||||
// Add ServiceAccount with the same org name in noNs, "ns1" and "ns2" namespaces
|
// Add ServiceAccount with the same org name in noNs, "ns1" and "ns2" namespaces
|
||||||
AddWithName(orgname, map[string]interface{}{
|
AddWithName(orgname, map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
@@ -810,7 +816,7 @@ func TestNameReferenceClusterWide(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}}).ResMap()
|
}}).ResMap()
|
||||||
|
|
||||||
expected := resmaptest_test.NewSeededRmBuilderDefault(t, m.ShallowCopy()).
|
expected := resmaptest_test.NewSeededRmBuilder(t, rf, m.ShallowCopy()).
|
||||||
ReplaceResource(
|
ReplaceResource(
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"apiVersion": "rbac.authorization.k8s.io/v1",
|
"apiVersion": "rbac.authorization.k8s.io/v1",
|
||||||
@@ -883,7 +889,6 @@ func TestNameReferenceClusterWide(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
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("actual doesn't match expected: %v", err)
|
||||||
}
|
}
|
||||||
@@ -893,7 +898,9 @@ func TestNameReferenceClusterWide(t *testing.T) {
|
|||||||
// object with the same original names (uniquename) in different namespaces
|
// object with the same original names (uniquename) in different namespaces
|
||||||
// and with different current Id.
|
// and with different current Id.
|
||||||
func TestNameReferenceNamespaceTransformation(t *testing.T) {
|
func TestNameReferenceNamespaceTransformation(t *testing.T) {
|
||||||
m := resmaptest_test.NewRmBuilderDefault(t).
|
rf := resource.NewFactory(
|
||||||
|
kunstruct.NewKunstructuredFactoryImpl())
|
||||||
|
m := resmaptest_test.NewRmBuilder(t, rf).
|
||||||
AddWithNsAndName(ns4, orgname, map[string]interface{}{
|
AddWithNsAndName(ns4, orgname, map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
"kind": "Secret",
|
"kind": "Secret",
|
||||||
@@ -957,7 +964,7 @@ func TestNameReferenceNamespaceTransformation(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}}).ResMap()
|
}}).ResMap()
|
||||||
|
|
||||||
expected := resmaptest_test.NewSeededRmBuilderDefault(t, m.ShallowCopy()).
|
expected := resmaptest_test.NewSeededRmBuilder(t, rf, m.ShallowCopy()).
|
||||||
ReplaceResource(
|
ReplaceResource(
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"apiVersion": "rbac.authorization.k8s.io/v1",
|
"apiVersion": "rbac.authorization.k8s.io/v1",
|
||||||
@@ -1010,7 +1017,6 @@ func TestNameReferenceNamespaceTransformation(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
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("actual doesn't match expected: %v", err)
|
||||||
}
|
}
|
||||||
@@ -1020,7 +1026,9 @@ func TestNameReferenceNamespaceTransformation(t *testing.T) {
|
|||||||
// It validates the change done is IsSameFuzzyNamespace which
|
// It validates the change done is IsSameFuzzyNamespace which
|
||||||
// uses the IsNsEquals method instead of the simple == operator.
|
// uses the IsNsEquals method instead of the simple == operator.
|
||||||
func TestNameReferenceCandidateSelection(t *testing.T) {
|
func TestNameReferenceCandidateSelection(t *testing.T) {
|
||||||
m := resmaptest_test.NewRmBuilderDefault(t).
|
rf := resource.NewFactory(
|
||||||
|
kunstruct.NewKunstructuredFactoryImpl())
|
||||||
|
m := resmaptest_test.NewRmBuilder(t, rf).
|
||||||
AddWithName("cm1", map[string]interface{}{
|
AddWithName("cm1", map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
"kind": "ConfigMap",
|
"kind": "ConfigMap",
|
||||||
@@ -1037,7 +1045,7 @@ func TestNameReferenceCandidateSelection(t *testing.T) {
|
|||||||
AddWithName("deploy1", deploymentMap("", "p1-deploy1", "cm1", "secret1")).
|
AddWithName("deploy1", deploymentMap("", "p1-deploy1", "cm1", "secret1")).
|
||||||
ResMap()
|
ResMap()
|
||||||
|
|
||||||
expected := resmaptest_test.NewSeededRmBuilderDefault(t, m.ShallowCopy()).
|
expected := resmaptest_test.NewSeededRmBuilder(t, rf, m.ShallowCopy()).
|
||||||
ReplaceResource(deploymentMap("", "p1-deploy1", "p1-cm1-hash", "p1-secret1-hash")).
|
ReplaceResource(deploymentMap("", "p1-deploy1", "p1-cm1-hash", "p1-secret1-hash")).
|
||||||
ResMap()
|
ResMap()
|
||||||
|
|
||||||
@@ -1047,7 +1055,6 @@ func TestNameReferenceCandidateSelection(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
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("actual doesn't match expected: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,19 @@
|
|||||||
package accumulator
|
package accumulator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
expansion2 "sigs.k8s.io/kustomize/api/internal/accumulator/expansion"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filters/refvar"
|
"sigs.k8s.io/kustomize/api/filters/refvar"
|
||||||
"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/filtersutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
type refVarTransformer struct {
|
type refVarTransformer struct {
|
||||||
varMap map[string]interface{}
|
varMap map[string]interface{}
|
||||||
replacementCounts map[string]int
|
replacementCounts map[string]int
|
||||||
fieldSpecs []types.FieldSpec
|
fieldSpecs []types.FieldSpec
|
||||||
|
mappingFunc func(string) interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// newRefVarTransformer returns a new refVarTransformer
|
// newRefVarTransformer returns a new refVarTransformer
|
||||||
@@ -31,7 +35,8 @@ func newRefVarTransformer(
|
|||||||
func (rv *refVarTransformer) UnusedVars() []string {
|
func (rv *refVarTransformer) UnusedVars() []string {
|
||||||
var unused []string
|
var unused []string
|
||||||
for k := range rv.varMap {
|
for k := range rv.varMap {
|
||||||
if _, ok := rv.replacementCounts[k]; !ok {
|
_, ok := rv.replacementCounts[k]
|
||||||
|
if !ok {
|
||||||
unused = append(unused, k)
|
unused = append(unused, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,13 +46,14 @@ func (rv *refVarTransformer) UnusedVars() []string {
|
|||||||
// Transform replaces $(VAR) style variables with values.
|
// Transform replaces $(VAR) style variables with values.
|
||||||
func (rv *refVarTransformer) Transform(m resmap.ResMap) error {
|
func (rv *refVarTransformer) Transform(m resmap.ResMap) error {
|
||||||
rv.replacementCounts = make(map[string]int)
|
rv.replacementCounts = make(map[string]int)
|
||||||
mf := refvar.MakePrimitiveReplacer(rv.replacementCounts, rv.varMap)
|
rv.mappingFunc = expansion2.MappingFuncFor(
|
||||||
|
rv.replacementCounts, rv.varMap)
|
||||||
for _, res := range m.Resources() {
|
for _, res := range m.Resources() {
|
||||||
for _, fieldSpec := range rv.fieldSpecs {
|
for _, fieldSpec := range rv.fieldSpecs {
|
||||||
err := res.ApplyFilter(refvar.Filter{
|
err := filtersutil.ApplyToJSON(refvar.Filter{
|
||||||
MappingFunc: mf,
|
MappingFunc: rv.mappingFunc,
|
||||||
FieldSpec: fieldSpec,
|
FieldSpec: fieldSpec,
|
||||||
})
|
}, res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,10 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"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"
|
||||||
resmaptest_test "sigs.k8s.io/kustomize/api/testutils/resmaptest"
|
resmaptest_test "sigs.k8s.io/kustomize/api/testutils/resmaptest"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
@@ -45,7 +46,8 @@ func TestRefVarTransformer(t *testing.T) {
|
|||||||
{Gvk: resid.Gvk{Version: "v1", Kind: "ConfigMap"}, Path: "data/interface"},
|
{Gvk: resid.Gvk{Version: "v1", Kind: "ConfigMap"}, Path: "data/interface"},
|
||||||
{Gvk: resid.Gvk{Version: "v1", Kind: "ConfigMap"}, Path: "data/num"},
|
{Gvk: resid.Gvk{Version: "v1", Kind: "ConfigMap"}, Path: "data/num"},
|
||||||
},
|
},
|
||||||
res: resmaptest_test.NewRmBuilderDefault(t).
|
res: resmaptest_test.NewRmBuilder(
|
||||||
|
t, resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())).
|
||||||
Add(map[string]interface{}{
|
Add(map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
"kind": "ConfigMap",
|
"kind": "ConfigMap",
|
||||||
@@ -75,7 +77,8 @@ func TestRefVarTransformer(t *testing.T) {
|
|||||||
}}).ResMap(),
|
}}).ResMap(),
|
||||||
},
|
},
|
||||||
expected: expected{
|
expected: expected{
|
||||||
res: resmaptest_test.NewRmBuilderDefault(t).
|
res: resmaptest_test.NewRmBuilder(
|
||||||
|
t, resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())).
|
||||||
Add(map[string]interface{}{
|
Add(map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
"kind": "ConfigMap",
|
"kind": "ConfigMap",
|
||||||
@@ -113,7 +116,8 @@ func TestRefVarTransformer(t *testing.T) {
|
|||||||
fs: []types.FieldSpec{
|
fs: []types.FieldSpec{
|
||||||
{Gvk: resid.Gvk{Version: "v1", Kind: "ConfigMap"}, Path: "data/slice"},
|
{Gvk: resid.Gvk{Version: "v1", Kind: "ConfigMap"}, Path: "data/slice"},
|
||||||
},
|
},
|
||||||
res: resmaptest_test.NewRmBuilderDefault(t).
|
res: resmaptest_test.NewRmBuilder(
|
||||||
|
t, resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())).
|
||||||
Add(map[string]interface{}{
|
Add(map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
"kind": "ConfigMap",
|
"kind": "ConfigMap",
|
||||||
@@ -124,19 +128,8 @@ func TestRefVarTransformer(t *testing.T) {
|
|||||||
"slice": []interface{}{5}, // noticeably *not* a []string
|
"slice": []interface{}{5}, // noticeably *not* a []string
|
||||||
}}).ResMap(),
|
}}).ResMap(),
|
||||||
},
|
},
|
||||||
// TODO(#3304): DECISION - kyaml better; not a bug.
|
errMessage: `obj '{"apiVersion": "v1", "data": {"slice": [5]}, "kind": "ConfigMap", "metadata": {"name": "cm1"}}
|
||||||
errMessage: konfig.IfApiMachineryElseKyaml(
|
|
||||||
`obj '{"apiVersion": "v1", "data": {"slice": [5]}, "kind": "ConfigMap", "metadata": {"name": "cm1"}}
|
|
||||||
' at path 'data/slice': invalid value type expect a string`,
|
' at path 'data/slice': invalid value type expect a string`,
|
||||||
`obj 'apiVersion: v1
|
|
||||||
data:
|
|
||||||
slice:
|
|
||||||
- 5
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: cm1
|
|
||||||
' at path 'data/slice': invalid value type expect a string`,
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "var replacement in nil",
|
description: "var replacement in nil",
|
||||||
@@ -145,7 +138,8 @@ metadata:
|
|||||||
fs: []types.FieldSpec{
|
fs: []types.FieldSpec{
|
||||||
{Gvk: resid.Gvk{Version: "v1", Kind: "ConfigMap"}, Path: "data/nil"},
|
{Gvk: resid.Gvk{Version: "v1", Kind: "ConfigMap"}, Path: "data/nil"},
|
||||||
},
|
},
|
||||||
res: resmaptest_test.NewRmBuilderDefault(t).
|
res: resmaptest_test.NewRmBuilder(
|
||||||
|
t, resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())).
|
||||||
Add(map[string]interface{}{
|
Add(map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
"kind": "ConfigMap",
|
"kind": "ConfigMap",
|
||||||
@@ -157,7 +151,8 @@ metadata:
|
|||||||
}}).ResMap(),
|
}}).ResMap(),
|
||||||
},
|
},
|
||||||
expected: expected{
|
expected: expected{
|
||||||
res: resmaptest_test.NewRmBuilderDefault(t).
|
res: resmaptest_test.NewRmBuilder(
|
||||||
|
t, resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())).
|
||||||
Add(map[string]interface{}{
|
Add(map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
"kind": "ConfigMap",
|
"kind": "ConfigMap",
|
||||||
|
|||||||
@@ -41,11 +41,13 @@ func (ra *ResAccumulator) Vars() []types.Var {
|
|||||||
return ra.varSet.AsSlice()
|
return ra.varSet.AsSlice()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ra *ResAccumulator) AppendAll(resources resmap.ResMap) error {
|
func (ra *ResAccumulator) AppendAll(
|
||||||
|
resources resmap.ResMap) error {
|
||||||
return ra.resMap.AppendAll(resources)
|
return ra.resMap.AppendAll(resources)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ra *ResAccumulator) AbsorbAll(resources resmap.ResMap) error {
|
func (ra *ResAccumulator) AbsorbAll(
|
||||||
|
resources resmap.ResMap) error {
|
||||||
return ra.resMap.AbsorbAll(resources)
|
return ra.resMap.AbsorbAll(resources)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,15 +61,6 @@ func (ra *ResAccumulator) GetTransformerConfig() *builtinconfig.TransformerConfi
|
|||||||
return ra.tConfig
|
return ra.tConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// MergeVars accumulates vars into ResAccumulator.
|
|
||||||
// A Var is a tuple of name, object reference and field reference.
|
|
||||||
// This func takes a list of vars from the current kustomization file and
|
|
||||||
// annotates the accumulated resources with the names of the vars that match
|
|
||||||
// those resources. E.g. if there's a var named "sam" that wants to get
|
|
||||||
// its data from a ConfigMap named "james", and the resource list contains a
|
|
||||||
// ConfigMap named "james", then that ConfigMap will be annotated with the
|
|
||||||
// var name "sam". Later this annotation is used to find the data for "sam"
|
|
||||||
// by digging into a particular fieldpath of "james".
|
|
||||||
func (ra *ResAccumulator) MergeVars(incoming []types.Var) error {
|
func (ra *ResAccumulator) MergeVars(incoming []types.Var) error {
|
||||||
for _, v := range incoming {
|
for _, v := range incoming {
|
||||||
targetId := resid.NewResIdWithNamespace(v.ObjRef.GVK(), v.ObjRef.Name, v.ObjRef.Namespace)
|
targetId := resid.NewResIdWithNamespace(v.ObjRef.GVK(), v.ObjRef.Name, v.ObjRef.Namespace)
|
||||||
@@ -113,10 +106,12 @@ func (ra *ResAccumulator) findVarValueFromResources(v types.Var) (interface{}, e
|
|||||||
"field specified in var '%v' "+
|
"field specified in var '%v' "+
|
||||||
"not found in corresponding resource", v)
|
"not found in corresponding resource", v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", fmt.Errorf(
|
return "", fmt.Errorf(
|
||||||
"var '%v' cannot be mapped to a field "+
|
"var '%v' cannot be mapped to a field "+
|
||||||
"in the set of known resources", v)
|
"in the set of known resources", v)
|
||||||
@@ -132,8 +127,10 @@ func (ra *ResAccumulator) makeVarReplacementMap() (map[string]interface{}, error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result[v.Name] = s
|
result[v.Name] = s
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,6 +161,6 @@ func (ra *ResAccumulator) FixBackReferences() (err error) {
|
|||||||
if ra.tConfig.NameReference == nil {
|
if ra.tConfig.NameReference == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return ra.Transform(
|
return ra.Transform(newNameReferenceTransformer(
|
||||||
newNameReferenceTransformer(ra.tConfig.NameReference))
|
ra.tConfig.NameReference))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
. "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/provider"
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"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"
|
||||||
@@ -20,14 +20,16 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeResAccumulator(t *testing.T) *ResAccumulator {
|
func makeResAccumulator(t *testing.T) (*ResAccumulator, *resource.Factory) {
|
||||||
ra := MakeEmptyAccumulator()
|
ra := MakeEmptyAccumulator()
|
||||||
err := ra.MergeConfig(builtinconfig.MakeDefaultConfig())
|
err := ra.MergeConfig(builtinconfig.MakeDefaultConfig())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected err: %v", err)
|
t.Fatalf("unexpected err: %v", err)
|
||||||
}
|
}
|
||||||
|
rf := resource.NewFactory(
|
||||||
|
kunstruct.NewKunstructuredFactoryImpl())
|
||||||
err = ra.AppendAll(
|
err = ra.AppendAll(
|
||||||
resmaptest_test.NewRmBuilderDefault(t).
|
resmaptest_test.NewRmBuilder(t, rf).
|
||||||
Add(map[string]interface{}{
|
Add(map[string]interface{}{
|
||||||
"apiVersion": "apps/v1",
|
"apiVersion": "apps/v1",
|
||||||
"kind": "Deployment",
|
"kind": "Deployment",
|
||||||
@@ -64,11 +66,11 @@ func makeResAccumulator(t *testing.T) *ResAccumulator {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected err: %v", err)
|
t.Fatalf("unexpected err: %v", err)
|
||||||
}
|
}
|
||||||
return ra
|
return ra, rf
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveVarsHappy(t *testing.T) {
|
func TestResolveVarsHappy(t *testing.T) {
|
||||||
ra := makeResAccumulator(t)
|
ra, _ := makeResAccumulator(t)
|
||||||
err := ra.MergeVars([]types.Var{
|
err := ra.MergeVars([]types.Var{
|
||||||
{
|
{
|
||||||
Name: "SERVICE_ONE",
|
Name: "SERVICE_ONE",
|
||||||
@@ -97,7 +99,7 @@ func TestResolveVarsHappy(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveVarsOneUnused(t *testing.T) {
|
func TestResolveVarsOneUnused(t *testing.T) {
|
||||||
ra := makeResAccumulator(t)
|
ra, _ := makeResAccumulator(t)
|
||||||
err := ra.MergeVars([]types.Var{
|
err := ra.MergeVars([]types.Var{
|
||||||
{
|
{
|
||||||
Name: "SERVICE_ONE",
|
Name: "SERVICE_ONE",
|
||||||
@@ -138,10 +140,11 @@ func expectLog(t *testing.T, log bytes.Buffer, expect string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveVarsVarNeedsDisambiguation(t *testing.T) {
|
func TestResolveVarsVarNeedsDisambiguation(t *testing.T) {
|
||||||
ra := makeResAccumulator(t)
|
ra, rf := makeResAccumulator(t)
|
||||||
|
|
||||||
rm0 := resmap.New()
|
rm0 := resmap.New()
|
||||||
err := rm0.Append(
|
err := rm0.Append(
|
||||||
provider.NewDefaultDepProvider().GetResourceFactory().FromMap(
|
rf.FromMap(
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
"kind": "Service",
|
"kind": "Service",
|
||||||
@@ -210,7 +213,8 @@ func makeVarToNamepaceAndPath(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveVarConflicts(t *testing.T) {
|
func TestResolveVarConflicts(t *testing.T) {
|
||||||
rf := provider.NewDefaultDepProvider().GetResourceFactory()
|
rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
||||||
|
|
||||||
// create configmaps in foo and bar namespaces with `data.provider` values.
|
// create configmaps in foo and bar namespaces with `data.provider` values.
|
||||||
fooAws := makeNamespacedConfigMapWithDataProviderValue("foo", "aws")
|
fooAws := makeNamespacedConfigMapWithDataProviderValue("foo", "aws")
|
||||||
barAws := makeNamespacedConfigMapWithDataProviderValue("bar", "aws")
|
barAws := makeNamespacedConfigMapWithDataProviderValue("bar", "aws")
|
||||||
@@ -257,7 +261,7 @@ func TestResolveVarConflicts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveVarsGoodResIdBadField(t *testing.T) {
|
func TestResolveVarsGoodResIdBadField(t *testing.T) {
|
||||||
ra := makeResAccumulator(t)
|
ra, _ := makeResAccumulator(t)
|
||||||
err := ra.MergeVars([]types.Var{
|
err := ra.MergeVars([]types.Var{
|
||||||
{
|
{
|
||||||
Name: "SERVICE_ONE",
|
Name: "SERVICE_ONE",
|
||||||
@@ -282,7 +286,7 @@ func TestResolveVarsGoodResIdBadField(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveVarsUnmappableVar(t *testing.T) {
|
func TestResolveVarsUnmappableVar(t *testing.T) {
|
||||||
ra := makeResAccumulator(t)
|
ra, _ := makeResAccumulator(t)
|
||||||
err := ra.MergeVars([]types.Var{
|
err := ra.MergeVars([]types.Var{
|
||||||
{
|
{
|
||||||
Name: "SERVICE_THREE",
|
Name: "SERVICE_THREE",
|
||||||
@@ -306,7 +310,7 @@ func TestResolveVarsUnmappableVar(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveVarsWithNoambiguation(t *testing.T) {
|
func TestResolveVarsWithNoambiguation(t *testing.T) {
|
||||||
ra1 := makeResAccumulator(t)
|
ra1, rf := makeResAccumulator(t)
|
||||||
err := ra1.MergeVars([]types.Var{
|
err := ra1.MergeVars([]types.Var{
|
||||||
{
|
{
|
||||||
Name: "SERVICE_ONE",
|
Name: "SERVICE_ONE",
|
||||||
@@ -323,7 +327,7 @@ func TestResolveVarsWithNoambiguation(t *testing.T) {
|
|||||||
// Create another accumulator having a resource with different prefix
|
// Create another accumulator having a resource with different prefix
|
||||||
ra2 := MakeEmptyAccumulator()
|
ra2 := MakeEmptyAccumulator()
|
||||||
|
|
||||||
m := resmaptest_test.NewRmBuilderDefault(t).
|
m := resmaptest_test.NewRmBuilder(t, rf).
|
||||||
Add(map[string]interface{}{
|
Add(map[string]interface{}{
|
||||||
"apiVersion": "apps/v1",
|
"apiVersion": "apps/v1",
|
||||||
"kind": "Deployment",
|
"kind": "Deployment",
|
||||||
@@ -355,8 +359,7 @@ func TestResolveVarsWithNoambiguation(t *testing.T) {
|
|||||||
// went through a prefix transformer.
|
// went through a prefix transformer.
|
||||||
r := m.GetByIndex(1)
|
r := m.GetByIndex(1)
|
||||||
r.AddNamePrefix("sub-")
|
r.AddNamePrefix("sub-")
|
||||||
r.SetName("sub-backendOne")
|
r.SetName("sub-backendOne") // original name remains "backendOne"
|
||||||
r.SetOriginalName("backendOne", true)
|
|
||||||
|
|
||||||
err = ra2.AppendAll(m)
|
err = ra2.AppendAll(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package conflict
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
|
||||||
)
|
|
||||||
|
|
||||||
type cdFactory struct{}
|
|
||||||
|
|
||||||
var _ resource.ConflictDetectorFactory = &cdFactory{}
|
|
||||||
|
|
||||||
// NewFactory returns a new conflict detector factory.
|
|
||||||
func NewFactory() resource.ConflictDetectorFactory {
|
|
||||||
return &cdFactory{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns an instance of smPatchMergeOnlyDetector.
|
|
||||||
func (c cdFactory) New(_ resid.Gvk) (resource.ConflictDetector, error) {
|
|
||||||
return &smPatchMergeOnlyDetector{}, nil
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package conflict
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
|
||||||
)
|
|
||||||
|
|
||||||
// smPatchMergeOnlyDetector ignores conflicts,
|
|
||||||
// but does real strategic merge patching.
|
|
||||||
// This is part of an effort to eliminate dependence on
|
|
||||||
// apimachinery package to allow kustomize integration
|
|
||||||
// into kubectl (#2506 and #1500)
|
|
||||||
type smPatchMergeOnlyDetector struct{}
|
|
||||||
|
|
||||||
var _ resource.ConflictDetector = &smPatchMergeOnlyDetector{}
|
|
||||||
|
|
||||||
func (c *smPatchMergeOnlyDetector) HasConflict(
|
|
||||||
_, _ *resource.Resource) (bool, error) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// There's at least one case that doesn't work. Suppose one has a
|
|
||||||
// Deployment with a volume with the bizarre "emptyDir: {}" entry.
|
|
||||||
// If you want to get rid of this entry via a patch containing
|
|
||||||
// the entry "emptyDir: null", then the following won't work,
|
|
||||||
// because null entries are eliminated.
|
|
||||||
func (c *smPatchMergeOnlyDetector) MergePatches(
|
|
||||||
r, patch *resource.Resource) (*resource.Resource, error) {
|
|
||||||
err := r.ApplySmPatch(patch)
|
|
||||||
return r, err
|
|
||||||
}
|
|
||||||
@@ -7,13 +7,14 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var fileReader = kunstruct.NewKunstructuredFactoryImpl()
|
||||||
|
|
||||||
// This document is meant to be used at the elasticsearch document type.
|
// This document is meant to be used at the elasticsearch document type.
|
||||||
// Fields are serialized as-is to elasticsearch, where indices are built
|
// Fields are serialized as-is to elasticsearch, where indices are built
|
||||||
// to facilitate text search queries. Identifiers, Values, FilePath,
|
// to facilitate text search queries. Identifiers, Values, FilePath,
|
||||||
@@ -41,7 +42,6 @@ type KustomizationDocument struct {
|
|||||||
Kinds []string `json:"kinds,omitempty"`
|
Kinds []string `json:"kinds,omitempty"`
|
||||||
Identifiers []string `json:"identifiers,omitempty"`
|
Identifiers []string `json:"identifiers,omitempty"`
|
||||||
Values []string `json:"values,omitempty"`
|
Values []string `json:"values,omitempty"`
|
||||||
resFactory *resource.Factory
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type set map[string]struct{}
|
type set map[string]struct{}
|
||||||
@@ -52,7 +52,6 @@ func (doc *KustomizationDocument) Copy() *KustomizationDocument {
|
|||||||
Kinds: doc.Kinds,
|
Kinds: doc.Kinds,
|
||||||
Identifiers: doc.Identifiers,
|
Identifiers: doc.Identifiers,
|
||||||
Values: doc.Values,
|
Values: doc.Values,
|
||||||
resFactory: provider.NewDefaultDepProvider().GetResourceFactory(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,7 +150,7 @@ func (doc *KustomizationDocument) readBytes() ([]map[string]interface{}, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
configs := make([]map[string]interface{}, 0)
|
configs := make([]map[string]interface{}, 0)
|
||||||
ks, err := doc.resFactory.SliceFromBytes(data)
|
ks, err := fileReader.SliceFromBytes(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to parse resource: %v", err)
|
return nil, fmt.Errorf("unable to parse resource: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module sigs.k8s.io/kustomize/api/internal/crawl
|
module sigs.k8s.io/kustomize/api/internal/crawl
|
||||||
|
|
||||||
go 1.15
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/elastic/go-elasticsearch/v6 v6.8.5
|
github.com/elastic/go-elasticsearch/v6 v6.8.5
|
||||||
|
|||||||
@@ -145,7 +145,6 @@ github.com/go-toolsmith/pkgload v0.0.0-20181119091011-e9e65178eee8/go.mod h1:WoM
|
|||||||
github.com/go-toolsmith/pkgload v1.0.0/go.mod h1:5eFArkbO80v7Z0kdngIxsRXRMTaX4Ilcwuh3clNrQJc=
|
github.com/go-toolsmith/pkgload v1.0.0/go.mod h1:5eFArkbO80v7Z0kdngIxsRXRMTaX4Ilcwuh3clNrQJc=
|
||||||
github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8=
|
github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8=
|
||||||
github.com/go-toolsmith/typep v1.0.0/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
|
github.com/go-toolsmith/typep v1.0.0/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
|
||||||
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
|
|
||||||
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
|
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
|
||||||
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
|
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
|
||||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||||
@@ -220,7 +219,6 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
|
|||||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
github.com/imdario/mergo v0.3.5 h1:JboBksRwiiAJWvIYJVo46AfV+IAIKZpfrSzVKj42R4Q=
|
|
||||||
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||||
@@ -255,7 +253,6 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN
|
|||||||
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||||
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
|
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
|
||||||
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
|
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
|
||||||
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
|
|
||||||
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
|
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
|
||||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||||
@@ -320,7 +317,6 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
|
|||||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/securego/gosec v0.0.0-20191002120514-e680875ea14d/go.mod h1:w5+eXa0mYznDkHaMCXA4XYffjlH+cy1oyKbfzJXa2Do=
|
github.com/securego/gosec v0.0.0-20191002120514-e680875ea14d/go.mod h1:w5+eXa0mYznDkHaMCXA4XYffjlH+cy1oyKbfzJXa2Do=
|
||||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||||
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
|
||||||
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||||
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lzhCH5h/3YBfyVA3VbLYjlMZZAQcW9ojMexNc=
|
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lzhCH5h/3YBfyVA3VbLYjlMZZAQcW9ojMexNc=
|
||||||
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
|
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
|
||||||
@@ -359,8 +355,8 @@ github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e/go.mod h1:Qimiff
|
|||||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||||
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
|
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
|
||||||
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
|
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
|
||||||
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
|
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
|
||||||
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
|
||||||
github.com/ultraware/funlen v0.0.2/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
|
github.com/ultraware/funlen v0.0.2/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
|
||||||
github.com/ultraware/whitespace v0.0.4/go.mod h1:aVMh/gQve5Maj9hQ/hg+F75lr/X5A89uZnzAmWSineA=
|
github.com/ultraware/whitespace v0.0.4/go.mod h1:aVMh/gQve5Maj9hQ/hg+F75lr/X5A89uZnzAmWSineA=
|
||||||
github.com/uudashr/gocognit v0.0.0-20190926065955-1655d0de0517/go.mod h1:j44Ayx2KW4+oB6SWMv8KsmHzZrOInQav7D3cQMJ5JUM=
|
github.com/uudashr/gocognit v0.0.0-20190926065955-1655d0de0517/go.mod h1:j44Ayx2KW4+oB6SWMv8KsmHzZrOInQav7D3cQMJ5JUM=
|
||||||
@@ -373,8 +369,8 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q
|
|||||||
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca h1:1CFlNzQhALwjS9mBAUkycX616GzgsuYUOCHA5+HSlXI=
|
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca h1:1CFlNzQhALwjS9mBAUkycX616GzgsuYUOCHA5+HSlXI=
|
||||||
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
|
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
|
||||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
||||||
github.com/yujunz/go-getter v1.5.1-lite.0.20201201013212-6d9c071adddf h1:gvEmqF83GB8R5XtrMseJb6A6R0OCtNAS8f4TmZg2dGc=
|
github.com/yujunz/go-getter v1.4.1-lite h1:FhvNc94AXMZkfqUwfMKhnQEC9phkphSGdPTL7tIdhOM=
|
||||||
github.com/yujunz/go-getter v1.5.1-lite.0.20201201013212-6d9c071adddf/go.mod h1:bL0Pr07HEdsMZ1WBqZIxXj96r5LnFsY4LgPaPEGkw1k=
|
github.com/yujunz/go-getter v1.4.1-lite/go.mod h1:sbmqxXjyLunH1PkF3n7zSlnVeMvmYUuIl9ZVs/7NyCc=
|
||||||
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||||
go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
|
go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
|
||||||
go.mongodb.org/mongo-driver v1.1.1/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
|
go.mongodb.org/mongo-driver v1.1.1/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
|
||||||
@@ -507,13 +503,12 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
||||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 h1:Xe2gvTZUJpsvOWUnvmL/tmhVBZUmHSvLbMjRj6NUUKo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||||
@@ -534,8 +529,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.9.3 h1:kZ5HnNmmnbndSXFivrAVM6u3Ik1dwu4kbpaV8KNwy8I=
|
||||||
sigs.k8s.io/kustomize/kyaml v0.10.6/go.mod h1:K9yg1k/HB/6xNOf5VH3LhTo1DK9/5ykSZO5uIv+Y/1k=
|
sigs.k8s.io/kustomize/kyaml v0.9.3/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
|
||||||
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=
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package generators
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MakeConfigMap makes a configmap.
|
|
||||||
//
|
|
||||||
// ConfigMap: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#configmap-v1-core
|
|
||||||
//
|
|
||||||
// ConfigMaps and Secrets are similar.
|
|
||||||
//
|
|
||||||
// Both objects have a `data` field, which contains a map from keys to
|
|
||||||
// values that must be UTF-8 valid strings. Such data might be simple text,
|
|
||||||
// or whoever made the data may have done so by performing a base64 encoding
|
|
||||||
// on binary data. Regardless, k8s has no means to know this, so it treats
|
|
||||||
// the data field as a string.
|
|
||||||
//
|
|
||||||
// The ConfigMap has an additional field `binaryData`, also a map, but its
|
|
||||||
// values are _intended_ to be interpreted as a base64 encoding of []byte,
|
|
||||||
// by whatever makes use of the ConfigMap.
|
|
||||||
//
|
|
||||||
// In a ConfigMap, any key used in `data` cannot also be used in `binaryData`
|
|
||||||
// and vice-versa. A key must be unique across both maps.
|
|
||||||
func MakeConfigMap(
|
|
||||||
ldr ifc.KvLoader, args *types.ConfigMapArgs) (rn *yaml.RNode, err error) {
|
|
||||||
rn, err = makeBaseNode("ConfigMap", args.Name, args.Namespace)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
m, err := makeValidatedDataMap(ldr, args.Name, args.KvPairSources)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err = rn.LoadMapIntoConfigMapData(m); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
copyLabelsAndAnnotations(rn, args.Options)
|
|
||||||
return rn, nil
|
|
||||||
}
|
|
||||||
@@ -1,223 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package generators_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
|
||||||
. "sigs.k8s.io/kustomize/api/internal/generators"
|
|
||||||
"sigs.k8s.io/kustomize/api/kv"
|
|
||||||
"sigs.k8s.io/kustomize/api/loader"
|
|
||||||
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
var binaryHello = []byte{
|
|
||||||
0xff, // non-utf8
|
|
||||||
0x68, // h
|
|
||||||
0x65, // e
|
|
||||||
0x6c, // l
|
|
||||||
0x6c, // l
|
|
||||||
0x6f, // o
|
|
||||||
}
|
|
||||||
|
|
||||||
func manyHellos(count int) (result []byte) {
|
|
||||||
for i := 0; i < count; i++ {
|
|
||||||
result = append(result, binaryHello...)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMakeConfigMap(t *testing.T) {
|
|
||||||
type expected struct {
|
|
||||||
out string
|
|
||||||
errMsg string
|
|
||||||
}
|
|
||||||
|
|
||||||
testCases := map[string]struct {
|
|
||||||
args types.ConfigMapArgs
|
|
||||||
exp expected
|
|
||||||
}{
|
|
||||||
"construct config map from env": {
|
|
||||||
args: types.ConfigMapArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "envConfigMap",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
EnvSources: []string{
|
|
||||||
filepath.Join("configmap", "app.env"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: envConfigMap
|
|
||||||
data:
|
|
||||||
DB_PASSWORD: qwerty
|
|
||||||
DB_USERNAME: admin
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"construct config map from text file": {
|
|
||||||
args: types.ConfigMapArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "fileConfigMap1",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
FileSources: []string{
|
|
||||||
filepath.Join("configmap", "app-init.ini"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: fileConfigMap1
|
|
||||||
data:
|
|
||||||
app-init.ini: |
|
|
||||||
FOO=bar
|
|
||||||
BAR=baz
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"construct config map from text and binary file": {
|
|
||||||
args: types.ConfigMapArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "fileConfigMap2",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
FileSources: []string{
|
|
||||||
filepath.Join("configmap", "app-init.ini"),
|
|
||||||
filepath.Join("configmap", "app.bin"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: fileConfigMap2
|
|
||||||
data:
|
|
||||||
app-init.ini: |
|
|
||||||
FOO=bar
|
|
||||||
BAR=baz
|
|
||||||
binaryData:
|
|
||||||
app.bin: |
|
|
||||||
/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbG
|
|
||||||
xv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hl
|
|
||||||
bGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2
|
|
||||||
hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"construct config map from literal": {
|
|
||||||
args: types.ConfigMapArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "literalConfigMap1",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
LiteralSources: []string{"a=x", "b=y", "c=\"Hello World\"", "d='true'"},
|
|
||||||
},
|
|
||||||
Options: &types.GeneratorOptions{
|
|
||||||
Labels: map[string]string{
|
|
||||||
"foo": "bar",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: literalConfigMap1
|
|
||||||
labels:
|
|
||||||
foo: 'bar'
|
|
||||||
data:
|
|
||||||
a: x
|
|
||||||
b: y
|
|
||||||
c: Hello World
|
|
||||||
d: "true"
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"construct config map from literal with GeneratorOptions in ConfigMapArgs": {
|
|
||||||
args: types.ConfigMapArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "literalConfigMap2",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
LiteralSources: []string{"a=x", "b=y", "c=\"Hello World\"", "d='true'"},
|
|
||||||
},
|
|
||||||
Options: &types.GeneratorOptions{
|
|
||||||
Labels: map[string]string{
|
|
||||||
"veggie": "celery",
|
|
||||||
"dog": "beagle",
|
|
||||||
"cat": "annoying",
|
|
||||||
},
|
|
||||||
Annotations: map[string]string{
|
|
||||||
"river": "Missouri",
|
|
||||||
"city": "Iowa City",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: literalConfigMap2
|
|
||||||
labels:
|
|
||||||
cat: 'annoying'
|
|
||||||
dog: 'beagle'
|
|
||||||
veggie: 'celery'
|
|
||||||
annotations:
|
|
||||||
city: 'Iowa City'
|
|
||||||
river: 'Missouri'
|
|
||||||
data:
|
|
||||||
a: x
|
|
||||||
b: y
|
|
||||||
c: Hello World
|
|
||||||
d: "true"
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
fSys := filesys.MakeFsInMemory()
|
|
||||||
fSys.WriteFile(
|
|
||||||
filesys.RootedPath("configmap", "app.env"),
|
|
||||||
[]byte("DB_USERNAME=admin\nDB_PASSWORD=qwerty\n"))
|
|
||||||
fSys.WriteFile(
|
|
||||||
filesys.RootedPath("configmap", "app-init.ini"),
|
|
||||||
[]byte("FOO=bar\nBAR=baz\n"))
|
|
||||||
fSys.WriteFile(
|
|
||||||
filesys.RootedPath("configmap", "app.bin"),
|
|
||||||
manyHellos(30))
|
|
||||||
kvLdr := kv.NewLoader(
|
|
||||||
loader.NewFileLoaderAtRoot(fSys),
|
|
||||||
valtest_test.MakeFakeValidator())
|
|
||||||
|
|
||||||
for n := range testCases {
|
|
||||||
tc := testCases[n]
|
|
||||||
t.Run(n, func(t *testing.T) {
|
|
||||||
rn, err := MakeConfigMap(kvLdr, &tc.args)
|
|
||||||
if err != nil {
|
|
||||||
if !assert.EqualError(t, err, tc.exp.errMsg) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if tc.exp.errMsg != "" {
|
|
||||||
t.Fatalf("%s: should return error '%s'", n, tc.exp.errMsg)
|
|
||||||
}
|
|
||||||
output := rn.MustString()
|
|
||||||
if !assert.Equal(t, tc.exp.out, output) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package generators
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MakeSecret makes a kubernetes Secret.
|
|
||||||
//
|
|
||||||
// Secret: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#secret-v1-core
|
|
||||||
//
|
|
||||||
// ConfigMaps and Secrets are similar.
|
|
||||||
//
|
|
||||||
// Like a ConfigMap, a Secret has a `data` field, but unlike a ConfigMap it has
|
|
||||||
// no `binaryData` field.
|
|
||||||
//
|
|
||||||
// All of a Secret's data is assumed to be opaque in nature, and assumed to be
|
|
||||||
// base64 encoded from its original representation, regardless of whether the
|
|
||||||
// original data was UTF-8 text or binary.
|
|
||||||
//
|
|
||||||
// This encoding provides no secrecy. It's just a neutral, common means to
|
|
||||||
// represent opaque text and binary data. Beneath the base64 encoding
|
|
||||||
// is presumably further encoding under control of the Secret's consumer.
|
|
||||||
//
|
|
||||||
// A Secret has string field `type` which holds an identifier, used by the
|
|
||||||
// client, to choose the algorithm to interpret the `data` field. Kubernetes
|
|
||||||
// cannot make use of this data; it's up to a controller or some pod's service
|
|
||||||
// to interpret the value, using `type` as a clue as to how to do this.
|
|
||||||
func MakeSecret(
|
|
||||||
ldr ifc.KvLoader, args *types.SecretArgs) (rn *yaml.RNode, err error) {
|
|
||||||
rn, err = makeBaseNode("Secret", args.Name, args.Namespace)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
t := "Opaque"
|
|
||||||
if args.Type != "" {
|
|
||||||
t = args.Type
|
|
||||||
}
|
|
||||||
if _, err := rn.Pipe(
|
|
||||||
yaml.FieldSetter{
|
|
||||||
Name: "type",
|
|
||||||
Value: yaml.NewStringRNode(t)}); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
m, err := makeValidatedDataMap(ldr, args.Name, args.KvPairSources)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err = rn.LoadMapIntoSecretData(m); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
copyLabelsAndAnnotations(rn, args.Options)
|
|
||||||
return rn, nil
|
|
||||||
}
|
|
||||||
@@ -1,231 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package generators_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
|
||||||
. "sigs.k8s.io/kustomize/api/internal/generators"
|
|
||||||
"sigs.k8s.io/kustomize/api/kv"
|
|
||||||
"sigs.k8s.io/kustomize/api/loader"
|
|
||||||
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMakeSecret(t *testing.T) {
|
|
||||||
type expected struct {
|
|
||||||
out string
|
|
||||||
errMsg string
|
|
||||||
}
|
|
||||||
|
|
||||||
testCases := map[string]struct {
|
|
||||||
args types.SecretArgs
|
|
||||||
exp expected
|
|
||||||
}{
|
|
||||||
"construct secret from env": {
|
|
||||||
args: types.SecretArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "envSecret",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
EnvSources: []string{
|
|
||||||
filepath.Join("secret", "app.env"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: envSecret
|
|
||||||
type: Opaque
|
|
||||||
data:
|
|
||||||
DB_PASSWORD: cXdlcnR5
|
|
||||||
DB_USERNAME: YWRtaW4=
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"construct secret from text file": {
|
|
||||||
args: types.SecretArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "fileSecret1",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
FileSources: []string{
|
|
||||||
filepath.Join("secret", "app-init.ini"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: fileSecret1
|
|
||||||
type: Opaque
|
|
||||||
data:
|
|
||||||
app-init.ini: Rk9PPWJhcgpCQVI9YmF6Cg==
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"construct secret from text and binary file": {
|
|
||||||
args: types.SecretArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "fileSecret2",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
FileSources: []string{
|
|
||||||
filepath.Join("secret", "app-init.ini"),
|
|
||||||
filepath.Join("secret", "app.bin"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: fileSecret2
|
|
||||||
type: Opaque
|
|
||||||
data:
|
|
||||||
app-init.ini: Rk9PPWJhcgpCQVI9YmF6Cg==
|
|
||||||
app.bin: //0=
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"construct secret from literal": {
|
|
||||||
args: types.SecretArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "literalSecret1",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
LiteralSources: []string{"a=x", "b=y", "c=\"Hello World\"", "d='true'"},
|
|
||||||
},
|
|
||||||
Options: &types.GeneratorOptions{
|
|
||||||
Labels: map[string]string{
|
|
||||||
"foo": "bar",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: literalSecret1
|
|
||||||
labels:
|
|
||||||
foo: 'bar'
|
|
||||||
type: Opaque
|
|
||||||
data:
|
|
||||||
a: eA==
|
|
||||||
b: eQ==
|
|
||||||
c: SGVsbG8gV29ybGQ=
|
|
||||||
d: dHJ1ZQ==
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"construct secret with type": {
|
|
||||||
args: types.SecretArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "literalSecret1",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
LiteralSources: []string{"a=x"},
|
|
||||||
},
|
|
||||||
Options: &types.GeneratorOptions{
|
|
||||||
Labels: map[string]string{
|
|
||||||
"foo": "bar",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Type: "foobar",
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: literalSecret1
|
|
||||||
labels:
|
|
||||||
foo: 'bar'
|
|
||||||
type: foobar
|
|
||||||
data:
|
|
||||||
a: eA==
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"construct secret from literal with GeneratorOptions in SecretArgs": {
|
|
||||||
args: types.SecretArgs{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{
|
|
||||||
Name: "literalSecret2",
|
|
||||||
KvPairSources: types.KvPairSources{
|
|
||||||
LiteralSources: []string{"a=x", "b=y", "c=\"Hello World\"", "d='true'"},
|
|
||||||
},
|
|
||||||
Options: &types.GeneratorOptions{
|
|
||||||
Labels: map[string]string{
|
|
||||||
"veggie": "celery",
|
|
||||||
"dog": "beagle",
|
|
||||||
"cat": "annoying",
|
|
||||||
},
|
|
||||||
Annotations: map[string]string{
|
|
||||||
"river": "Missouri",
|
|
||||||
"city": "Iowa City",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
exp: expected{
|
|
||||||
out: `apiVersion: v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: literalSecret2
|
|
||||||
labels:
|
|
||||||
cat: 'annoying'
|
|
||||||
dog: 'beagle'
|
|
||||||
veggie: 'celery'
|
|
||||||
annotations:
|
|
||||||
city: 'Iowa City'
|
|
||||||
river: 'Missouri'
|
|
||||||
type: Opaque
|
|
||||||
data:
|
|
||||||
a: eA==
|
|
||||||
b: eQ==
|
|
||||||
c: SGVsbG8gV29ybGQ=
|
|
||||||
d: dHJ1ZQ==
|
|
||||||
`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
fSys := filesys.MakeFsInMemory()
|
|
||||||
fSys.WriteFile(
|
|
||||||
filesys.RootedPath("secret", "app.env"),
|
|
||||||
[]byte("DB_USERNAME=admin\nDB_PASSWORD=qwerty\n"))
|
|
||||||
fSys.WriteFile(
|
|
||||||
filesys.RootedPath("secret", "app-init.ini"),
|
|
||||||
[]byte("FOO=bar\nBAR=baz\n"))
|
|
||||||
fSys.WriteFile(
|
|
||||||
filesys.RootedPath("secret", "app.bin"),
|
|
||||||
[]byte{0xff, 0xfd})
|
|
||||||
kvLdr := kv.NewLoader(
|
|
||||||
loader.NewFileLoaderAtRoot(fSys),
|
|
||||||
valtest_test.MakeFakeValidator())
|
|
||||||
|
|
||||||
for n := range testCases {
|
|
||||||
tc := testCases[n]
|
|
||||||
t.Run(n, func(t *testing.T) {
|
|
||||||
rn, err := MakeSecret(kvLdr, &tc.args)
|
|
||||||
if err != nil {
|
|
||||||
if !assert.EqualError(t, err, tc.exp.errMsg) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if tc.exp.errMsg != "" {
|
|
||||||
t.Fatalf("%s: should return error '%s'", n, tc.exp.errMsg)
|
|
||||||
}
|
|
||||||
output := rn.MustString()
|
|
||||||
if !assert.Equal(t, tc.exp.out, output) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package generators
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
|
||||||
)
|
|
||||||
|
|
||||||
func makeBaseNode(kind, name, namespace string) (*yaml.RNode, error) {
|
|
||||||
rn, err := yaml.Parse(fmt.Sprintf(`
|
|
||||||
apiVersion: v1
|
|
||||||
kind: %s
|
|
||||||
`, kind))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if name == "" {
|
|
||||||
return nil, errors.Errorf("a configmap must have a name")
|
|
||||||
}
|
|
||||||
if _, err := rn.Pipe(yaml.SetK8sName(name)); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if namespace != "" {
|
|
||||||
if _, err := rn.Pipe(yaml.SetK8sNamespace(namespace)); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return rn, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeValidatedDataMap(
|
|
||||||
ldr ifc.KvLoader, name string, sources types.KvPairSources) (map[string]string, error) {
|
|
||||||
pairs, err := ldr.Load(sources)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.WrapPrefix(err, "loading KV pairs", 0)
|
|
||||||
}
|
|
||||||
knownKeys := make(map[string]string)
|
|
||||||
for _, p := range pairs {
|
|
||||||
// legal key: alphanumeric characters, '-', '_' or '.'
|
|
||||||
if err := ldr.Validator().ErrIfInvalidKey(p.Key); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if _, ok := knownKeys[p.Key]; ok {
|
|
||||||
return nil, errors.Errorf(
|
|
||||||
"configmap %s illegally repeats the key `%s`", name, p.Key)
|
|
||||||
}
|
|
||||||
knownKeys[p.Key] = p.Value
|
|
||||||
}
|
|
||||||
return knownKeys, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// copyLabelsAndAnnotations copies labels and annotations from
|
|
||||||
// GeneratorOptions into the given object.
|
|
||||||
func copyLabelsAndAnnotations(
|
|
||||||
rn *yaml.RNode, opts *types.GeneratorOptions) error {
|
|
||||||
if opts == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for _, k := range yaml.SortedMapKeys(opts.Labels) {
|
|
||||||
v := opts.Labels[k]
|
|
||||||
if _, err := rn.Pipe(yaml.SetLabel(k, v)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, k := range yaml.SortedMapKeys(opts.Annotations) {
|
|
||||||
v := opts.Annotations[k]
|
|
||||||
if _, err := rn.Pipe(yaml.SetAnnotation(k, v)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -4,6 +4,10 @@
|
|||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,29 +18,70 @@ type Cloner func(repoSpec *RepoSpec) error
|
|||||||
// to say, some remote API, to obtain a local clone of
|
// to say, some remote API, to obtain a local clone of
|
||||||
// a remote repo.
|
// a remote repo.
|
||||||
func ClonerUsingGitExec(repoSpec *RepoSpec) error {
|
func ClonerUsingGitExec(repoSpec *RepoSpec) error {
|
||||||
r, err := newCmdRunner()
|
gitProgram, err := exec.LookPath("git")
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "no 'git' program on path")
|
||||||
|
}
|
||||||
|
repoSpec.Dir, err = filesys.NewTmpConfirmedDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
repoSpec.Dir = r.dir
|
|
||||||
if err = r.run("init"); err != nil {
|
cmd := exec.Command(
|
||||||
return err
|
gitProgram,
|
||||||
|
"clone",
|
||||||
|
"--depth=1",
|
||||||
|
repoSpec.CloneSpec(),
|
||||||
|
repoSpec.Dir.String())
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error cloning git repo: %s", out)
|
||||||
|
return errors.Wrapf(
|
||||||
|
err,
|
||||||
|
"trouble cloning git repo %v in %s",
|
||||||
|
repoSpec.CloneSpec(), repoSpec.Dir.String())
|
||||||
}
|
}
|
||||||
if err = r.run(
|
|
||||||
"remote", "add", "origin", repoSpec.CloneSpec()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
ref := "HEAD"
|
|
||||||
if repoSpec.Ref != "" {
|
if repoSpec.Ref != "" {
|
||||||
ref = repoSpec.Ref
|
cmd = exec.Command(
|
||||||
|
gitProgram,
|
||||||
|
"fetch",
|
||||||
|
"--depth=1",
|
||||||
|
"origin",
|
||||||
|
repoSpec.Ref)
|
||||||
|
cmd.Dir = repoSpec.Dir.String()
|
||||||
|
out, err = cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error fetching ref: %s", out)
|
||||||
|
return errors.Wrapf(err, "trouble fetching %s", repoSpec.Ref)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd = exec.Command(
|
||||||
|
gitProgram,
|
||||||
|
"checkout",
|
||||||
|
"FETCH_HEAD")
|
||||||
|
cmd.Dir = repoSpec.Dir.String()
|
||||||
|
out, err = cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error checking out ref: %s", out)
|
||||||
|
return errors.Wrapf(err, "trouble checking out %s", repoSpec.Ref)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err = r.run("fetch", "--depth=1", "origin", ref); err != nil {
|
|
||||||
return err
|
cmd = exec.Command(
|
||||||
|
gitProgram,
|
||||||
|
"submodule",
|
||||||
|
"update",
|
||||||
|
"--init",
|
||||||
|
"--recursive")
|
||||||
|
cmd.Dir = repoSpec.Dir.String()
|
||||||
|
out, err = cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error fetching submodules: %s", out)
|
||||||
|
return errors.Wrapf(err, "trouble fetching submodules for %s", repoSpec.CloneSpec())
|
||||||
}
|
}
|
||||||
if err = r.run("checkout", "FETCH_HEAD"); err != nil {
|
|
||||||
return err
|
return nil
|
||||||
}
|
|
||||||
return r.run("submodule", "update", "--init", "--recursive")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoNothingCloner returns a cloner that only sets
|
// DoNothingCloner returns a cloner that only sets
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package git
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os/exec"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
|
||||||
"sigs.k8s.io/kustomize/api/internal/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Arbitrary, but non-infinite, timeout for running commands.
|
|
||||||
const defaultDuration = 27 * time.Second
|
|
||||||
|
|
||||||
// gitRunner runs the external git binary.
|
|
||||||
type gitRunner struct {
|
|
||||||
gitProgram string
|
|
||||||
duration time.Duration
|
|
||||||
dir filesys.ConfirmedDir
|
|
||||||
}
|
|
||||||
|
|
||||||
// newCmdRunner returns a gitRunner if it can find the binary.
|
|
||||||
// It also creats a temp directory for cloning repos.
|
|
||||||
func newCmdRunner() (*gitRunner, error) {
|
|
||||||
gitProgram, err := exec.LookPath("git")
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "no 'git' program on path")
|
|
||||||
}
|
|
||||||
dir, err := filesys.NewTmpConfirmedDir()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &gitRunner{
|
|
||||||
gitProgram: gitProgram,
|
|
||||||
duration: defaultDuration,
|
|
||||||
dir: dir,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// run a command with a timeout.
|
|
||||||
func (r gitRunner) run(args ...string) error {
|
|
||||||
//nolint: gosec
|
|
||||||
cmd := exec.Command(r.gitProgram, args...)
|
|
||||||
cmd.Dir = r.dir.String()
|
|
||||||
return utils.TimedCall(
|
|
||||||
cmd.String(),
|
|
||||||
r.duration,
|
|
||||||
func() error {
|
|
||||||
_, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "git cmd = '%s'", cmd.String())
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package conflict
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
jsonpatch "github.com/evanphx/json-patch"
|
|
||||||
"k8s.io/apimachinery/pkg/util/mergepatch"
|
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
|
||||||
)
|
|
||||||
|
|
||||||
// conflictDetectorJson detects conflicts in a list of JSON patches.
|
|
||||||
type conflictDetectorJson struct {
|
|
||||||
resourceFactory *resource.Factory
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ resource.ConflictDetector = &conflictDetectorJson{}
|
|
||||||
|
|
||||||
func (cd *conflictDetectorJson) HasConflict(
|
|
||||||
p1, p2 *resource.Resource) (bool, error) {
|
|
||||||
return mergepatch.HasConflicts(p1.Map(), p2.Map())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cd *conflictDetectorJson) MergePatches(
|
|
||||||
patch1, patch2 *resource.Resource) (*resource.Resource, error) {
|
|
||||||
baseBytes, err := json.Marshal(patch1.Map())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
patchBytes, err := json.Marshal(patch2.Map())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
mergedBytes, err := jsonpatch.MergeMergePatches(baseBytes, patchBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
mergedMap := make(map[string]interface{})
|
|
||||||
err = json.Unmarshal(mergedBytes, &mergedMap)
|
|
||||||
return cd.resourceFactory.FromMap(mergedMap), err
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package conflict
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
|
||||||
)
|
|
||||||
|
|
||||||
// conflictDetectorSm detects conflicts in a list of strategic merge patches.
|
|
||||||
type conflictDetectorSm struct {
|
|
||||||
lookupPatchMeta strategicpatch.LookupPatchMeta
|
|
||||||
resourceFactory *resource.Factory
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ resource.ConflictDetector = &conflictDetectorSm{}
|
|
||||||
|
|
||||||
func (cd *conflictDetectorSm) HasConflict(
|
|
||||||
p1, p2 *resource.Resource) (bool, error) {
|
|
||||||
return strategicpatch.MergingMapsHaveConflicts(
|
|
||||||
p1.Map(), p2.Map(), cd.lookupPatchMeta)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cd *conflictDetectorSm) MergePatches(
|
|
||||||
patch1, patch2 *resource.Resource) (*resource.Resource, error) {
|
|
||||||
if cd.hasDeleteDirectiveMarker(patch2.Map()) {
|
|
||||||
if cd.hasDeleteDirectiveMarker(patch1.Map()) {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"cannot merge patches both containing '$patch: delete' directives")
|
|
||||||
}
|
|
||||||
patch1, patch2 = patch2, patch1
|
|
||||||
}
|
|
||||||
mergedMap, err := strategicpatch.MergeStrategicMergeMapPatchUsingLookupPatchMeta(
|
|
||||||
cd.lookupPatchMeta, patch1.Map(), patch2.Map())
|
|
||||||
return cd.resourceFactory.FromMap(mergedMap), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cd *conflictDetectorSm) hasDeleteDirectiveMarker(
|
|
||||||
patch map[string]interface{}) bool {
|
|
||||||
if v, ok := patch["$patch"]; ok && v == "delete" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
for _, v := range patch {
|
|
||||||
switch typedV := v.(type) {
|
|
||||||
case map[string]interface{}:
|
|
||||||
if cd.hasDeleteDirectiveMarker(typedV) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
case []interface{}:
|
|
||||||
for _, sv := range typedV {
|
|
||||||
typedE, ok := sv.(map[string]interface{})
|
|
||||||
if !ok {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if cd.hasDeleteDirectiveMarker(typedE) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package conflict
|
|
||||||
|
|
||||||
import (
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
||||||
sp "k8s.io/apimachinery/pkg/util/strategicpatch"
|
|
||||||
"k8s.io/client-go/kubernetes/scheme"
|
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
|
||||||
)
|
|
||||||
|
|
||||||
type cdFactory struct {
|
|
||||||
rf *resource.Factory
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ resource.ConflictDetectorFactory = &cdFactory{}
|
|
||||||
|
|
||||||
// NewFactory returns a conflict detector factory.
|
|
||||||
// The detector uses a resource factory to convert resources to/from
|
|
||||||
// json/yaml/maps representations.
|
|
||||||
func NewFactory(rf *resource.Factory) resource.ConflictDetectorFactory {
|
|
||||||
return &cdFactory{rf: rf}
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns a conflict detector that's aware of the GVK type.
|
|
||||||
func (f *cdFactory) New(gvk resid.Gvk) (resource.ConflictDetector, error) {
|
|
||||||
// Convert to apimachinery representation of object
|
|
||||||
obj, err := scheme.Scheme.New(schema.GroupVersionKind{
|
|
||||||
Group: gvk.Group,
|
|
||||||
Version: gvk.Version,
|
|
||||||
Kind: gvk.Kind,
|
|
||||||
})
|
|
||||||
if err == nil {
|
|
||||||
meta, err := sp.NewPatchMetaFromStruct(obj)
|
|
||||||
return &conflictDetectorSm{
|
|
||||||
lookupPatchMeta: meta, resourceFactory: f.rf}, err
|
|
||||||
}
|
|
||||||
if runtime.IsNotRegisteredError(err) {
|
|
||||||
return &conflictDetectorJson{resourceFactory: f.rf}, nil
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
239
api/internal/k8sdeps/merge/merginator.go
Normal file
239
api/internal/k8sdeps/merge/merginator.go
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package merge
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
jsonpatch "github.com/evanphx/json-patch"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/apimachinery/pkg/util/mergepatch"
|
||||||
|
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||||
|
"k8s.io/client-go/kubernetes/scheme"
|
||||||
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
|
)
|
||||||
|
|
||||||
|
type conflictDetector interface {
|
||||||
|
hasConflict(patch1, patch2 *resource.Resource) (bool, error)
|
||||||
|
findConflict(
|
||||||
|
conflictingPatchIdx int,
|
||||||
|
patches []*resource.Resource) (*resource.Resource, error)
|
||||||
|
mergePatches(patch1, patch2 *resource.Resource) (*resource.Resource, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsonMergePatch struct {
|
||||||
|
resourceFactory *resource.Factory
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ conflictDetector = &jsonMergePatch{}
|
||||||
|
|
||||||
|
func newJMPConflictDetector(rf *resource.Factory) conflictDetector {
|
||||||
|
return &jsonMergePatch{resourceFactory: rf}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jmp *jsonMergePatch) hasConflict(
|
||||||
|
patch1, patch2 *resource.Resource) (bool, error) {
|
||||||
|
return mergepatch.HasConflicts(patch1.Map(), patch2.Map())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jmp *jsonMergePatch) findConflict(
|
||||||
|
conflictingPatchIdx int,
|
||||||
|
patches []*resource.Resource) (*resource.Resource, error) {
|
||||||
|
for i, patch := range patches {
|
||||||
|
if i == conflictingPatchIdx {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !patches[conflictingPatchIdx].OrgId().Equals(patch.OrgId()) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
conflict, err := mergepatch.HasConflicts(
|
||||||
|
patch.Map(),
|
||||||
|
patches[conflictingPatchIdx].Map())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if conflict {
|
||||||
|
return patch, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jmp *jsonMergePatch) mergePatches(
|
||||||
|
patch1, patch2 *resource.Resource) (*resource.Resource, error) {
|
||||||
|
baseBytes, err := json.Marshal(patch1.Map())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
patchBytes, err := json.Marshal(patch2.Map())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
mergedBytes, err := jsonpatch.MergeMergePatches(baseBytes, patchBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
mergedMap := make(map[string]interface{})
|
||||||
|
err = json.Unmarshal(mergedBytes, &mergedMap)
|
||||||
|
return jmp.resourceFactory.FromMap(mergedMap), err
|
||||||
|
}
|
||||||
|
|
||||||
|
type strategicMergePatch struct {
|
||||||
|
lookupPatchMeta strategicpatch.LookupPatchMeta
|
||||||
|
rf *resource.Factory
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ conflictDetector = &strategicMergePatch{}
|
||||||
|
|
||||||
|
func newSMPConflictDetector(
|
||||||
|
versionedObj runtime.Object,
|
||||||
|
rf *resource.Factory) (conflictDetector, error) {
|
||||||
|
lookupPatchMeta, err := strategicpatch.NewPatchMetaFromStruct(versionedObj)
|
||||||
|
return &strategicMergePatch{lookupPatchMeta: lookupPatchMeta, rf: rf}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (smp *strategicMergePatch) hasConflict(
|
||||||
|
p1, p2 *resource.Resource) (bool, error) {
|
||||||
|
return strategicpatch.MergingMapsHaveConflicts(
|
||||||
|
p1.Map(), p2.Map(), smp.lookupPatchMeta)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (smp *strategicMergePatch) findConflict(
|
||||||
|
conflictingPatchIdx int,
|
||||||
|
patches []*resource.Resource) (*resource.Resource, error) {
|
||||||
|
for i, patch := range patches {
|
||||||
|
if i == conflictingPatchIdx {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !patches[conflictingPatchIdx].OrgId().Equals(patch.OrgId()) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
conflict, err := strategicpatch.MergingMapsHaveConflicts(
|
||||||
|
patch.Map(),
|
||||||
|
patches[conflictingPatchIdx].Map(),
|
||||||
|
smp.lookupPatchMeta)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if conflict {
|
||||||
|
return patch, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (smp *strategicMergePatch) mergePatches(
|
||||||
|
patch1, patch2 *resource.Resource) (*resource.Resource, error) {
|
||||||
|
if hasDeleteDirectiveMarker(patch2.Map()) {
|
||||||
|
if hasDeleteDirectiveMarker(patch1.Map()) {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"cannot merge patches both containing '$patch: delete' directives")
|
||||||
|
}
|
||||||
|
patch1, patch2 = patch2, patch1
|
||||||
|
}
|
||||||
|
mergeJSONMap, err := strategicpatch.MergeStrategicMergeMapPatchUsingLookupPatchMeta(
|
||||||
|
smp.lookupPatchMeta, patch1.Map(), patch2.Map())
|
||||||
|
return smp.rf.FromMap(mergeJSONMap), err
|
||||||
|
}
|
||||||
|
|
||||||
|
type merginatorImpl struct {
|
||||||
|
rf *resource.Factory
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMerginator returns a new implementation of resmap.Merginator.
|
||||||
|
func NewMerginator(rf *resource.Factory) resmap.Merginator {
|
||||||
|
return &merginatorImpl{rf: rf}
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ resmap.Merginator = (*merginatorImpl)(nil)
|
||||||
|
|
||||||
|
// Merge merges the incoming resources into a new resmap.ResMap.
|
||||||
|
// Returns an error on conflict.
|
||||||
|
func (m *merginatorImpl) Merge(
|
||||||
|
patches []*resource.Resource) (resmap.ResMap, error) {
|
||||||
|
rc := resmap.New()
|
||||||
|
for ix, patch := range patches {
|
||||||
|
id := patch.OrgId()
|
||||||
|
existing := rc.GetMatchingResourcesByOriginalId(id.Equals)
|
||||||
|
if len(existing) == 0 {
|
||||||
|
rc.Append(patch)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(existing) > 1 {
|
||||||
|
return nil, fmt.Errorf("self conflict in patches")
|
||||||
|
}
|
||||||
|
|
||||||
|
versionedObj, err := scheme.Scheme.New(toSchemaGvk(id.Gvk))
|
||||||
|
if err != nil && !runtime.IsNotRegisteredError(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var cd conflictDetector
|
||||||
|
if err != nil {
|
||||||
|
cd = newJMPConflictDetector(m.rf)
|
||||||
|
} else {
|
||||||
|
cd, err = newSMPConflictDetector(versionedObj, m.rf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conflict, err := cd.hasConflict(existing[0], patch)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if conflict {
|
||||||
|
conflictingPatch, err := cd.findConflict(ix, patches)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"conflict between %#v and %#v",
|
||||||
|
conflictingPatch.Map(), patch.Map())
|
||||||
|
}
|
||||||
|
merged, err := cd.mergePatches(existing[0], patch)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rc.Replace(merged)
|
||||||
|
}
|
||||||
|
return rc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// toSchemaGvk converts to a schema.GroupVersionKind.
|
||||||
|
func toSchemaGvk(x resid.Gvk) schema.GroupVersionKind {
|
||||||
|
return schema.GroupVersionKind{
|
||||||
|
Group: x.Group,
|
||||||
|
Version: x.Version,
|
||||||
|
Kind: x.Kind,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasDeleteDirectiveMarker(patch map[string]interface{}) bool {
|
||||||
|
if v, ok := patch["$patch"]; ok && v == "delete" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
for _, v := range patch {
|
||||||
|
switch typedV := v.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
if hasDeleteDirectiveMarker(typedV) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case []interface{}:
|
||||||
|
for _, sv := range typedV {
|
||||||
|
typedE, ok := sv.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if hasDeleteDirectiveMarker(typedE) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
25
api/internal/merge/merginator.go
Normal file
25
api/internal/merge/merginator.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
// Copyright 2020 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package merge
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Merginator implements resmap.Merginator using kyaml libs.
|
||||||
|
type Merginator struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ resmap.Merginator = (*Merginator)(nil)
|
||||||
|
|
||||||
|
func NewMerginator(_ *resource.Factory) *Merginator {
|
||||||
|
return &Merginator{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge implements resmap.Merginator
|
||||||
|
func (m Merginator) Merge(
|
||||||
|
resources []*resource.Resource) (resmap.ResMap, error) {
|
||||||
|
panic("TODO(#Merginator): implement Merge")
|
||||||
|
}
|
||||||
4
api/internal/merge/merginator_test.go
Normal file
4
api/internal/merge/merginator_test.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// Copyright 2020 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package merge_test
|
||||||
@@ -13,10 +13,12 @@ import (
|
|||||||
. "sigs.k8s.io/kustomize/api/internal/plugins/execplugin"
|
. "sigs.k8s.io/kustomize/api/internal/plugins/execplugin"
|
||||||
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
|
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
|
||||||
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
fLdr "sigs.k8s.io/kustomize/api/loader"
|
fLdr "sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
|
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExecPluginConfig(t *testing.T) {
|
func TestExecPluginConfig(t *testing.T) {
|
||||||
@@ -31,9 +33,10 @@ s/$BAR/bar baz/g
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
pvd := provider.NewDefaultDepProvider()
|
|
||||||
rf := resmap.NewFactory(
|
rf := resmap.NewFactory(
|
||||||
pvd.GetResourceFactory(), pvd.GetConflictDetectorFactory())
|
resource.NewFactory(
|
||||||
|
kunstruct.NewKunstructuredFactoryImpl()), nil)
|
||||||
|
v := valtest_test.MakeFakeValidator()
|
||||||
pluginConfig := rf.RF().FromMap(
|
pluginConfig := rf.RF().FromMap(
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"apiVersion": "someteam.example.com/v1",
|
"apiVersion": "someteam.example.com/v1",
|
||||||
@@ -45,7 +48,6 @@ s/$BAR/bar baz/g
|
|||||||
"argsFromFile": "sed-input.txt",
|
"argsFromFile": "sed-input.txt",
|
||||||
})
|
})
|
||||||
|
|
||||||
pluginConfig.RemoveBuildAnnotations()
|
|
||||||
p := NewExecPlugin(
|
p := NewExecPlugin(
|
||||||
pLdr.AbsolutePluginPath(
|
pLdr.AbsolutePluginPath(
|
||||||
konfig.DisabledPluginConfig(),
|
konfig.DisabledPluginConfig(),
|
||||||
@@ -60,7 +62,7 @@ s/$BAR/bar baz/g
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected err: %v", err)
|
t.Fatalf("unexpected err: %v", err)
|
||||||
}
|
}
|
||||||
p.Config(resmap.NewPluginHelpers(ldr, pvd.GetFieldValidator(), rf), yaml)
|
p.Config(resmap.NewPluginHelpers(ldr, v, rf), yaml)
|
||||||
|
|
||||||
expected := "someteam.example.com/v1/sedtransformer/SedTransformer"
|
expected := "someteam.example.com/v1/sedtransformer/SedTransformer"
|
||||||
if !strings.HasSuffix(p.Path(), expected) {
|
if !strings.HasSuffix(p.Path(), expected) {
|
||||||
|
|||||||
@@ -8,10 +8,11 @@ import (
|
|||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
. "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
. "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||||
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"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/api/provider"
|
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||||
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"
|
||||||
@@ -50,9 +51,8 @@ func TestLoader(t *testing.T) {
|
|||||||
BuildGoPlugin("builtin", "", "SecretGenerator").
|
BuildGoPlugin("builtin", "", "SecretGenerator").
|
||||||
BuildGoPlugin("someteam.example.com", "v1", "SomeServiceGenerator")
|
BuildGoPlugin("someteam.example.com", "v1", "SomeServiceGenerator")
|
||||||
defer th.Reset()
|
defer th.Reset()
|
||||||
p := provider.NewDefaultDepProvider()
|
rmF := resmap.NewFactory(resource.NewFactory(
|
||||||
rmF := resmap.NewFactory(
|
kunstruct.NewKunstructuredFactoryImpl()), nil)
|
||||||
p.GetResourceFactory(), p.GetConflictDetectorFactory())
|
|
||||||
fLdr, err := loader.NewLoader(
|
fLdr, err := loader.NewLoader(
|
||||||
loader.RestrictionRootOnly,
|
loader.RestrictionRootOnly,
|
||||||
filesys.Separator, filesys.MakeFsInMemory())
|
filesys.Separator, filesys.MakeFsInMemory())
|
||||||
|
|||||||
@@ -9,10 +9,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
|
||||||
"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"
|
||||||
@@ -65,7 +64,7 @@ func strptr(s string) *string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateResourceOptions(t *testing.T) {
|
func TestUpdateResourceOptions(t *testing.T) {
|
||||||
rf := provider.NewDefaultDepProvider().GetResourceFactory()
|
rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
||||||
in := resmap.New()
|
in := resmap.New()
|
||||||
expected := resmap.New()
|
expected := resmap.New()
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
@@ -88,12 +87,28 @@ func TestUpdateResourceOptions(t *testing.T) {
|
|||||||
expected.Append(makeConfigMapOptions(rf, name, c.behavior, !c.needsHash))
|
expected.Append(makeConfigMapOptions(rf, name, c.behavior, !c.needsHash))
|
||||||
}
|
}
|
||||||
actual, err := UpdateResourceOptions(in)
|
actual, err := UpdateResourceOptions(in)
|
||||||
assert.NoError(t, err)
|
if err != nil {
|
||||||
assert.NoError(t, expected.ErrorIfNotEqualLists(actual))
|
t.Fatalf("unexpected error: %v", err.Error())
|
||||||
|
}
|
||||||
|
for i, a := range expected.Resources() {
|
||||||
|
b := actual.GetByIndex(i)
|
||||||
|
if b == nil {
|
||||||
|
t.Fatalf("resource %d missing from processed map", i)
|
||||||
|
}
|
||||||
|
if !a.Equals(b) {
|
||||||
|
t.Errorf("expected %v got %v", a, b)
|
||||||
|
}
|
||||||
|
if a.NeedHashSuffix() != b.NeedHashSuffix() {
|
||||||
|
t.Errorf("")
|
||||||
|
}
|
||||||
|
if a.Behavior() != b.Behavior() {
|
||||||
|
t.Errorf("expected %v got %v", a.Behavior(), b.Behavior())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateResourceOptionsWithInvalidHashAnnotationValues(t *testing.T) {
|
func TestUpdateResourceOptionsWithInvalidHashAnnotationValues(t *testing.T) {
|
||||||
rf := provider.NewDefaultDepProvider().GetResourceFactory()
|
rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
||||||
cases := []string{
|
cases := []string{
|
||||||
"",
|
"",
|
||||||
"FaLsE",
|
"FaLsE",
|
||||||
|
|||||||
@@ -235,18 +235,7 @@ func (kt *KustTarget) runGenerators(
|
|||||||
|
|
||||||
func (kt *KustTarget) configureExternalGenerators() ([]resmap.Generator, error) {
|
func (kt *KustTarget) configureExternalGenerators() ([]resmap.Generator, error) {
|
||||||
ra := accumulator.MakeEmptyAccumulator()
|
ra := accumulator.MakeEmptyAccumulator()
|
||||||
var generatorPaths []string
|
ra, err := kt.accumulateResources(ra, kt.kustomization.Generators)
|
||||||
for _, p := range kt.kustomization.Generators {
|
|
||||||
// handle inline generators
|
|
||||||
rm, err := kt.rFactory.NewResMapFromBytes([]byte(p))
|
|
||||||
if err != nil {
|
|
||||||
// not an inline config
|
|
||||||
generatorPaths = append(generatorPaths, p)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ra.AppendAll(rm)
|
|
||||||
}
|
|
||||||
ra, err := kt.accumulateResources(ra, generatorPaths)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -271,18 +260,7 @@ func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error {
|
|||||||
|
|
||||||
func (kt *KustTarget) configureExternalTransformers(transformers []string) ([]resmap.Transformer, error) {
|
func (kt *KustTarget) configureExternalTransformers(transformers []string) ([]resmap.Transformer, error) {
|
||||||
ra := accumulator.MakeEmptyAccumulator()
|
ra := accumulator.MakeEmptyAccumulator()
|
||||||
var transformerPaths []string
|
ra, err := kt.accumulateResources(ra, transformers)
|
||||||
for _, p := range transformers {
|
|
||||||
// handle inline transformers
|
|
||||||
rm, err := kt.rFactory.NewResMapFromBytes([]byte(p))
|
|
||||||
if err != nil {
|
|
||||||
// not an inline config
|
|
||||||
transformerPaths = append(transformerPaths, p)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ra.AppendAll(rm)
|
|
||||||
}
|
|
||||||
ra, err := kt.accumulateResources(ra, transformerPaths)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -183,7 +183,8 @@ var transformerConfigurators = map[builtinhelpers.BuiltinPluginType]func(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var c struct {
|
var c struct {
|
||||||
Paths []types.PatchStrategicMerge `json:"paths,omitempty" yaml:"paths,omitempty"`
|
Paths []types.PatchStrategicMerge `json:"paths,omitempty" yaml:"paths,omitempty"`
|
||||||
|
Patches string `json:"patches,omitempty" yaml:"patches,omitempty"`
|
||||||
}
|
}
|
||||||
c.Paths = kt.kustomization.PatchesStrategicMerge
|
c.Paths = kt.kustomization.PatchesStrategicMerge
|
||||||
p := f()
|
p := f()
|
||||||
|
|||||||
@@ -8,10 +8,9 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||||
@@ -77,7 +76,8 @@ commonLabels:
|
|||||||
}
|
}
|
||||||
|
|
||||||
kt := makeKustTargetWithRf(
|
kt := makeKustTargetWithRf(
|
||||||
t, th.GetFSys(), "/", provider.NewDefaultDepProvider())
|
t, th.GetFSys(), "/",
|
||||||
|
resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()))
|
||||||
for tn, tc := range testCases {
|
for tn, tc := range testCases {
|
||||||
t.Run(tn, func(t *testing.T) {
|
t.Run(tn, func(t *testing.T) {
|
||||||
th.WriteK("/", tc.content)
|
th.WriteK("/", tc.content)
|
||||||
@@ -148,8 +148,7 @@ metadata:
|
|||||||
{"op": "add", "path": "/spec/replica", "value": "3"}
|
{"op": "add", "path": "/spec/replica", "value": "3"}
|
||||||
]`)
|
]`)
|
||||||
|
|
||||||
pvd := provider.NewDefaultDepProvider()
|
resFactory := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
||||||
resFactory := pvd.GetResourceFactory()
|
|
||||||
|
|
||||||
resources := []*resource.Resource{
|
resources := []*resource.Resource{
|
||||||
resFactory.FromMapWithName("dply1", map[string]interface{}{
|
resFactory.FromMapWithName("dply1", map[string]interface{}{
|
||||||
@@ -244,16 +243,19 @@ metadata:
|
|||||||
t.Fatalf("unexpected error %v", err)
|
t.Fatalf("unexpected error %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expected.RemoveBuildAnnotations()
|
|
||||||
expYaml, err := expected.AsYaml()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
kt := makeKustTargetWithRf(t, th.GetFSys(), "/whatever", pvd)
|
kt := makeKustTargetWithRf(
|
||||||
assert.NoError(t, kt.Load())
|
t, th.GetFSys(), "/whatever", resFactory)
|
||||||
|
err := kt.Load()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected Resources error %v", err)
|
||||||
|
}
|
||||||
actual, err := kt.MakeCustomizedResMap()
|
actual, err := kt.MakeCustomizedResMap()
|
||||||
assert.NoError(t, err)
|
if err != nil {
|
||||||
actual.RemoveBuildAnnotations()
|
t.Fatalf("unexpected Resources error %v", err)
|
||||||
actYaml, err := actual.AsYaml()
|
}
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expYaml, actYaml)
|
if err = expected.ErrorIfNotEqualLists(actual); err != nil {
|
||||||
|
t.Fatalf("unexpected inequality: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,12 +7,14 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
|
"sigs.k8s.io/kustomize/api/internal/k8sdeps/merge"
|
||||||
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||||
"sigs.k8s.io/kustomize/api/internal/target"
|
"sigs.k8s.io/kustomize/api/internal/target"
|
||||||
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
fLdr "sigs.k8s.io/kustomize/api/loader"
|
fLdr "sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,7 +22,9 @@ func makeAndLoadKustTarget(
|
|||||||
t *testing.T,
|
t *testing.T,
|
||||||
fSys filesys.FileSystem,
|
fSys filesys.FileSystem,
|
||||||
root string) *target.KustTarget {
|
root string) *target.KustTarget {
|
||||||
kt := makeKustTargetWithRf(t, fSys, root, provider.NewDefaultDepProvider())
|
kt := makeKustTargetWithRf(
|
||||||
|
t, fSys, root,
|
||||||
|
resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()))
|
||||||
if err := kt.Load(); err != nil {
|
if err := kt.Load(); err != nil {
|
||||||
t.Fatalf("Unexpected load error %v", err)
|
t.Fatalf("Unexpected load error %v", err)
|
||||||
}
|
}
|
||||||
@@ -31,13 +35,13 @@ func makeKustTargetWithRf(
|
|||||||
t *testing.T,
|
t *testing.T,
|
||||||
fSys filesys.FileSystem,
|
fSys filesys.FileSystem,
|
||||||
root string,
|
root string,
|
||||||
pvd *provider.DepProvider) *target.KustTarget {
|
resourceFactory *resource.Factory) *target.KustTarget {
|
||||||
ldr, err := fLdr.NewLoader(fLdr.RestrictionRootOnly, root, fSys)
|
ldr, err := fLdr.NewLoader(fLdr.RestrictionRootOnly, root, fSys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
rf := resmap.NewFactory(
|
rf := resmap.NewFactory(
|
||||||
pvd.GetResourceFactory(), pvd.GetConflictDetectorFactory())
|
resourceFactory, merge.NewMerginator(resourceFactory))
|
||||||
pc := konfig.DisabledPluginConfig()
|
pc := konfig.DisabledPluginConfig()
|
||||||
return target.NewKustTarget(
|
return target.NewKustTarget(
|
||||||
ldr,
|
ldr,
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type errTimeOut struct {
|
|
||||||
duration time.Duration
|
|
||||||
cmd string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewErrTimeOut(d time.Duration, c string) errTimeOut {
|
|
||||||
return errTimeOut{duration: d, cmd: c}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e errTimeOut) Error() string {
|
|
||||||
return fmt.Sprintf("hit %s timeout running '%s'", e.duration, e.cmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsErrTimeout(err error) bool {
|
|
||||||
if err == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
_, ok := err.(errTimeOut)
|
|
||||||
if ok {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
_, ok = errors.Cause(err).(errTimeOut)
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TimedCall runs fn, failing if it doesn't complete in the given duration.
|
|
||||||
// The description is used in the timeout error message.
|
|
||||||
func TimedCall(description string, d time.Duration, fn func() error) error {
|
|
||||||
done := make(chan error)
|
|
||||||
timer := time.NewTimer(d)
|
|
||||||
defer timer.Stop()
|
|
||||||
go func() { done <- fn() }()
|
|
||||||
select {
|
|
||||||
case err := <-done:
|
|
||||||
return err
|
|
||||||
case <-timer.C:
|
|
||||||
return NewErrTimeOut(d, description)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package utils_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
. "sigs.k8s.io/kustomize/api/internal/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
timeToWait = 10 * time.Millisecond
|
|
||||||
tooSlow = 2 * timeToWait
|
|
||||||
)
|
|
||||||
|
|
||||||
func errMsg(msg string) string {
|
|
||||||
return fmt.Sprintf("hit %s timeout running '%s'", timeToWait, msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTimedCallFastNoError(t *testing.T) {
|
|
||||||
err := TimedCall(
|
|
||||||
"fast no error", timeToWait,
|
|
||||||
func() error { return nil })
|
|
||||||
if !assert.NoError(t, err) {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTimedCallFastWithError(t *testing.T) {
|
|
||||||
err := TimedCall(
|
|
||||||
"fast with error", timeToWait,
|
|
||||||
func() error { return assert.AnError })
|
|
||||||
if assert.Error(t, err) {
|
|
||||||
assert.EqualError(t, err, assert.AnError.Error())
|
|
||||||
} else {
|
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTimedCallSlowNoError(t *testing.T) {
|
|
||||||
err := TimedCall(
|
|
||||||
"slow no error", timeToWait,
|
|
||||||
func() error { time.Sleep(tooSlow); return nil })
|
|
||||||
if assert.Error(t, err) {
|
|
||||||
assert.EqualError(t, err, errMsg("slow no error"))
|
|
||||||
} else {
|
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTimedCallSlowWithError(t *testing.T) {
|
|
||||||
err := TimedCall(
|
|
||||||
"slow with error", timeToWait,
|
|
||||||
func() error { time.Sleep(tooSlow); return assert.AnError })
|
|
||||||
if assert.Error(t, err) {
|
|
||||||
assert.EqualError(t, err, errMsg("slow with error"))
|
|
||||||
} else {
|
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,10 +10,6 @@ import (
|
|||||||
// FieldValidator implements ifc.Validator to check
|
// FieldValidator implements ifc.Validator to check
|
||||||
// the values of various KRM string fields,
|
// the values of various KRM string fields,
|
||||||
// e.g. labels, annotations, names, namespaces.
|
// e.g. labels, annotations, names, namespaces.
|
||||||
//
|
|
||||||
// TODO: Have this use kyaml/yaml/internal/k8sgen/pkg/labels
|
|
||||||
// which has label and annotation validation code, but is internal
|
|
||||||
// so this impl would need to move to kyaml (a fine idea).
|
|
||||||
type FieldValidator struct {
|
type FieldValidator struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,105 +4,38 @@
|
|||||||
package wrappy
|
package wrappy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/hasher"
|
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/internal/generators"
|
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// WNodeFactory makes instances of WNode.
|
// WNodeFactory makes instances of WNode.
|
||||||
//
|
|
||||||
// These instances in turn adapt
|
// These instances in turn adapt
|
||||||
// sigs.k8s.io/kustomize/kyaml/yaml.RNode
|
// sigs.k8s.io/kustomize/kyaml/yaml.RNode
|
||||||
// to implement ifc.Unstructured.
|
// to implement ifc.Unstructured.
|
||||||
// This factory is meant to implement ifc.KunstructuredFactory.
|
// This factory is meant to implement ifc.KunstructuredFactory.
|
||||||
//
|
|
||||||
// This implementation should be thin, as both WNode and WNodeFactory must be
|
|
||||||
// factored away (deleted) along with ifc.Kunstructured in favor of direct use
|
|
||||||
// of RNode methods upon completion of
|
|
||||||
// https://github.com/kubernetes-sigs/kustomize/issues/2506.
|
|
||||||
//
|
|
||||||
// See also api/krusty/internal/provider/depprovider.go
|
|
||||||
type WNodeFactory struct {
|
type WNodeFactory struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ ifc.KunstructuredFactory = (*WNodeFactory)(nil)
|
var _ ifc.KunstructuredFactory = (*WNodeFactory)(nil)
|
||||||
|
|
||||||
func (k *WNodeFactory) SliceFromBytes(bs []byte) ([]ifc.Kunstructured, error) {
|
func (k *WNodeFactory) SliceFromBytes(bs []byte) ([]ifc.Kunstructured, error) {
|
||||||
yamlRNodes, err := kio.FromBytes(bs)
|
panic("TODO(#WNodeFactory): implement SliceFromBytes")
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var result []ifc.Kunstructured
|
|
||||||
for i := range yamlRNodes {
|
|
||||||
rn := yamlRNodes[i]
|
|
||||||
meta, err := rn.GetValidatedMetadata()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if !shouldDropObject(meta) {
|
|
||||||
if foundNil, path := rn.HasNilEntryInList(); foundNil {
|
|
||||||
return nil, fmt.Errorf("empty item at %v in object %v", path, rn)
|
|
||||||
}
|
|
||||||
result = append(result, FromRNode(rn))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// shouldDropObject returns true if the resource should not be accumulated.
|
|
||||||
func shouldDropObject(m yaml.ResourceMeta) bool {
|
|
||||||
_, y := m.ObjectMeta.Annotations[konfig.IgnoredByKustomizeAnnotation]
|
|
||||||
return y
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *WNodeFactory) FromMap(m map[string]interface{}) ifc.Kunstructured {
|
func (k *WNodeFactory) FromMap(m map[string]interface{}) ifc.Kunstructured {
|
||||||
rn, err := FromMap(m)
|
panic("TODO(#WNodeFactory): implement FromMap")
|
||||||
if err != nil {
|
|
||||||
// TODO(#WNodeFactory): handle or bubble error"
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return rn
|
|
||||||
}
|
|
||||||
|
|
||||||
// kustHash computes a hash of an unstructured object.
|
|
||||||
type kustHash struct{}
|
|
||||||
|
|
||||||
// Hash returns a hash of the given object
|
|
||||||
func (h *kustHash) Hash(m ifc.Kunstructured) (string, error) {
|
|
||||||
node, err := filtersutil.GetRNode(m)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return hasher.HashRNode(node)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *WNodeFactory) Hasher() ifc.KunstructuredHasher {
|
func (k *WNodeFactory) Hasher() ifc.KunstructuredHasher {
|
||||||
return &kustHash{}
|
panic("TODO(#WNodeFactory): implement Hasher")
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeConfigMap makes a wrapped configmap.
|
|
||||||
func (k *WNodeFactory) MakeConfigMap(
|
func (k *WNodeFactory) MakeConfigMap(
|
||||||
ldr ifc.KvLoader, args *types.ConfigMapArgs) (ifc.Kunstructured, error) {
|
kvLdr ifc.KvLoader, args *types.ConfigMapArgs) (ifc.Kunstructured, error) {
|
||||||
rn, err := generators.MakeConfigMap(ldr, args)
|
panic("TODO(#WNodeFactory): implement MakeConfigMap")
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return FromRNode(rn), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeSecret makes a wrapped secret.
|
|
||||||
func (k *WNodeFactory) MakeSecret(
|
func (k *WNodeFactory) MakeSecret(
|
||||||
ldr ifc.KvLoader, args *types.SecretArgs) (ifc.Kunstructured, error) {
|
kvLdr ifc.KvLoader, args *types.SecretArgs) (ifc.Kunstructured, error) {
|
||||||
rn, err := generators.MakeSecret(ldr, args)
|
panic("TODO(#WNodeFactory): implement MakeSecret")
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return FromRNode(rn), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,321 +1,4 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
// Copyright 2020 The Kubernetes Authors.
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package wrappy_test
|
package wrappy
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
. "sigs.k8s.io/kustomize/api/internal/wrappy"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestHasher(t *testing.T) {
|
|
||||||
input := `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: foo
|
|
||||||
data:
|
|
||||||
one: ""
|
|
||||||
binaryData:
|
|
||||||
two: ""
|
|
||||||
`
|
|
||||||
expect := "698h7c7t9m"
|
|
||||||
|
|
||||||
factory := &WNodeFactory{}
|
|
||||||
k, err := factory.SliceFromBytes([]byte(input))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
hasher := factory.Hasher()
|
|
||||||
result, err := hasher.Hash(k[0])
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if result != expect {
|
|
||||||
t.Fatalf("expect %s but got %s", expect, result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSliceFromBytes(t *testing.T) {
|
|
||||||
factory := &WNodeFactory{}
|
|
||||||
testConfigMap :=
|
|
||||||
map[string]interface{}{
|
|
||||||
"apiVersion": "v1",
|
|
||||||
"kind": "ConfigMap",
|
|
||||||
"metadata": map[string]interface{}{
|
|
||||||
"name": "winnie",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
testConfigMapList :=
|
|
||||||
map[string]interface{}{
|
|
||||||
"apiVersion": "v1",
|
|
||||||
"kind": "ConfigMapList",
|
|
||||||
"items": []interface{}{
|
|
||||||
testConfigMap,
|
|
||||||
testConfigMap,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
testDeploymentSpec := map[string]interface{}{
|
|
||||||
"template": map[string]interface{}{
|
|
||||||
"spec": map[string]interface{}{
|
|
||||||
"hostAliases": []interface{}{
|
|
||||||
map[string]interface{}{
|
|
||||||
"hostnames": []interface{}{
|
|
||||||
"a.example.com",
|
|
||||||
},
|
|
||||||
"ip": "8.8.8.8",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
testDeploymentA := map[string]interface{}{
|
|
||||||
"apiVersion": "apps/v1",
|
|
||||||
"kind": "Deployment",
|
|
||||||
"metadata": map[string]interface{}{
|
|
||||||
"name": "deployment-a",
|
|
||||||
},
|
|
||||||
"spec": testDeploymentSpec,
|
|
||||||
}
|
|
||||||
testDeploymentB := map[string]interface{}{
|
|
||||||
"apiVersion": "apps/v1",
|
|
||||||
"kind": "Deployment",
|
|
||||||
"metadata": map[string]interface{}{
|
|
||||||
"name": "deployment-b",
|
|
||||||
},
|
|
||||||
"spec": testDeploymentSpec,
|
|
||||||
}
|
|
||||||
testDeploymentList :=
|
|
||||||
map[string]interface{}{
|
|
||||||
"apiVersion": "v1",
|
|
||||||
"kind": "DeploymentList",
|
|
||||||
"items": []interface{}{
|
|
||||||
testDeploymentA,
|
|
||||||
testDeploymentB,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
type expected struct {
|
|
||||||
out []map[string]interface{}
|
|
||||||
isErr bool
|
|
||||||
}
|
|
||||||
|
|
||||||
testCases := map[string]struct {
|
|
||||||
input []byte
|
|
||||||
exp expected
|
|
||||||
}{
|
|
||||||
"garbage": {
|
|
||||||
input: []byte("garbageIn: garbageOut"),
|
|
||||||
exp: expected{
|
|
||||||
isErr: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"noBytes": {
|
|
||||||
input: []byte{},
|
|
||||||
exp: expected{
|
|
||||||
out: []map[string]interface{}{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"goodJson": {
|
|
||||||
input: []byte(`
|
|
||||||
{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie"}}
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
out: []map[string]interface{}{testConfigMap},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"goodYaml1": {
|
|
||||||
input: []byte(`
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: winnie
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
out: []map[string]interface{}{testConfigMap},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"goodYaml2": {
|
|
||||||
input: []byte(`
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: winnie
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: winnie
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
out: []map[string]interface{}{testConfigMap, testConfigMap},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"localConfigYaml": {
|
|
||||||
input: []byte(`
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: winnie-skip
|
|
||||||
annotations:
|
|
||||||
# this annotation causes the Resource to be ignored by kustomize
|
|
||||||
config.kubernetes.io/local-config: ""
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: winnie
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
out: []map[string]interface{}{testConfigMap},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"garbageInOneOfTwoObjects": {
|
|
||||||
input: []byte(`
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: winnie
|
|
||||||
---
|
|
||||||
WOOOOOOOOOOOOOOOOOOOOOOOOT: woot
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
isErr: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"emptyObjects": {
|
|
||||||
input: []byte(`
|
|
||||||
---
|
|
||||||
#a comment
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
out: []map[string]interface{}{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"Missing .metadata.name in object": {
|
|
||||||
input: []byte(`
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Namespace
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
foo: bar
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
isErr: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"nil value in list": {
|
|
||||||
input: []byte(`
|
|
||||||
apiVersion: builtin
|
|
||||||
kind: ConfigMapGenerator
|
|
||||||
metadata:
|
|
||||||
name: kube100-site
|
|
||||||
labels:
|
|
||||||
app: web
|
|
||||||
testList:
|
|
||||||
- testA
|
|
||||||
-
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
isErr: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"List": {
|
|
||||||
input: []byte(`
|
|
||||||
apiVersion: v1
|
|
||||||
kind: List
|
|
||||||
items:
|
|
||||||
- apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: winnie
|
|
||||||
- apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: winnie
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
out: []map[string]interface{}{
|
|
||||||
testConfigMap,
|
|
||||||
testConfigMap},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"ConfigMapList": {
|
|
||||||
input: []byte(`
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ConfigMapList
|
|
||||||
items:
|
|
||||||
- apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: winnie
|
|
||||||
- apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: winnie
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
out: []map[string]interface{}{testConfigMapList},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"listWithAnchors": {
|
|
||||||
input: []byte(`
|
|
||||||
apiVersion: v1
|
|
||||||
kind: DeploymentList
|
|
||||||
items:
|
|
||||||
- apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: deployment-a
|
|
||||||
spec: &hostAliases
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
hostAliases:
|
|
||||||
- hostnames:
|
|
||||||
- a.example.com
|
|
||||||
ip: 8.8.8.8
|
|
||||||
- apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: deployment-b
|
|
||||||
spec:
|
|
||||||
<<: *hostAliases
|
|
||||||
`),
|
|
||||||
exp: expected{
|
|
||||||
out: []map[string]interface{}{testDeploymentList},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for n := range testCases {
|
|
||||||
tc := testCases[n]
|
|
||||||
t.Run(n, func(t *testing.T) {
|
|
||||||
rs, err := factory.SliceFromBytes(tc.input)
|
|
||||||
if err != nil {
|
|
||||||
assert.True(t, tc.exp.isErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
assert.False(t, tc.exp.isErr)
|
|
||||||
assert.Equal(t, len(tc.exp.out), len(rs))
|
|
||||||
for i := range rs {
|
|
||||||
assert.Equal(
|
|
||||||
t, fmt.Sprintf("%v", tc.exp.out[i]), fmt.Sprintf("%v", rs[i].Map()))
|
|
||||||
if n != "listWithAnchors" {
|
|
||||||
// https://github.com/kubernetes-sigs/kustomize/issues/3271
|
|
||||||
if !reflect.DeepEqual(tc.exp.out[i], rs[i].Map()) {
|
|
||||||
t.Fatalf("%s:\nexpected: %v\n actual: %v",
|
|
||||||
n, tc.exp.out[i], rs[i].Map())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ package wrappy
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
@@ -33,22 +31,10 @@ func NewWNode() *WNode {
|
|||||||
return FromRNode(yaml.NewRNode(nil))
|
return FromRNode(yaml.NewRNode(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromMap(m map[string]interface{}) (*WNode, error) {
|
|
||||||
n, err := yaml.FromMap(m)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return FromRNode(n), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func FromRNode(node *yaml.RNode) *WNode {
|
func FromRNode(node *yaml.RNode) *WNode {
|
||||||
return &WNode{node: node}
|
return &WNode{node: node}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wn *WNode) AsRNode() *yaml.RNode {
|
|
||||||
return wn.node
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wn *WNode) demandMetaData(label string) yaml.ResourceMeta {
|
func (wn *WNode) demandMetaData(label string) yaml.ResourceMeta {
|
||||||
meta, err := wn.node.GetMeta()
|
meta, err := wn.node.GetMeta()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -68,35 +54,9 @@ func (wn *WNode) GetAnnotations() map[string]string {
|
|||||||
return wn.demandMetaData("GetAnnotations").Annotations
|
return wn.demandMetaData("GetAnnotations").Annotations
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertSliceIndex traverses the items in `fields` and find
|
|
||||||
// if there is a slice index in the item and change it to a
|
|
||||||
// valid Lookup field path. For example, 'ports[0]' will be
|
|
||||||
// converted to 'ports' and '0'.
|
|
||||||
func convertSliceIndex(fields []string) []string {
|
|
||||||
var res []string
|
|
||||||
for _, s := range fields {
|
|
||||||
if !strings.HasSuffix(s, "]") {
|
|
||||||
res = append(res, s)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
re := regexp.MustCompile(`^(.*)\[(\d+)\]$`)
|
|
||||||
groups := re.FindStringSubmatch(s)
|
|
||||||
if len(groups) == 0 {
|
|
||||||
// no match, add to result
|
|
||||||
res = append(res, s)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if groups[1] != "" {
|
|
||||||
res = append(res, groups[1])
|
|
||||||
}
|
|
||||||
res = append(res, groups[2])
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFieldValue implements ifc.Kunstructured.
|
// GetFieldValue implements ifc.Kunstructured.
|
||||||
func (wn *WNode) GetFieldValue(path string) (interface{}, error) {
|
func (wn *WNode) GetFieldValue(path string) (interface{}, error) {
|
||||||
fields := convertSliceIndex(strings.Split(path, "."))
|
fields := strings.Split(path, ".")
|
||||||
rn, err := wn.node.Pipe(yaml.Lookup(fields...))
|
rn, err := wn.node.Pipe(yaml.Lookup(fields...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -123,34 +83,14 @@ func (wn *WNode) GetFieldValue(path string) (interface{}, error) {
|
|||||||
// Return value as slice for SequenceNode kind
|
// Return value as slice for SequenceNode kind
|
||||||
if yn.Kind == yaml.SequenceNode {
|
if yn.Kind == yaml.SequenceNode {
|
||||||
var result []interface{}
|
var result []interface{}
|
||||||
if err := yn.Decode(&result); err != nil {
|
for _, node := range yn.Content {
|
||||||
return nil, err
|
result = append(result, node.Value)
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
if yn.Kind != yaml.ScalarNode {
|
|
||||||
return nil, fmt.Errorf("expected ScalarNode, got Kind=%d", yn.Kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: When doing kustomize var replacement, which is likely a
|
// Return value value directly for all other (ScalarNode) kinds
|
||||||
// a primary use of this function and the reason it returns interface{}
|
return yn.Value, nil
|
||||||
// rather than string, we do conversion from Nodes to Go types and back
|
|
||||||
// to nodes. We should figure out how to do replacement using raw nodes,
|
|
||||||
// assuming we keep the var feature in kustomize.
|
|
||||||
// The other end of this is: refvar.go:updateNodeValue.
|
|
||||||
switch yn.Tag {
|
|
||||||
case yaml.NodeTagString:
|
|
||||||
return yn.Value, nil
|
|
||||||
case yaml.NodeTagInt:
|
|
||||||
return strconv.Atoi(yn.Value)
|
|
||||||
case yaml.NodeTagFloat:
|
|
||||||
return strconv.ParseFloat(yn.Value, 64)
|
|
||||||
case yaml.NodeTagBool:
|
|
||||||
return strconv.ParseBool(yn.Value)
|
|
||||||
default:
|
|
||||||
// Possibly this should be an error or log.
|
|
||||||
return yn.Value, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetGvk implements ifc.Kunstructured.
|
// GetGvk implements ifc.Kunstructured.
|
||||||
@@ -160,16 +100,6 @@ func (wn *WNode) GetGvk() resid.Gvk {
|
|||||||
return resid.Gvk{Group: g, Version: v, Kind: meta.Kind}
|
return resid.Gvk{Group: g, Version: v, Kind: meta.Kind}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDataMap implements ifc.Kunstructured.
|
|
||||||
func (wn *WNode) GetDataMap() map[string]string {
|
|
||||||
return wn.node.GetDataMap()
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetDataMap implements ifc.Kunstructured.
|
|
||||||
func (wn *WNode) SetDataMap(m map[string]string) {
|
|
||||||
wn.node.SetDataMap(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetKind implements ifc.Kunstructured.
|
// GetKind implements ifc.Kunstructured.
|
||||||
func (wn *WNode) GetKind() string {
|
func (wn *WNode) GetKind() string {
|
||||||
return wn.demandMetaData("GetKind").Kind
|
return wn.demandMetaData("GetKind").Kind
|
||||||
@@ -211,7 +141,12 @@ func (wn *WNode) GetString(path string) (string, error) {
|
|||||||
|
|
||||||
// Map implements ifc.Kunstructured.
|
// Map implements ifc.Kunstructured.
|
||||||
func (wn *WNode) Map() map[string]interface{} {
|
func (wn *WNode) Map() map[string]interface{} {
|
||||||
return wn.node.Map()
|
var result map[string]interface{}
|
||||||
|
if err := wn.node.YNode().Decode(&result); err != nil {
|
||||||
|
// Log and die since interface doesn't allow error.
|
||||||
|
log.Fatalf("failed to decode ynode: %v", err)
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements ifc.Kunstructured.
|
// MarshalJSON implements ifc.Kunstructured.
|
||||||
@@ -220,49 +155,47 @@ func (wn *WNode) MarshalJSON() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MatchesAnnotationSelector implements ifc.Kunstructured.
|
// MatchesAnnotationSelector implements ifc.Kunstructured.
|
||||||
func (wn *WNode) MatchesAnnotationSelector(selector string) (bool, error) {
|
func (wn *WNode) MatchesAnnotationSelector(string) (bool, error) {
|
||||||
return wn.node.MatchesAnnotationSelector(selector)
|
panic("TODO(#WNode) MatchesAnnotationSelector; implement or drop from API")
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatchesLabelSelector implements ifc.Kunstructured.
|
// MatchesLabelSelector implements ifc.Kunstructured.
|
||||||
func (wn *WNode) MatchesLabelSelector(selector string) (bool, error) {
|
func (wn *WNode) MatchesLabelSelector(string) (bool, error) {
|
||||||
return wn.node.MatchesLabelSelector(selector)
|
panic("TODO(#WNode) MatchesLabelSelector; implement or drop from API")
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetAnnotations implements ifc.Kunstructured.
|
// SetAnnotations implements ifc.Kunstructured.
|
||||||
func (wn *WNode) SetAnnotations(annotations map[string]string) {
|
func (wn *WNode) SetAnnotations(annotations map[string]string) {
|
||||||
if err := wn.node.SetAnnotations(annotations); err != nil {
|
wn.setField(yaml.NewMapRNode(&annotations), yaml.MetadataField, yaml.AnnotationsField)
|
||||||
log.Fatal(err) // interface doesn't allow error.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetGvk implements ifc.Kunstructured.
|
// SetGvk implements ifc.Kunstructured.
|
||||||
func (wn *WNode) SetGvk(gvk resid.Gvk) {
|
func (wn *WNode) SetGvk(gvk resid.Gvk) {
|
||||||
wn.setMapField(yaml.NewScalarRNode(gvk.Kind), yaml.KindField)
|
wn.setField(yaml.NewScalarRNode(gvk.Kind), yaml.KindField)
|
||||||
wn.setMapField(yaml.NewScalarRNode(gvk.ApiVersion()), yaml.APIVersionField)
|
wn.setField(yaml.NewScalarRNode(fmt.Sprintf("%s/%s", gvk.Group, gvk.Version)), yaml.APIVersionField)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLabels implements ifc.Kunstructured.
|
// SetLabels implements ifc.Kunstructured.
|
||||||
func (wn *WNode) SetLabels(labels map[string]string) {
|
func (wn *WNode) SetLabels(labels map[string]string) {
|
||||||
if err := wn.node.SetLabels(labels); err != nil {
|
wn.setField(yaml.NewMapRNode(&labels), yaml.MetadataField, yaml.LabelsField)
|
||||||
log.Fatal(err) // interface doesn't allow error.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetName implements ifc.Kunstructured.
|
// SetName implements ifc.Kunstructured.
|
||||||
func (wn *WNode) SetName(name string) {
|
func (wn *WNode) SetName(name string) {
|
||||||
wn.setMapField(yaml.NewScalarRNode(name), yaml.MetadataField, yaml.NameField)
|
wn.setField(yaml.NewScalarRNode(name), yaml.MetadataField, yaml.NameField)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetNamespace implements ifc.Kunstructured.
|
// SetNamespace implements ifc.Kunstructured.
|
||||||
func (wn *WNode) SetNamespace(ns string) {
|
func (wn *WNode) SetNamespace(ns string) {
|
||||||
if err := wn.node.SetNamespace(ns); err != nil {
|
wn.setField(yaml.NewScalarRNode(ns), yaml.MetadataField, yaml.NamespaceField)
|
||||||
log.Fatal(err) // interface doesn't allow error.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wn *WNode) setMapField(value *yaml.RNode, path ...string) {
|
func (wn *WNode) setField(value *yaml.RNode, path ...string) {
|
||||||
if err := wn.node.SetMapField(value, path...); err != nil {
|
err := wn.node.PipeE(
|
||||||
|
yaml.LookupCreate(yaml.MappingNode, path[0:len(path)-1]...),
|
||||||
|
yaml.SetField(path[len(path)-1], value),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
// Log and die since interface doesn't allow error.
|
// Log and die since interface doesn't allow error.
|
||||||
log.Fatalf("failed to set field %v: %v", path, err)
|
log.Fatalf("failed to set field %v: %v", path, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
|
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -378,39 +378,6 @@ func TestGetFieldValueReturnsSlice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetFieldValueReturnsSliceOfMappings(t *testing.T) {
|
|
||||||
bytes, err := yaml.Marshal(makeBigMap())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected yaml.Marshal err: %v", err)
|
|
||||||
}
|
|
||||||
rNode, err := kyaml.Parse(string(bytes))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected yaml.Marshal err: %v", err)
|
|
||||||
}
|
|
||||||
wn := FromRNode(rNode)
|
|
||||||
expected := []interface{}{
|
|
||||||
map[string]interface{}{
|
|
||||||
"field1": "idx0foo",
|
|
||||||
"field2": "idx0bar",
|
|
||||||
},
|
|
||||||
map[string]interface{}{
|
|
||||||
"field1": "idx1foo",
|
|
||||||
"field2": "idx1bar",
|
|
||||||
},
|
|
||||||
map[string]interface{}{
|
|
||||||
"field1": "idx2foo",
|
|
||||||
"field2": "idx2bar",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
actual, err := wn.GetFieldValue("those")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error getting slice: %v", err)
|
|
||||||
}
|
|
||||||
if diff := cmp.Diff(expected, actual); diff != "" {
|
|
||||||
t.Fatalf("actual slice does not deep equal expected slice:\n%v", diff)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetFieldValueReturnsString(t *testing.T) {
|
func TestGetFieldValueReturnsString(t *testing.T) {
|
||||||
wn := NewWNode()
|
wn := NewWNode()
|
||||||
if err := wn.UnmarshalJSON([]byte(deploymentBiggerJson)); err != nil {
|
if err := wn.UnmarshalJSON([]byte(deploymentBiggerJson)); err != nil {
|
||||||
@@ -481,10 +448,6 @@ func TestGetSlice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMapEmpty(t *testing.T) {
|
|
||||||
assert.Equal(t, 0, len(NewWNode().Map()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMap(t *testing.T) {
|
func TestMap(t *testing.T) {
|
||||||
wn := NewWNode()
|
wn := NewWNode()
|
||||||
if err := wn.UnmarshalJSON([]byte(deploymentLittleJson)); err != nil {
|
if err := wn.UnmarshalJSON([]byte(deploymentLittleJson)); err != nil {
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/util/yaml"
|
"k8s.io/apimachinery/pkg/util/yaml"
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/internal/k8sdeps/configmapandsecret"
|
"sigs.k8s.io/kustomize/api/internal/k8sdeps/configmapandsecret"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -117,6 +116,10 @@ func (kf *KunstructuredFactoryImpl) validate(u unstructured.Unstructured) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nonKustomizableResourceAnnotation if set on a Resource will cause Kustomize to
|
||||||
|
// ignore the Resource rather than Kustomize it.
|
||||||
|
const ignoredByKustomizeResourceAnnotation = "config.kubernetes.io/local-config"
|
||||||
|
|
||||||
// skipResource returns true if the Resource should not be accumulated
|
// skipResource returns true if the Resource should not be accumulated
|
||||||
func (kf *KunstructuredFactoryImpl) skipResource(u unstructured.Unstructured) bool {
|
func (kf *KunstructuredFactoryImpl) skipResource(u unstructured.Unstructured) bool {
|
||||||
an := u.GetAnnotations()
|
an := u.GetAnnotations()
|
||||||
@@ -125,7 +128,7 @@ func (kf *KunstructuredFactoryImpl) skipResource(u unstructured.Unstructured) bo
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// check if the Resource has opt-ed out of kustomize
|
// check if the Resource has opt-ed out of kustomize
|
||||||
_, found := an[konfig.IgnoredByKustomizeAnnotation]
|
_, found := an[ignoredByKustomizeResourceAnnotation]
|
||||||
return found
|
return found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ package kunstruct
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
jsonpatch "github.com/evanphx/json-patch"
|
jsonpatch "github.com/evanphx/json-patch"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@@ -250,29 +249,6 @@ func (fs *UnstructAdapter) GetStringMap(path string) (map[string]string, error)
|
|||||||
return nil, NoFieldError{Field: path}
|
return nil, NoFieldError{Field: path}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *UnstructAdapter) GetDataMap() map[string]string {
|
|
||||||
m, err := fs.GetStringMap("data")
|
|
||||||
if err != nil {
|
|
||||||
return map[string]string{}
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fs *UnstructAdapter) SetDataMap(m map[string]string) {
|
|
||||||
if m == nil {
|
|
||||||
unstructured.RemoveNestedField(fs.Object, "data")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
s := make(map[string]interface{}, len(m))
|
|
||||||
for i, v := range m {
|
|
||||||
s[i] = v
|
|
||||||
}
|
|
||||||
err := unstructured.SetNestedMap(fs.Object, s, "data")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetMap returns value at the given fieldpath.
|
// GetMap returns value at the given fieldpath.
|
||||||
func (fs *UnstructAdapter) GetMap(path string) (map[string]interface{}, error) {
|
func (fs *UnstructAdapter) GetMap(path string) (map[string]interface{}, error) {
|
||||||
content, fields, found, err := fs.selectSubtree(path)
|
content, fields, found, err := fs.selectSubtree(path)
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ package kunstruct
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var kunstructured = NewKunstructuredFactoryImpl().FromMap(map[string]interface{}{
|
var kunstructured = NewKunstructuredFactoryImpl().FromMap(map[string]interface{}{
|
||||||
@@ -559,139 +557,3 @@ func compareValues(t *testing.T, name string, pathToField string, expectedValue
|
|||||||
t.Logf("%T value at `%s`", typedV, pathToField)
|
t.Logf("%T value at `%s`", typedV, pathToField)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKunstGetDataMap(t *testing.T) {
|
|
||||||
emptyMap := map[string]string{}
|
|
||||||
testCases := map[string]struct {
|
|
||||||
theMap map[string]interface{}
|
|
||||||
expected map[string]string
|
|
||||||
}{
|
|
||||||
"actuallyNil": {
|
|
||||||
theMap: nil,
|
|
||||||
expected: emptyMap,
|
|
||||||
},
|
|
||||||
"empty": {
|
|
||||||
theMap: map[string]interface{}{},
|
|
||||||
expected: emptyMap,
|
|
||||||
},
|
|
||||||
"mostlyEmpty": {
|
|
||||||
theMap: map[string]interface{}{
|
|
||||||
"hey": "there",
|
|
||||||
},
|
|
||||||
expected: emptyMap,
|
|
||||||
},
|
|
||||||
"noNameConfigMap": {
|
|
||||||
theMap: map[string]interface{}{
|
|
||||||
"apiVersion": "v1",
|
|
||||||
"kind": "ConfigMap",
|
|
||||||
},
|
|
||||||
expected: emptyMap,
|
|
||||||
},
|
|
||||||
"configMap": {
|
|
||||||
theMap: map[string]interface{}{
|
|
||||||
"apiVersion": "v1",
|
|
||||||
"kind": "ConfigMap",
|
|
||||||
"metadata": map[string]interface{}{
|
|
||||||
"name": "winnie",
|
|
||||||
},
|
|
||||||
"data": map[string]interface{}{
|
|
||||||
"wine": "cabernet",
|
|
||||||
"truck": "ford",
|
|
||||||
"rocket": "falcon9",
|
|
||||||
"planet": "mars",
|
|
||||||
"city": "brownsville",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
// order irrelevant, because assert.Equals is smart about maps.
|
|
||||||
expected: map[string]string{
|
|
||||||
"city": "brownsville",
|
|
||||||
"wine": "cabernet",
|
|
||||||
"planet": "mars",
|
|
||||||
"rocket": "falcon9",
|
|
||||||
"truck": "ford",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for n := range testCases {
|
|
||||||
tc := testCases[n]
|
|
||||||
t.Run(n, func(t *testing.T) {
|
|
||||||
kunstr := NewKunstructuredFactoryImpl().FromMap(tc.theMap)
|
|
||||||
m := kunstr.GetDataMap()
|
|
||||||
if !assert.Equal(t, tc.expected, m) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestKunstSetDataMap(t *testing.T) {
|
|
||||||
testCases := map[string]struct {
|
|
||||||
theMap map[string]interface{}
|
|
||||||
input map[string]string
|
|
||||||
expected map[string]string
|
|
||||||
}{
|
|
||||||
"empty": {
|
|
||||||
theMap: map[string]interface{}{},
|
|
||||||
input: map[string]string{
|
|
||||||
"wine": "cabernet",
|
|
||||||
"truck": "ford",
|
|
||||||
},
|
|
||||||
expected: map[string]string{
|
|
||||||
"wine": "cabernet",
|
|
||||||
"truck": "ford",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"replace": {
|
|
||||||
theMap: map[string]interface{}{
|
|
||||||
"foo": 3,
|
|
||||||
"data": map[string]string{
|
|
||||||
"rocket": "falcon9",
|
|
||||||
"planet": "mars",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
input: map[string]string{
|
|
||||||
"wine": "cabernet",
|
|
||||||
"truck": "ford",
|
|
||||||
},
|
|
||||||
expected: map[string]string{
|
|
||||||
"wine": "cabernet",
|
|
||||||
"truck": "ford",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"clear1": {
|
|
||||||
theMap: map[string]interface{}{
|
|
||||||
"foo": 3,
|
|
||||||
"data": map[string]string{
|
|
||||||
"rocket": "falcon9",
|
|
||||||
"planet": "mars",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
input: map[string]string{},
|
|
||||||
expected: map[string]string{},
|
|
||||||
},
|
|
||||||
"clear2": {
|
|
||||||
theMap: map[string]interface{}{
|
|
||||||
"foo": 3,
|
|
||||||
"data": map[string]string{
|
|
||||||
"rocket": "falcon9",
|
|
||||||
"planet": "mars",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
input: nil,
|
|
||||||
expected: map[string]string{},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for n := range testCases {
|
|
||||||
tc := testCases[n]
|
|
||||||
t.Run(n, func(t *testing.T) {
|
|
||||||
kunstr := NewKunstructuredFactoryImpl().FromMap(tc.theMap)
|
|
||||||
kunstr.SetDataMap(tc.input)
|
|
||||||
m := kunstr.GetDataMap()
|
|
||||||
if !assert.Equal(t, tc.expected, m) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -51,11 +51,6 @@ commonLabels:
|
|||||||
group: apps
|
group: apps
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
|
||||||
- path: spec/template/spec/topologySpreadConstraints/labelSelector/matchLabels
|
|
||||||
create: false
|
|
||||||
group: apps
|
|
||||||
kind: Deployment
|
|
||||||
|
|
||||||
- path: spec/selector/matchLabels
|
- path: spec/selector/matchLabels
|
||||||
create: true
|
create: true
|
||||||
kind: ReplicaSet
|
kind: ReplicaSet
|
||||||
@@ -102,11 +97,6 @@ commonLabels:
|
|||||||
group: apps
|
group: apps
|
||||||
kind: StatefulSet
|
kind: StatefulSet
|
||||||
|
|
||||||
- path: spec/template/spec/topologySpreadConstraints/labelSelector/matchLabels
|
|
||||||
create: false
|
|
||||||
group: apps
|
|
||||||
kind: StatefulSet
|
|
||||||
|
|
||||||
- path: spec/volumeClaimTemplates[]/metadata/labels
|
- path: spec/volumeClaimTemplates[]/metadata/labels
|
||||||
create: true
|
create: true
|
||||||
group: apps
|
group: apps
|
||||||
|
|||||||
@@ -121,10 +121,6 @@ nameReference:
|
|||||||
kind: CronJob
|
kind: CronJob
|
||||||
- path: spec/configSource/configMap
|
- path: spec/configSource/configMap
|
||||||
kind: Node
|
kind: Node
|
||||||
- path: rules/resourceNames
|
|
||||||
kind: Role
|
|
||||||
- path: rules/resourceNames
|
|
||||||
kind: ClusterRole
|
|
||||||
|
|
||||||
- kind: Secret
|
- kind: Secret
|
||||||
version: v1
|
version: v1
|
||||||
@@ -262,8 +258,6 @@ nameReference:
|
|||||||
kind: Service
|
kind: Service
|
||||||
group: serving.knative.dev
|
group: serving.knative.dev
|
||||||
version: v1
|
version: v1
|
||||||
- path: spec/azureFile/secretName
|
|
||||||
kind: PersistentVolume
|
|
||||||
|
|
||||||
- kind: Service
|
- kind: Service
|
||||||
version: v1
|
version: v1
|
||||||
|
|||||||
@@ -19,32 +19,7 @@ func DefaultKustomizationFileName() string {
|
|||||||
return RecognizedKustomizationFileNames()[0]
|
return RecognizedKustomizationFileNames()[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// IfApiMachineryElseKyaml returns true if executing the apimachinery code
|
|
||||||
// path, else we're executing the kyaml code paths.
|
|
||||||
func IfApiMachineryElseKyaml(s1, s2 string) string {
|
|
||||||
if !FlagEnableKyamlDefaultValue {
|
|
||||||
return s1
|
|
||||||
}
|
|
||||||
return s2
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// FlagEnableKyamlDefaultValue is the default value for the --enable_kyaml
|
|
||||||
// flag. This value is also used in unit tests. See provider.DepProvider.
|
|
||||||
//
|
|
||||||
// TODO(#3304): eliminate branching on this constant.
|
|
||||||
// Details: https://github.com/kubernetes-sigs/kustomize/issues/3304
|
|
||||||
//
|
|
||||||
// All tests should pass for either true or false values
|
|
||||||
// of this constant, without having to check its value.
|
|
||||||
// In the cases where there's a different outcome, either decide
|
|
||||||
// that the difference is acceptable, or make the difference go away.
|
|
||||||
//
|
|
||||||
// Historically, tests passed for enable_kyaml == false, i.e. using
|
|
||||||
// apimachinery libs. This doesn't mean the code was better, it just
|
|
||||||
// means regression tests preserved those outcomes.
|
|
||||||
FlagEnableKyamlDefaultValue = true
|
|
||||||
|
|
||||||
// An environment variable to consult for kustomization
|
// An environment variable to consult for kustomization
|
||||||
// configuration data. See:
|
// configuration data. See:
|
||||||
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||||
@@ -56,12 +31,6 @@ const (
|
|||||||
// A program name, for use in help, finding the XDG_CONFIG_DIR, etc.
|
// A program name, for use in help, finding the XDG_CONFIG_DIR, etc.
|
||||||
ProgramName = "kustomize"
|
ProgramName = "kustomize"
|
||||||
|
|
||||||
// ConfigAnnoDomain is configuration-related annotation namespace.
|
|
||||||
ConfigAnnoDomain = "config.kubernetes.io"
|
|
||||||
|
|
||||||
// If a resource has this annotation, kustomize will drop it.
|
|
||||||
IgnoredByKustomizeAnnotation = ConfigAnnoDomain + "/local-config"
|
|
||||||
|
|
||||||
// Label key that indicates the resources are built from Kustomize
|
// Label key that indicates the resources are built from Kustomize
|
||||||
ManagedbyLabelKey = "app.kubernetes.io/managed-by"
|
ManagedbyLabelKey = "app.kubernetes.io/managed-by"
|
||||||
|
|
||||||
|
|||||||
@@ -1,102 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package krusty_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestBasicIO_1(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
opts := th.MakeDefaultOptions()
|
|
||||||
if !opts.UseKyaml {
|
|
||||||
// This test won't pass under apimachinery, because in the bowels of
|
|
||||||
// that code (see GetAnnotations in v0.17.0 of
|
|
||||||
// k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructured.go)
|
|
||||||
// an error returned from NestedStringMap is discarded, and an
|
|
||||||
// empty annotation map is silently returned, making this test fail
|
|
||||||
// The swallowed error arises from code like:
|
|
||||||
// var v interface{}
|
|
||||||
// v = true
|
|
||||||
// if str, ok := v.(string); ok {
|
|
||||||
// save the value in a map (doesn't happen)
|
|
||||||
// } else {
|
|
||||||
// return an error (that is then ignored by GetAnnotations)
|
|
||||||
// }
|
|
||||||
// The error happens when any annotation value can be interpreted as
|
|
||||||
// a boolean or number. Such annotations cannot be successfully applied
|
|
||||||
// to an object in a cluster unless they are quoted.
|
|
||||||
t.SkipNow()
|
|
||||||
}
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- service.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("service.yaml", `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
port: 8080
|
|
||||||
happy: true
|
|
||||||
color: green
|
|
||||||
name: demo
|
|
||||||
spec:
|
|
||||||
clusterIP: None
|
|
||||||
`)
|
|
||||||
m := th.Run(".", opts)
|
|
||||||
// The annotations are sorted by key, hence the order change.
|
|
||||||
// Quotes are added intentionally.
|
|
||||||
th.AssertActualEqualsExpected(
|
|
||||||
m, `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
color: green
|
|
||||||
happy: "true"
|
|
||||||
port: "8080"
|
|
||||||
name: demo
|
|
||||||
spec:
|
|
||||||
clusterIP: None
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBasicIO_2(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
opts := th.MakeDefaultOptions()
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- service.yaml
|
|
||||||
`)
|
|
||||||
// All the annotation values are quoted in the input.
|
|
||||||
th.WriteF("service.yaml", `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
port: "8080"
|
|
||||||
happy: "true"
|
|
||||||
color: green
|
|
||||||
name: demo
|
|
||||||
spec:
|
|
||||||
clusterIP: None
|
|
||||||
`)
|
|
||||||
m := th.Run(".", opts)
|
|
||||||
// The annotations are sorted by key, hence the order change.
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
color: green
|
|
||||||
happy: "true"
|
|
||||||
port: "8080"
|
|
||||||
name: demo
|
|
||||||
spec:
|
|
||||||
clusterIP: None
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
@@ -39,8 +39,8 @@ resources:
|
|||||||
configMapGenerator:
|
configMapGenerator:
|
||||||
- name: my-configmap
|
- name: my-configmap
|
||||||
literals:
|
literals:
|
||||||
- testValue=purple
|
- testValue=1
|
||||||
- otherValue=green
|
- otherValue=10
|
||||||
`)
|
`)
|
||||||
th.WriteF("/app/base/deploy.yaml", `
|
th.WriteF("/app/base/deploy.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
@@ -64,8 +64,8 @@ configMapGenerator:
|
|||||||
- name: my-configmap
|
- name: my-configmap
|
||||||
behavior: merge
|
behavior: merge
|
||||||
literals:
|
literals:
|
||||||
- testValue=blue
|
- testValue=2
|
||||||
- compValue=red
|
- compValue=5
|
||||||
`)
|
`)
|
||||||
th.WriteF("/app/comp/stub.yaml", `
|
th.WriteF("/app/comp/stub.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
@@ -125,12 +125,14 @@ spec:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
compValue: red
|
compValue: "5"
|
||||||
otherValue: green
|
otherValue: "10"
|
||||||
testValue: blue
|
testValue: "2"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: comp-my-configmap-97647ckcmg
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
|
name: comp-my-configmap-kc6k2kmkh9
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -154,7 +156,7 @@ configMapGenerator:
|
|||||||
- name: my-configmap
|
- name: my-configmap
|
||||||
behavior: merge
|
behavior: merge
|
||||||
literals:
|
literals:
|
||||||
- otherValue=orange
|
- otherValue=9
|
||||||
`),
|
`),
|
||||||
writeK("/app/prod", `
|
writeK("/app/prod", `
|
||||||
resources:
|
resources:
|
||||||
@@ -177,12 +179,14 @@ spec:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
compValue: red
|
compValue: "5"
|
||||||
otherValue: orange
|
otherValue: "9"
|
||||||
testValue: blue
|
testValue: "2"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: comp-my-configmap-g486mb229k
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
|
name: comp-my-configmap-55249mf5kb
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -208,7 +212,7 @@ configMapGenerator:
|
|||||||
- name: my-configmap
|
- name: my-configmap
|
||||||
behavior: merge
|
behavior: merge
|
||||||
literals:
|
literals:
|
||||||
- otherValue=orange
|
- otherValue=9
|
||||||
`),
|
`),
|
||||||
writeK("/app/prod", `
|
writeK("/app/prod", `
|
||||||
resources:
|
resources:
|
||||||
@@ -230,12 +234,14 @@ spec:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
compValue: red
|
compValue: "5"
|
||||||
otherValue: orange
|
otherValue: "9"
|
||||||
testValue: blue
|
testValue: "2"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: comp-my-configmap-g486mb229k
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
|
name: comp-my-configmap-55249mf5kb
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -273,11 +279,11 @@ spec:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
otherValue: green
|
otherValue: "10"
|
||||||
testValue: purple
|
testValue: "1"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: my-configmap-9cd648hm8f
|
name: my-configmap-2g9c94mhb8
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -288,12 +294,14 @@ spec:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
compValue: red
|
compValue: "5"
|
||||||
otherValue: green
|
otherValue: "10"
|
||||||
testValue: blue
|
testValue: "2"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: comp-my-configmap-97647ckcmg
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
|
name: comp-my-configmap-kc6k2kmkh9
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -319,8 +327,8 @@ configMapGenerator:
|
|||||||
- name: my-configmap
|
- name: my-configmap
|
||||||
behavior: merge
|
behavior: merge
|
||||||
literals:
|
literals:
|
||||||
- compValue=red
|
- compValue=5
|
||||||
- testValue=blue
|
- testValue=2
|
||||||
`),
|
`),
|
||||||
},
|
},
|
||||||
runPath: "/app/direct-component",
|
runPath: "/app/direct-component",
|
||||||
@@ -334,12 +342,14 @@ spec:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
compValue: red
|
compValue: "5"
|
||||||
otherValue: green
|
otherValue: "10"
|
||||||
testValue: blue
|
testValue: "2"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: my-configmap-97647ckcmg
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
|
name: my-configmap-kc6k2kmkh9
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
"missing-optional-component-api-version": {
|
"missing-optional-component-api-version": {
|
||||||
@@ -350,7 +360,7 @@ configMapGenerator:
|
|||||||
- name: my-configmap
|
- name: my-configmap
|
||||||
behavior: merge
|
behavior: merge
|
||||||
literals:
|
literals:
|
||||||
- otherValue=orange
|
- otherValue=9
|
||||||
`),
|
`),
|
||||||
},
|
},
|
||||||
runPath: "/app/prod",
|
runPath: "/app/prod",
|
||||||
@@ -364,11 +374,13 @@ spec:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
otherValue: orange
|
otherValue: "9"
|
||||||
testValue: purple
|
testValue: "1"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: my-configmap-6hhdg8gkdg
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
|
name: my-configmap-5g7gh5mgt5
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -411,11 +423,11 @@ spec:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
otherValue: green
|
otherValue: "10"
|
||||||
testValue: purple
|
testValue: "1"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: my-configmap-a-b-9cd648hm8f
|
name: my-configmap-a-b-2g9c94mhb8
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -426,11 +438,11 @@ spec:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
otherValue: green
|
otherValue: "10"
|
||||||
testValue: purple
|
testValue: "1"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: my-configmap-b-9cd648hm8f
|
name: my-configmap-b-2g9c94mhb8
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -562,7 +574,7 @@ configMapGenerator:
|
|||||||
- name: my-configmap
|
- name: my-configmap
|
||||||
behavior: merge
|
behavior: merge
|
||||||
literals:
|
literals:
|
||||||
- otherValue=orange
|
- otherValue=9
|
||||||
`),
|
`),
|
||||||
},
|
},
|
||||||
runPath: "/app/prod",
|
runPath: "/app/prod",
|
||||||
|
|||||||
@@ -4,121 +4,11 @@
|
|||||||
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"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Numbers and booleans are quoted
|
|
||||||
func TestGeneratorIntVsStringNoMerge(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- service.yaml
|
|
||||||
configMapGenerator:
|
|
||||||
- name: bob
|
|
||||||
literals:
|
|
||||||
- fruit=Indian Gooseberry
|
|
||||||
- year=2020
|
|
||||||
- crisis=true
|
|
||||||
`)
|
|
||||||
th.WriteF("service.yaml", `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: demo
|
|
||||||
spec:
|
|
||||||
clusterIP: None
|
|
||||||
`)
|
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(
|
|
||||||
m, `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: demo
|
|
||||||
spec:
|
|
||||||
clusterIP: None
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
crisis: "true"
|
|
||||||
fruit: Indian Gooseberry
|
|
||||||
year: "2020"
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: bob-79t79mt227
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGeneratorIntVsStringWithMerge(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK("base", `
|
|
||||||
configMapGenerator:
|
|
||||||
- name: bob
|
|
||||||
literals:
|
|
||||||
- fruit=Indian Gooseberry
|
|
||||||
- year=2020
|
|
||||||
- crisis=true
|
|
||||||
`)
|
|
||||||
th.WriteK("overlay", `
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
configMapGenerator:
|
|
||||||
- name: bob
|
|
||||||
behavior: merge
|
|
||||||
literals:
|
|
||||||
- month=12
|
|
||||||
`)
|
|
||||||
m := th.Run("overlay", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `apiVersion: v1
|
|
||||||
data:
|
|
||||||
crisis: "true"
|
|
||||||
fruit: Indian Gooseberry
|
|
||||||
month: "12"
|
|
||||||
year: "2020"
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: bob-bk46gm59c6
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGeneratorFromProperties(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK("base", `
|
|
||||||
configMapGenerator:
|
|
||||||
- name: test-configmap
|
|
||||||
behavior: create
|
|
||||||
envs:
|
|
||||||
- properties
|
|
||||||
`)
|
|
||||||
th.WriteF("base/properties", `
|
|
||||||
VAR1=100
|
|
||||||
`)
|
|
||||||
th.WriteK("overlay", `
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
configMapGenerator:
|
|
||||||
- name: test-configmap
|
|
||||||
behavior: "merge"
|
|
||||||
envs:
|
|
||||||
- properties
|
|
||||||
`)
|
|
||||||
th.WriteF("overlay/properties", `
|
|
||||||
VAR2=200
|
|
||||||
`)
|
|
||||||
m := th.Run("overlay", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `apiVersion: v1
|
|
||||||
data:
|
|
||||||
VAR1: "100"
|
|
||||||
VAR2: "200"
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: test-configmap-hdghb5ddkg
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate a Secret and a ConfigMap from the same data
|
// Generate a Secret and a ConfigMap from the same data
|
||||||
// to compare the result.
|
// to compare the result.
|
||||||
func TestGeneratorBasics(t *testing.T) {
|
func TestGeneratorBasics(t *testing.T) {
|
||||||
@@ -169,9 +59,10 @@ electromagnetic
|
|||||||
strong nuclear
|
strong nuclear
|
||||||
weak nuclear
|
weak nuclear
|
||||||
`)
|
`)
|
||||||
opts := th.MakeDefaultOptions()
|
|
||||||
m := th.Run("/app", opts)
|
m := th.Run("/app", th.MakeDefaultOptions())
|
||||||
expFmt := `apiVersion: v1
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
MOUNTAIN: everest
|
MOUNTAIN: everest
|
||||||
OCEAN: pacific
|
OCEAN: pacific
|
||||||
@@ -204,30 +95,15 @@ apiVersion: v1
|
|||||||
data:
|
data:
|
||||||
MOUNTAIN: ZXZlcmVzdA==
|
MOUNTAIN: ZXZlcmVzdA==
|
||||||
OCEAN: cGFjaWZpYw==
|
OCEAN: cGFjaWZpYw==
|
||||||
forces.txt: %s
|
forces.txt: CmdyYXZpdGF0aW9uYWwKZWxlY3Ryb21hZ25ldGljCnN0cm9uZyBudWNsZWFyCndlYWsgbnVjbGVhcgo=
|
||||||
fruit: YXBwbGU=
|
fruit: YXBwbGU=
|
||||||
passphrase: %s
|
passphrase: CkxpZmUgaXMgc2hvcnQuCkJ1dCB0aGUgeWVhcnMgYXJlIGxvbmcuCk5vdCB3aGlsZSB0aGUgZXZpbCBkYXlzIGNvbWUgbm90Lgo=
|
||||||
vegetable: YnJvY2NvbGk=
|
vegetable: YnJvY2NvbGk=
|
||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
name: blah-bob-%s
|
name: blah-bob-ftht6hfgmb
|
||||||
type: Opaque
|
type: Opaque
|
||||||
`
|
`)
|
||||||
th.AssertActualEqualsExpected(
|
|
||||||
m,
|
|
||||||
// TODO(#3304): DECISION - kyaml better; not a bug.
|
|
||||||
opts.IfApiMachineryElseKyaml(
|
|
||||||
fmt.Sprintf(
|
|
||||||
expFmt,
|
|
||||||
`CmdyYXZpdGF0aW9uYWwKZWxlY3Ryb21hZ25ldGljCnN0cm9uZyBudWNsZWFyCndlYWsgbnVjbGVhcgo=`,
|
|
||||||
`CkxpZmUgaXMgc2hvcnQuCkJ1dCB0aGUgeWVhcnMgYXJlIGxvbmcuCk5vdCB3aGlsZSB0aGUgZXZpbCBkYXlzIGNvbWUgbm90Lgo=`,
|
|
||||||
`ftht6hfgmb`),
|
|
||||||
fmt.Sprintf(
|
|
||||||
expFmt, `|
|
|
||||||
CmdyYXZpdGF0aW9uYWwKZWxlY3Ryb21hZ25ldGljCnN0cm9uZyBudWNsZWFyCndlYWsgbn
|
|
||||||
VjbGVhcgo=`, `|
|
|
||||||
CkxpZmUgaXMgc2hvcnQuCkJ1dCB0aGUgeWVhcnMgYXJlIGxvbmcuCk5vdCB3aGlsZSB0aG
|
|
||||||
UgZXZpbCBkYXlzIGNvbWUgbm90Lgo=`, `9t25t44gg4`)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: These should be errors instead.
|
// TODO: These should be errors instead.
|
||||||
@@ -283,68 +159,6 @@ metadata:
|
|||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIssue3393(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- cm.yaml
|
|
||||||
configMapGenerator:
|
|
||||||
- name: project
|
|
||||||
behavior: merge
|
|
||||||
literals:
|
|
||||||
- ANOTHER_ENV_VARIABLE="bar"
|
|
||||||
`)
|
|
||||||
th.WriteF("cm.yaml", `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: project
|
|
||||||
data:
|
|
||||||
A_FIRST_ENV_VARIABLE: "foo"
|
|
||||||
`)
|
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
A_FIRST_ENV_VARIABLE: foo
|
|
||||||
ANOTHER_ENV_VARIABLE: bar
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: project
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGeneratorSimpleOverlay(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK("base", `
|
|
||||||
namePrefix: p-
|
|
||||||
configMapGenerator:
|
|
||||||
- name: cm
|
|
||||||
behavior: create
|
|
||||||
literals:
|
|
||||||
- fruit=apple
|
|
||||||
`)
|
|
||||||
th.WriteK("overlay", `
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
configMapGenerator:
|
|
||||||
- name: cm
|
|
||||||
behavior: merge
|
|
||||||
literals:
|
|
||||||
- veggie=broccoli
|
|
||||||
`)
|
|
||||||
m := th.Run("overlay", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
fruit: apple
|
|
||||||
veggie: broccoli
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: p-cm-877mt5hc89
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGeneratorOverlays(t *testing.T) {
|
func TestGeneratorOverlays(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
th.WriteK("/app/base1", `
|
th.WriteK("/app/base1", `
|
||||||
@@ -401,6 +215,8 @@ data:
|
|||||||
from: overlay
|
from: overlay
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
name: p1-com1-8tc62428t2
|
name: p1-com1-8tc62428t2
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
@@ -408,6 +224,8 @@ data:
|
|||||||
from: overlay
|
from: overlay
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
name: p2-com2-87mcggf7d7
|
name: p2-com2-87mcggf7d7
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
@@ -454,6 +272,8 @@ data:
|
|||||||
foo: bar
|
foo: bar
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
name: o1-cm-ft9mmdc8c6
|
name: o1-cm-ft9mmdc8c6
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
@@ -462,43 +282,8 @@ data:
|
|||||||
foo: bar
|
foo: bar
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
name: cm-o2-5k95kd76ft
|
name: cm-o2-5k95kd76ft
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfigMapGeneratorLiteralNewline(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK("/app", `
|
|
||||||
generators:
|
|
||||||
- configmaps.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("/app/configmaps.yaml", `
|
|
||||||
apiVersion: builtin
|
|
||||||
kind: ConfigMapGenerator
|
|
||||||
metadata:
|
|
||||||
name: testing
|
|
||||||
literals:
|
|
||||||
- |
|
|
||||||
initial.txt=greetings
|
|
||||||
everyone
|
|
||||||
- |
|
|
||||||
final.txt=different
|
|
||||||
behavior
|
|
||||||
---
|
|
||||||
`)
|
|
||||||
m := th.Run("/app", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(
|
|
||||||
m, `
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
final.txt: |
|
|
||||||
different
|
|
||||||
behavior
|
|
||||||
initial.txt: |
|
|
||||||
greetings
|
|
||||||
everyone
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: testing-tt4769fb52
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ spec:
|
|||||||
action: fly
|
action: fly
|
||||||
`)
|
`)
|
||||||
th.WriteF("/app/base/mykind.yaml", `
|
th.WriteF("/app/base/mykind.yaml", `
|
||||||
apiVersion: jingfang.example.com/v1
|
apiVersion: jingfang.example.com/v1beta1
|
||||||
kind: MyKind
|
kind: MyKind
|
||||||
metadata:
|
metadata:
|
||||||
name: mykind
|
name: mykind
|
||||||
@@ -236,7 +236,7 @@ kind: Secret
|
|||||||
metadata:
|
metadata:
|
||||||
name: x-crdsecret
|
name: x-crdsecret
|
||||||
---
|
---
|
||||||
apiVersion: jingfang.example.com/v1
|
apiVersion: jingfang.example.com/v1beta1
|
||||||
kind: MyKind
|
kind: MyKind
|
||||||
metadata:
|
metadata:
|
||||||
name: x-mykind
|
name: x-mykind
|
||||||
@@ -285,7 +285,7 @@ kind: Secret
|
|||||||
metadata:
|
metadata:
|
||||||
name: prod-x-crdsecret
|
name: prod-x-crdsecret
|
||||||
---
|
---
|
||||||
apiVersion: jingfang.example.com/v1
|
apiVersion: jingfang.example.com/v1beta1
|
||||||
kind: MyKind
|
kind: MyKind
|
||||||
metadata:
|
metadata:
|
||||||
name: prod-x-mykind
|
name: prod-x-mykind
|
||||||
|
|||||||
@@ -1,622 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package krusty_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Regression test for https://github.com/kubernetes-sigs/kustomize/issues/3280
|
|
||||||
// GVKN shouldn't change with default options
|
|
||||||
func TestKeepOriginalGVKN(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteF("apps/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteF("apps/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("apps", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
`)
|
|
||||||
|
|
||||||
m := th.Run("apps", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: nginx
|
|
||||||
name: nginx
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://github.com/kubernetes-sigs/kustomize/issues/3280
|
|
||||||
// These tests document behavior that will change
|
|
||||||
func TestChangeName(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteF("apps/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteF("apps/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("apps", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
`)
|
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// name should become `new-name`
|
|
||||||
m := th.Run("apps", options)
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: nginx
|
|
||||||
name: nginx
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestChangeKind(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteF("apps/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteF("apps/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("apps", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
`)
|
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// kind should become `StatefulSet`
|
|
||||||
m := th.Run("apps", options)
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: nginx
|
|
||||||
name: nginx
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestChangeNameAndKind(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteF("apps/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteF("apps/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("apps", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
`)
|
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// kind should become `StatefulSet`
|
|
||||||
// name should become `new-name`
|
|
||||||
m := th.Run("apps", options)
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: nginx
|
|
||||||
name: nginx
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://github.com/kubernetes-sigs/kustomize/issues/3280
|
|
||||||
// Should be able to refer to a resource with either its
|
|
||||||
// original GVKN or its current one
|
|
||||||
func TestPatchOriginalName(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`)
|
|
||||||
th.WriteF("base/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
`)
|
|
||||||
th.WriteK("base", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
patchesStrategicMerge:
|
|
||||||
- depPatch.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("overlay/depPatch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
replicas: 999
|
|
||||||
`)
|
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// name should become `new-name`
|
|
||||||
m := th.Run("overlay", options)
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
replicas: 999
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: nginx
|
|
||||||
name: nginx
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPatchNewName(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`)
|
|
||||||
th.WriteF("base/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
`)
|
|
||||||
th.WriteK("base", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
patchesStrategicMerge:
|
|
||||||
- depPatch.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("overlay/depPatch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
spec:
|
|
||||||
replicas: 999
|
|
||||||
`)
|
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// depPatch cannot find target with the name `new-name`
|
|
||||||
// because base/patch.yaml can't yet edit the name
|
|
||||||
assert.Error(t, th.RunWithErr("overlay", options))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPatchOriginalNameAndKind(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`)
|
|
||||||
th.WriteF("base/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
`)
|
|
||||||
th.WriteK("base", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
patchesStrategicMerge:
|
|
||||||
- depPatch.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("overlay/depPatch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
replicas: 999
|
|
||||||
`)
|
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// kind should become `StatefulSet`
|
|
||||||
// name should become `new-name`
|
|
||||||
m := th.Run("overlay", options)
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
replicas: 999
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: nginx
|
|
||||||
name: nginx
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPatchNewNameAndKind(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`)
|
|
||||||
th.WriteF("base/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
`)
|
|
||||||
th.WriteK("base", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
patchesStrategicMerge:
|
|
||||||
- depPatch.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("overlay/depPatch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
spec:
|
|
||||||
replicas: 999
|
|
||||||
`)
|
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// depPatch cannot find target with kind `StatefulSet` and name `new-name`
|
|
||||||
// because base/patch.yaml can't yet edit the kind or name
|
|
||||||
assert.Error(t, th.RunWithErr("overlay", options))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use original name, but new kind
|
|
||||||
// Should fail, even after #3280 is done, because this ID is invalid
|
|
||||||
func TestPatchOriginalNameAndNewKind(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: old-name
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`)
|
|
||||||
th.WriteF("base/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
`)
|
|
||||||
th.WriteK("base", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
patchesStrategicMerge:
|
|
||||||
- depPatch.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("overlay/depPatch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: new-name
|
|
||||||
spec:
|
|
||||||
replicas: 999
|
|
||||||
`)
|
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// depPatch cannot find target with kind `Deployment` and name `new-name`
|
|
||||||
// because the resource never had this GVKN
|
|
||||||
assert.Error(t, th.RunWithErr("overlay", options))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Here is a structure of a kustomization of two components, component1
|
|
||||||
// and component2, that both use a shared deployment definition, which
|
|
||||||
// component2 adjusts by changing the kind via a patch. This test case
|
|
||||||
// checks that it does not have a conflicting definition.
|
|
||||||
// Currently documents broken behavior.
|
|
||||||
//
|
|
||||||
// root
|
|
||||||
// / \
|
|
||||||
// component1/overlay component2/overlay
|
|
||||||
// | |
|
|
||||||
// component1/base component2/base
|
|
||||||
// \ /
|
|
||||||
// base
|
|
||||||
//
|
|
||||||
// This is the directory layout:
|
|
||||||
//
|
|
||||||
// ├── component1
|
|
||||||
// │ ├── base
|
|
||||||
// │ │ └── kustomization.yaml
|
|
||||||
// │ └── overlay
|
|
||||||
// │ └── kustomization.yaml
|
|
||||||
// ├── component2
|
|
||||||
// │ ├── base
|
|
||||||
// │ │ └── kustomization.yaml
|
|
||||||
// │ └── overlay
|
|
||||||
// │ └── kustomization.yaml
|
|
||||||
// ├── shared
|
|
||||||
// │ ├── kustomization.yaml
|
|
||||||
// │ └── deployment.yaml
|
|
||||||
// ├── kustomization.yaml
|
|
||||||
|
|
||||||
func TestBaseReuseNameAndKindConflict(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteK("/app/shared", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("/app/shared/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: my-deploy
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("/app/component1/base", `
|
|
||||||
resources:
|
|
||||||
- ../../shared
|
|
||||||
`)
|
|
||||||
th.WriteK("/app/component1/overlay", `
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
|
|
||||||
namePrefix: overlay-
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("/app/component2/base", `
|
|
||||||
resources:
|
|
||||||
- ../../shared
|
|
||||||
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
name: my-deploy
|
|
||||||
`)
|
|
||||||
th.WriteF("/app/component2/base/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: my-stateful-set
|
|
||||||
`)
|
|
||||||
th.WriteK("/app/component2/overlay", `
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
|
|
||||||
namePrefix: overlay-
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("/app", `
|
|
||||||
resources:
|
|
||||||
- component1/overlay
|
|
||||||
- component2/overlay
|
|
||||||
`)
|
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// Error occurs when app/component2/base tries to load the shared resources
|
|
||||||
// because the kind is not (yet) allowed to change yet
|
|
||||||
// so it loads a second Deployment with the name my-deploy
|
|
||||||
// instead of a StatefulSet as specified by the patch.
|
|
||||||
// Will be fixed by #3280.
|
|
||||||
assert.Error(t, th.RunWithErr("overlay", options))
|
|
||||||
}
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package krusty_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestInlineTransformer(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeEnhancedHarness(t)
|
|
||||||
defer th.Reset()
|
|
||||||
|
|
||||||
th.WriteF("/app/resource.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: whatever
|
|
||||||
data: {}
|
|
||||||
`)
|
|
||||||
th.WriteK("/app", `
|
|
||||||
resources:
|
|
||||||
- resource.yaml
|
|
||||||
transformers:
|
|
||||||
- |-
|
|
||||||
apiVersion: builtin
|
|
||||||
kind: NamespaceTransformer
|
|
||||||
metadata:
|
|
||||||
name: not-important-to-example
|
|
||||||
namespace: test
|
|
||||||
fieldSpecs:
|
|
||||||
- path: metadata/namespace
|
|
||||||
create: true
|
|
||||||
`)
|
|
||||||
|
|
||||||
expected := `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
data: {}
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: whatever
|
|
||||||
namespace: test
|
|
||||||
`
|
|
||||||
|
|
||||||
m := th.Run("/app", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, expected)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInlineGenerator(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeEnhancedHarness(t)
|
|
||||||
defer th.Reset()
|
|
||||||
|
|
||||||
th.WriteK("/app", `
|
|
||||||
generators:
|
|
||||||
- |-
|
|
||||||
apiVersion: builtin
|
|
||||||
kind: ConfigMapGenerator
|
|
||||||
metadata:
|
|
||||||
name: mymap
|
|
||||||
literals:
|
|
||||||
- FRUIT=apple
|
|
||||||
- VEGETABLE=carrot
|
|
||||||
`)
|
|
||||||
|
|
||||||
expected := `
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
FRUIT: apple
|
|
||||||
VEGETABLE: carrot
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: mymap-kfd8tf729k
|
|
||||||
`
|
|
||||||
|
|
||||||
m := th.Run("/app", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, expected)
|
|
||||||
}
|
|
||||||
@@ -5,13 +5,13 @@ package provider
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/internal/conflict"
|
"sigs.k8s.io/kustomize/api/internal/k8sdeps/merge"
|
||||||
k8sconflict "sigs.k8s.io/kustomize/api/internal/k8sdeps/conflict"
|
kmerge "sigs.k8s.io/kustomize/api/internal/merge"
|
||||||
"sigs.k8s.io/kustomize/api/internal/validate"
|
"sigs.k8s.io/kustomize/api/internal/validate"
|
||||||
"sigs.k8s.io/kustomize/api/internal/wrappy"
|
"sigs.k8s.io/kustomize/api/internal/wrappy"
|
||||||
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||||
"sigs.k8s.io/kustomize/api/k8sdeps/validator"
|
"sigs.k8s.io/kustomize/api/k8sdeps/validator"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -95,27 +95,17 @@ import (
|
|||||||
// would really reduce the work
|
// would really reduce the work
|
||||||
// (e.g. drop Vars, drop ReplacementTranformer).
|
// (e.g. drop Vars, drop ReplacementTranformer).
|
||||||
//
|
//
|
||||||
// - resource.ConflictDetector
|
// - resmap.Merginator
|
||||||
//
|
//
|
||||||
// 1) api/internal/k8sdeps/conflict.conflictDetectorJson
|
// 1) api/internal/k8sdeps/merge.Merginator
|
||||||
// api/internal/k8sdeps/conflict.conflictDetectorSm
|
|
||||||
//
|
//
|
||||||
// Uses k8s.io/apimachinery/pkg/util/strategicpatch,
|
// Uses k8s.io/apimachinery/pkg/util/strategicpatch,
|
||||||
// apimachinery/pkg/util/mergepatch, etc. to merge
|
// apimachinery/pkg/util/mergepatch, etc. to merge
|
||||||
// resource.Resource instances.
|
// resource.Resource instances.
|
||||||
//
|
//
|
||||||
// 2) api/internal/conflict.smPatchMergeOnlyDetector
|
// 2) api/internal/merge.Merginator
|
||||||
//
|
//
|
||||||
// At time of writing, this doesn't report conflicts,
|
// At time of writing, this is unimplemented.
|
||||||
// but it does know how to merge patches. Conflict
|
|
||||||
// reporting isn't vital to kustomize function. It's
|
|
||||||
// rare that a person would configure one transformer
|
|
||||||
// with many patches, much less so many that it became
|
|
||||||
// hard to spot conflicts. In the case of an undetected
|
|
||||||
// conflict, the last patch applied wins, likely what
|
|
||||||
// the user wants anyway. Regardless, the effect of this
|
|
||||||
// is plainly visible and usable in the output, even if
|
|
||||||
// a conflict happened but wasn't reported as an error.
|
|
||||||
//
|
//
|
||||||
// - ifc.Validator
|
// - ifc.Validator
|
||||||
//
|
//
|
||||||
@@ -126,7 +116,6 @@ import (
|
|||||||
//
|
//
|
||||||
// 2) api/internal/validate.FieldValidator
|
// 2) api/internal/validate.FieldValidator
|
||||||
//
|
//
|
||||||
// See TODO inside the validator for status.
|
|
||||||
// At time of writing, this is a do-nothing
|
// At time of writing, this is a do-nothing
|
||||||
// validator as it's not critical to kustomize function.
|
// validator as it's not critical to kustomize function.
|
||||||
//
|
//
|
||||||
@@ -149,20 +138,18 @@ import (
|
|||||||
// If you're reading this, plan not done.
|
// If you're reading this, plan not done.
|
||||||
//
|
//
|
||||||
type DepProvider struct {
|
type DepProvider struct {
|
||||||
kFactory ifc.KunstructuredFactory
|
resourceFactory *resource.Factory
|
||||||
resourceFactory *resource.Factory
|
merginator resmap.Merginator
|
||||||
conflictDectectorFactory resource.ConflictDetectorFactory
|
fieldValidator ifc.Validator
|
||||||
fieldValidator ifc.Validator
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeK8sdepBasedInstances() *DepProvider {
|
func makeK8sdepBasedInstances() *DepProvider {
|
||||||
kf := kunstruct.NewKunstructuredFactoryImpl()
|
kf := kunstruct.NewKunstructuredFactoryImpl()
|
||||||
rf := resource.NewFactory(kf)
|
rf := resource.NewFactory(kf)
|
||||||
return &DepProvider{
|
return &DepProvider{
|
||||||
kFactory: kf,
|
resourceFactory: rf,
|
||||||
resourceFactory: rf,
|
merginator: merge.NewMerginator(rf),
|
||||||
conflictDectectorFactory: k8sconflict.NewFactory(rf),
|
fieldValidator: validator.NewKustValidator(),
|
||||||
fieldValidator: validator.NewKustValidator(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,10 +157,9 @@ func makeKyamlBasedInstances() *DepProvider {
|
|||||||
kf := &wrappy.WNodeFactory{}
|
kf := &wrappy.WNodeFactory{}
|
||||||
rf := resource.NewFactory(kf)
|
rf := resource.NewFactory(kf)
|
||||||
return &DepProvider{
|
return &DepProvider{
|
||||||
kFactory: kf,
|
resourceFactory: rf,
|
||||||
resourceFactory: rf,
|
merginator: kmerge.NewMerginator(rf),
|
||||||
conflictDectectorFactory: conflict.NewFactory(),
|
fieldValidator: validate.NewFieldValidator(),
|
||||||
fieldValidator: validate.NewFieldValidator(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,20 +170,12 @@ func NewDepProvider(useKyaml bool) *DepProvider {
|
|||||||
return makeK8sdepBasedInstances()
|
return makeK8sdepBasedInstances()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultDepProvider() *DepProvider {
|
|
||||||
return NewDepProvider(konfig.FlagEnableKyamlDefaultValue)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dp *DepProvider) GetKunstructuredFactory() ifc.KunstructuredFactory {
|
|
||||||
return dp.kFactory
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dp *DepProvider) GetResourceFactory() *resource.Factory {
|
func (dp *DepProvider) GetResourceFactory() *resource.Factory {
|
||||||
return dp.resourceFactory
|
return dp.resourceFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dp *DepProvider) GetConflictDetectorFactory() resource.ConflictDetectorFactory {
|
func (dp *DepProvider) GetMerginator() resmap.Merginator {
|
||||||
return dp.conflictDectectorFactory
|
return dp.merginator
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dp *DepProvider) GetFieldValidator() ifc.Validator {
|
func (dp *DepProvider) GetFieldValidator() ifc.Validator {
|
||||||
@@ -11,9 +11,9 @@ import (
|
|||||||
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||||
"sigs.k8s.io/kustomize/api/internal/target"
|
"sigs.k8s.io/kustomize/api/internal/target"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
|
"sigs.k8s.io/kustomize/api/krusty/internal/provider"
|
||||||
fLdr "sigs.k8s.io/kustomize/api/loader"
|
fLdr "sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/api/provenance"
|
"sigs.k8s.io/kustomize/api/provenance"
|
||||||
"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"
|
||||||
)
|
)
|
||||||
@@ -53,7 +53,7 @@ func MakeKustomizer(fSys filesys.FileSystem, o *Options) *Kustomizer {
|
|||||||
func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
|
func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
|
||||||
resmapFactory := resmap.NewFactory(
|
resmapFactory := resmap.NewFactory(
|
||||||
b.depProvider.GetResourceFactory(),
|
b.depProvider.GetResourceFactory(),
|
||||||
b.depProvider.GetConflictDetectorFactory())
|
b.depProvider.GetMerginator())
|
||||||
lr := fLdr.RestrictionNone
|
lr := fLdr.RestrictionNone
|
||||||
if b.options.LoadRestrictions == types.LoadRestrictionsRootOnly {
|
if b.options.LoadRestrictions == types.LoadRestrictionsRootOnly {
|
||||||
lr = fLdr.RestrictionRootOnly
|
lr = fLdr.RestrictionRootOnly
|
||||||
@@ -85,7 +85,7 @@ func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
|
|||||||
t := builtins.LabelTransformerPlugin{
|
t := builtins.LabelTransformerPlugin{
|
||||||
Labels: map[string]string{
|
Labels: map[string]string{
|
||||||
konfig.ManagedbyLabelKey: fmt.Sprintf(
|
konfig.ManagedbyLabelKey: fmt.Sprintf(
|
||||||
"kustomize-%s", provenance.GetProvenance().Semver())},
|
"kustomize-%s", provenance.GetProvenance().Version)},
|
||||||
FieldSpecs: []types.FieldSpec{{
|
FieldSpecs: []types.FieldSpec{{
|
||||||
Path: "metadata/labels",
|
Path: "metadata/labels",
|
||||||
CreateIfNotPresent: true,
|
CreateIfNotPresent: true,
|
||||||
@@ -93,6 +93,5 @@ func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
|
|||||||
}
|
}
|
||||||
t.Transform(m)
|
t.Transform(m)
|
||||||
}
|
}
|
||||||
m.RemoveBuildAnnotations()
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,334 +4,12 @@
|
|||||||
package krusty_test
|
package krusty_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRemoveEmptyDirWithNullFieldInSmp(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
patchesStrategicMerge:
|
|
||||||
- patch.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
volumes:
|
|
||||||
- name: fancyDisk
|
|
||||||
emptyDir: {}
|
|
||||||
`)
|
|
||||||
th.WriteF("patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
volumes:
|
|
||||||
- name: fancyDisk
|
|
||||||
emptyDir: null
|
|
||||||
`)
|
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
volumes:
|
|
||||||
- name: fancyDisk
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRemoveEmptyDirAddPersistentDisk(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
patchesStrategicMerge:
|
|
||||||
- patch.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
volumes:
|
|
||||||
- name: fancyDisk
|
|
||||||
emptyDir: {}
|
|
||||||
`)
|
|
||||||
th.WriteF("patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
volumes:
|
|
||||||
- name: fancyDisk
|
|
||||||
emptyDir: null
|
|
||||||
gcePersistentDisk:
|
|
||||||
pdName: fancyDisk
|
|
||||||
`)
|
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
volumes:
|
|
||||||
- gcePersistentDisk:
|
|
||||||
pdName: fancyDisk
|
|
||||||
name: fancyDisk
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestVolumeRemoveEmptyDirInOverlay(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK("base", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
configMapGenerator:
|
|
||||||
- name: baseCm
|
|
||||||
literals:
|
|
||||||
- foo=bar
|
|
||||||
`)
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
volumeMounts:
|
|
||||||
- name: fancyDisk
|
|
||||||
mountPath: /tmp/ps
|
|
||||||
volumes:
|
|
||||||
- name: fancyDisk
|
|
||||||
emptyDir: {}
|
|
||||||
- configMap:
|
|
||||||
name: baseCm
|
|
||||||
name: baseCm
|
|
||||||
`)
|
|
||||||
m := th.Run("base", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: nginx
|
|
||||||
name: nginx
|
|
||||||
volumeMounts:
|
|
||||||
- mountPath: /tmp/ps
|
|
||||||
name: fancyDisk
|
|
||||||
volumes:
|
|
||||||
- emptyDir: {}
|
|
||||||
name: fancyDisk
|
|
||||||
- configMap:
|
|
||||||
name: baseCm-798k5k7g9f
|
|
||||||
name: baseCm
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
foo: bar
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: baseCm-798k5k7g9f
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
|
||||||
patchesStrategicMerge:
|
|
||||||
- patch.yaml
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
configMapGenerator:
|
|
||||||
- name: overlayCm
|
|
||||||
literals:
|
|
||||||
- hello=world
|
|
||||||
`)
|
|
||||||
th.WriteF("overlay/patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
volumes:
|
|
||||||
- name: fancyDisk
|
|
||||||
emptyDir: null
|
|
||||||
gcePersistentDisk:
|
|
||||||
pdName: fancyDisk
|
|
||||||
- configMap:
|
|
||||||
name: overlayCm
|
|
||||||
name: overlayCm
|
|
||||||
`)
|
|
||||||
m = th.Run("overlay", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: nginx
|
|
||||||
name: nginx
|
|
||||||
volumeMounts:
|
|
||||||
- mountPath: /tmp/ps
|
|
||||||
name: fancyDisk
|
|
||||||
volumes:
|
|
||||||
- gcePersistentDisk:
|
|
||||||
pdName: fancyDisk
|
|
||||||
name: fancyDisk
|
|
||||||
- configMap:
|
|
||||||
name: overlayCm-dc6fm46dhm
|
|
||||||
name: overlayCm
|
|
||||||
- configMap:
|
|
||||||
name: baseCm-798k5k7g9f
|
|
||||||
name: baseCm
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
foo: bar
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: baseCm-798k5k7g9f
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
hello: world
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: overlayCm-dc6fm46dhm
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRemoveEmptyDirWithPatchesAtSameLevel(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK("base", `
|
|
||||||
resources:
|
|
||||||
- deployment.yaml
|
|
||||||
`)
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
- name: sidecar
|
|
||||||
image: sidecar:latest
|
|
||||||
volumes:
|
|
||||||
- name: nginx-persistent-storage
|
|
||||||
emptyDir: {}
|
|
||||||
`)
|
|
||||||
th.WriteK("overlay", `
|
|
||||||
patchesStrategicMerge:
|
|
||||||
- deployment-patch1.yaml
|
|
||||||
- deployment-patch2.yaml
|
|
||||||
resources:
|
|
||||||
- ../base
|
|
||||||
`)
|
|
||||||
th.WriteF("overlay/deployment-patch1.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
volumes:
|
|
||||||
- name: nginx-persistent-storage
|
|
||||||
emptyDir: null
|
|
||||||
gcePersistentDisk:
|
|
||||||
pdName: nginx-persistent-storage
|
|
||||||
`)
|
|
||||||
th.WriteF("overlay/deployment-patch2.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: nginx
|
|
||||||
name: nginx
|
|
||||||
env:
|
|
||||||
- name: ANOTHERENV
|
|
||||||
value: FOO
|
|
||||||
volumes:
|
|
||||||
- name: nginx-persistent-storage
|
|
||||||
`)
|
|
||||||
opts := th.MakeDefaultOptions()
|
|
||||||
m := th.Run("overlay", opts)
|
|
||||||
expFmt := `apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginx
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- env:
|
|
||||||
- name: ANOTHERENV
|
|
||||||
value: FOO
|
|
||||||
image: nginx
|
|
||||||
name: nginx
|
|
||||||
- image: sidecar:latest
|
|
||||||
name: sidecar
|
|
||||||
volumes:%s
|
|
||||||
name: nginx-persistent-storage
|
|
||||||
`
|
|
||||||
th.AssertActualEqualsExpected(
|
|
||||||
// TODO(#3394): Should be possible to delete emptyDir with a patch.
|
|
||||||
// TODO(#3304): DECISION - still a bug, emptyDir should be deleted.
|
|
||||||
m, opts.IfApiMachineryElseKyaml(
|
|
||||||
fmt.Sprintf(expFmt, `
|
|
||||||
- gcePersistentDisk:
|
|
||||||
pdName: nginx-persistent-storage`),
|
|
||||||
fmt.Sprintf(expFmt, `
|
|
||||||
- emptyDir: {}
|
|
||||||
gcePersistentDisk:
|
|
||||||
pdName: nginx-persistent-storage`),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSimpleMultiplePatches(t *testing.T) {
|
func TestSimpleMultiplePatches(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
th.WriteK("base", `
|
th.WriteK("base", `
|
||||||
@@ -363,6 +41,8 @@ spec:
|
|||||||
- name: sidecar
|
- name: sidecar
|
||||||
image: sidecar:latest
|
image: sidecar:latest
|
||||||
volumes:
|
volumes:
|
||||||
|
- name: nginx-persistent-storage
|
||||||
|
emptyDir: {}
|
||||||
- configMap:
|
- configMap:
|
||||||
name: configmap-in-base
|
name: configmap-in-base
|
||||||
name: configmap-in-base
|
name: configmap-in-base
|
||||||
@@ -406,6 +86,7 @@ spec:
|
|||||||
value: ENVVALUE
|
value: ENVVALUE
|
||||||
volumes:
|
volumes:
|
||||||
- name: nginx-persistent-storage
|
- name: nginx-persistent-storage
|
||||||
|
emptyDir: null
|
||||||
gcePersistentDisk:
|
gcePersistentDisk:
|
||||||
pdName: nginx-persistent-storage
|
pdName: nginx-persistent-storage
|
||||||
- configMap:
|
- configMap:
|
||||||
@@ -425,6 +106,8 @@ spec:
|
|||||||
env:
|
env:
|
||||||
- name: ANOTHERENV
|
- name: ANOTHERENV
|
||||||
value: FOO
|
value: FOO
|
||||||
|
volumes:
|
||||||
|
- name: nginx-persistent-storage
|
||||||
`)
|
`)
|
||||||
m := th.Run("overlay", th.MakeDefaultOptions())
|
m := th.Run("overlay", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
@@ -505,7 +188,7 @@ metadata:
|
|||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeCommonFilesForMultiplePatchTests(th kusttest_test.Harness) {
|
func makeCommonFileForMultiplePatchTest(th kusttest_test.Harness) {
|
||||||
th.WriteK("/app/base", `
|
th.WriteK("/app/base", `
|
||||||
namePrefix: team-foo-
|
namePrefix: team-foo-
|
||||||
commonLabels:
|
commonLabels:
|
||||||
@@ -544,6 +227,8 @@ spec:
|
|||||||
- name: sidecar
|
- name: sidecar
|
||||||
image: sidecar:latest
|
image: sidecar:latest
|
||||||
volumes:
|
volumes:
|
||||||
|
- name: nginx-persistent-storage
|
||||||
|
emptyDir: {}
|
||||||
- configMap:
|
- configMap:
|
||||||
name: configmap-in-base
|
name: configmap-in-base
|
||||||
name: configmap-in-base
|
name: configmap-in-base
|
||||||
@@ -579,7 +264,7 @@ configMapGenerator:
|
|||||||
|
|
||||||
func TestMultiplePatchesNoConflict(t *testing.T) {
|
func TestMultiplePatchesNoConflict(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
makeCommonFilesForMultiplePatchTests(th)
|
makeCommonFileForMultiplePatchTest(th)
|
||||||
th.WriteF("/app/overlay/staging/deployment-patch1.yaml", `
|
th.WriteF("/app/overlay/staging/deployment-patch1.yaml", `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -596,6 +281,7 @@ spec:
|
|||||||
value: ENVVALUE
|
value: ENVVALUE
|
||||||
volumes:
|
volumes:
|
||||||
- name: nginx-persistent-storage
|
- name: nginx-persistent-storage
|
||||||
|
emptyDir: null
|
||||||
gcePersistentDisk:
|
gcePersistentDisk:
|
||||||
pdName: nginx-persistent-storage
|
pdName: nginx-persistent-storage
|
||||||
- configMap:
|
- configMap:
|
||||||
@@ -615,6 +301,8 @@ spec:
|
|||||||
env:
|
env:
|
||||||
- name: ANOTHERENV
|
- name: ANOTHERENV
|
||||||
value: FOO
|
value: FOO
|
||||||
|
volumes:
|
||||||
|
- name: nginx-persistent-storage
|
||||||
`)
|
`)
|
||||||
m := th.Run("/app/overlay/staging", th.MakeDefaultOptions())
|
m := th.Run("/app/overlay/staging", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
@@ -717,7 +405,7 @@ metadata:
|
|||||||
|
|
||||||
func TestMultiplePatchesWithConflict(t *testing.T) {
|
func TestMultiplePatchesWithConflict(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
makeCommonFilesForMultiplePatchTests(th)
|
makeCommonFileForMultiplePatchTest(th)
|
||||||
th.WriteF("/app/overlay/staging/deployment-patch1.yaml", `
|
th.WriteF("/app/overlay/staging/deployment-patch1.yaml", `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -733,6 +421,7 @@ spec:
|
|||||||
value: TRUE
|
value: TRUE
|
||||||
volumes:
|
volumes:
|
||||||
- name: nginx-persistent-storage
|
- name: nginx-persistent-storage
|
||||||
|
emptyDir: null
|
||||||
gcePersistentDisk:
|
gcePersistentDisk:
|
||||||
pdName: nginx-persistent-storage
|
pdName: nginx-persistent-storage
|
||||||
- configMap:
|
- configMap:
|
||||||
@@ -753,114 +442,13 @@ spec:
|
|||||||
- name: ENABLE_FEATURE_FOO
|
- name: ENABLE_FEATURE_FOO
|
||||||
value: FALSE
|
value: FALSE
|
||||||
`)
|
`)
|
||||||
opts := th.MakeDefaultOptions()
|
err := th.RunWithErr("/app/overlay/staging", th.MakeDefaultOptions())
|
||||||
if opts.UseKyaml {
|
if err == nil {
|
||||||
// kyaml doesn't try to detect conflicts in patches
|
t.Fatalf("expected conflict")
|
||||||
// (so ENABLE_FEATURE_FOO FALSE wins).
|
}
|
||||||
m := th.Run("/app/overlay/staging", opts)
|
if !strings.Contains(
|
||||||
th.AssertActualEqualsExpected(m, `
|
err.Error(), "conflict between ") {
|
||||||
apiVersion: apps/v1
|
t.Fatalf("Unexpected err: %v", err)
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
note: This is a test annotation
|
|
||||||
labels:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
name: staging-team-foo-nginx
|
|
||||||
spec:
|
|
||||||
selector:
|
|
||||||
matchLabels:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
template:
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
note: This is a test annotation
|
|
||||||
labels:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- env:
|
|
||||||
- name: ENABLE_FEATURE_FOO
|
|
||||||
value: false
|
|
||||||
image: nginx
|
|
||||||
name: nginx
|
|
||||||
volumeMounts:
|
|
||||||
- mountPath: /tmp/ps
|
|
||||||
name: nginx-persistent-storage
|
|
||||||
- image: sidecar:latest
|
|
||||||
name: sidecar
|
|
||||||
volumes:
|
|
||||||
- gcePersistentDisk:
|
|
||||||
pdName: nginx-persistent-storage
|
|
||||||
name: nginx-persistent-storage
|
|
||||||
- configMap:
|
|
||||||
name: staging-configmap-in-overlay-dc6fm46dhm
|
|
||||||
name: configmap-in-overlay
|
|
||||||
- configMap:
|
|
||||||
name: staging-team-foo-configmap-in-base-798k5k7g9f
|
|
||||||
name: configmap-in-base
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
note: This is a test annotation
|
|
||||||
labels:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
name: staging-team-foo-nginx
|
|
||||||
spec:
|
|
||||||
ports:
|
|
||||||
- port: 80
|
|
||||||
selector:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
foo: bar
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
note: This is a test annotation
|
|
||||||
labels:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
name: staging-team-foo-configmap-in-base-798k5k7g9f
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
hello: world
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
env: staging
|
|
||||||
name: staging-configmap-in-overlay-dc6fm46dhm
|
|
||||||
`)
|
|
||||||
} else {
|
|
||||||
err := th.RunWithErr("/app/overlay/staging", opts)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("expected conflict")
|
|
||||||
}
|
|
||||||
if !strings.Contains(
|
|
||||||
err.Error(), "conflict between ") {
|
|
||||||
t.Fatalf("Unexpected err: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -909,7 +497,8 @@ spec:
|
|||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
t.Run(c.name, func(t *testing.T) {
|
t.Run(c.name, func(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
makeCommonFilesForMultiplePatchTests(th)
|
|
||||||
|
makeCommonFileForMultiplePatchTest(th)
|
||||||
th.WriteF("/app/overlay/staging/deployment-patch1.yaml", c.patch1)
|
th.WriteF("/app/overlay/staging/deployment-patch1.yaml", c.patch1)
|
||||||
th.WriteF("/app/overlay/staging/deployment-patch2.yaml", c.patch2)
|
th.WriteF("/app/overlay/staging/deployment-patch2.yaml", c.patch2)
|
||||||
m := th.Run("/app/overlay/staging", th.MakeDefaultOptions())
|
m := th.Run("/app/overlay/staging", th.MakeDefaultOptions())
|
||||||
@@ -951,6 +540,8 @@ spec:
|
|||||||
- mountPath: /tmp/ps
|
- mountPath: /tmp/ps
|
||||||
name: nginx-persistent-storage
|
name: nginx-persistent-storage
|
||||||
volumes:
|
volumes:
|
||||||
|
- emptyDir: {}
|
||||||
|
name: nginx-persistent-storage
|
||||||
- configMap:
|
- configMap:
|
||||||
name: staging-team-foo-configmap-in-base-798k5k7g9f
|
name: staging-team-foo-configmap-in-base-798k5k7g9f
|
||||||
name: configmap-in-base
|
name: configmap-in-base
|
||||||
@@ -1004,7 +595,7 @@ metadata:
|
|||||||
|
|
||||||
func TestMultiplePatchesBothWithPatchDeleteDirective(t *testing.T) {
|
func TestMultiplePatchesBothWithPatchDeleteDirective(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
makeCommonFilesForMultiplePatchTests(th)
|
makeCommonFileForMultiplePatchTest(th)
|
||||||
th.WriteF("/app/overlay/staging/deployment-patch1.yaml", `
|
th.WriteF("/app/overlay/staging/deployment-patch1.yaml", `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -1029,98 +620,12 @@ spec:
|
|||||||
- $patch: delete
|
- $patch: delete
|
||||||
name: nginx
|
name: nginx
|
||||||
`)
|
`)
|
||||||
opt := th.MakeDefaultOptions()
|
err := th.RunWithErr("/app/overlay/staging", th.MakeDefaultOptions())
|
||||||
if opt.UseKyaml {
|
if err == nil {
|
||||||
// kyaml doesn't fail on conflicts in patches; both containers
|
t.Fatalf("Expected error")
|
||||||
// (nginx and sidecar) are deleted per this patching instruction.
|
}
|
||||||
m := th.Run("/app/overlay/staging", opt)
|
if !strings.Contains(
|
||||||
th.AssertActualEqualsExpected(m, `
|
err.Error(), "both containing ") {
|
||||||
apiVersion: apps/v1
|
t.Fatalf("Unexpected err: %v", err)
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
note: This is a test annotation
|
|
||||||
labels:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
name: staging-team-foo-nginx
|
|
||||||
spec:
|
|
||||||
selector:
|
|
||||||
matchLabels:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
template:
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
note: This is a test annotation
|
|
||||||
labels:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
spec:
|
|
||||||
containers: []
|
|
||||||
volumes:
|
|
||||||
- configMap:
|
|
||||||
name: staging-team-foo-configmap-in-base-798k5k7g9f
|
|
||||||
name: configmap-in-base
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
note: This is a test annotation
|
|
||||||
labels:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
name: staging-team-foo-nginx
|
|
||||||
spec:
|
|
||||||
ports:
|
|
||||||
- port: 80
|
|
||||||
selector:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
foo: bar
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
annotations:
|
|
||||||
note: This is a test annotation
|
|
||||||
labels:
|
|
||||||
app: mynginx
|
|
||||||
env: staging
|
|
||||||
org: example.com
|
|
||||||
team: foo
|
|
||||||
name: staging-team-foo-configmap-in-base-798k5k7g9f
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
hello: world
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
env: staging
|
|
||||||
name: staging-configmap-in-overlay-dc6fm46dhm
|
|
||||||
`)
|
|
||||||
} else {
|
|
||||||
// No kyaml means error on a patch conflict.
|
|
||||||
err := th.RunWithErr("/app/overlay/staging", opt)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("Expected error")
|
|
||||||
}
|
|
||||||
if !strings.Contains(
|
|
||||||
err.Error(), "both containing ") {
|
|
||||||
t.Fatalf("Unexpected err: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ configMapGenerator:
|
|||||||
literals:
|
literals:
|
||||||
- MYSQL_USER=default
|
- MYSQL_USER=default
|
||||||
- MYSQL_DATABASE=default
|
- MYSQL_DATABASE=default
|
||||||
- HOST=everest
|
- PORT=3306
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK(".", `
|
th.WriteK(".", `
|
||||||
@@ -82,13 +82,15 @@ patches:
|
|||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
HOST: everest
|
|
||||||
MYSQL_DATABASE: db
|
MYSQL_DATABASE: db
|
||||||
MYSQL_PASSWORD: correct horse battery staple
|
MYSQL_PASSWORD: correct horse battery staple
|
||||||
MYSQL_USER: my-user
|
MYSQL_USER: my-user
|
||||||
|
PORT: "3306"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: mysql-t7tt4cdbmf
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
|
name: mysql-9792mdchtg
|
||||||
---
|
---
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -102,10 +104,10 @@ spec:
|
|||||||
- valueFrom:
|
- valueFrom:
|
||||||
configMapKeyRef:
|
configMapKeyRef:
|
||||||
key: MYSQL_DATABASE
|
key: MYSQL_DATABASE
|
||||||
name: mysql-t7tt4cdbmf
|
name: mysql-9792mdchtg
|
||||||
envFrom:
|
envFrom:
|
||||||
- configMapRef:
|
- configMapRef:
|
||||||
name: mysql-t7tt4cdbmf
|
name: mysql-9792mdchtg
|
||||||
name: handler
|
name: handler
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ namespace: base
|
|||||||
configMapGenerator:
|
configMapGenerator:
|
||||||
- name: testCase
|
- name: testCase
|
||||||
literals:
|
literals:
|
||||||
- base=apple
|
- base=true
|
||||||
`)
|
`)
|
||||||
th.WriteK("/app/overlay", `
|
th.WriteK("/app/overlay", `
|
||||||
resources:
|
resources:
|
||||||
@@ -92,17 +92,19 @@ configMapGenerator:
|
|||||||
- name: testCase
|
- name: testCase
|
||||||
behavior: merge
|
behavior: merge
|
||||||
literals:
|
literals:
|
||||||
- overlay=peach
|
- overlay=true
|
||||||
`)
|
`)
|
||||||
m := th.Run("/app/overlay", th.MakeDefaultOptions())
|
m := th.Run("/app/overlay", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
base: apple
|
base: "true"
|
||||||
overlay: peach
|
overlay: "true"
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: testCase-gmfch8gkbt
|
annotations: {}
|
||||||
|
labels: {}
|
||||||
|
name: testCase-bcbmmg48hd
|
||||||
namespace: overlay
|
namespace: overlay
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -307,7 +307,7 @@ metadata:
|
|||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This series of constants is used to prove the need of
|
// This serie of constants is used to prove the need of
|
||||||
// the namespace field in the objref field of the var declaration.
|
// the namespace field in the objref field of the var declaration.
|
||||||
// The following tests demonstrate that it creates umbiguous variable
|
// The following tests demonstrate that it creates umbiguous variable
|
||||||
// declaration if two entities of the kind with the same name
|
// declaration if two entities of the kind with the same name
|
||||||
@@ -472,12 +472,10 @@ spec:
|
|||||||
// not specified
|
// not specified
|
||||||
func TestVariablesAmbiguous(t *testing.T) {
|
func TestVariablesAmbiguous(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
th.WriteK(".", namespaceNeedInVarMyApp)
|
th.WriteK("/namespaceNeedInVar/myapp", namespaceNeedInVarMyApp)
|
||||||
th.WriteF("elasticsearch-dev-service.yaml",
|
th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
|
||||||
namespaceNeedInVarDevResources)
|
th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-test-service.yaml", namespaceNeedInVarTestResources)
|
||||||
th.WriteF("elasticsearch-test-service.yaml",
|
err := th.RunWithErr("/namespaceNeedInVar/myapp", th.MakeDefaultOptions())
|
||||||
namespaceNeedInVarTestResources)
|
|
||||||
err := th.RunWithErr(".", th.MakeDefaultOptions())
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected error")
|
t.Fatalf("expected error")
|
||||||
}
|
}
|
||||||
@@ -531,17 +529,16 @@ vars:
|
|||||||
// and resources into multiple kustomization context/folders instead of one.
|
// and resources into multiple kustomization context/folders instead of one.
|
||||||
func TestVariablesAmbiguousWorkaround(t *testing.T) {
|
func TestVariablesAmbiguousWorkaround(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
opts := th.MakeDefaultOptions()
|
th.WriteK("/namespaceNeedInVar/dev", namespaceNeedInVarDevFolder)
|
||||||
th.WriteK("dev", namespaceNeedInVarDevFolder)
|
th.WriteF("/namespaceNeedInVar/dev/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
|
||||||
th.WriteF("dev/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
|
th.WriteK("/namespaceNeedInVar/test", namespaceNeedInVarTestFolder)
|
||||||
th.WriteK("test", namespaceNeedInVarTestFolder)
|
th.WriteF("/namespaceNeedInVar/test/elasticsearch-test-service.yaml", namespaceNeedInVarTestResources)
|
||||||
th.WriteF("test/elasticsearch-test-service.yaml", namespaceNeedInVarTestResources)
|
th.WriteK("/namespaceNeedInVar/workaround", `
|
||||||
th.WriteK("workaround", `
|
|
||||||
resources:
|
resources:
|
||||||
- ../dev
|
- ../dev
|
||||||
- ../test
|
- ../test
|
||||||
`)
|
`)
|
||||||
m := th.Run("workaround", opts)
|
m := th.Run("/namespaceNeedInVar/workaround", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput)
|
th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -588,68 +585,9 @@ vars:
|
|||||||
// to the variable declarations allows to disambiguate the variables.
|
// to the variable declarations allows to disambiguate the variables.
|
||||||
func TestVariablesDisambiguatedWithNamespace(t *testing.T) {
|
func TestVariablesDisambiguatedWithNamespace(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
th.WriteK(".", namespaceNeedInVarMyAppWithNamespace)
|
th.WriteK("/namespaceNeedInVar/myapp", namespaceNeedInVarMyAppWithNamespace)
|
||||||
th.WriteF("elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
|
th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
|
||||||
th.WriteF("elasticsearch-test-service.yaml", namespaceNeedInVarTestResources)
|
th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-test-service.yaml", namespaceNeedInVarTestResources)
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
m := th.Run("/namespaceNeedInVar/myapp", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput)
|
th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestAddNamePrefixWithNamespace tests that adding a name prefix works within
|
|
||||||
// namespaces other than the default namespace.
|
|
||||||
// Test for issue #3430
|
|
||||||
func TestAddNamePrefixWithNamespace(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
|
|
||||||
th.WriteF("/app/serviceaccount.yaml", `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ServiceAccount
|
|
||||||
metadata:
|
|
||||||
name: prometheus
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteF("/app/clusterrolebinding.yaml", `
|
|
||||||
apiVersion: rbac.authorization.k8s.io/v1beta1
|
|
||||||
kind: ClusterRoleBinding
|
|
||||||
metadata:
|
|
||||||
name: prometheus
|
|
||||||
roleRef:
|
|
||||||
apiGroup: rbac.authorization.k8s.io
|
|
||||||
kind: ClusterRole
|
|
||||||
name: prometheus
|
|
||||||
subjects:
|
|
||||||
- kind: ServiceAccount
|
|
||||||
name: prometheus
|
|
||||||
namespace: iter8-monitoring
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("/app", `
|
|
||||||
namePrefix: iter8-
|
|
||||||
namespace: iter8-monitoring
|
|
||||||
resources:
|
|
||||||
- clusterrolebinding.yaml
|
|
||||||
- serviceaccount.yaml
|
|
||||||
`)
|
|
||||||
|
|
||||||
m := th.Run("/app", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: rbac.authorization.k8s.io/v1beta1
|
|
||||||
kind: ClusterRoleBinding
|
|
||||||
metadata:
|
|
||||||
name: iter8-prometheus
|
|
||||||
roleRef:
|
|
||||||
apiGroup: rbac.authorization.k8s.io
|
|
||||||
kind: ClusterRole
|
|
||||||
name: prometheus
|
|
||||||
subjects:
|
|
||||||
- kind: ServiceAccount
|
|
||||||
name: iter8-prometheus
|
|
||||||
namespace: iter8-monitoring
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ServiceAccount
|
|
||||||
metadata:
|
|
||||||
name: iter8-prometheus
|
|
||||||
namespace: iter8-monitoring
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ subjects:
|
|||||||
- kind: ServiceAccount
|
- kind: ServiceAccount
|
||||||
name: default
|
name: default
|
||||||
namespace: foo
|
namespace: foo
|
||||||
---
|
---
|
||||||
apiVersion: rbac.authorization.k8s.io/v1
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
kind: Role
|
kind: Role
|
||||||
metadata:
|
metadata:
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
package krusty
|
package krusty
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
|
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
@@ -37,40 +36,16 @@ type Options struct {
|
|||||||
// When true, use kyaml/ packages to manipulate KRM yaml.
|
// When true, use kyaml/ packages to manipulate KRM yaml.
|
||||||
// When false, use k8sdeps/ instead (uses k8s.io/api* packages).
|
// When false, use k8sdeps/ instead (uses k8s.io/api* packages).
|
||||||
UseKyaml bool
|
UseKyaml bool
|
||||||
|
|
||||||
// When true, allow name and kind changing via a patch
|
|
||||||
// When false, patch name/kind don't overwrite target name/kind
|
|
||||||
AllowResourceIdChanges bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeDefaultOptions returns a default instance of Options.
|
// MakeDefaultOptions returns a default instance of Options.
|
||||||
func MakeDefaultOptions() *Options {
|
func MakeDefaultOptions() *Options {
|
||||||
return &Options{
|
return &Options{
|
||||||
DoLegacyResourceSort: false,
|
DoLegacyResourceSort: false,
|
||||||
AddManagedbyLabel: false,
|
AddManagedbyLabel: false,
|
||||||
LoadRestrictions: types.LoadRestrictionsRootOnly,
|
LoadRestrictions: types.LoadRestrictionsRootOnly,
|
||||||
DoPrune: false,
|
DoPrune: false,
|
||||||
PluginConfig: konfig.DisabledPluginConfig(),
|
PluginConfig: konfig.DisabledPluginConfig(),
|
||||||
UseKyaml: konfig.FlagEnableKyamlDefaultValue,
|
UseKyaml: false,
|
||||||
AllowResourceIdChanges: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o Options) IfApiMachineryElseKyaml(s1, s2 string) string {
|
|
||||||
if !o.UseKyaml {
|
|
||||||
return s1
|
|
||||||
}
|
|
||||||
return s2
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetBuiltinPluginNames returns a list of builtin plugin names
|
|
||||||
func GetBuiltinPluginNames() []string {
|
|
||||||
var ret []string
|
|
||||||
for k := range builtinhelpers.GeneratorFactories {
|
|
||||||
ret = append(ret, k.String())
|
|
||||||
}
|
|
||||||
for k := range builtinhelpers.TransformerFactories {
|
|
||||||
ret = append(ret, k.String())
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
package krusty_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPatchDeleteOfNotExistingAttributesShouldNotAddExtraElements(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeEnhancedHarness(t)
|
|
||||||
defer th.Reset()
|
|
||||||
|
|
||||||
th.WriteF("resource.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: whatever
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- env:
|
|
||||||
- name: EXISTING
|
|
||||||
value: EXISTING_VALUE
|
|
||||||
- name: FOR_REMOVAL
|
|
||||||
value: FOR_REMOVAL_VALUE
|
|
||||||
name: whatever
|
|
||||||
image: helloworld
|
|
||||||
`)
|
|
||||||
th.WriteF("patch.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: whatever
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: whatever
|
|
||||||
env:
|
|
||||||
- name: FOR_REMOVAL
|
|
||||||
$patch: delete
|
|
||||||
- name: NOT_EXISTING_FOR_REMOVAL
|
|
||||||
$patch: delete
|
|
||||||
`)
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- resource.yaml
|
|
||||||
patches:
|
|
||||||
- path: patch.yaml
|
|
||||||
target:
|
|
||||||
kind: Deployment
|
|
||||||
`)
|
|
||||||
|
|
||||||
// It's expected that removal of not existing elements should not introduce extra values,
|
|
||||||
// as a patch can be applied to multiple resources, not all of them can have all the elements being deleted.
|
|
||||||
expected := `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: whatever
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- env:
|
|
||||||
- name: EXISTING
|
|
||||||
value: EXISTING_VALUE
|
|
||||||
image: helloworld
|
|
||||||
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())
|
|
||||||
th.AssertActualEqualsExpected(m, erroneousActual)
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
// Copyright 2020 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package krusty_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPodDisruptionBudgetBasics(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteF("pdbLiteral.yaml", `
|
|
||||||
apiVersion: policy/v1beta1
|
|
||||||
kind: PodDisruptionBudget
|
|
||||||
metadata:
|
|
||||||
name: pdbLiteral
|
|
||||||
spec:
|
|
||||||
maxUnavailable: 90
|
|
||||||
`)
|
|
||||||
th.WriteF("pdbPercentage.yaml", `
|
|
||||||
apiVersion: policy/v1beta1
|
|
||||||
kind: PodDisruptionBudget
|
|
||||||
metadata:
|
|
||||||
name: pdbPercentage
|
|
||||||
spec:
|
|
||||||
maxUnavailable: 90%
|
|
||||||
`)
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- pdbLiteral.yaml
|
|
||||||
- pdbPercentage.yaml
|
|
||||||
`)
|
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
|
||||||
// In a PodDisruptionBudget, the fields maxUnavailable
|
|
||||||
// minAvailable are mutually exclusive, and both can hold
|
|
||||||
// either an integer, i.e. 10, or string that has to be
|
|
||||||
// an int followed by a percent sign, e.g. 10%.
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: policy/v1beta1
|
|
||||||
kind: PodDisruptionBudget
|
|
||||||
metadata:
|
|
||||||
name: pdbLiteral
|
|
||||||
spec:
|
|
||||||
maxUnavailable: 90
|
|
||||||
---
|
|
||||||
apiVersion: policy/v1beta1
|
|
||||||
kind: PodDisruptionBudget
|
|
||||||
metadata:
|
|
||||||
name: pdbPercentage
|
|
||||||
spec:
|
|
||||||
maxUnavailable: 90%
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPodDisruptionBudgetMerging(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
opts := th.MakeDefaultOptions()
|
|
||||||
th.WriteF("pdb-patch.yaml", `
|
|
||||||
apiVersion: policy/v1beta1
|
|
||||||
kind: PodDisruptionBudget
|
|
||||||
metadata:
|
|
||||||
name: generic-pdb
|
|
||||||
spec:
|
|
||||||
maxUnavailable: 1
|
|
||||||
`)
|
|
||||||
th.WriteF("my_file.yaml", `
|
|
||||||
apiVersion: policy/v1beta1
|
|
||||||
kind: PodDisruptionBudget
|
|
||||||
metadata:
|
|
||||||
name: championships-api
|
|
||||||
labels:
|
|
||||||
faceit-pdb: default
|
|
||||||
spec:
|
|
||||||
maxUnavailable: 100%
|
|
||||||
---
|
|
||||||
apiVersion: policy/v1beta1
|
|
||||||
kind: PodDisruptionBudget
|
|
||||||
metadata:
|
|
||||||
name: championships-api-2
|
|
||||||
labels:
|
|
||||||
faceit-pdb: default
|
|
||||||
spec:
|
|
||||||
maxUnavailable: 100%
|
|
||||||
`)
|
|
||||||
th.WriteK(".", `
|
|
||||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
|
||||||
kind: Kustomization
|
|
||||||
patches:
|
|
||||||
- path: pdb-patch.yaml
|
|
||||||
target:
|
|
||||||
kind: PodDisruptionBudget
|
|
||||||
labelSelector: faceit-pdb=default
|
|
||||||
|
|
||||||
resources:
|
|
||||||
- my_file.yaml
|
|
||||||
`)
|
|
||||||
m := th.Run(".", opts)
|
|
||||||
expFmt := `
|
|
||||||
apiVersion: policy/v1beta1
|
|
||||||
kind: PodDisruptionBudget
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
faceit-pdb: default
|
|
||||||
name: championships-api
|
|
||||||
spec:
|
|
||||||
maxUnavailable: %s
|
|
||||||
---
|
|
||||||
apiVersion: policy/v1beta1
|
|
||||||
kind: PodDisruptionBudget
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
faceit-pdb: default
|
|
||||||
name: championships-api-2
|
|
||||||
spec:
|
|
||||||
maxUnavailable: %s
|
|
||||||
`
|
|
||||||
// In a PodDisruptionBudget, the fields maxUnavailable
|
|
||||||
// minAvailable are mutually exclusive, and both can hold
|
|
||||||
// either an integer, i.e. 10, or string that has to be
|
|
||||||
// an int followed by a percent sign, e.g. 10%.
|
|
||||||
// In the former case - bare integer - they should NOT be quoted
|
|
||||||
// as the api server will reject it. In the latter case with
|
|
||||||
// the percent sign, quotes can be added and the API server will
|
|
||||||
// accept it, but they don't have to be added.
|
|
||||||
th.AssertActualEqualsExpected(
|
|
||||||
m,
|
|
||||||
// TODO(#3304): DECISION - kyaml better; not a bug.
|
|
||||||
opts.IfApiMachineryElseKyaml(
|
|
||||||
fmt.Sprintf(expFmt, `"1"`, `"1"`),
|
|
||||||
fmt.Sprintf(expFmt, `1`, `1`)))
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package krusty_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
|
||||||
"sigs.k8s.io/kustomize/api/internal/utils"
|
|
||||||
"sigs.k8s.io/kustomize/api/krusty"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestRemoteLoad(t *testing.T) {
|
|
||||||
fSys := filesys.MakeFsOnDisk()
|
|
||||||
b := krusty.MakeKustomizer(fSys, krusty.MakeDefaultOptions())
|
|
||||||
m, err := b.Run(
|
|
||||||
"github.com/kubernetes-sigs/kustomize/examples/multibases/dev/?ref=v1.0.6")
|
|
||||||
if utils.IsErrTimeout(err) {
|
|
||||||
// Don't fail on timeouts.
|
|
||||||
t.SkipNow()
|
|
||||||
}
|
|
||||||
if !assert.NoError(t, err) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
yml, err := m.AsYaml()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, `apiVersion: v1
|
|
||||||
kind: Pod
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
app: myapp
|
|
||||||
name: dev-myapp-pod
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: nginx:1.7.9
|
|
||||||
name: nginx
|
|
||||||
`, string(yml))
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package krusty_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSimple1(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteF("/dep.yaml", `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: clown
|
|
||||||
spec:
|
|
||||||
numReplicas: 1
|
|
||||||
`)
|
|
||||||
th.WriteF("/patch.yaml", `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: clown
|
|
||||||
spec:
|
|
||||||
numReplicas: 999
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteK("/", `
|
|
||||||
resources:
|
|
||||||
- dep.yaml
|
|
||||||
patchesStrategicMerge:
|
|
||||||
- patch.yaml
|
|
||||||
`)
|
|
||||||
m := th.Run("/", th.MakeDefaultOptions())
|
|
||||||
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: clown
|
|
||||||
spec:
|
|
||||||
numReplicas: 999
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
@@ -74,20 +74,20 @@ kind: ConfigMap
|
|||||||
metadata:
|
metadata:
|
||||||
name: configmap-a
|
name: configmap-a
|
||||||
annotations:
|
annotations:
|
||||||
fruit: peach
|
kustomize.k8s.io/Generated: "false"
|
||||||
data:
|
data:
|
||||||
foo: $FOO
|
foo: $FOO
|
||||||
`)
|
`)
|
||||||
|
|
||||||
m := th.Run("/app", th.MakeOptionsPluginsEnabled())
|
m := th.Run("/app", th.MakeOptionsPluginsEnabled())
|
||||||
th.AssertActualEqualsExpectedNoIdAnnotations(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
foo: foo
|
foo: foo
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
fruit: peach
|
kustomize.k8s.io/Generated: "false"
|
||||||
name: configmap-a
|
name: configmap-a
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
|
|||||||
@@ -360,187 +360,6 @@ resources:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSimpleServicePortVarReplace(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- service.yaml
|
|
||||||
- statefulset.yaml
|
|
||||||
vars:
|
|
||||||
- name: THE_PORT
|
|
||||||
objref:
|
|
||||||
kind: StatefulSet
|
|
||||||
name: cockroachdb
|
|
||||||
apiVersion: apps/v1beta1
|
|
||||||
fieldref:
|
|
||||||
fieldpath: spec.template.spec.containers[0].ports[1].containerPort
|
|
||||||
`)
|
|
||||||
th.WriteF("service.yaml", `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: myService
|
|
||||||
spec:
|
|
||||||
ports:
|
|
||||||
- port: $(THE_PORT)
|
|
||||||
targetPort: $(THE_PORT)
|
|
||||||
name: grpc
|
|
||||||
`)
|
|
||||||
th.WriteF("statefulset.yaml", `
|
|
||||||
apiVersion: apps/v1beta1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: cockroachdb
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: cockroachdb
|
|
||||||
image: cockroachdb/cockroach:v1.1.5
|
|
||||||
ports:
|
|
||||||
- containerPort: 26257
|
|
||||||
name: grpc
|
|
||||||
- containerPort: 8888
|
|
||||||
name: http
|
|
||||||
`)
|
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: myService
|
|
||||||
spec:
|
|
||||||
ports:
|
|
||||||
- name: grpc
|
|
||||||
port: 8888
|
|
||||||
targetPort: 8888
|
|
||||||
---
|
|
||||||
apiVersion: apps/v1beta1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: cockroachdb
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- image: cockroachdb/cockroach:v1.1.5
|
|
||||||
name: cockroachdb
|
|
||||||
ports:
|
|
||||||
- containerPort: 26257
|
|
||||||
name: grpc
|
|
||||||
- containerPort: 8888
|
|
||||||
name: http
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(3449): Yield bare primitives in var replacements from configmaps.
|
|
||||||
// The ConfigMap data field is always strings, and anything that looks
|
|
||||||
// like a boolean or int or float must be quoted, or the API server won't
|
|
||||||
// accept the map. This creates a problem if one wants to use a var to
|
|
||||||
// inject a raw number or raw boolean sourced from a configmap, because as
|
|
||||||
// far as the configmap representation is concerned, it's a string.
|
|
||||||
// A workaround would be to source the var from another Kind, from a field
|
|
||||||
// that allowed unquoted vars or booleans.
|
|
||||||
func TestIssue3449(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- workflow.yaml
|
|
||||||
|
|
||||||
configurations:
|
|
||||||
- kustomization-config.yaml
|
|
||||||
|
|
||||||
configMapGenerator:
|
|
||||||
- name: kustomize-vars
|
|
||||||
envs:
|
|
||||||
- vars.env
|
|
||||||
|
|
||||||
vars:
|
|
||||||
- name: DBT_TARGET
|
|
||||||
objref: &config-map-ref
|
|
||||||
kind: ConfigMap
|
|
||||||
name: kustomize-vars
|
|
||||||
apiVersion: v1
|
|
||||||
fieldref:
|
|
||||||
fieldpath: data.DBT_TARGET
|
|
||||||
- name: SUSPENDED
|
|
||||||
objref: *config-map-ref
|
|
||||||
fieldref:
|
|
||||||
fieldpath: data.SUSPENDED
|
|
||||||
`)
|
|
||||||
th.WriteF("workflow.yaml", `
|
|
||||||
apiVersion: argoproj.io/v1alpha1
|
|
||||||
kind: CronWorkflow
|
|
||||||
metadata:
|
|
||||||
name: cron-core-load-workflow
|
|
||||||
spec:
|
|
||||||
schedule: "45 2 * * *"
|
|
||||||
timezone: "Europe/Vienna"
|
|
||||||
concurrencyPolicy: Forbid
|
|
||||||
suspend: $(SUSPENDED)
|
|
||||||
workflowMetadata:
|
|
||||||
labels:
|
|
||||||
workflowName: core-load-workflow
|
|
||||||
workflowSpec:
|
|
||||||
workflowTemplateRef:
|
|
||||||
name: core-load-pipeline
|
|
||||||
arguments:
|
|
||||||
parameters:
|
|
||||||
- name: dbt_target
|
|
||||||
value: $(DBT_TARGET)
|
|
||||||
`)
|
|
||||||
th.WriteF("kustomization-config.yaml", `
|
|
||||||
nameReference:
|
|
||||||
- kind: ConfigMap
|
|
||||||
version: v1
|
|
||||||
fieldSpecs:
|
|
||||||
- kind: CronWorkflow
|
|
||||||
version: v1alpha1
|
|
||||||
path: spec/workflowSpec/arguments/parameters/value
|
|
||||||
varReference:
|
|
||||||
- path: spec/workflowSpec/arguments/parameters/value
|
|
||||||
kind: CronWorkflow
|
|
||||||
apiVersion: argoproj.io/v1alpha1
|
|
||||||
- path: spec
|
|
||||||
kind: CronWorkflow
|
|
||||||
apiVersion: argoproj.io/v1alpha1
|
|
||||||
`)
|
|
||||||
th.WriteF("vars.env", `
|
|
||||||
DBT_TARGET=development
|
|
||||||
SUSPENDED=True
|
|
||||||
`)
|
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: argoproj.io/v1alpha1
|
|
||||||
kind: CronWorkflow
|
|
||||||
metadata:
|
|
||||||
name: cron-core-load-workflow
|
|
||||||
spec:
|
|
||||||
concurrencyPolicy: Forbid
|
|
||||||
schedule: 45 2 * * *
|
|
||||||
suspend: "True"
|
|
||||||
timezone: Europe/Vienna
|
|
||||||
workflowMetadata:
|
|
||||||
labels:
|
|
||||||
workflowName: core-load-workflow
|
|
||||||
workflowSpec:
|
|
||||||
arguments:
|
|
||||||
parameters:
|
|
||||||
- name: dbt_target
|
|
||||||
value: development
|
|
||||||
workflowTemplateRef:
|
|
||||||
name: core-load-pipeline
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
data:
|
|
||||||
DBT_TARGET: development
|
|
||||||
SUSPENDED: "True"
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: kustomize-vars-7mhm8cg5kg
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestVarRefBig(t *testing.T) {
|
func TestVarRefBig(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
th.WriteK("/app/base", `
|
th.WriteK("/app/base", `
|
||||||
@@ -1106,64 +925,7 @@ metadata:
|
|||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVariableRefIngressBasic(t *testing.T) {
|
func TestVariableRefIngress(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
|
||||||
th.WriteK(".", `
|
|
||||||
resources:
|
|
||||||
- ingress.yaml
|
|
||||||
- deployment.yaml
|
|
||||||
|
|
||||||
vars:
|
|
||||||
- name: DEPLOYMENT_NAME
|
|
||||||
objref:
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
name: nginxDep
|
|
||||||
fieldref:
|
|
||||||
fieldpath: metadata.name
|
|
||||||
`)
|
|
||||||
th.WriteF("deployment.yaml", `
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginxDep
|
|
||||||
`)
|
|
||||||
|
|
||||||
th.WriteF("ingress.yaml", `
|
|
||||||
apiVersion: networking.k8s.io/v1beta1
|
|
||||||
kind: Ingress
|
|
||||||
metadata:
|
|
||||||
name: nginxIngress
|
|
||||||
spec:
|
|
||||||
rules:
|
|
||||||
- host: $(DEPLOYMENT_NAME).example.com
|
|
||||||
tls:
|
|
||||||
- hosts:
|
|
||||||
- $(DEPLOYMENT_NAME).example.com
|
|
||||||
secretName: $(DEPLOYMENT_NAME).example.com-tls
|
|
||||||
`)
|
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
|
||||||
th.AssertActualEqualsExpected(m, `
|
|
||||||
apiVersion: networking.k8s.io/v1beta1
|
|
||||||
kind: Ingress
|
|
||||||
metadata:
|
|
||||||
name: nginxIngress
|
|
||||||
spec:
|
|
||||||
rules:
|
|
||||||
- host: nginxDep.example.com
|
|
||||||
tls:
|
|
||||||
- hosts:
|
|
||||||
- nginxDep.example.com
|
|
||||||
secretName: nginxDep.example.com-tls
|
|
||||||
---
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: nginxDep
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestVariableRefIngressOverlay(t *testing.T) {
|
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
th.WriteK("/app/base", `
|
th.WriteK("/app/base", `
|
||||||
resources:
|
resources:
|
||||||
@@ -2210,64 +1972,67 @@ spec:
|
|||||||
|
|
||||||
func TestDeploymentAnnotations(t *testing.T) {
|
func TestDeploymentAnnotations(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
th.WriteK(".", `
|
th.WriteK("/app", `
|
||||||
|
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
|
kind: Kustomization
|
||||||
|
|
||||||
configMapGenerator:
|
configMapGenerator:
|
||||||
- name: theConfigMap
|
- name: testConfigMap
|
||||||
envs:
|
envs:
|
||||||
- test.properties
|
- test.properties
|
||||||
|
|
||||||
vars:
|
vars:
|
||||||
- name: SOMERIVER
|
- name: FOO
|
||||||
objref:
|
objref:
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
name: theConfigMap
|
name: testConfigMap
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
fieldref:
|
fieldref:
|
||||||
fieldpath: data.waterway
|
fieldpath: data.foo
|
||||||
|
|
||||||
commonAnnotations:
|
commonAnnotations:
|
||||||
river: $(SOMERIVER)
|
foo: $(FOO)
|
||||||
|
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteF("deployment.yaml", `
|
th.WriteF("/app/deployment.yaml", `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: theDeployment
|
name: test
|
||||||
spec:
|
spec:
|
||||||
template:
|
template:
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: test
|
- name: test
|
||||||
`)
|
`)
|
||||||
th.WriteF("test.properties", `waterway=mississippi`)
|
th.WriteF("/app/test.properties", `foo=bar`)
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
m := th.Run("/app", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
river: mississippi
|
foo: bar
|
||||||
name: theDeployment
|
name: test
|
||||||
spec:
|
spec:
|
||||||
template:
|
template:
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
river: mississippi
|
foo: bar
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: test
|
- name: test
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
waterway: mississippi
|
foo: bar
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
river: mississippi
|
foo: bar
|
||||||
name: theConfigMap-hdd8h8cgdt
|
name: testConfigMap-798k5k7g9f
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/yujunz/go-getter"
|
"github.com/yujunz/go-getter"
|
||||||
"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/internal/git"
|
"sigs.k8s.io/kustomize/api/internal/git"
|
||||||
"sigs.k8s.io/kustomize/api/internal/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type remoteTargetSpec struct {
|
type remoteTargetSpec struct {
|
||||||
@@ -30,12 +28,7 @@ type remoteTargetSpec struct {
|
|||||||
// Getter is a function that can gets resource
|
// Getter is a function that can gets resource
|
||||||
type remoteTargetGetter func(rs *remoteTargetSpec) error
|
type remoteTargetGetter func(rs *remoteTargetSpec) error
|
||||||
|
|
||||||
func newLoaderAtGetter(
|
func newLoaderAtGetter(raw string, fSys filesys.FileSystem, referrer *fileLoader, cloner git.Cloner, getter remoteTargetGetter) (ifc.Loader, error) {
|
||||||
raw string,
|
|
||||||
fSys filesys.FileSystem,
|
|
||||||
referrer *fileLoader,
|
|
||||||
cloner git.Cloner,
|
|
||||||
getter remoteTargetGetter) (ifc.Loader, error) {
|
|
||||||
rs := &remoteTargetSpec{
|
rs := &remoteTargetSpec{
|
||||||
Raw: raw,
|
Raw: raw,
|
||||||
}
|
}
|
||||||
@@ -87,13 +80,12 @@ func getRemoteTarget(rs *remoteTargetSpec) error {
|
|||||||
Mode: getter.ClientModeAny,
|
Mode: getter.ClientModeAny,
|
||||||
Detectors: []getter.Detector{
|
Detectors: []getter.Detector{
|
||||||
new(getter.GitHubDetector),
|
new(getter.GitHubDetector),
|
||||||
new(getter.GitLabDetector),
|
|
||||||
new(getter.GitDetector),
|
new(getter.GitDetector),
|
||||||
new(getter.BitBucketDetector),
|
new(getter.BitBucketDetector),
|
||||||
},
|
},
|
||||||
Options: opts,
|
Options: opts,
|
||||||
}
|
}
|
||||||
return utils.TimedCall("go-getter client.Get", 21*time.Second, client.Get)
|
return client.Get()
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNothing(rs *remoteTargetSpec) error {
|
func getNothing(rs *remoteTargetSpec) error {
|
||||||
|
|||||||
@@ -21,22 +21,24 @@ import (
|
|||||||
func NewLoader(
|
func NewLoader(
|
||||||
lr LoadRestrictorFunc,
|
lr LoadRestrictorFunc,
|
||||||
target string, fSys filesys.FileSystem) (ifc.Loader, error) {
|
target string, fSys filesys.FileSystem) (ifc.Loader, error) {
|
||||||
ldr, errGet := newLoaderAtGetter(
|
|
||||||
target, fSys, nil, git.ClonerUsingGitExec, getRemoteTarget)
|
ldr, errGet := newLoaderAtGetter(target, fSys, nil, git.ClonerUsingGitExec, getRemoteTarget)
|
||||||
if errGet == nil {
|
if errGet == nil {
|
||||||
return ldr, nil
|
return ldr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
repoSpec, errGit := git.NewRepoSpecFromUrl(target)
|
repoSpec, errGit := git.NewRepoSpecFromUrl(target)
|
||||||
if errGit == nil {
|
if errGit == nil {
|
||||||
// The target qualifies as a remote git target.
|
// The target qualifies as a remote git target.
|
||||||
return newLoaderAtGitClone(
|
return newLoaderAtGitClone(
|
||||||
repoSpec, fSys, nil, git.ClonerUsingGitExec, getRemoteTarget)
|
repoSpec, fSys, nil, git.ClonerUsingGitExec, getRemoteTarget)
|
||||||
}
|
}
|
||||||
|
|
||||||
root, errDir := demandDirectoryRoot(fSys, target)
|
root, errDir := demandDirectoryRoot(fSys, target)
|
||||||
if errDir == nil {
|
if errDir == nil {
|
||||||
return newLoaderAtConfirmedDir(
|
return newLoaderAtConfirmedDir(lr, root, fSys, nil, git.ClonerUsingGitExec, getRemoteTarget), nil
|
||||||
lr, root, fSys, nil, git.ClonerUsingGitExec, getRemoteTarget), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"error creating new loader with git: %v, dir: %v, get: %v",
|
"error creating new loader with git: %v, dir: %v, get: %v",
|
||||||
errGit, errDir, errGet)
|
errGit, errDir, errGet)
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ package provenance
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -58,11 +57,3 @@ func (v Provenance) Short() string {
|
|||||||
BuildDate: v.BuildDate,
|
BuildDate: v.BuildDate,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Semver returns the semantic version of kustomize.
|
|
||||||
// kustomize version is set in format "kustomize/vX.X.X" in every release.
|
|
||||||
// X.X.X is a semver. If the version string is not in this format,
|
|
||||||
// return the original version string
|
|
||||||
func (v Provenance) Semver() string {
|
|
||||||
return strings.TrimPrefix(v.Version, "kustomize/")
|
|
||||||
}
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user