mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 18:40:55 +00:00
Compare commits
1 Commits
removeRepl
...
updateRele
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f59f12947b |
27
Makefile
27
Makefile
@@ -15,8 +15,7 @@ verify-kustomize: \
|
||||
lint-kustomize \
|
||||
test-unit-kustomize-all \
|
||||
test-examples-kustomize-against-HEAD \
|
||||
test-examples-kustomize-against-3.8.0 \
|
||||
test-examples-kustomize-against-3.8.2
|
||||
test-examples-kustomize-against-3.8.0
|
||||
|
||||
# The following target referenced by a file in
|
||||
# https://github.com/kubernetes/test-infra/tree/master/config/jobs/kubernetes-sigs/kustomize
|
||||
@@ -27,8 +26,7 @@ prow-presubmit-check: \
|
||||
test-unit-cmd-all \
|
||||
test-go-mod \
|
||||
test-examples-kustomize-against-HEAD \
|
||||
test-examples-kustomize-against-3.8.0 \
|
||||
test-examples-kustomize-against-3.8.2
|
||||
test-examples-kustomize-against-3.8.0
|
||||
|
||||
.PHONY: verify-kustomize-e2e
|
||||
verify-kustomize-e2e: test-examples-e2e-kustomize
|
||||
@@ -47,18 +45,17 @@ $(MYGOBIN)/golangci-lint-kustomize:
|
||||
GO111MODULE=on go build -tags=tools -o $(MYGOBIN)/golangci-lint-kustomize github.com/golangci/golangci-lint/cmd/golangci-lint; \
|
||||
)
|
||||
|
||||
$(MYGOBIN)/gorepomod:
|
||||
cd api; \
|
||||
go install github.com/monopole/gorepomod
|
||||
|
||||
# Version pinned by api/go.mod
|
||||
$(MYGOBIN)/mdrip:
|
||||
cd api; \
|
||||
go install github.com/monopole/mdrip
|
||||
|
||||
# Version pinned by api/go.mod
|
||||
$(MYGOBIN)/stringer:
|
||||
cd api; \
|
||||
go install golang.org/x/tools/cmd/stringer
|
||||
|
||||
# Version pinned by api/go.mod
|
||||
$(MYGOBIN)/goimports:
|
||||
cd api; \
|
||||
go install golang.org/x/tools/cmd/goimports
|
||||
@@ -84,7 +81,6 @@ $(MYGOBIN)/kustomize:
|
||||
install-tools: \
|
||||
$(MYGOBIN)/goimports \
|
||||
$(MYGOBIN)/golangci-lint-kustomize \
|
||||
$(MYGOBIN)/gorepomod \
|
||||
$(MYGOBIN)/mdrip \
|
||||
$(MYGOBIN)/pluginator \
|
||||
$(MYGOBIN)/stringer
|
||||
@@ -249,19 +245,6 @@ test-examples-kustomize-against-3.8.0: $(MYGOBIN)/mdrip
|
||||
cd kustomize; go install .; \
|
||||
)
|
||||
|
||||
.PHONY:
|
||||
test-examples-kustomize-against-3.8.2: $(MYGOBIN)/mdrip
|
||||
( \
|
||||
set -e; \
|
||||
tag=v3.8.2; \
|
||||
/bin/rm -f $(MYGOBIN)/kustomize; \
|
||||
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.
|
||||
# This is for testing an example plugin that
|
||||
# uses kubeval for validation.
|
||||
|
||||
@@ -11,4 +11,3 @@ aliases:
|
||||
- pwittrock
|
||||
- mortent
|
||||
- phanimarupaka
|
||||
- Shell32-Natsu
|
||||
|
||||
@@ -31,15 +31,14 @@ func (p *ImageTagTransformerPlugin) Config(
|
||||
|
||||
func (p *ImageTagTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
for _, r := range m.Resources() {
|
||||
// traverse all fields at first
|
||||
err := filtersutil.ApplyToJSON(imagetag.LegacyFilter{
|
||||
ImageTag: p.ImageTag,
|
||||
}, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// then use user specified field specs
|
||||
err = filtersutil.ApplyToJSON(imagetag.Filter{
|
||||
// If you're here because someone expected any field containing
|
||||
// the string "containers" or "initContainers" to get an image
|
||||
// update (not just spec/template/spec/containers[], etc.) then
|
||||
// a code change is needed. See api/filters/imagetag/legacy
|
||||
// for the start of an implementation that won't use an
|
||||
// allowlist like FsSlice, and instead walks the object looking
|
||||
// for fields named containers or initContainers.
|
||||
err := filtersutil.ApplyToJSON(imagetag.Filter{
|
||||
ImageTag: p.ImageTag,
|
||||
FsSlice: p.FieldSpecs,
|
||||
}, r)
|
||||
|
||||
@@ -51,9 +51,6 @@ func (fltr Filter) filter(obj *yaml.RNode) error {
|
||||
// found the field -- set its value
|
||||
return fltr.SetValue(obj)
|
||||
}
|
||||
if obj.IsTaggedNull() {
|
||||
return nil
|
||||
}
|
||||
switch obj.YNode().Kind {
|
||||
case yaml.SequenceNode:
|
||||
return fltr.seq(obj)
|
||||
@@ -70,7 +67,7 @@ func (fltr Filter) field(obj *yaml.RNode) error {
|
||||
// lookup the field matching the next path element
|
||||
var lookupField yaml.Filter
|
||||
var kind yaml.Kind
|
||||
tag := yaml.NodeTagEmpty
|
||||
tag := "" // TODO: change to yaml.NodeTagEmpty
|
||||
switch {
|
||||
case !fltr.FieldSpec.CreateIfNotPresent || fltr.CreateKind == 0 || isSeq:
|
||||
// dont' create the field if we don't find it
|
||||
@@ -98,10 +95,9 @@ func (fltr Filter) field(obj *yaml.RNode) error {
|
||||
return errors.WrapPrefixf(err, "fieldName: %s", fieldName)
|
||||
}
|
||||
|
||||
// if the value exists, but is null and kind is set,
|
||||
// then change it to the creation type
|
||||
// if the value exists, but is null, then change it to the creation type
|
||||
// TODO: update yaml.LookupCreate to support this
|
||||
if field.YNode().Tag == yaml.NodeTagNull && yaml.IsCreate(kind) {
|
||||
if field.YNode().Tag == yaml.NodeTagNull {
|
||||
field.YNode().Kind = kind
|
||||
field.YNode().Tag = tag
|
||||
}
|
||||
|
||||
@@ -433,6 +433,7 @@ spec:
|
||||
SetValue: filtersutil.SetScalar("bar"),
|
||||
CreateKind: yaml.ScalarNode,
|
||||
},
|
||||
error: "obj '' at path 'spec/containers/image': expected sequence or mapping node",
|
||||
},
|
||||
{
|
||||
name: "filedname with slash '/'",
|
||||
|
||||
@@ -168,44 +168,6 @@ metadata:
|
||||
map:
|
||||
name: newName
|
||||
namespace: oldNs
|
||||
`,
|
||||
filter: Filter{
|
||||
FieldSpec: types.FieldSpec{Path: "map"},
|
||||
Target: resid.Gvk{
|
||||
Group: "apps",
|
||||
Version: "v1",
|
||||
Kind: "Secret",
|
||||
},
|
||||
},
|
||||
},
|
||||
"null value": {
|
||||
input: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: dep
|
||||
map:
|
||||
name: null
|
||||
`,
|
||||
candidates: `
|
||||
apiVersion: apps/v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: newName
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: NotSecret
|
||||
metadata:
|
||||
name: newName2
|
||||
`,
|
||||
originalNames: []string{"oldName", ""},
|
||||
expected: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: dep
|
||||
map:
|
||||
name: null
|
||||
`,
|
||||
filter: Filter{
|
||||
FieldSpec: types.FieldSpec{Path: "map"},
|
||||
@@ -255,6 +217,27 @@ func TestNamerefFilterUnhappy(t *testing.T) {
|
||||
filter Filter
|
||||
originalNames []string
|
||||
}{
|
||||
"invalid node type": {
|
||||
input: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: dep
|
||||
ref:
|
||||
name: null
|
||||
`,
|
||||
candidates: "",
|
||||
originalNames: []string{},
|
||||
expected: "obj '' at path 'ref/name': node is expected to be either a string or a slice of string or a map of string",
|
||||
filter: Filter{
|
||||
FieldSpec: types.FieldSpec{Path: "ref/name"},
|
||||
Target: resid.Gvk{
|
||||
Group: "apps",
|
||||
Version: "v1",
|
||||
Kind: "Secret",
|
||||
},
|
||||
},
|
||||
},
|
||||
"multiple match": {
|
||||
input: `
|
||||
apiVersion: apps/v1
|
||||
|
||||
@@ -69,7 +69,7 @@ func (ns Filter) hacks(obj *yaml.RNode) error {
|
||||
// metaNamespaceHack is a hack for implementing the namespace transform
|
||||
// for the metadata.namespace field on namespace scoped resources.
|
||||
// namespace scoped resources are determined by NOT being present
|
||||
// in a hard-coded list of cluster-scoped resource types (by apiVersion and kind).
|
||||
// in a blacklist of cluster-scoped resource types (by apiVersion and kind).
|
||||
//
|
||||
// This hack should be updated to allow individual resources to specify
|
||||
// if they are cluster scoped through either an annotation on the resources,
|
||||
|
||||
@@ -188,26 +188,6 @@ data:
|
||||
FieldSpec: types.FieldSpec{Path: "data/slice2"},
|
||||
},
|
||||
},
|
||||
"null value": {
|
||||
input: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: dep
|
||||
data:
|
||||
FOO: null`,
|
||||
expected: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: dep
|
||||
data:
|
||||
FOO: null`,
|
||||
filter: Filter{
|
||||
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{}),
|
||||
FieldSpec: types.FieldSpec{Path: "data/FOO"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for tn, tc := range testCases {
|
||||
@@ -280,6 +260,20 @@ data:
|
||||
FieldSpec: types.FieldSpec{Path: "data"},
|
||||
},
|
||||
},
|
||||
"null input": {
|
||||
input: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: dep
|
||||
data:
|
||||
FOO: null`,
|
||||
expectedError: "obj '' at path 'data/FOO': invalid type encountered 0",
|
||||
filter: Filter{
|
||||
MappingFunc: expansion2.MappingFuncFor(replacementCounts, map[string]interface{}{}),
|
||||
FieldSpec: types.FieldSpec{Path: "data/FOO"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for tn, tc := range testCases {
|
||||
|
||||
@@ -10,13 +10,15 @@ require (
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/stretchr/testify v1.4.0
|
||||
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-20191010075000-0337d82405ff
|
||||
gopkg.in/yaml.v2 v2.3.0
|
||||
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71
|
||||
k8s.io/api v0.17.0
|
||||
k8s.io/apimachinery v0.17.0
|
||||
k8s.io/client-go v0.17.0
|
||||
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a
|
||||
sigs.k8s.io/kustomize/kyaml v0.8.0
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1
|
||||
sigs.k8s.io/yaml v1.2.0
|
||||
)
|
||||
|
||||
replace sigs.k8s.io/kustomize/kyaml v0.6.1 => ../kyaml
|
||||
|
||||
@@ -307,8 +307,6 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
|
||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/2gBQ3RWajuToeY6ZtZTIKv2v7ThUy5KKusIT0yc0=
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4=
|
||||
github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
|
||||
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
@@ -528,8 +526,6 @@ golang.org/x/tools v0.0.0-20190910044552-dd2b5c81c578/go.mod h1:b+2E5dAYhXwXZwtn
|
||||
golang.org/x/tools v0.0.0-20190930201159-7c411dea38b0/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff h1:XdBG6es/oFDr1HwaxkxgVve7NB281QhxgK/i4voubFs=
|
||||
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e h1:aZzprAO9/8oim3qStq3wc1Xuxx4QmAGriC4VU4ojemQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
@@ -588,8 +584,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/unparam v0.0.0-20190720180237-d51796306d8f h1:Cq7MalBHYACRd6EesksG1Q8EoIAKOsiZviGKbOLIej4=
|
||||
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
||||
sigs.k8s.io/kustomize/kyaml v0.8.0 h1:/MqPML99XAm2pbrD/eTpePh5rnU5bpnuTPqb29LpSz4=
|
||||
sigs.k8s.io/kustomize/kyaml v0.8.0/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
|
||||
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.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
|
||||
|
||||
@@ -132,7 +132,7 @@ func TestRefVarTransformer(t *testing.T) {
|
||||
' at path 'data/slice': invalid value type expect a string`,
|
||||
},
|
||||
{
|
||||
description: "var replacement in nil",
|
||||
description: "var replacement panic in nil",
|
||||
given: given{
|
||||
varMap: map[string]interface{}{},
|
||||
fs: []types.FieldSpec{
|
||||
@@ -150,19 +150,7 @@ func TestRefVarTransformer(t *testing.T) {
|
||||
"nil": nil, // noticeably *not* a []string
|
||||
}}).ResMap(),
|
||||
},
|
||||
expected: expected{
|
||||
res: resmaptest_test.NewRmBuilder(
|
||||
t, resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())).
|
||||
Add(map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
"kind": "ConfigMap",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "cm1",
|
||||
},
|
||||
"data": map[string]interface{}{
|
||||
"nil": nil, // noticeably *not* a []string
|
||||
}}).ResMap(),
|
||||
},
|
||||
errMessage: `obj '' at path 'data/nil': invalid type encountered 0`,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -217,7 +217,7 @@ overview of each component with the following sections going into more details.
|
||||
|
||||
The overall structure is outlined in the following figure:
|
||||

|
||||
https://sigs.k8s.io/kustomize/internal/tools/pictures/sys_arch.png)
|
||||
|
||||
#### Crawler
|
||||
The leftmost component consists of a crawler with an http cache of GitHub
|
||||
|
||||
@@ -271,8 +271,6 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
|
||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/2gBQ3RWajuToeY6ZtZTIKv2v7ThUy5KKusIT0yc0=
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4=
|
||||
github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
|
||||
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
@@ -472,7 +470,6 @@ golang.org/x/tools v0.0.0-20190719005602-e377ae9d6386/go.mod h1:jcCCGcm9btYwXyDq
|
||||
golang.org/x/tools v0.0.0-20190910044552-dd2b5c81c578/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20190930201159-7c411dea38b0/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
@@ -527,8 +524,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/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=
|
||||
sigs.k8s.io/kustomize/kyaml v0.8.0 h1:/MqPML99XAm2pbrD/eTpePh5rnU5bpnuTPqb29LpSz4=
|
||||
sigs.k8s.io/kustomize/kyaml v0.8.0/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1 h1:mwffj5vt3MPdbWV3fZnnwol8SO7sUoGdgejBlvseyak=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1/go.mod h1:bEzbO5pN9OvlEeCLvFHo8Pu7SA26Herc2m60UeWZBdI=
|
||||
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/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||
|
||||
@@ -8,9 +8,6 @@ const (
|
||||
namespace:
|
||||
- path: metadata/namespace
|
||||
create: true
|
||||
- path: metadata/name
|
||||
kind: Namespace
|
||||
create: true
|
||||
- path: subjects
|
||||
kind: RoleBinding
|
||||
- path: subjects
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||
)
|
||||
|
||||
func TestNullValues1(t *testing.T) {
|
||||
func TestNullValues(t *testing.T) {
|
||||
th := kusttest_test.MakeHarness(t)
|
||||
th.WriteF("/app/deployment.yaml", `
|
||||
apiVersion: apps/v1
|
||||
@@ -62,36 +62,3 @@ spec:
|
||||
name: example
|
||||
`)
|
||||
}
|
||||
|
||||
func TestNullValues2(t *testing.T) {
|
||||
th := kusttest_test.MakeHarness(t)
|
||||
th.WriteF("deploy.yaml", `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: test
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: test
|
||||
volumes: null
|
||||
`)
|
||||
th.WriteK(".", `
|
||||
resources:
|
||||
- deploy.yaml
|
||||
`)
|
||||
m := th.Run(".", th.MakeDefaultOptions())
|
||||
th.AssertActualEqualsExpected(m, `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: test
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: test
|
||||
volumes: null
|
||||
`)
|
||||
}
|
||||
|
||||
@@ -215,25 +215,25 @@ spec2:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- image: nginx:v2
|
||||
- image: nginx:v1
|
||||
name: nginx3
|
||||
- image: my-nginx:previous
|
||||
- image: my-nginx:latest
|
||||
name: nginx4
|
||||
spec3:
|
||||
template:
|
||||
spec:
|
||||
initContainers:
|
||||
- image: my-postgres:v3
|
||||
- image: postgres:alpine-9
|
||||
name: postgresdb
|
||||
- image: my-docker@sha256:25a0d4b4
|
||||
- image: docker:17-git
|
||||
name: init-docker
|
||||
- image: myprivaterepohostname:1234/my/image:v1.0.1
|
||||
- image: myprivaterepohostname:1234/my/image:latest
|
||||
name: myImage
|
||||
- image: myprivaterepohostname:1234/my/image:v1.0.1
|
||||
- image: myprivaterepohostname:1234/my/image
|
||||
name: myImage2
|
||||
- image: my-app-image:v1
|
||||
name: my-app
|
||||
- image: my-cool-app:latest
|
||||
- image: gcr.io:8080/my-project/my-cool-app:latest
|
||||
name: my-cool-app
|
||||
`)
|
||||
}
|
||||
|
||||
@@ -20,9 +20,6 @@ type remoteTargetSpec struct {
|
||||
|
||||
// Dir is where the resource is saved
|
||||
Dir filesys.ConfirmedDir
|
||||
|
||||
// TempDir is the directory created to hold all resources, including Dir
|
||||
TempDir filesys.ConfirmedDir
|
||||
}
|
||||
|
||||
// Getter is a function that can gets resource
|
||||
@@ -34,7 +31,7 @@ func newLoaderAtGetter(raw string, fSys filesys.FileSystem, referrer *fileLoader
|
||||
}
|
||||
|
||||
cleaner := func() error {
|
||||
return fSys.RemoveAll(rs.TempDir.String())
|
||||
return fSys.RemoveAll(rs.Dir.String())
|
||||
}
|
||||
|
||||
if err := getter(rs); err != nil {
|
||||
@@ -58,12 +55,12 @@ func newLoaderAtGetter(raw string, fSys filesys.FileSystem, referrer *fileLoader
|
||||
func getRemoteTarget(rs *remoteTargetSpec) error {
|
||||
var err error
|
||||
|
||||
rs.TempDir, err = filesys.NewTmpConfirmedDir()
|
||||
rs.Dir, err = filesys.NewTmpConfirmedDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rs.Dir = filesys.ConfirmedDir(rs.TempDir.Join("repo"))
|
||||
rs.Dir = filesys.ConfirmedDir(rs.Dir.Join("repo"))
|
||||
|
||||
// Get the pwd
|
||||
pwd, err := os.Getwd()
|
||||
|
||||
@@ -39,7 +39,7 @@ var (
|
||||
ExitOnError = &commands.ExitOnError
|
||||
)
|
||||
|
||||
// AddCommands adds the cfg and fn commands to kustomize.
|
||||
// AddCommands adds the cfg, fn and live commands to kustomize.
|
||||
func AddCommands(root *cobra.Command, name string) *cobra.Command {
|
||||
commands.ExitOnError = true
|
||||
|
||||
@@ -48,6 +48,7 @@ func AddCommands(root *cobra.Command, name string) *cobra.Command {
|
||||
|
||||
root.AddCommand(GetCfg(name))
|
||||
root.AddCommand(GetFn(name))
|
||||
root.AddCommand(GetLive(name))
|
||||
|
||||
root.AddCommand(&cobra.Command{
|
||||
Use: "docs-merge",
|
||||
|
||||
@@ -53,7 +53,7 @@ func GetLive(name string) *cobra.Command {
|
||||
|
||||
cmd.AddCommand(
|
||||
applyCmd,
|
||||
initcmd.NewCmdInit(f, ioStreams),
|
||||
initcmd.NewCmdInit(ioStreams),
|
||||
preview.GetPreviewRunner(f, ioStreams).Command,
|
||||
diff.NewCmdDiff(f, ioStreams),
|
||||
destroy.GetDestroyRunner(f, ioStreams).Command)
|
||||
|
||||
@@ -14,6 +14,8 @@ require (
|
||||
k8s.io/cli-runtime v0.17.3
|
||||
k8s.io/client-go v0.17.3
|
||||
k8s.io/kubectl v0.0.0-20191219154910-1528d4eea6dd
|
||||
sigs.k8s.io/cli-utils v0.20.1
|
||||
sigs.k8s.io/kustomize/kyaml v0.8.0
|
||||
sigs.k8s.io/cli-utils v0.19.0
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1
|
||||
)
|
||||
|
||||
replace sigs.k8s.io/kustomize/kyaml => ../../kyaml
|
||||
|
||||
@@ -283,8 +283,6 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
|
||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/2gBQ3RWajuToeY6ZtZTIKv2v7ThUy5KKusIT0yc0=
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4=
|
||||
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
@@ -617,8 +615,8 @@ modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
|
||||
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
|
||||
modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs=
|
||||
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
|
||||
sigs.k8s.io/cli-utils v0.20.1 h1:S3IM4Rmely9oWNAeytx46PhngTwdRxsJ4ixtoGWPc3o=
|
||||
sigs.k8s.io/cli-utils v0.20.1/go.mod h1:fQkEMmLXduMNmUf1+dgQ5GRJa4OrrLMw3qzAN8YXAU8=
|
||||
sigs.k8s.io/cli-utils v0.19.0 h1:lAoR5okhSV/dIusodaQp5VbDpHMcKnvjqKYHU+AB3a4=
|
||||
sigs.k8s.io/cli-utils v0.19.0/go.mod h1:B7KdqkSkHNIUn3cFbaR4aKUZMKtr+Benboi1w/HW/Fg=
|
||||
sigs.k8s.io/controller-runtime v0.4.0 h1:wATM6/m+3w8lj8FXNaO6Fs/rq/vqoOjO1Q116Z9NPsg=
|
||||
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
|
||||
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
|
||||
|
||||
@@ -4,12 +4,9 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
@@ -34,8 +31,6 @@ func NewAnnotateRunner(parent string) *AnnotateRunner {
|
||||
c.Flags().StringVar(&r.Name, "name", "", "Resource name to annotate")
|
||||
c.Flags().StringVar(&r.Namespace, "namespace", "", "Resource namespace to annotate")
|
||||
c.Flags().StringSliceVar(&r.Values, "kv", []string{}, "annotation as KEY=VALUE")
|
||||
c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", false,
|
||||
"add annotations recursively in all the nested subpackages")
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -44,14 +39,13 @@ func AnnotateCommand(parent string) *cobra.Command {
|
||||
}
|
||||
|
||||
type AnnotateRunner struct {
|
||||
Command *cobra.Command
|
||||
Values []string
|
||||
Kind string
|
||||
Name string
|
||||
ApiVersion string
|
||||
Namespace string
|
||||
Path string
|
||||
RecurseSubPackages bool
|
||||
Command *cobra.Command
|
||||
Values []string
|
||||
Kind string
|
||||
Name string
|
||||
ApiVersion string
|
||||
Namespace string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (r *AnnotateRunner) runE(c *cobra.Command, args []string) error {
|
||||
@@ -61,54 +55,16 @@ func (r *AnnotateRunner) runE(c *cobra.Command, args []string) error {
|
||||
rw := &kio.ByteReadWriter{Reader: c.InOrStdin(), Writer: c.OutOrStdout()}
|
||||
input = []kio.Reader{rw}
|
||||
output = []kio.Writer{rw}
|
||||
|
||||
return handleError(c, kio.Pipeline{
|
||||
Inputs: input,
|
||||
Filters: []kio.Filter{r},
|
||||
Outputs: output,
|
||||
}.Execute())
|
||||
} else {
|
||||
rw := &kio.LocalPackageReadWriter{PackagePath: args[0], NoDeleteFiles: true}
|
||||
input = []kio.Reader{rw}
|
||||
output = []kio.Writer{rw}
|
||||
}
|
||||
|
||||
e := executeCmdOnPkgs{
|
||||
writer: c.OutOrStdout(),
|
||||
needOpenAPI: false,
|
||||
recurseSubPackages: r.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
rootPkgPath: args[0],
|
||||
}
|
||||
|
||||
err := e.execute()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AnnotateRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rw := &kio.LocalPackageReadWriter{PackagePath: pkgPath, NoDeleteFiles: true, PackageFileName: openAPIFileName}
|
||||
input := []kio.Reader{rw}
|
||||
output := []kio.Writer{rw}
|
||||
err = kio.Pipeline{
|
||||
return handleError(c, kio.Pipeline{
|
||||
Inputs: input,
|
||||
Filters: []kio.Filter{r},
|
||||
Outputs: output,
|
||||
}.Execute()
|
||||
if err != nil {
|
||||
// return err if there is only package
|
||||
if !r.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if there are multiple packages to annotate
|
||||
fmt.Fprintf(w, "%s\n", err.Error())
|
||||
}
|
||||
} else {
|
||||
fmt.Fprint(w, "added annotations in the package\n")
|
||||
}
|
||||
return nil
|
||||
}.Execute())
|
||||
}
|
||||
|
||||
func (r *AnnotateRunner) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
|
||||
@@ -12,9 +12,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
)
|
||||
|
||||
func TestAnnotateCommand(t *testing.T) {
|
||||
@@ -488,77 +486,3 @@ spec:
|
||||
replicas: 3
|
||||
`
|
||||
)
|
||||
|
||||
func TestAnnotateSubPackages(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
dataset string
|
||||
packagePath string
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "annotate-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
args: []string{"--kv", "foo=bar", "-R"},
|
||||
expected: `${baseDir}/mysql/
|
||||
added annotations in the package
|
||||
|
||||
${baseDir}/mysql/storage/
|
||||
added annotations in the package
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "annotate-top-level-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql",
|
||||
args: []string{"--kv", "foo=bar"},
|
||||
expected: `${baseDir}/mysql/
|
||||
added annotations in the package
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "annotate-nested-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql/storage",
|
||||
args: []string{"--kv", "foo=bar"},
|
||||
expected: `${baseDir}/mysql/storage/
|
||||
added annotations in the package
|
||||
`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// reset the openAPI afterward
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
sourceDir := filepath.Join("test", "testdata", test.dataset)
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
copyutil.CopyDir(sourceDir, baseDir)
|
||||
defer os.RemoveAll(baseDir)
|
||||
runner := NewAnnotateRunner("")
|
||||
actual := &bytes.Buffer{}
|
||||
runner.Command.SetOut(actual)
|
||||
runner.Command.SetArgs(append([]string{filepath.Join(baseDir, test.packagePath)}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expected, "\\", "/", -1)
|
||||
if !assert.Equal(t, expectedNormalized, actualNormalized) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,9 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
@@ -21,14 +19,15 @@ import (
|
||||
func GetCatRunner(name string) *CatRunner {
|
||||
r := &CatRunner{}
|
||||
c := &cobra.Command{
|
||||
Use: "cat DIR",
|
||||
Use: "cat DIR...",
|
||||
Short: commands.CatShort,
|
||||
Long: commands.CatLong,
|
||||
Example: commands.CatExamples,
|
||||
RunE: r.runE,
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
}
|
||||
fixDocs(name, c)
|
||||
c.Flags().BoolVar(&r.IncludeSubpackages, "include-subpackages", true,
|
||||
"also print resources from subpackages.")
|
||||
c.Flags().BoolVar(&r.Format, "format", true,
|
||||
"format resource config yaml before printing.")
|
||||
c.Flags().BoolVar(&r.KeepAnnotations, "annotate", false,
|
||||
@@ -50,8 +49,6 @@ func GetCatRunner(name string) *CatRunner {
|
||||
"if true, exclude non-local-config in the output.")
|
||||
c.Flags().StringVar(&r.OutputDest, "dest", "",
|
||||
"if specified, write output to a file rather than stdout")
|
||||
c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", true,
|
||||
"print resources recursively in all the nested subpackages")
|
||||
r.Command = c
|
||||
return r
|
||||
}
|
||||
@@ -62,6 +59,7 @@ func CatCommand(name string) *cobra.Command {
|
||||
|
||||
// CatRunner contains the run function
|
||||
type CatRunner struct {
|
||||
IncludeSubpackages bool
|
||||
Format bool
|
||||
KeepAnnotations bool
|
||||
WrapKind string
|
||||
@@ -73,102 +71,56 @@ type CatRunner struct {
|
||||
IncludeLocal bool
|
||||
ExcludeNonLocal bool
|
||||
Command *cobra.Command
|
||||
RecurseSubPackages bool
|
||||
}
|
||||
|
||||
func (r *CatRunner) runE(c *cobra.Command, args []string) error {
|
||||
var writer = c.OutOrStdout()
|
||||
if r.OutputDest != "" {
|
||||
o, err := os.Create(r.OutputDest)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
defer o.Close()
|
||||
writer = o
|
||||
}
|
||||
if len(args) == 0 {
|
||||
input := &kio.ByteReader{Reader: c.InOrStdin()}
|
||||
// if there is a function-config specified, emit it
|
||||
outputs, err := r.out(writer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return handleError(c, kio.Pipeline{Inputs: []kio.Reader{input}, Filters: r.catFilters(), Outputs: outputs}.Execute())
|
||||
}
|
||||
|
||||
e := executeCmdOnPkgs{
|
||||
writer: writer,
|
||||
needOpenAPI: false,
|
||||
recurseSubPackages: r.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
rootPkgPath: args[0],
|
||||
skipPkgPathPrint: true,
|
||||
}
|
||||
|
||||
return e.execute()
|
||||
}
|
||||
|
||||
func (r *CatRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input := kio.LocalPackageReader{PackagePath: pkgPath, PackageFileName: openAPIFileName}
|
||||
outputs, err := r.out(w)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = kio.Pipeline{
|
||||
Inputs: []kio.Reader{input},
|
||||
Filters: r.catFilters(),
|
||||
Outputs: outputs,
|
||||
}.Execute()
|
||||
|
||||
if err != nil {
|
||||
// return err if there is only package
|
||||
if !r.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if there are multiple packages to annotate
|
||||
fmt.Fprintf(w, "%s in package %q\n", err.Error(), pkgPath)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(w, "---")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *CatRunner) catFilters() []kio.Filter {
|
||||
var fltrs []kio.Filter
|
||||
// don't include reconcilers
|
||||
fltrs = append(fltrs, &filters.IsLocalConfig{
|
||||
IncludeLocalConfig: r.IncludeLocal,
|
||||
ExcludeNonLocalConfig: r.ExcludeNonLocal,
|
||||
})
|
||||
if r.Format {
|
||||
fltrs = append(fltrs, filters.FormatFilter{})
|
||||
}
|
||||
if r.StripComments {
|
||||
fltrs = append(fltrs, filters.StripCommentsFilter{})
|
||||
}
|
||||
return fltrs
|
||||
}
|
||||
|
||||
func (r *CatRunner) out(w io.Writer) ([]kio.Writer, error) {
|
||||
var outputs []kio.Writer
|
||||
// if there is a function-config specified, emit it
|
||||
var functionConfig *yaml.RNode
|
||||
if r.FunctionConfig != "" {
|
||||
configs, err := kio.LocalPackageReader{PackagePath: r.FunctionConfig,
|
||||
OmitReaderAnnotations: !r.KeepAnnotations}.Read()
|
||||
if err != nil {
|
||||
return outputs, err
|
||||
return err
|
||||
}
|
||||
if len(configs) != 1 {
|
||||
return outputs, fmt.Errorf("expected exactly 1 functionConfig, found %d", len(configs))
|
||||
return fmt.Errorf("expected exactly 1 functionConfig, found %d", len(configs))
|
||||
}
|
||||
functionConfig = configs[0]
|
||||
}
|
||||
|
||||
var inputs []kio.Reader
|
||||
for _, a := range args {
|
||||
inputs = append(inputs, kio.LocalPackageReader{
|
||||
PackagePath: a,
|
||||
IncludeSubpackages: r.IncludeSubpackages,
|
||||
})
|
||||
}
|
||||
if len(inputs) == 0 {
|
||||
inputs = append(inputs, &kio.ByteReader{Reader: c.InOrStdin()})
|
||||
}
|
||||
var fltr []kio.Filter
|
||||
// don't include reconcilers
|
||||
fltr = append(fltr, &filters.IsLocalConfig{
|
||||
IncludeLocalConfig: r.IncludeLocal,
|
||||
ExcludeNonLocalConfig: r.ExcludeNonLocal,
|
||||
})
|
||||
if r.Format {
|
||||
fltr = append(fltr, filters.FormatFilter{})
|
||||
}
|
||||
if r.StripComments {
|
||||
fltr = append(fltr, filters.StripCommentsFilter{})
|
||||
}
|
||||
|
||||
var out = c.OutOrStdout()
|
||||
if r.OutputDest != "" {
|
||||
o, err := os.Create(r.OutputDest)
|
||||
if err != nil {
|
||||
return handleError(c, errors.Wrap(err))
|
||||
}
|
||||
defer o.Close()
|
||||
out = o
|
||||
}
|
||||
|
||||
// remove this annotation explicitly, the ByteWriter won't clear it by
|
||||
// default because it doesn't set it
|
||||
clear := []string{"config.kubernetes.io/path"}
|
||||
@@ -176,8 +128,9 @@ func (r *CatRunner) out(w io.Writer) ([]kio.Writer, error) {
|
||||
clear = nil
|
||||
}
|
||||
|
||||
var outputs []kio.Writer
|
||||
outputs = append(outputs, kio.ByteWriter{
|
||||
Writer: w,
|
||||
Writer: out,
|
||||
KeepReaderAnnotations: r.KeepAnnotations,
|
||||
WrappingKind: r.WrapKind,
|
||||
WrappingAPIVersion: r.WrapApiVersion,
|
||||
@@ -186,5 +139,5 @@ func (r *CatRunner) out(w io.Writer) ([]kio.Writer, error) {
|
||||
ClearAnnotations: clear,
|
||||
})
|
||||
|
||||
return outputs, nil
|
||||
return handleError(c, kio.Pipeline{Inputs: inputs, Filters: fltr, Outputs: outputs}.Execute())
|
||||
}
|
||||
|
||||
@@ -8,13 +8,10 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
)
|
||||
|
||||
// TODO(pwittrock): write tests for reading / writing ResourceLists
|
||||
@@ -115,7 +112,7 @@ metadata:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
---`, b.String()) {
|
||||
`, b.String()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -171,7 +168,8 @@ metadata:
|
||||
annotations:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3`), 0600)
|
||||
replicas: 3
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
@@ -225,7 +223,7 @@ metadata:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
---`, b.String()) {
|
||||
`, b.String()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -307,7 +305,7 @@ metadata:
|
||||
image: gcr.io/example/reconciler:v1
|
||||
spec:
|
||||
replicas: 3
|
||||
---`, b.String()) {
|
||||
`, b.String()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -364,7 +362,8 @@ metadata:
|
||||
annotations:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3`), 0600)
|
||||
replicas: 3
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
@@ -421,7 +420,7 @@ metadata:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
---`, string(actual)) {
|
||||
`, string(actual)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -478,7 +477,8 @@ metadata:
|
||||
annotations:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3`), 0600)
|
||||
replicas: 3
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
@@ -536,132 +536,7 @@ metadata:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
---`, string(actual)) {
|
||||
`, string(actual)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestCatSubPackages(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
dataset string
|
||||
packagePath string
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "cat-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
expected: `# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: mysql-deployment
|
||||
namespace: myspace
|
||||
spec:
|
||||
replicas: 3
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: mysql
|
||||
image: mysql:1.7.9
|
||||
---
|
||||
# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: storage-deployment
|
||||
namespace: myspace
|
||||
spec:
|
||||
replicas: 4
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: storage
|
||||
image: storage:1.7.7
|
||||
---`,
|
||||
},
|
||||
{
|
||||
name: "cat-top-level-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
args: []string{"-R=false"},
|
||||
packagePath: "mysql",
|
||||
expected: `# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: mysql-deployment
|
||||
namespace: myspace
|
||||
spec:
|
||||
replicas: 3
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: mysql
|
||||
image: mysql:1.7.9
|
||||
---`,
|
||||
},
|
||||
{
|
||||
name: "cat-nested-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql/storage",
|
||||
args: []string{"-R=false"},
|
||||
expected: `# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: storage-deployment
|
||||
namespace: myspace
|
||||
spec:
|
||||
replicas: 4
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: storage
|
||||
image: storage:1.7.7
|
||||
---`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// reset the openAPI afterward
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
sourceDir := filepath.Join("test", "testdata", test.dataset)
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
copyutil.CopyDir(sourceDir, baseDir)
|
||||
defer os.RemoveAll(baseDir)
|
||||
runner := commands.GetCatRunner("")
|
||||
actual := &bytes.Buffer{}
|
||||
runner.Command.SetOut(actual)
|
||||
runner.Command.SetArgs(append([]string{filepath.Join(baseDir, test.packagePath)}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expected, "\\", "/", -1)
|
||||
if !assert.Equal(t, expectedNormalized, actualNormalized) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,7 @@ package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-openapi/spec"
|
||||
@@ -16,7 +13,9 @@ import (
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters2/settersutil"
|
||||
)
|
||||
@@ -60,8 +59,6 @@ func NewCreateSetterRunner(parent string) *CreateSetterRunner {
|
||||
set.Flags().StringVar(&r.SchemaPath, "schema-path", "",
|
||||
`openAPI schema file path for setter constraints -- file content `+
|
||||
`e.g. {"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}`)
|
||||
set.Flags().BoolVarP(&r.CreateSetter.RecurseSubPackages, "recurse-subpackages", "R", false,
|
||||
"creates setter recursively in all the nested subpackages")
|
||||
set.Flags().MarkHidden("version")
|
||||
fixDocs(parent, set)
|
||||
r.Command = set
|
||||
@@ -81,7 +78,7 @@ type CreateSetterRunner struct {
|
||||
}
|
||||
|
||||
func (r *CreateSetterRunner) runE(c *cobra.Command, args []string) error {
|
||||
return handleError(c, r.createSetter(c, args))
|
||||
return handleError(c, r.set(c, args))
|
||||
}
|
||||
|
||||
func (r *CreateSetterRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
@@ -114,6 +111,42 @@ func (r *CreateSetterRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
if setterVersion == "v2" {
|
||||
var err error
|
||||
r.OpenAPIFile, err = ext.GetOpenAPIFile(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := openapi.AddSchemaFromFile(r.OpenAPIFile); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// check if substitution with same name exists and throw error
|
||||
ref, err := spec.NewRef(fieldmeta.DefinitionsPrefix + fieldmeta.SubstitutionDefinitionPrefix + r.CreateSetter.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
subst, _ := openapi.Resolve(&ref)
|
||||
// if substitution already exists with the input setter name, throw error
|
||||
if subst != nil {
|
||||
return errors.Errorf("substitution with name %s already exists, "+
|
||||
"substitution and setter can't have same name", r.CreateSetter.Name)
|
||||
}
|
||||
|
||||
// check if setter with same name exists and throw error
|
||||
ref, err = spec.NewRef(fieldmeta.DefinitionsPrefix + fieldmeta.SetterDefinitionPrefix + r.CreateSetter.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
setter, _ := openapi.Resolve(&ref)
|
||||
// if setter already exists with the input setter name, throw error
|
||||
if setter != nil {
|
||||
return errors.Errorf("setter with name %s already exists, "+
|
||||
"if you want to modify it, please delete the existing setter and recreate it", r.CreateSetter.Name)
|
||||
}
|
||||
|
||||
r.CreateSetter.Description = r.Set.SetPartialField.Description
|
||||
r.CreateSetter.SetBy = r.Set.SetPartialField.SetBy
|
||||
r.CreateSetter.Type = r.Set.SetPartialField.Type
|
||||
@@ -177,20 +210,9 @@ func (r *CreateSetterRunner) processSchema() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *CreateSetterRunner) createSetter(c *cobra.Command, args []string) error {
|
||||
func (r *CreateSetterRunner) set(c *cobra.Command, args []string) error {
|
||||
if setterVersion == "v2" {
|
||||
e := executeCmdOnPkgs{
|
||||
needOpenAPI: true,
|
||||
writer: c.OutOrStdout(),
|
||||
rootPkgPath: args[0],
|
||||
recurseSubPackages: r.CreateSetter.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
}
|
||||
err := e.execute()
|
||||
if err != nil {
|
||||
return handleError(c, err)
|
||||
}
|
||||
return nil
|
||||
return r.CreateSetter.Create(r.OpenAPIFile, args[0])
|
||||
}
|
||||
|
||||
rw := &kio.LocalPackageReadWriter{PackagePath: args[0]}
|
||||
@@ -204,41 +226,6 @@ func (r *CreateSetterRunner) createSetter(c *cobra.Command, args []string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *CreateSetterRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.CreateSetter = settersutil.SetterCreator{
|
||||
Name: r.CreateSetter.Name,
|
||||
SetBy: r.CreateSetter.SetBy,
|
||||
Description: r.CreateSetter.Description,
|
||||
Type: r.CreateSetter.Type,
|
||||
Schema: r.CreateSetter.Schema,
|
||||
FieldName: r.CreateSetter.FieldName,
|
||||
FieldValue: r.CreateSetter.FieldValue,
|
||||
Required: r.CreateSetter.Required,
|
||||
RecurseSubPackages: r.CreateSetter.RecurseSubPackages,
|
||||
OpenAPIFileName: openAPIFileName,
|
||||
OpenAPIPath: filepath.Join(pkgPath, openAPIFileName),
|
||||
ResourcesPath: pkgPath,
|
||||
}
|
||||
|
||||
err = r.CreateSetter.Create()
|
||||
if err != nil {
|
||||
// return err if RecurseSubPackages is false
|
||||
if !r.CreateSetter.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if RecurseSubPackages is true
|
||||
fmt.Fprintf(w, "%s\n", err.Error())
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(w, "created setter %q\n", r.CreateSetter.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// schemaFromFile reads the contents from schemaPath and returns schema
|
||||
func schemaFromFile(schemaPath string) (*spec.Schema, error) {
|
||||
sc := &spec.Schema{}
|
||||
|
||||
@@ -7,13 +7,12 @@ import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
)
|
||||
|
||||
@@ -44,7 +43,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created setter "replicas"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -83,7 +81,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created setter "replicas"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -125,7 +122,7 @@ openAPI:
|
||||
- marker: ${my-tag-setter}
|
||||
ref: '#/definitions/io.k8s.cli.setters.my-tag-setter'
|
||||
`,
|
||||
err: `substitution with name "my-image" already exists, substitution and setter can't have same name`,
|
||||
err: "substitution with name my-image already exists, substitution and setter can't have same name",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -143,7 +140,7 @@ openAPI:
|
||||
name: my-image
|
||||
value: "nginx"
|
||||
`,
|
||||
err: `setter with name "my-image" already exists, if you want to modify it, please delete the existing setter and recreate it`,
|
||||
err: "setter with name my-image already exists, if you want to modify it, please delete the existing setter and recreate it",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -162,7 +159,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created setter "replicas"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -204,7 +200,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created setter replicas`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -259,7 +254,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created setter "list"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -360,7 +354,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created setter replicas`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -407,7 +400,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created setter "replicas"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -445,7 +437,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created setter "foo.bar"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -596,7 +587,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created setter "replicas"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -722,7 +712,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created setter "replicas"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -753,14 +742,13 @@ spec:
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
f, err := ioutil.TempFile("", "k8s-cli-")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
defer os.RemoveAll(baseDir)
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
f := filepath.Join(baseDir, "Krmfile")
|
||||
err = ioutil.WriteFile(f, []byte(test.inputOpenAPI), 0600)
|
||||
err = ioutil.WriteFile(f.Name(), []byte(test.inputOpenAPI), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -780,7 +768,13 @@ spec:
|
||||
test.args = append(test.args, "--schema-path", sch.Name())
|
||||
}
|
||||
|
||||
r, err := ioutil.TempFile(baseDir, "k8s-cli-*.yaml")
|
||||
old := ext.GetOpenAPIFile
|
||||
defer func() { ext.GetOpenAPIFile = old }()
|
||||
ext.GetOpenAPIFile = func(args []string) (s string, err error) {
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
r, err := ioutil.TempFile("", "k8s-cli-*.yaml")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -793,7 +787,7 @@ spec:
|
||||
runner := commands.NewCreateSetterRunner("")
|
||||
out := &bytes.Buffer{}
|
||||
runner.Command.SetOut(out)
|
||||
runner.Command.SetArgs(append([]string{baseDir}, test.args...))
|
||||
runner.Command.SetArgs(append([]string{r.Name()}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if test.err != "" {
|
||||
if !assert.NotNil(t, err) {
|
||||
@@ -807,14 +801,7 @@ spec:
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
expectedOut := strings.Replace(test.out, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expectedOut, "\\", "/", -1)
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(out.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
if !assert.Contains(t, actualNormalized, expectedNormalized) {
|
||||
if !assert.Equal(t, test.out, out.String()) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
@@ -828,7 +815,7 @@ spec:
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
actualOpenAPI, err := ioutil.ReadFile(f)
|
||||
actualOpenAPI, err := ioutil.ReadFile(f.Name())
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -840,92 +827,3 @@ spec:
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSetterSubPackages(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
dataset string
|
||||
packagePath string
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "create-setter-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
args: []string{"namespace", "myspace", "-R"},
|
||||
expected: `${baseDir}/mysql/
|
||||
created setter "namespace"
|
||||
|
||||
${baseDir}/mysql/storage/
|
||||
created setter "namespace"
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "create-setter-top-level-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql",
|
||||
args: []string{"namespace", "myspace"},
|
||||
expected: `${baseDir}/mysql/
|
||||
created setter "namespace"
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "create-setter-nested-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql/storage",
|
||||
args: []string{"namespace", "myspace"},
|
||||
expected: `${baseDir}/mysql/storage/
|
||||
created setter "namespace"
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "create-setter-already-exists",
|
||||
dataset: "dataset-with-setters",
|
||||
packagePath: "mysql",
|
||||
args: []string{"namespace", "myspace", "-R"},
|
||||
expected: `${baseDir}/mysql/
|
||||
setter with name "namespace" already exists, if you want to modify it, please delete the existing setter and recreate it
|
||||
|
||||
${baseDir}/mysql/nosetters/
|
||||
created setter "namespace"
|
||||
|
||||
${baseDir}/mysql/storage/
|
||||
setter with name "namespace" already exists, if you want to modify it, please delete the existing setter and recreate it
|
||||
`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// reset the openAPI afterward
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
sourceDir := filepath.Join("test", "testdata", test.dataset)
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
copyutil.CopyDir(sourceDir, baseDir)
|
||||
defer os.RemoveAll(baseDir)
|
||||
runner := commands.NewCreateSetterRunner("")
|
||||
actual := &bytes.Buffer{}
|
||||
runner.Command.SetOut(actual)
|
||||
runner.Command.SetArgs(append([]string{filepath.Join(baseDir, test.packagePath)}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expected, "\\", "/", -1)
|
||||
if !assert.Equal(t, expectedNormalized, actualNormalized) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,13 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-openapi/spec"
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters2/settersutil"
|
||||
)
|
||||
|
||||
@@ -17,10 +19,10 @@ import (
|
||||
func NewCreateSubstitutionRunner(parent string) *CreateSubstitutionRunner {
|
||||
r := &CreateSubstitutionRunner{}
|
||||
cs := &cobra.Command{
|
||||
Use: "create-subst DIR NAME",
|
||||
Args: cobra.ExactArgs(2),
|
||||
PreRun: r.preRun,
|
||||
RunE: r.runE,
|
||||
Use: "create-subst DIR NAME",
|
||||
Args: cobra.ExactArgs(2),
|
||||
PreRunE: r.preRunE,
|
||||
RunE: r.runE,
|
||||
}
|
||||
cs.Flags().StringVar(&r.CreateSubstitution.FieldName, "field", "",
|
||||
"name of the field to set -- e.g. --field image")
|
||||
@@ -28,8 +30,6 @@ func NewCreateSubstitutionRunner(parent string) *CreateSubstitutionRunner {
|
||||
"value of the field to create substitution for -- e.g. --field-value nginx:0.1.0")
|
||||
cs.Flags().StringVar(&r.CreateSubstitution.Pattern, "pattern", "",
|
||||
`substitution pattern -- e.g. --pattern \${my-image-setter}:\${my-tag-setter}`)
|
||||
cs.Flags().BoolVarP(&r.CreateSubstitution.RecurseSubPackages, "recurse-subpackages", "R", false,
|
||||
"creates substitution recursively in all the nested subpackages")
|
||||
_ = cs.MarkFlagRequired("pattern")
|
||||
_ = cs.MarkFlagRequired("field-value")
|
||||
fixDocs(parent, cs)
|
||||
@@ -49,52 +49,49 @@ type CreateSubstitutionRunner struct {
|
||||
}
|
||||
|
||||
func (r *CreateSubstitutionRunner) runE(c *cobra.Command, args []string) error {
|
||||
e := executeCmdOnPkgs{
|
||||
needOpenAPI: true,
|
||||
writer: c.OutOrStdout(),
|
||||
rootPkgPath: args[0],
|
||||
recurseSubPackages: r.CreateSubstitution.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
}
|
||||
err := e.execute()
|
||||
if err != nil {
|
||||
return handleError(c, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return handleError(c, r.CreateSubstitution.Create(r.OpenAPIFile, args[0]))
|
||||
}
|
||||
|
||||
func (r *CreateSubstitutionRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
func (r *CreateSubstitutionRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
var err error
|
||||
r.CreateSubstitution.Name = args[1]
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.CreateSubstitution = settersutil.SubstitutionCreator{
|
||||
Name: r.CreateSubstitution.Name,
|
||||
FieldName: r.CreateSubstitution.FieldName,
|
||||
FieldValue: r.CreateSubstitution.FieldValue,
|
||||
RecurseSubPackages: r.CreateSubstitution.RecurseSubPackages,
|
||||
Pattern: r.CreateSubstitution.Pattern,
|
||||
OpenAPIFileName: openAPIFileName,
|
||||
OpenAPIPath: filepath.Join(pkgPath, openAPIFileName),
|
||||
ResourcesPath: pkgPath,
|
||||
|
||||
r.OpenAPIFile, err = ext.GetOpenAPIFile(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.CreateSubstitution.Create()
|
||||
if err != nil {
|
||||
// return err if RecurseSubPackages is false
|
||||
if !r.CreateSubstitution.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if RecurseSubPackages is true
|
||||
fmt.Fprintf(w, "%s\n", err.Error())
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(w, "created substitution %q\n", r.CreateSubstitution.Name)
|
||||
if err := openapi.AddSchemaFromFile(r.OpenAPIFile); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// check if substitution with same name exists and throw error
|
||||
ref, err := spec.NewRef(fieldmeta.DefinitionsPrefix + fieldmeta.SubstitutionDefinitionPrefix + r.CreateSubstitution.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
subst, _ := openapi.Resolve(&ref)
|
||||
// if substitution already exists with the input substitution name, throw error
|
||||
if subst != nil {
|
||||
return errors.Errorf("substitution with name %s already exists", r.CreateSubstitution.Name)
|
||||
}
|
||||
|
||||
// check if setter with same name exists and throw error
|
||||
ref, err = spec.NewRef(fieldmeta.DefinitionsPrefix + fieldmeta.SetterDefinitionPrefix + r.CreateSubstitution.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
setter, _ := openapi.Resolve(&ref)
|
||||
// if setter already exists with input substitution name, throw error
|
||||
if setter != nil {
|
||||
return errors.Errorf(fmt.Sprintf("setter with name %s already exists, "+
|
||||
"substitution and setter can't have same name", r.CreateSubstitution.Name))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *CreateSubstitutionRunner) preRun(c *cobra.Command, args []string) {
|
||||
r.CreateSubstitution.Name = args[1]
|
||||
}
|
||||
|
||||
@@ -7,13 +7,12 @@ import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
)
|
||||
|
||||
@@ -63,7 +62,6 @@ openAPI:
|
||||
name: my-tag-setter
|
||||
value: "1.7.9"
|
||||
`,
|
||||
out: `created substitution "my-image-subst"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -125,7 +123,7 @@ openAPI:
|
||||
- marker: ${my-tag-setter}
|
||||
ref: '#/definitions/io.k8s.cli.setters.my-tag-setter'
|
||||
`,
|
||||
err: `substitution with name "my-image" already exists`,
|
||||
err: "substitution with name my-image already exists",
|
||||
},
|
||||
{
|
||||
name: "error if setter with same name exists",
|
||||
@@ -142,7 +140,7 @@ openAPI:
|
||||
name: my-image
|
||||
value: "nginx"
|
||||
`,
|
||||
err: `setter with name "my-image" already exists, substitution and setter can't have same name`,
|
||||
err: "setter with name my-image already exists, substitution and setter can't have same name",
|
||||
},
|
||||
{
|
||||
name: "substitution and create setters 1",
|
||||
@@ -167,7 +165,6 @@ spec:
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
`,
|
||||
out: `created substitution "my-image-subst"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -256,7 +253,6 @@ openAPI:
|
||||
- marker: ${my-tag-setter}
|
||||
ref: '#/definitions/io.k8s.cli.setters.my-tag-setter'
|
||||
`,
|
||||
out: `created substitution "my-nested-subst"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -345,7 +341,7 @@ spec:
|
||||
substVal: prefix-1234
|
||||
|
||||
`,
|
||||
err: `setters must have different name than the substitution: foo`,
|
||||
err: "setters must have different name than the substitution: foo",
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
@@ -355,18 +351,22 @@ spec:
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
f, err := ioutil.TempFile("", "k8s-cli-")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
defer os.RemoveAll(baseDir)
|
||||
f := filepath.Join(baseDir, "Krmfile")
|
||||
err = ioutil.WriteFile(f, []byte(test.inputOpenAPI), 0600)
|
||||
defer os.Remove(f.Name())
|
||||
err = ioutil.WriteFile(f.Name(), []byte(test.inputOpenAPI), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
old := ext.GetOpenAPIFile
|
||||
defer func() { ext.GetOpenAPIFile = old }()
|
||||
ext.GetOpenAPIFile = func(args []string) (s string, err error) {
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
r, err := ioutil.TempFile(baseDir, "k8s-cli-*.yaml")
|
||||
r, err := ioutil.TempFile("", "k8s-cli-*.yaml")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -379,7 +379,7 @@ spec:
|
||||
runner := commands.NewCreateSubstitutionRunner("")
|
||||
out := &bytes.Buffer{}
|
||||
runner.Command.SetOut(out)
|
||||
runner.Command.SetArgs(append([]string{baseDir}, test.args...))
|
||||
runner.Command.SetArgs(append([]string{r.Name()}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
|
||||
if test.err != "" {
|
||||
@@ -393,14 +393,7 @@ spec:
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
expectedOut := strings.Replace(test.out, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expectedOut, "\\", "/", -1)
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(out.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
if !assert.Contains(t, actualNormalized, expectedNormalized) {
|
||||
if !assert.Equal(t, test.out, out.String()) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
@@ -414,7 +407,7 @@ spec:
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
actualOpenAPI, err := ioutil.ReadFile(f)
|
||||
actualOpenAPI, err := ioutil.ReadFile(f.Name())
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -426,89 +419,3 @@ spec:
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSubstSubPackages(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
dataset string
|
||||
packagePath string
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "create-subst-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
args: []string{"image-tag", "--field-value", "mysql:1.7.9", "--pattern", "${image}:${tag}", "-R"},
|
||||
expected: `${baseDir}/mysql/
|
||||
created substitution "image-tag"
|
||||
|
||||
${baseDir}/mysql/storage/
|
||||
created substitution "image-tag"
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "create-subst-top-level-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql",
|
||||
args: []string{"image-tag", "--field-value", "mysql:1.7.9", "--pattern", "${image}:${tag}"},
|
||||
expected: `${baseDir}/mysql/
|
||||
created substitution "image-tag"`,
|
||||
},
|
||||
{
|
||||
name: "create-subst-nested-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql/storage",
|
||||
args: []string{"image-tag", "--field-value", "storage:1.7.9", "--pattern", "${image}:${tag}"},
|
||||
expected: `${baseDir}/mysql/storage/
|
||||
created substitution "image-tag"`,
|
||||
},
|
||||
{
|
||||
name: "create-subst-already-exists",
|
||||
dataset: "dataset-with-setters",
|
||||
packagePath: "mysql",
|
||||
args: []string{"image-tag", "--field-value", "mysql:1.7.9", "--pattern", "${image}:${tag}", "-R"},
|
||||
expected: `${baseDir}/mysql/
|
||||
substitution with name "image-tag" already exists
|
||||
|
||||
${baseDir}/mysql/nosetters/
|
||||
created substitution "image-tag"
|
||||
|
||||
${baseDir}/mysql/storage/
|
||||
created substitution "image-tag"`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// reset the openAPI afterward
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
sourceDir := filepath.Join("test", "testdata", test.dataset)
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
copyutil.CopyDir(sourceDir, baseDir)
|
||||
defer os.RemoveAll(baseDir)
|
||||
runner := commands.NewCreateSubstitutionRunner("")
|
||||
actual := &bytes.Buffer{}
|
||||
runner.Command.SetOut(actual)
|
||||
runner.Command.SetArgs(append([]string{filepath.Join(baseDir, test.packagePath)}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expected, "\\", "/", -1)
|
||||
if !assert.Equal(t, strings.TrimSpace(expectedNormalized), strings.TrimSpace(actualNormalized)) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,11 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters2/settersutil"
|
||||
)
|
||||
|
||||
@@ -27,8 +24,6 @@ func NewDeleteSetterRunner(parent string) *DeleteSetterRunner {
|
||||
PreRunE: r.preRunE,
|
||||
RunE: r.runE,
|
||||
}
|
||||
c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", false,
|
||||
"deletes setter recursively in all the nested subpackages")
|
||||
fixDocs(parent, c)
|
||||
r.Command = c
|
||||
|
||||
@@ -40,10 +35,9 @@ func DeleteSetterCommand(parent string) *cobra.Command {
|
||||
}
|
||||
|
||||
type DeleteSetterRunner struct {
|
||||
Command *cobra.Command
|
||||
DeleteSetter settersutil.DeleterCreator
|
||||
OpenAPIFile string
|
||||
RecurseSubPackages bool
|
||||
Command *cobra.Command
|
||||
DeleteSetter settersutil.DeleterCreator
|
||||
OpenAPIFile string
|
||||
}
|
||||
|
||||
func (r *DeleteSetterRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
@@ -56,49 +50,17 @@ func (r *DeleteSetterRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := openapi.AddSchemaFromFile(r.OpenAPIFile); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *DeleteSetterRunner) runE(c *cobra.Command, args []string) error {
|
||||
e := executeCmdOnPkgs{
|
||||
needOpenAPI: true,
|
||||
writer: c.OutOrStdout(),
|
||||
rootPkgPath: args[0],
|
||||
recurseSubPackages: r.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
}
|
||||
err := e.execute()
|
||||
if err != nil {
|
||||
return handleError(c, err)
|
||||
}
|
||||
return nil
|
||||
return handleError(c, r.delete(c, args))
|
||||
}
|
||||
|
||||
func (r *DeleteSetterRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.DeleteSetter = settersutil.DeleterCreator{
|
||||
Name: r.DeleteSetter.Name,
|
||||
DefinitionPrefix: fieldmeta.SetterDefinitionPrefix,
|
||||
RecurseSubPackages: r.RecurseSubPackages,
|
||||
OpenAPIFileName: openAPIFileName,
|
||||
OpenAPIPath: filepath.Join(pkgPath, openAPIFileName),
|
||||
ResourcesPath: pkgPath,
|
||||
}
|
||||
|
||||
err = r.DeleteSetter.Delete()
|
||||
if err != nil {
|
||||
// return err if RecurseSubPackages is false
|
||||
if !r.DeleteSetter.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if RecurseSubPackages is true
|
||||
fmt.Fprintf(w, "%s\n", err.Error())
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(w, "deleted setter %q\n", r.DeleteSetter.Name)
|
||||
}
|
||||
return nil
|
||||
func (r *DeleteSetterRunner) delete(c *cobra.Command, args []string) error {
|
||||
return r.DeleteSetter.Delete(r.OpenAPIFile, args[0])
|
||||
}
|
||||
|
||||
@@ -7,13 +7,12 @@ import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
)
|
||||
|
||||
@@ -53,7 +52,6 @@ openAPI:
|
||||
value: "3"
|
||||
setBy: me
|
||||
`,
|
||||
out: `deleted setter "replicas-setter"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -97,7 +95,6 @@ openAPI:
|
||||
name: image
|
||||
value: nginx
|
||||
`,
|
||||
out: `deleted setter "replicas-setter"`,
|
||||
expectedOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -161,7 +158,6 @@ spec:
|
||||
- "b"
|
||||
- "c"
|
||||
`,
|
||||
out: `deleted setter "list"`,
|
||||
expectedResources: `
|
||||
apiVersion: example.com/v1beta1
|
||||
kind: Example1
|
||||
@@ -230,7 +226,7 @@ metadata:
|
||||
spec:
|
||||
replicas: 3 # {"$openapi" : "replicas-setter"}
|
||||
`,
|
||||
err: `setter "image" does not exist`,
|
||||
err: `setter with name image does not exist`,
|
||||
},
|
||||
{
|
||||
name: "delete setter used in substitution error",
|
||||
@@ -291,7 +287,7 @@ openAPI:
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
`,
|
||||
err: `setter "image-name" is used in substitution "image", please delete the parent substitution first`,
|
||||
err: `setter is used in substitution image, please delete the parent substitution first`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
@@ -301,18 +297,24 @@ kind: Deployment
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
f, err := ioutil.TempFile("", "k8s-cli-")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
defer os.RemoveAll(baseDir)
|
||||
f := filepath.Join(baseDir, "Krmfile")
|
||||
err = ioutil.WriteFile(f, []byte(test.inputOpenAPI), 0600)
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
err = ioutil.WriteFile(f.Name(), []byte(test.inputOpenAPI), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
r, err := ioutil.TempFile(baseDir, "k8s-cli-*.yaml")
|
||||
old := ext.GetOpenAPIFile
|
||||
defer func() { ext.GetOpenAPIFile = old }()
|
||||
ext.GetOpenAPIFile = func(args []string) (s string, err error) {
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
r, err := ioutil.TempFile("", "k8s-cli-*.yaml")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -325,29 +327,21 @@ kind: Deployment
|
||||
runner := commands.NewDeleteSetterRunner("")
|
||||
out := &bytes.Buffer{}
|
||||
runner.Command.SetOut(out)
|
||||
runner.Command.SetArgs(append([]string{baseDir}, test.args...))
|
||||
runner.Command.SetArgs(append([]string{r.Name()}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
|
||||
if test.err != "" {
|
||||
if !assert.NotNil(t, err) {
|
||||
t.FailNow()
|
||||
} else {
|
||||
assert.Equal(t, err.Error(), test.err)
|
||||
return
|
||||
}
|
||||
assert.Equal(t, test.err, err.Error())
|
||||
return
|
||||
}
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNorm := strings.Replace(
|
||||
strings.Replace(out.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expectedOut := strings.Replace(test.out, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expectedOut, "\\", "/", -1)
|
||||
|
||||
if !assert.Contains(t, strings.TrimSpace(actualNorm), expectedNormalized) {
|
||||
if !assert.Equal(t, test.out, out.String()) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
@@ -361,7 +355,7 @@ kind: Deployment
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
actualOpenAPI, err := ioutil.ReadFile(f)
|
||||
actualOpenAPI, err := ioutil.ReadFile(f.Name())
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -373,80 +367,3 @@ kind: Deployment
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSetterSubPackages(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
dataset string
|
||||
packagePath string
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "delete-setter-recurse-subpackages",
|
||||
dataset: "dataset-with-setters",
|
||||
args: []string{"namespace", "-R"},
|
||||
expected: `${baseDir}/mysql/
|
||||
deleted setter "namespace"
|
||||
|
||||
${baseDir}/mysql/nosetters/
|
||||
setter "namespace" does not exist
|
||||
|
||||
${baseDir}/mysql/storage/
|
||||
deleted setter "namespace"
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "delete-setter-top-level-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-with-setters",
|
||||
packagePath: "mysql",
|
||||
args: []string{"namespace"},
|
||||
expected: `${baseDir}/mysql/
|
||||
deleted setter "namespace"
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "delete-setter-nested-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-with-setters",
|
||||
packagePath: "mysql/storage",
|
||||
args: []string{"namespace"},
|
||||
expected: `${baseDir}/mysql/storage/
|
||||
deleted setter "namespace"
|
||||
`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// reset the openAPI afterward
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
sourceDir := filepath.Join("test", "testdata", test.dataset)
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
copyutil.CopyDir(sourceDir, baseDir)
|
||||
//defer os.RemoveAll(baseDir)
|
||||
runner := commands.NewDeleteSetterRunner("")
|
||||
actual := &bytes.Buffer{}
|
||||
runner.Command.SetOut(actual)
|
||||
runner.Command.SetArgs(append([]string{filepath.Join(baseDir, test.packagePath)}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expected, "\\", "/", -1)
|
||||
if !assert.Equal(t, expectedNormalized, actualNormalized) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,10 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters2/settersutil"
|
||||
)
|
||||
|
||||
@@ -23,8 +20,6 @@ func NewDeleteSubstitutionRunner(parent string) *DeleteSubstitutionRunner {
|
||||
PreRunE: r.preRunE,
|
||||
RunE: r.runE,
|
||||
}
|
||||
c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", false,
|
||||
"deletes substitution recursively in all the nested subpackages")
|
||||
fixDocs(parent, c)
|
||||
r.Command = c
|
||||
|
||||
@@ -39,7 +34,6 @@ type DeleteSubstitutionRunner struct {
|
||||
Command *cobra.Command
|
||||
DeleteSubstitution settersutil.DeleterCreator
|
||||
OpenAPIFile string
|
||||
RecurseSubPackages bool
|
||||
}
|
||||
|
||||
func (r *DeleteSubstitutionRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
@@ -52,49 +46,17 @@ func (r *DeleteSubstitutionRunner) preRunE(c *cobra.Command, args []string) erro
|
||||
return err
|
||||
}
|
||||
|
||||
if err := openapi.AddSchemaFromFile(r.OpenAPIFile); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *DeleteSubstitutionRunner) runE(c *cobra.Command, args []string) error {
|
||||
e := executeCmdOnPkgs{
|
||||
needOpenAPI: true,
|
||||
writer: c.OutOrStdout(),
|
||||
rootPkgPath: args[0],
|
||||
recurseSubPackages: r.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
}
|
||||
err := e.execute()
|
||||
if err != nil {
|
||||
return handleError(c, err)
|
||||
}
|
||||
return nil
|
||||
return handleError(c, r.delete(c, args))
|
||||
}
|
||||
|
||||
func (r *DeleteSubstitutionRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.DeleteSubstitution = settersutil.DeleterCreator{
|
||||
Name: r.DeleteSubstitution.Name,
|
||||
DefinitionPrefix: fieldmeta.SubstitutionDefinitionPrefix,
|
||||
RecurseSubPackages: r.RecurseSubPackages,
|
||||
OpenAPIFileName: openAPIFileName,
|
||||
OpenAPIPath: filepath.Join(pkgPath, openAPIFileName),
|
||||
ResourcesPath: pkgPath,
|
||||
}
|
||||
|
||||
err = r.DeleteSubstitution.Delete()
|
||||
if err != nil {
|
||||
// return err if RecurseSubPackages is false
|
||||
if !r.DeleteSubstitution.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if RecurseSubPackages is true
|
||||
fmt.Fprintf(w, "%s\n", err.Error())
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(w, "deleted substitution %q\n", r.DeleteSubstitution.Name)
|
||||
}
|
||||
return nil
|
||||
func (r *DeleteSubstitutionRunner) delete(c *cobra.Command, args []string) error {
|
||||
return r.DeleteSubstitution.Delete(r.OpenAPIFile, args[0])
|
||||
}
|
||||
|
||||
@@ -7,13 +7,12 @@ import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
)
|
||||
|
||||
@@ -80,7 +79,6 @@ spec:
|
||||
- name: sidecar
|
||||
image: nginx:1.7.9 # {"$ref":"#/definitions/io.k8s.cli.substitutions.my.image"}
|
||||
`,
|
||||
out: `deleted substitution "my.image"`,
|
||||
expectedResources: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
@@ -171,7 +169,6 @@ spec:
|
||||
- name: sidecar
|
||||
image: nginx:1.7.9 # {"$openapi":"my-image-sub"}
|
||||
`,
|
||||
out: `deleted substitution "my-image-sub"`,
|
||||
expectedResources: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
@@ -300,7 +297,7 @@ openAPI:
|
||||
value: "3"
|
||||
setBy: me
|
||||
`,
|
||||
err: `substitution "my-image-sub-not-present" does not exist`,
|
||||
err: "substitution with name my-image-sub-not-present does not exist",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -418,7 +415,7 @@ spec:
|
||||
- name: sidecar
|
||||
image: nginx::1.7.9 # {"$openapi":"my-image-subst"}
|
||||
`,
|
||||
err: `substitution "my-image-subst" is used in substitution "my-nested-subst", please delete the parent substitution first`,
|
||||
err: "substitution is used in substitution my-nested-subst, please delete the parent substitution first",
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
@@ -428,18 +425,24 @@ spec:
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
f, err := ioutil.TempFile("", "k8s-cli-")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
defer os.RemoveAll(baseDir)
|
||||
f := filepath.Join(baseDir, "Krmfile")
|
||||
err = ioutil.WriteFile(f, []byte(test.inputOpenAPI), 0600)
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
err = ioutil.WriteFile(f.Name(), []byte(test.inputOpenAPI), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
r, err := ioutil.TempFile(baseDir, "k8s-cli-*.yaml")
|
||||
old := ext.GetOpenAPIFile
|
||||
defer func() { ext.GetOpenAPIFile = old }()
|
||||
ext.GetOpenAPIFile = func(args []string) (s string, err error) {
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
r, err := ioutil.TempFile("", "k8s-cli-*.yaml")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -452,38 +455,21 @@ spec:
|
||||
runner := commands.NewDeleteSubstitutionRunner("")
|
||||
out := &bytes.Buffer{}
|
||||
runner.Command.SetOut(out)
|
||||
runner.Command.SetArgs(append([]string{baseDir}, test.args...))
|
||||
runner.Command.SetArgs(append([]string{r.Name()}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
|
||||
if test.err != "" {
|
||||
if !assert.NotNil(t, err) {
|
||||
t.FailNow()
|
||||
} else {
|
||||
assert.Equal(t, err.Error(), test.err)
|
||||
return
|
||||
}
|
||||
assert.Equal(t, test.err, err.Error())
|
||||
return
|
||||
}
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNorm := strings.Replace(
|
||||
strings.Replace(out.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
expectedOut := strings.Replace(test.out, "${baseDir}", baseDir, -1)
|
||||
expectedNorm := strings.Replace(expectedOut, "\\", "/", -1)
|
||||
|
||||
if !assert.Contains(t, strings.TrimSpace(actualNorm), expectedNorm) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
actualOpenAPI, err := ioutil.ReadFile(f)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
if !assert.Equal(t,
|
||||
strings.TrimSpace(test.expectedOpenAPI),
|
||||
strings.TrimSpace(string(actualOpenAPI))) {
|
||||
if !assert.Equal(t, test.out, out.String()) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
@@ -494,74 +480,16 @@ spec:
|
||||
if !assert.Equal(t,
|
||||
strings.TrimSpace(test.expectedResources),
|
||||
strings.TrimSpace(string(actualResources))) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSubstitutionSubPackages(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
dataset string
|
||||
packagePath string
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "delete-substitution-recurse-subpackages",
|
||||
dataset: "dataset-with-setters",
|
||||
args: []string{"image-tag", "-R"},
|
||||
expected: `${baseDir}/mysql/
|
||||
deleted substitution "image-tag"
|
||||
|
||||
${baseDir}/mysql/nosetters/
|
||||
substitution "image-tag" does not exist
|
||||
|
||||
${baseDir}/mysql/storage/
|
||||
substitution "image-tag" does not exist
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "delete-setter-top-level-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-with-setters",
|
||||
packagePath: "mysql",
|
||||
args: []string{"image-tag"},
|
||||
expected: `${baseDir}/mysql/
|
||||
deleted substitution "image-tag"
|
||||
`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// reset the openAPI afterward
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
sourceDir := filepath.Join("test", "testdata", test.dataset)
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
copyutil.CopyDir(sourceDir, baseDir)
|
||||
//defer os.RemoveAll(baseDir)
|
||||
runner := commands.NewDeleteSubstitutionRunner("")
|
||||
actual := &bytes.Buffer{}
|
||||
runner.Command.SetOut(actual)
|
||||
runner.Command.SetArgs(append([]string{filepath.Join(baseDir, test.packagePath)}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expected, "\\", "/", -1)
|
||||
if !assert.Equal(t, expectedNormalized, actualNormalized) {
|
||||
return
|
||||
}
|
||||
|
||||
actualOpenAPI, err := ioutil.ReadFile(f.Name())
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
if !assert.Equal(t,
|
||||
strings.TrimSpace(test.expectedOpenAPI),
|
||||
strings.TrimSpace(string(actualOpenAPI))) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -7,14 +7,15 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
|
||||
"sigs.k8s.io/kustomize/kyaml/pathutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters2"
|
||||
)
|
||||
@@ -35,8 +36,6 @@ func NewListSettersRunner(parent string) *ListSettersRunner {
|
||||
"output as github markdown")
|
||||
c.Flags().BoolVar(&r.IncludeSubst, "include-subst", false,
|
||||
"include substitutions in the output")
|
||||
c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", true,
|
||||
"list setters recursively in all the nested subpackages")
|
||||
fixDocs(parent, c)
|
||||
r.Command = c
|
||||
return r
|
||||
@@ -47,12 +46,11 @@ func ListSettersCommand(parent string) *cobra.Command {
|
||||
}
|
||||
|
||||
type ListSettersRunner struct {
|
||||
Command *cobra.Command
|
||||
Lookup setters.LookupSetters
|
||||
List setters2.List
|
||||
Markdown bool
|
||||
IncludeSubst bool
|
||||
RecurseSubPackages bool
|
||||
Command *cobra.Command
|
||||
Lookup setters.LookupSetters
|
||||
List setters2.List
|
||||
Markdown bool
|
||||
IncludeSubst bool
|
||||
}
|
||||
|
||||
func (r *ListSettersRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
@@ -67,50 +65,48 @@ func (r *ListSettersRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
|
||||
func (r *ListSettersRunner) runE(c *cobra.Command, args []string) error {
|
||||
if setterVersion == "v2" {
|
||||
e := executeCmdOnPkgs{
|
||||
needOpenAPI: true,
|
||||
writer: c.OutOrStdout(),
|
||||
rootPkgPath: args[0],
|
||||
recurseSubPackages: r.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := e.execute()
|
||||
openAPIPaths, err := pathutil.SubDirsWithFile(args[0], openAPIFileName)
|
||||
if err != nil {
|
||||
return handleError(c, err)
|
||||
return err
|
||||
}
|
||||
if len(openAPIPaths) == 0 {
|
||||
return errors.Errorf("unable to find %s in %s", openAPIFileName, args[0])
|
||||
}
|
||||
|
||||
// list setters for all the subpackages with openAPI file paths
|
||||
for _, openAPIPath := range openAPIPaths {
|
||||
r.List = setters2.List{
|
||||
Name: r.List.Name,
|
||||
OpenAPIFileName: openAPIFileName,
|
||||
}
|
||||
resourcePath := strings.TrimSuffix(openAPIPath, openAPIFileName)
|
||||
fmt.Fprintf(c.OutOrStdout(), "%s\n", resourcePath)
|
||||
if err := r.ListSetters(c, openAPIPath, resourcePath); err != nil {
|
||||
return err
|
||||
}
|
||||
if r.IncludeSubst {
|
||||
if err := r.ListSubstitutions(c, openAPIPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return handleError(c, lookup(r.Lookup, c, args))
|
||||
}
|
||||
|
||||
func (r *ListSettersRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.List = setters2.List{
|
||||
Name: r.List.Name,
|
||||
OpenAPIFileName: openAPIFileName,
|
||||
}
|
||||
openAPIPath := filepath.Join(pkgPath, openAPIFileName)
|
||||
if err := r.ListSetters(w, openAPIPath, pkgPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if r.IncludeSubst {
|
||||
if err := r.ListSubstitutions(w, openAPIPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ListSettersRunner) ListSetters(w io.Writer, openAPIPath, resourcePath string) error {
|
||||
func (r *ListSettersRunner) ListSetters(c *cobra.Command, openAPIPath, resourcePath string) error {
|
||||
// use setters v2
|
||||
if err := r.List.ListSetters(openAPIPath, resourcePath); err != nil {
|
||||
return err
|
||||
}
|
||||
table := newTable(w, r.Markdown)
|
||||
table := newTable(c.OutOrStdout(), r.Markdown)
|
||||
table.SetHeader([]string{"NAME", "VALUE", "SET BY", "DESCRIPTION", "COUNT", "REQUIRED"})
|
||||
for i := range r.List.Setters {
|
||||
s := r.List.Setters[i]
|
||||
@@ -141,12 +137,12 @@ func (r *ListSettersRunner) ListSetters(w io.Writer, openAPIPath, resourcePath s
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ListSettersRunner) ListSubstitutions(w io.Writer, openAPIPath string) error {
|
||||
func (r *ListSettersRunner) ListSubstitutions(c *cobra.Command, openAPIPath string) error {
|
||||
// use setters v2
|
||||
if err := r.List.ListSubst(openAPIPath); err != nil {
|
||||
return err
|
||||
}
|
||||
table := newTable(w, r.Markdown)
|
||||
table := newTable(c.OutOrStdout(), r.Markdown)
|
||||
b := tablewriter.Border{Top: true}
|
||||
table.SetBorders(b)
|
||||
|
||||
|
||||
@@ -471,11 +471,9 @@ func TestListSettersSubPackages(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "list-replicas",
|
||||
dataset: "dataset-with-setters",
|
||||
dataset: "dataset1",
|
||||
args: []string{"--include-subst"},
|
||||
expected: `
|
||||
|
||||
test/testdata/dataset-with-setters/mysql/
|
||||
expected: `test/testdata/dataset1/mysql/
|
||||
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
|
||||
image mysql 1 No
|
||||
namespace myspace 1 No
|
||||
@@ -483,26 +481,11 @@ test/testdata/dataset-with-setters/mysql/
|
||||
--------------- ----------------- --------------
|
||||
SUBSTITUTION PATTERN REFERENCES
|
||||
image-tag ${image}:${tag} [image,tag]
|
||||
|
||||
test/testdata/dataset-with-setters/mysql/nosetters/
|
||||
test/testdata/dataset1/mysql/nosetters/
|
||||
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
|
||||
|
||||
test/testdata/dataset-with-setters/mysql/storage/
|
||||
test/testdata/dataset1/mysql/storage/
|
||||
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
|
||||
namespace myspace 1 No
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "list-replicas",
|
||||
dataset: "dataset-with-setters/mysql",
|
||||
args: []string{"--recurse-subpackages=false"},
|
||||
expected: `
|
||||
|
||||
test/testdata/dataset-with-setters/mysql/
|
||||
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
|
||||
image mysql 1 No
|
||||
namespace myspace 1 No
|
||||
tag 1.7.9 1 No
|
||||
`,
|
||||
},
|
||||
}
|
||||
@@ -526,7 +509,7 @@ test/testdata/dataset-with-setters/mysql/
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(actual.String(), "\\", "/", -1)
|
||||
|
||||
if !assert.Equal(t, strings.TrimSpace(test.expected), strings.TrimSpace(actualNormalized)) {
|
||||
if !assert.Equal(t, test.expected, actualNormalized) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -5,9 +5,7 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -41,8 +39,6 @@ func NewSetRunner(parent string) *SetRunner {
|
||||
"annotate the field with a description of its value")
|
||||
c.Flags().StringVar(&setterVersion, "version", "",
|
||||
"use this version of the setter format")
|
||||
c.Flags().BoolVarP(&r.Set.RecurseSubPackages, "recurse-subpackages", "R", false,
|
||||
"sets recursively in all the nested subpackages")
|
||||
c.Flags().MarkHidden("version")
|
||||
|
||||
return r
|
||||
@@ -130,23 +126,15 @@ func (r *SetRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SetRunner) runE(c *cobra.Command, args []string) error {
|
||||
if setterVersion == "v2" {
|
||||
e := executeCmdOnPkgs{
|
||||
needOpenAPI: true,
|
||||
writer: c.OutOrStdout(),
|
||||
rootPkgPath: args[0],
|
||||
recurseSubPackages: r.Set.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
}
|
||||
err := e.execute()
|
||||
if err != nil {
|
||||
return handleError(c, err)
|
||||
}
|
||||
return nil
|
||||
count, err := r.Set.Set(r.OpenAPIFile, args[0])
|
||||
fmt.Fprintf(c.OutOrStdout(), "set %d fields\n", count)
|
||||
return handleError(c, err)
|
||||
}
|
||||
if len(args) > 2 || c.Flag("values").Changed {
|
||||
return handleError(c, r.perform(c, args))
|
||||
@@ -154,38 +142,6 @@ func (r *SetRunner) runE(c *cobra.Command, args []string) error {
|
||||
return handleError(c, lookup(r.Lookup, c, args))
|
||||
}
|
||||
|
||||
func (r *SetRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.Set = settersutil.FieldSetter{
|
||||
Name: r.Set.Name,
|
||||
Value: r.Set.Value,
|
||||
ListValues: r.Set.ListValues,
|
||||
Description: r.Set.Description,
|
||||
SetBy: r.Set.SetBy,
|
||||
Count: 0,
|
||||
OpenAPIPath: filepath.Join(pkgPath, openAPIFileName),
|
||||
OpenAPIFileName: openAPIFileName,
|
||||
ResourcesPath: pkgPath,
|
||||
RecurseSubPackages: r.Set.RecurseSubPackages,
|
||||
}
|
||||
count, err := r.Set.Set()
|
||||
if err != nil {
|
||||
// return err if RecurseSubPackages is false
|
||||
if !r.Set.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if RecurseSubPackages is true
|
||||
fmt.Fprintf(w, "%s\n", err.Error())
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(w, "set %d field(s)\n", count)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func lookup(l setters.LookupSetters, c *cobra.Command, args []string) error {
|
||||
// lookup the setters
|
||||
err := kio.Pipeline{
|
||||
|
||||
@@ -7,13 +7,12 @@ import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
)
|
||||
|
||||
@@ -31,7 +30,7 @@ func TestSetCommand(t *testing.T) {
|
||||
{
|
||||
name: "set replicas",
|
||||
args: []string{"replicas", "4", "--description", "hi there", "--set-by", "pw"},
|
||||
out: "set 1 field(s)\n",
|
||||
out: "set 1 fields\n",
|
||||
inputOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -127,7 +126,7 @@ spec:
|
||||
{
|
||||
name: "set replicas no description",
|
||||
args: []string{"replicas", "4"},
|
||||
out: "set 1 field(s)\n",
|
||||
out: "set 1 fields\n",
|
||||
inputOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -175,7 +174,7 @@ spec:
|
||||
{
|
||||
name: "set image with value",
|
||||
args: []string{"tag", "1.8.1"},
|
||||
out: "set 1 field(s)\n",
|
||||
out: "set 1 fields\n",
|
||||
inputOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -510,7 +509,7 @@ list in body should have at most 2 items`,
|
||||
{
|
||||
name: "set replicas with value set by flag",
|
||||
args: []string{"replicas", "--values", "4", "--description", "hi there"},
|
||||
out: "set 1 field(s)\n",
|
||||
out: "set 1 fields\n",
|
||||
inputOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -606,7 +605,7 @@ spec:
|
||||
{
|
||||
name: "openAPI list values set by flag success",
|
||||
args: []string{"list", "--values", "10", "--values", "11"},
|
||||
out: "set 1 field(s)\n",
|
||||
out: "set 1 fields\n",
|
||||
inputOpenAPI: `
|
||||
kind: Kptfile
|
||||
openAPI:
|
||||
@@ -710,7 +709,7 @@ list in body should have at most 2 items`,
|
||||
{
|
||||
name: "nested substitution",
|
||||
args: []string{"my-image-setter", "ubuntu"},
|
||||
out: "set 2 field(s)\n",
|
||||
out: "set 2 fields\n",
|
||||
inputOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
@@ -1014,19 +1013,22 @@ spec:
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
f, err := ioutil.TempFile("", "k8s-cli-")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
defer os.RemoveAll(baseDir)
|
||||
|
||||
f := filepath.Join(baseDir, "Krmfile")
|
||||
err = ioutil.WriteFile(f, []byte(test.inputOpenAPI), 0600)
|
||||
defer os.Remove(f.Name())
|
||||
err = ioutil.WriteFile(f.Name(), []byte(test.inputOpenAPI), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
old := ext.GetOpenAPIFile
|
||||
defer func() { ext.GetOpenAPIFile = old }()
|
||||
ext.GetOpenAPIFile = func(args []string) (s string, err error) {
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
r, err := ioutil.TempFile(baseDir, "k8s-cli-*.yaml")
|
||||
r, err := ioutil.TempFile("", "k8s-cli-*.yaml")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -1039,7 +1041,7 @@ spec:
|
||||
runner := commands.NewSetRunner("")
|
||||
out := &bytes.Buffer{}
|
||||
runner.Command.SetOut(out)
|
||||
runner.Command.SetArgs(append([]string{baseDir}, test.args...))
|
||||
runner.Command.SetArgs(append([]string{r.Name()}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if test.errMsg != "" {
|
||||
if !assert.NotNil(t, err) {
|
||||
@@ -1054,7 +1056,7 @@ spec:
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if test.errMsg == "" && !assert.Contains(t, out.String(), strings.TrimSpace(test.out)) {
|
||||
if test.errMsg == "" && !assert.Equal(t, test.out, out.String()) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
@@ -1068,7 +1070,7 @@ spec:
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
actualOpenAPI, err := ioutil.ReadFile(f)
|
||||
actualOpenAPI, err := ioutil.ReadFile(f.Name())
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -1080,82 +1082,3 @@ spec:
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetSubPackages(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
dataset string
|
||||
packagePath string
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "set-recurse-subpackages",
|
||||
dataset: "dataset-with-setters",
|
||||
args: []string{"namespace", "otherspace", "-R"},
|
||||
expected: `${baseDir}/mysql/
|
||||
set 1 field(s)
|
||||
|
||||
${baseDir}/mysql/nosetters/
|
||||
setter "namespace" is not found
|
||||
|
||||
${baseDir}/mysql/storage/
|
||||
set 1 field(s)
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "set-top-level-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-with-setters",
|
||||
packagePath: "mysql",
|
||||
args: []string{"namespace", "otherspace"},
|
||||
expected: `${baseDir}/mysql/
|
||||
set 1 field(s)
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "set-nested-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-with-setters",
|
||||
packagePath: "mysql/storage",
|
||||
args: []string{"namespace", "otherspace"},
|
||||
expected: `${baseDir}/mysql/storage/
|
||||
set 1 field(s)
|
||||
`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// reset the openAPI afterward
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
sourceDir := filepath.Join("test", "testdata", test.dataset)
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
copyutil.CopyDir(sourceDir, baseDir)
|
||||
defer os.RemoveAll(baseDir)
|
||||
runner := commands.NewSetRunner("")
|
||||
actual := &bytes.Buffer{}
|
||||
runner.Command.SetOut(actual)
|
||||
runner.Command.SetArgs(append([]string{filepath.Join(baseDir, test.packagePath)}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(
|
||||
strings.Replace(expected, "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
if !assert.Equal(t, strings.TrimSpace(expectedNormalized), strings.TrimSpace(actualNormalized)) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ items:
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: mysql-deployment
|
||||
namespace: myspace
|
||||
namespace: myspace # {"$openapi":"namespace"}
|
||||
annotations:
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'config/mysql-deployment_deployment.yaml'
|
||||
@@ -192,7 +192,7 @@ items:
|
||||
spec:
|
||||
containers:
|
||||
- name: mysql
|
||||
image: mysql:1.7.9
|
||||
image: mysql:1.7.9 # {"$openapi":"image-tag"}
|
||||
- apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
@@ -212,7 +212,7 @@ items:
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: storage-deployment
|
||||
namespace: myspace
|
||||
namespace: myspace # {"$openapi":"namespace"}
|
||||
annotations:
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'config/storage-deployment_deployment.yaml'
|
||||
|
||||
@@ -5,11 +5,9 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/sets"
|
||||
@@ -19,18 +17,18 @@ import (
|
||||
func GetCountRunner(name string) *CountRunner {
|
||||
r := &CountRunner{}
|
||||
c := &cobra.Command{
|
||||
Use: "count [DIR]",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Use: "count DIR...",
|
||||
Short: commands.CountShort,
|
||||
Long: commands.CountLong,
|
||||
Example: commands.CountExamples,
|
||||
RunE: r.runE,
|
||||
}
|
||||
fixDocs(name, c)
|
||||
c.Flags().BoolVar(&r.IncludeSubpackages, "include-subpackages", true,
|
||||
"also print resources from subpackages.")
|
||||
c.Flags().BoolVar(&r.Kind, "kind", true,
|
||||
"count resources by kind.")
|
||||
c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", true,
|
||||
"prints count of resources recursively in all the nested subpackages")
|
||||
|
||||
r.Command = c
|
||||
return r
|
||||
}
|
||||
@@ -44,56 +42,20 @@ type CountRunner struct {
|
||||
IncludeSubpackages bool
|
||||
Kind bool
|
||||
Command *cobra.Command
|
||||
RecurseSubPackages bool
|
||||
}
|
||||
|
||||
func (r *CountRunner) runE(c *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
input := &kio.ByteReader{Reader: c.InOrStdin()}
|
||||
|
||||
return handleError(c, kio.Pipeline{
|
||||
Inputs: []kio.Reader{input},
|
||||
Outputs: r.out(c.OutOrStdout()),
|
||||
}.Execute())
|
||||
var inputs []kio.Reader
|
||||
for _, a := range args {
|
||||
inputs = append(inputs, kio.LocalPackageReader{
|
||||
PackagePath: a,
|
||||
IncludeSubpackages: r.IncludeSubpackages,
|
||||
})
|
||||
}
|
||||
if len(inputs) == 0 {
|
||||
inputs = append(inputs, &kio.ByteReader{Reader: c.InOrStdin()})
|
||||
}
|
||||
|
||||
e := executeCmdOnPkgs{
|
||||
writer: c.OutOrStdout(),
|
||||
needOpenAPI: false,
|
||||
recurseSubPackages: r.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
rootPkgPath: args[0],
|
||||
}
|
||||
|
||||
return e.execute()
|
||||
}
|
||||
|
||||
func (r *CountRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input := kio.LocalPackageReader{PackagePath: pkgPath, PackageFileName: openAPIFileName}
|
||||
|
||||
err = kio.Pipeline{
|
||||
Inputs: []kio.Reader{input},
|
||||
Outputs: r.out(w),
|
||||
}.Execute()
|
||||
|
||||
if err != nil {
|
||||
// return err if there is only package
|
||||
if !r.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if there are multiple packages to annotate
|
||||
fmt.Fprintf(w, "%s\n", err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *CountRunner) out(w io.Writer) []kio.Writer {
|
||||
var out []kio.Writer
|
||||
if r.Kind {
|
||||
out = append(out, kio.WriterFunc(func(nodes []*yaml.RNode) error {
|
||||
@@ -107,15 +69,20 @@ func (r *CountRunner) out(w io.Writer) []kio.Writer {
|
||||
order := k.List()
|
||||
sort.Strings(order)
|
||||
for _, k := range order {
|
||||
fmt.Fprintf(w, "%s: %d\n", k, count[k])
|
||||
fmt.Fprintf(c.OutOrStdout(), "%s: %d\n", k, count[k])
|
||||
}
|
||||
|
||||
return nil
|
||||
}))
|
||||
|
||||
} else {
|
||||
out = append(out, kio.WriterFunc(func(nodes []*yaml.RNode) error {
|
||||
fmt.Fprintf(w, "%d\n", len(nodes))
|
||||
fmt.Fprintf(c.OutOrStdout(), "%d\n", len(nodes))
|
||||
return nil
|
||||
}))
|
||||
}
|
||||
return out
|
||||
return handleError(c, kio.Pipeline{
|
||||
Inputs: inputs,
|
||||
Outputs: out,
|
||||
}.Execute())
|
||||
}
|
||||
|
||||
@@ -8,13 +8,10 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
)
|
||||
|
||||
func TestCountCommand_files(t *testing.T) {
|
||||
@@ -70,80 +67,7 @@ spec:
|
||||
return
|
||||
}
|
||||
|
||||
if !assert.Contains(t, b.String(), "Deployment: 2\nService: 1\n") {
|
||||
if !assert.Equal(t, "Deployment: 2\nService: 1\n", b.String()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountSubPackages(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
dataset string
|
||||
packagePath string
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "count-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
expected: `${baseDir}/mysql/
|
||||
Deployment: 1
|
||||
|
||||
${baseDir}/mysql/storage/
|
||||
Deployment: 1
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "count-top-level-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
args: []string{"-R=false"},
|
||||
packagePath: "mysql",
|
||||
expected: `${baseDir}/mysql/
|
||||
Deployment: 1
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "count-nested-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql/storage",
|
||||
args: []string{"-R=false"},
|
||||
expected: `${baseDir}/mysql/storage/
|
||||
Deployment: 1
|
||||
`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// reset the openAPI afterward
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
sourceDir := filepath.Join("test", "testdata", test.dataset)
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
copyutil.CopyDir(sourceDir, baseDir)
|
||||
defer os.RemoveAll(baseDir)
|
||||
runner := commands.GetCountRunner("")
|
||||
actual := &bytes.Buffer{}
|
||||
runner.Command.SetOut(actual)
|
||||
runner.Command.SetArgs(append([]string{filepath.Join(baseDir, test.packagePath)}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expected, "\\", "/", -1)
|
||||
if !assert.Equal(t, expectedNormalized, actualNormalized) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,7 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/filters"
|
||||
@@ -37,8 +33,6 @@ formatting substitution verbs {'%n': 'metadata.name', '%s': 'metadata.namespace'
|
||||
`if true, override existing filepath annotations.`)
|
||||
c.Flags().BoolVar(&r.UseSchema, "use-schema", false,
|
||||
`if true, uses openapi resource schema to format resources.`)
|
||||
c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", false,
|
||||
"formats resource files recursively in all the nested subpackages")
|
||||
r.Command = c
|
||||
return r
|
||||
}
|
||||
@@ -49,13 +43,12 @@ func FmtCommand(name string) *cobra.Command {
|
||||
|
||||
// FmtRunner contains the run function
|
||||
type FmtRunner struct {
|
||||
Command *cobra.Command
|
||||
FilenamePattern string
|
||||
SetFilenames bool
|
||||
KeepAnnotations bool
|
||||
Override bool
|
||||
UseSchema bool
|
||||
RecurseSubPackages bool
|
||||
Command *cobra.Command
|
||||
FilenamePattern string
|
||||
SetFilenames bool
|
||||
KeepAnnotations bool
|
||||
Override bool
|
||||
UseSchema bool
|
||||
}
|
||||
|
||||
func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
@@ -66,6 +59,17 @@ func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
func (r *FmtRunner) runE(c *cobra.Command, args []string) error {
|
||||
f := []kio.Filter{filters.FormatFilter{
|
||||
UseSchema: r.UseSchema,
|
||||
}}
|
||||
|
||||
// format with file names
|
||||
if r.SetFilenames {
|
||||
f = append(f, &filters.FileSetter{
|
||||
FilenamePattern: r.FilenamePattern,
|
||||
Override: r.Override,
|
||||
})
|
||||
}
|
||||
|
||||
// format stdin if there are no args
|
||||
if len(args) == 0 {
|
||||
@@ -75,64 +79,20 @@ func (r *FmtRunner) runE(c *cobra.Command, args []string) error {
|
||||
KeepReaderAnnotations: r.KeepAnnotations,
|
||||
}
|
||||
return handleError(c, kio.Pipeline{
|
||||
Inputs: []kio.Reader{rw}, Filters: r.fmtFilters(), Outputs: []kio.Writer{rw}}.Execute())
|
||||
Inputs: []kio.Reader{rw}, Filters: f, Outputs: []kio.Writer{rw}}.Execute())
|
||||
}
|
||||
|
||||
for _, rootPkgPath := range args {
|
||||
e := executeCmdOnPkgs{
|
||||
writer: c.OutOrStdout(),
|
||||
needOpenAPI: false,
|
||||
recurseSubPackages: r.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
rootPkgPath: rootPkgPath,
|
||||
}
|
||||
|
||||
err := e.execute()
|
||||
for i := range args {
|
||||
path := args[i]
|
||||
rw := &kio.LocalPackageReadWriter{
|
||||
NoDeleteFiles: true,
|
||||
PackagePath: path,
|
||||
KeepReaderAnnotations: r.KeepAnnotations}
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{rw}, Filters: f, Outputs: []kio.Writer{rw}}.Execute()
|
||||
if err != nil {
|
||||
return err
|
||||
return handleError(c, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *FmtRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rw := &kio.LocalPackageReadWriter{
|
||||
NoDeleteFiles: true,
|
||||
PackagePath: pkgPath,
|
||||
KeepReaderAnnotations: r.KeepAnnotations, PackageFileName: openAPIFileName}
|
||||
err = kio.Pipeline{
|
||||
Inputs: []kio.Reader{rw}, Filters: r.fmtFilters(), Outputs: []kio.Writer{rw}}.Execute()
|
||||
|
||||
if err != nil {
|
||||
// return err if RecurseSubPackages is false
|
||||
if !r.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if RecurseSubPackages is true
|
||||
fmt.Fprintf(w, "%s\n", err.Error())
|
||||
}
|
||||
} else {
|
||||
fmt.Fprint(w, "formatted resource files in the package\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *FmtRunner) fmtFilters() []kio.Filter {
|
||||
fmtFilters := []kio.Filter{filters.FormatFilter{
|
||||
UseSchema: r.UseSchema,
|
||||
}}
|
||||
|
||||
// format with file names
|
||||
if r.SetFilenames {
|
||||
fmtFilters = append(fmtFilters, &filters.FileSetter{
|
||||
FilenamePattern: r.FilenamePattern,
|
||||
Override: r.Override,
|
||||
})
|
||||
}
|
||||
return fmtFilters
|
||||
}
|
||||
|
||||
@@ -7,15 +7,12 @@ import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/filters/testyaml"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
"sigs.k8s.io/kustomize/kyaml/testutil"
|
||||
)
|
||||
|
||||
@@ -166,78 +163,3 @@ func TestCmd_failFileContents(t *testing.T) {
|
||||
// expect an error
|
||||
assert.EqualError(t, err, "yaml: line 1: did not find expected node content")
|
||||
}
|
||||
|
||||
func TestFmtSubPackages(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
dataset string
|
||||
packagePath string
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "fmt-recurse-subpackages",
|
||||
dataset: "dataset-with-setters",
|
||||
args: []string{"-R"},
|
||||
expected: `${baseDir}/mysql/
|
||||
formatted resource files in the package
|
||||
|
||||
${baseDir}/mysql/nosetters/
|
||||
formatted resource files in the package
|
||||
|
||||
${baseDir}/mysql/storage/
|
||||
formatted resource files in the package
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "fmt-top-level-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql",
|
||||
expected: `${baseDir}/mysql/
|
||||
formatted resource files in the package
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "fmt-nested-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql/storage",
|
||||
expected: `${baseDir}/mysql/storage/
|
||||
formatted resource files in the package
|
||||
`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// reset the openAPI afterward
|
||||
openapi.ResetOpenAPI()
|
||||
defer openapi.ResetOpenAPI()
|
||||
sourceDir := filepath.Join("test", "testdata", test.dataset)
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
copyutil.CopyDir(sourceDir, baseDir)
|
||||
defer os.RemoveAll(baseDir)
|
||||
runner := commands.GetFmtRunner("")
|
||||
actual := &bytes.Buffer{}
|
||||
runner.Command.SetOut(actual)
|
||||
runner.Command.SetArgs(append([]string{filepath.Join(baseDir, test.packagePath)}, test.args...))
|
||||
err = runner.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expected, "\\", "/", -1)
|
||||
if !assert.Equal(t, strings.TrimSpace(expectedNormalized), strings.TrimSpace(actualNormalized)) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,10 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/filters"
|
||||
@@ -20,21 +18,22 @@ import (
|
||||
func GetGrepRunner(name string) *GrepRunner {
|
||||
r := &GrepRunner{}
|
||||
c := &cobra.Command{
|
||||
Use: "grep QUERY [DIR]",
|
||||
Use: "grep QUERY [DIR]...",
|
||||
Short: commands.GrepShort,
|
||||
Long: commands.GrepLong,
|
||||
Example: commands.GrepExamples,
|
||||
PreRunE: r.preRunE,
|
||||
RunE: r.runE,
|
||||
Args: cobra.MaximumNArgs(2),
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
}
|
||||
fixDocs(name, c)
|
||||
c.Flags().BoolVar(&r.IncludeSubpackages, "include-subpackages", true,
|
||||
"also print resources from subpackages.")
|
||||
c.Flags().BoolVar(&r.KeepAnnotations, "annotate", true,
|
||||
"annotate resources with their file origins.")
|
||||
c.Flags().BoolVarP(&r.InvertMatch, "invert-match", "", false,
|
||||
"Selected Resources are those not matching any of the specified patterns..")
|
||||
c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", true,
|
||||
"also print resources recursively in all the nested subpackages")
|
||||
|
||||
r.Command = c
|
||||
return r
|
||||
}
|
||||
@@ -45,11 +44,11 @@ func GrepCommand(name string) *cobra.Command {
|
||||
|
||||
// GrepRunner contains the run function
|
||||
type GrepRunner struct {
|
||||
KeepAnnotations bool
|
||||
Command *cobra.Command
|
||||
IncludeSubpackages bool
|
||||
KeepAnnotations bool
|
||||
Command *cobra.Command
|
||||
filters.GrepFilter
|
||||
Format bool
|
||||
RecurseSubPackages bool
|
||||
Format bool
|
||||
}
|
||||
|
||||
func (r *GrepRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
@@ -102,57 +101,25 @@ func (r *GrepRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
func (r *GrepRunner) runE(c *cobra.Command, args []string) error {
|
||||
if len(args) == 1 {
|
||||
input := &kio.ByteReader{Reader: c.InOrStdin()}
|
||||
return handleError(c, kio.Pipeline{
|
||||
Inputs: []kio.Reader{input},
|
||||
Filters: []kio.Filter{r.GrepFilter},
|
||||
Outputs: []kio.Writer{kio.ByteWriter{
|
||||
Writer: c.OutOrStdout(),
|
||||
KeepReaderAnnotations: r.KeepAnnotations,
|
||||
}},
|
||||
}.Execute())
|
||||
var filters = []kio.Filter{r.GrepFilter}
|
||||
|
||||
var inputs []kio.Reader
|
||||
for _, a := range args[1:] {
|
||||
inputs = append(inputs, kio.LocalPackageReader{
|
||||
PackagePath: a,
|
||||
IncludeSubpackages: r.IncludeSubpackages,
|
||||
})
|
||||
}
|
||||
if len(inputs) == 0 {
|
||||
inputs = append(inputs, &kio.ByteReader{Reader: c.InOrStdin()})
|
||||
}
|
||||
|
||||
e := executeCmdOnPkgs{
|
||||
writer: c.OutOrStdout(),
|
||||
needOpenAPI: false,
|
||||
recurseSubPackages: r.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
rootPkgPath: args[1],
|
||||
skipPkgPathPrint: true,
|
||||
}
|
||||
|
||||
return e.execute()
|
||||
|
||||
}
|
||||
|
||||
func (r *GrepRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input := kio.LocalPackageReader{PackagePath: pkgPath, PackageFileName: openAPIFileName}
|
||||
|
||||
err = kio.Pipeline{
|
||||
Inputs: []kio.Reader{input},
|
||||
Filters: []kio.Filter{r.GrepFilter},
|
||||
return handleError(c, kio.Pipeline{
|
||||
Inputs: inputs,
|
||||
Filters: filters,
|
||||
Outputs: []kio.Writer{kio.ByteWriter{
|
||||
Writer: w,
|
||||
Writer: c.OutOrStdout(),
|
||||
KeepReaderAnnotations: r.KeepAnnotations,
|
||||
}},
|
||||
}.Execute()
|
||||
|
||||
if err != nil {
|
||||
// return err if there is only package
|
||||
if !r.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if there are multiple packages to annotate
|
||||
fmt.Fprintf(w, "%s\n", err.Error())
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(w, "---")
|
||||
return nil
|
||||
}.Execute())
|
||||
}
|
||||
|
||||
@@ -8,12 +8,10 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
)
|
||||
|
||||
// TestGrepCommand_files verifies grep reads the files and filters them
|
||||
@@ -70,7 +68,7 @@ spec:
|
||||
return
|
||||
}
|
||||
|
||||
if !assert.Contains(t, b.String(), `kind: Deployment
|
||||
if !assert.Equal(t, `kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx2
|
||||
@@ -92,7 +90,7 @@ metadata:
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
`) {
|
||||
`, b.String()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -138,7 +136,7 @@ spec:
|
||||
return
|
||||
}
|
||||
|
||||
if !assert.Contains(t, b.String(), `kind: Deployment
|
||||
if !assert.Equal(t, `kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx2
|
||||
@@ -158,7 +156,7 @@ metadata:
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
`) {
|
||||
`, b.String()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -252,7 +250,7 @@ spec:
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
if !assert.Contains(t, b.String(), `kind: Deployment
|
||||
if !assert.Equal(t, `kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx1.7
|
||||
@@ -266,142 +264,7 @@ spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
`) {
|
||||
`, b.String()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrepSubPackages(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
dataset string
|
||||
packagePath string
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "grep-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
args: []string{"kind=Deployment"},
|
||||
expected: `# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
namespace: myspace
|
||||
name: mysql-deployment
|
||||
annotations:
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'deployment.yaml'
|
||||
spec:
|
||||
replicas: 3
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: mysql
|
||||
image: mysql:1.7.9
|
||||
---
|
||||
# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
namespace: myspace
|
||||
name: storage-deployment
|
||||
annotations:
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'deployment.yaml'
|
||||
spec:
|
||||
replicas: 4
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: storage
|
||||
image: storage:1.7.7
|
||||
---`,
|
||||
},
|
||||
{
|
||||
name: "grep-top-level-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
args: []string{"kind=Deployment", "-R=false"},
|
||||
packagePath: "mysql",
|
||||
expected: `# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
namespace: myspace
|
||||
name: mysql-deployment
|
||||
annotations:
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'deployment.yaml'
|
||||
spec:
|
||||
replicas: 3
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: mysql
|
||||
image: mysql:1.7.9
|
||||
---`,
|
||||
},
|
||||
{
|
||||
name: "grep-nested-pkg-no-recurse-subpackages",
|
||||
dataset: "dataset-without-setters",
|
||||
packagePath: "mysql/storage",
|
||||
args: []string{"kind=Deployment", "-R=false"},
|
||||
expected: `# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
namespace: myspace
|
||||
name: storage-deployment
|
||||
annotations:
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'deployment.yaml'
|
||||
spec:
|
||||
replicas: 4
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: storage
|
||||
image: storage:1.7.7
|
||||
---`,
|
||||
},
|
||||
}
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
sourceDir := filepath.Join("test", "testdata", test.dataset)
|
||||
baseDir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
copyutil.CopyDir(sourceDir, baseDir)
|
||||
defer os.RemoveAll(baseDir)
|
||||
runner := commands.GetGrepRunner("")
|
||||
actual := &bytes.Buffer{}
|
||||
runner.Command.SetOut(actual)
|
||||
runner.Command.SetArgs(append(test.args, filepath.Join(baseDir, test.packagePath)))
|
||||
err = runner.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expected, "${baseDir}", baseDir, -1)
|
||||
expectedNormalized := strings.Replace(expected, "\\", "/", -1)
|
||||
if !assert.Equal(t, expectedNormalized, actualNormalized) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,13 +9,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/runfn"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
)
|
||||
|
||||
// GetCatRunner returns a RunFnRunner.
|
||||
@@ -67,11 +65,6 @@ func GetRunFnRunner(name string) *RunFnRunner {
|
||||
r.Command.Flags().StringArrayVar(
|
||||
&r.Mounts, "mount", []string{},
|
||||
"a list of storage options read from the filesystem")
|
||||
r.Command.Flags().BoolVar(
|
||||
&r.LogSteps, "log-steps", false, "log steps to stderr")
|
||||
r.Command.Flags().StringArrayVarP(
|
||||
&r.Env, "env", "e", []string{},
|
||||
"a list of environment variables to be used by functions")
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -98,8 +91,6 @@ type RunFnRunner struct {
|
||||
Network bool
|
||||
NetworkName string
|
||||
Mounts []string
|
||||
LogSteps bool
|
||||
Env []string
|
||||
}
|
||||
|
||||
func (r *RunFnRunner) runE(c *cobra.Command, args []string) error {
|
||||
@@ -108,7 +99,7 @@ func (r *RunFnRunner) runE(c *cobra.Command, args []string) error {
|
||||
|
||||
// getContainerFunctions parses the commandline flags and arguments into explicit
|
||||
// Functions to run.
|
||||
func (r *RunFnRunner) getContainerFunctions(c *cobra.Command, dataItems []string) (
|
||||
func (r *RunFnRunner) getContainerFunctions(c *cobra.Command, args, dataItems []string) (
|
||||
[]*yaml.RNode, error) {
|
||||
|
||||
if r.Image == "" && r.StarPath == "" && r.ExecPath == "" && r.StarURL == "" {
|
||||
@@ -202,7 +193,7 @@ data: {}
|
||||
}
|
||||
err = rc.PipeE(
|
||||
yaml.LookupCreate(yaml.MappingNode, "metadata", "annotations"),
|
||||
yaml.SetField(runtimeutil.FunctionAnnotationKey, yaml.NewScalarRNode(value)))
|
||||
yaml.SetField("config.kubernetes.io/function", yaml.NewScalarRNode(value)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -276,7 +267,7 @@ func (r *RunFnRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
return errors.Errorf("0 or 1 arguments supported, function arguments go after '--'")
|
||||
}
|
||||
|
||||
fns, err := r.getContainerFunctions(c, dataItems)
|
||||
fns, err := r.getContainerFunctions(c, args, dataItems)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -314,8 +305,6 @@ func (r *RunFnRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
EnableExec: r.EnableExec,
|
||||
StorageMounts: storageMounts,
|
||||
ResultsDir: r.ResultsDir,
|
||||
LogSteps: r.LogSteps,
|
||||
Env: r.Env,
|
||||
}
|
||||
|
||||
// don't consider args for the function
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/runfn"
|
||||
)
|
||||
|
||||
@@ -204,7 +203,6 @@ apiVersion: v1
|
||||
Path: "dir",
|
||||
NetworkName: "bridge",
|
||||
EnableStarlark: true,
|
||||
Env: []string{},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -258,7 +256,6 @@ apiVersion: v1
|
||||
Path: "dir",
|
||||
NetworkName: "bridge",
|
||||
ResultsDir: "foo/",
|
||||
Env: []string{},
|
||||
},
|
||||
expected: `
|
||||
metadata:
|
||||
@@ -286,27 +283,6 @@ apiVersion: v1
|
||||
args: []string{"run", "dir", "--image", "foo:bar", "--", "a=b", "c", "e=f"},
|
||||
err: "must have keys and values separated by",
|
||||
},
|
||||
{
|
||||
name: "log steps",
|
||||
args: []string{"run", "dir", "--log-steps"},
|
||||
path: "dir",
|
||||
expectedStruct: &runfn.RunFns{
|
||||
Path: "dir",
|
||||
NetworkName: "bridge",
|
||||
LogSteps: true,
|
||||
Env: []string{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "envs",
|
||||
args: []string{"run", "dir", "--env", "FOO=BAR", "-e", "BAR"},
|
||||
path: "dir",
|
||||
expectedStruct: &runfn.RunFns{
|
||||
Path: "dir",
|
||||
NetworkName: "bridge",
|
||||
Env: []string{"FOO=BAR", "BAR"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i := range tests {
|
||||
@@ -409,4 +385,5 @@ apiVersion: v1
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
apiVersion: krm.dev/v1alpha1
|
||||
kind: Krmfile
|
||||
metadata:
|
||||
name: mysql
|
||||
packageMetadata:
|
||||
shortDescription: sample description
|
||||
@@ -1,15 +0,0 @@
|
||||
# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
namespace: myspace
|
||||
name: mysql-deployment
|
||||
spec:
|
||||
replicas: 3
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: mysql
|
||||
image: mysql:1.7.9
|
||||
@@ -1,6 +0,0 @@
|
||||
apiVersion: krm.dev/v1alpha1
|
||||
kind: Krmfile
|
||||
metadata:
|
||||
name: storage
|
||||
packageMetadata:
|
||||
shortDescription: sample description
|
||||
@@ -1,15 +0,0 @@
|
||||
# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
namespace: myspace
|
||||
name: storage-deployment
|
||||
spec:
|
||||
replicas: 4
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: storage
|
||||
image: storage:1.7.7
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/filters"
|
||||
|
||||
@@ -27,6 +26,8 @@ func GetTreeRunner(name string) *TreeRunner {
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
}
|
||||
fixDocs(name, c)
|
||||
c.Flags().BoolVar(&r.IncludeSubpackages, "include-subpackages", true,
|
||||
"also print resources from subpackages.")
|
||||
|
||||
// TODO(pwittrock): Figure out if these are the right things to expose, and consider making it
|
||||
// a list of options instead of individual flags
|
||||
@@ -58,33 +59,29 @@ func TreeCommand(name string) *cobra.Command {
|
||||
|
||||
// TreeRunner contains the run function
|
||||
type TreeRunner struct {
|
||||
Command *cobra.Command
|
||||
name bool
|
||||
resources bool
|
||||
ports bool
|
||||
images bool
|
||||
replicas bool
|
||||
all bool
|
||||
env bool
|
||||
args bool
|
||||
cmd bool
|
||||
fields []string
|
||||
includeLocal bool
|
||||
excludeNonLocal bool
|
||||
structure string
|
||||
IncludeSubpackages bool
|
||||
Command *cobra.Command
|
||||
name bool
|
||||
resources bool
|
||||
ports bool
|
||||
images bool
|
||||
replicas bool
|
||||
all bool
|
||||
env bool
|
||||
args bool
|
||||
cmd bool
|
||||
fields []string
|
||||
includeLocal bool
|
||||
excludeNonLocal bool
|
||||
structure string
|
||||
}
|
||||
|
||||
func (r *TreeRunner) runE(c *cobra.Command, args []string) error {
|
||||
var input kio.Reader
|
||||
var root = "."
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
matchFilesGlob := append([]string{openAPIFileName}, kio.DefaultMatch...)
|
||||
if len(args) == 1 {
|
||||
root = filepath.Clean(args[0])
|
||||
input = kio.LocalPackageReader{PackagePath: args[0], MatchFilesGlob: matchFilesGlob}
|
||||
input = kio.LocalPackageReader{PackagePath: args[0]}
|
||||
} else {
|
||||
input = &kio.ByteReader{Reader: c.InOrStdin()}
|
||||
}
|
||||
@@ -159,12 +156,10 @@ func (r *TreeRunner) runE(c *cobra.Command, args []string) error {
|
||||
Inputs: []kio.Reader{input},
|
||||
Filters: fltrs,
|
||||
Outputs: []kio.Writer{kio.TreeWriter{
|
||||
Root: root,
|
||||
Writer: c.OutOrStdout(),
|
||||
Fields: fields,
|
||||
Structure: kio.TreeStructure(r.structure),
|
||||
OpenAPIFileName: openAPIFileName,
|
||||
}},
|
||||
Root: root,
|
||||
Writer: c.OutOrStdout(),
|
||||
Fields: fields,
|
||||
Structure: kio.TreeStructure(r.structure)}},
|
||||
}.Execute())
|
||||
}
|
||||
|
||||
|
||||
@@ -90,109 +90,6 @@ spec:
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeCommand_subpkgs(t *testing.T) {
|
||||
d, err := ioutil.TempDir("", "kustomize-tree-test")
|
||||
defer os.RemoveAll(d)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
err = os.MkdirAll(filepath.Join(d, "subpkg"), 0700)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
|
||||
apiVersion: v1
|
||||
kind: Abstraction
|
||||
metadata:
|
||||
name: foo
|
||||
configFn:
|
||||
container:
|
||||
image: gcr.io/example/reconciler:v1
|
||||
annotations:
|
||||
config.kubernetes.io/local-config: "true"
|
||||
spec:
|
||||
replicas: 1
|
||||
---
|
||||
kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx2
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
spec:
|
||||
replicas: 1
|
||||
---
|
||||
kind: Service
|
||||
metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(d, "subpkg", "f2.yaml"), []byte(`kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
name: bar
|
||||
annotations:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(filepath.Join(d, "Krmfile"), []byte(`apiVersion: kpt.dev/v1alpha1
|
||||
kind: Krmfile
|
||||
metadata:
|
||||
name: mainpkg
|
||||
openAPI:
|
||||
definitions:
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(d, "subpkg", "Krmfile"), []byte(`apiVersion: kpt.dev/v1alpha1
|
||||
kind: Krmfile
|
||||
metadata:
|
||||
name: subpkg
|
||||
openAPI:
|
||||
definitions:
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
// fmt the files
|
||||
b := &bytes.Buffer{}
|
||||
r := commands.GetTreeRunner("")
|
||||
r.Command.SetArgs([]string{d})
|
||||
r.Command.SetOut(b)
|
||||
if !assert.NoError(t, r.Command.Execute()) {
|
||||
return
|
||||
}
|
||||
|
||||
if !assert.Equal(t, fmt.Sprintf(`%s
|
||||
├── [Krmfile] Krmfile mainpkg
|
||||
├── [f1.yaml] Deployment foo
|
||||
├── [f1.yaml] Service foo
|
||||
└── Pkg: subpkg
|
||||
├── [Krmfile] Krmfile subpkg
|
||||
└── [f2.yaml] Deployment bar
|
||||
`, d), b.String()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeCommand_stdin(t *testing.T) {
|
||||
// fmt the files
|
||||
b := &bytes.Buffer{}
|
||||
|
||||
@@ -5,91 +5,13 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
"sigs.k8s.io/kustomize/kyaml/pathutil"
|
||||
)
|
||||
|
||||
// cmdRunner interface holds executeCmd definition which executes respective command's
|
||||
// implementation on single package
|
||||
type cmdRunner interface {
|
||||
executeCmd(w io.Writer, pkgPath string) error
|
||||
}
|
||||
|
||||
// executeCmdOnPkgs struct holds the parameters necessary to
|
||||
// execute the filter command on packages in rootPkgPath
|
||||
type executeCmdOnPkgs struct {
|
||||
rootPkgPath string
|
||||
recurseSubPackages bool
|
||||
needOpenAPI bool
|
||||
cmdRunner cmdRunner
|
||||
writer io.Writer
|
||||
skipPkgPathPrint bool
|
||||
}
|
||||
|
||||
// executeCmdOnPkgs takes the function definition for a command to be executed on single package, applies that definition
|
||||
// recursively on all the subpackages present in rootPkgPath if recurseSubPackages is true, else applies the command on rootPkgPath only
|
||||
func (e executeCmdOnPkgs) execute() error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pkgsPaths, err := pathutil.DirsWithFile(e.rootPkgPath, openAPIFileName, e.recurseSubPackages)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(pkgsPaths) == 0 {
|
||||
// at this point, there are no openAPI files in the rootPkgPath
|
||||
if e.needOpenAPI {
|
||||
// few executions need openAPI file to be present(ex: setters commands), if true throw an error
|
||||
return errors.Errorf("unable to find %q in package %q", openAPIFileName, e.rootPkgPath)
|
||||
}
|
||||
|
||||
// add root path for commands which doesn't need openAPI(ex: annotate, fmt)
|
||||
pkgsPaths = []string{e.rootPkgPath}
|
||||
}
|
||||
|
||||
for i := range pkgsPaths {
|
||||
pkgPath := pkgsPaths[i]
|
||||
// Add schema present in openAPI file for current package
|
||||
if e.needOpenAPI {
|
||||
if err := openapi.AddSchemaFromFile(filepath.Join(pkgPath, openAPIFileName)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !e.skipPkgPathPrint {
|
||||
fmt.Fprintf(e.writer, "%s/\n", pkgPath)
|
||||
}
|
||||
|
||||
err := e.cmdRunner.executeCmd(e.writer, pkgPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if i != len(pkgsPaths)-1 {
|
||||
fmt.Fprint(e.writer, "\n")
|
||||
}
|
||||
|
||||
// Delete schema present in openAPI file for current package
|
||||
if e.needOpenAPI {
|
||||
if err := openapi.DeleteSchemaInFile(filepath.Join(pkgPath, openAPIFileName)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseFieldPath parse a flag value into a field path
|
||||
func parseFieldPath(path string) ([]string, error) {
|
||||
// fixup '\.' so we don't split on it
|
||||
|
||||
@@ -1,184 +0,0 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
)
|
||||
|
||||
func TestExecuteCmdOnPkgs(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
recurse bool
|
||||
pkgPath string
|
||||
needOpenAPI bool
|
||||
errMsg string
|
||||
expectedOut string
|
||||
}{
|
||||
{
|
||||
name: "need_Krmfile_error",
|
||||
recurse: true,
|
||||
needOpenAPI: true,
|
||||
pkgPath: "subpkg1/subdir1",
|
||||
errMsg: `unable to find "Krmfile" in package`,
|
||||
},
|
||||
{
|
||||
name: "Krmfile_not_needed_no_err",
|
||||
recurse: true,
|
||||
needOpenAPI: false,
|
||||
pkgPath: "subpkg1/subdir1",
|
||||
expectedOut: `${baseDir}/subpkg1/subdir1/
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "executeCmd_returns_error",
|
||||
recurse: true,
|
||||
needOpenAPI: false,
|
||||
pkgPath: "subpkg4",
|
||||
expectedOut: `${baseDir}/subpkg4/
|
||||
`,
|
||||
errMsg: `this command returns an error if package has error.txt file`,
|
||||
},
|
||||
{
|
||||
name: "executeCmd_prints_pkgpaths",
|
||||
recurse: true,
|
||||
needOpenAPI: false,
|
||||
pkgPath: "subpkg2",
|
||||
expectedOut: `${baseDir}/subpkg2/
|
||||
|
||||
${baseDir}/subpkg2/subpkg3/
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
err = createTestDirStructure(dir)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
actual := &bytes.Buffer{}
|
||||
r := &TestRunner{}
|
||||
e := executeCmdOnPkgs{
|
||||
needOpenAPI: test.needOpenAPI,
|
||||
writer: actual,
|
||||
rootPkgPath: filepath.Join(dir, test.pkgPath),
|
||||
recurseSubPackages: test.recurse,
|
||||
cmdRunner: r,
|
||||
}
|
||||
err := e.execute()
|
||||
if test.errMsg == "" {
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
} else {
|
||||
if !assert.Error(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
if !assert.Contains(t, err.Error(), test.errMsg) {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
// normalize path format for windows
|
||||
actualNormalized := strings.Replace(
|
||||
strings.Replace(actual.String(), "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
|
||||
expected := strings.Replace(test.expectedOut, "${baseDir}", dir+"/", -1)
|
||||
expectedNormalized := strings.Replace(
|
||||
strings.Replace(expected, "\\", "/", -1),
|
||||
"//", "/", -1)
|
||||
if !assert.Equal(t, expectedNormalized, actualNormalized) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func createTestDirStructure(dir string) error {
|
||||
/*
|
||||
Adds the folders to the input dir with following structure
|
||||
dir
|
||||
├── subpkg1
|
||||
│ ├── Krmfile
|
||||
│ └── subdir1
|
||||
├── subpkg4
|
||||
│ ├── Krmfile
|
||||
│ └── error.txt
|
||||
└── subpkg2
|
||||
├── subpkg3
|
||||
│ ├── Krmfile
|
||||
│ └── subdir2
|
||||
└── Krmfile
|
||||
*/
|
||||
err := os.MkdirAll(filepath.Join(dir, "subpkg1/subdir1"), 0777|os.ModeDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.MkdirAll(filepath.Join(dir, "subpkg2/subpkg3/subdir2"), 0777|os.ModeDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.MkdirAll(filepath.Join(dir, "subpkg4"), 0777|os.ModeDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(dir, "subpkg1", "Krmfile"), []byte(""), 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(dir, "subpkg2", "Krmfile"), []byte(""), 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(dir, "subpkg2/subpkg3", "Krmfile"), []byte(""), 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(dir, "subpkg4", "error.txt"), []byte(""), 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(dir, "subpkg4", "Krmfile"), []byte(""), 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(dir, "Krmfile"), []byte(""), 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type TestRunner struct{}
|
||||
|
||||
func (r *TestRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
children, err := ioutil.ReadDir(pkgPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, child := range children {
|
||||
if child.Name() == "error.txt" {
|
||||
return errors.Errorf("this command returns an error if package has error.txt file")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
# 使用 kustomize 对 helm charts 进行修改
|
||||
# 使用 kustomize 对 helm chart s进行修改
|
||||
|
||||
[last mile]: https://testingclouds.wordpress.com/2018/07/20/844/
|
||||
[stable chart]: https://github.com/helm/charts/tree/master/stable
|
||||
|
||||
@@ -5,6 +5,6 @@ go 1.14
|
||||
require (
|
||||
k8s.io/apimachinery v0.18.3
|
||||
sigs.k8s.io/application v0.8.2
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1
|
||||
sigs.k8s.io/yaml v1.2.0
|
||||
)
|
||||
|
||||
@@ -559,8 +559,8 @@ sigs.k8s.io/application v0.8.2 h1:XB7C33f7eW+1MbCJXoZa0+nP+R+S/VbAvYfCd3ufP1I=
|
||||
sigs.k8s.io/application v0.8.2/go.mod h1:Mv+ht9RE/QNtITYCzRbt3XTIN6t6so6cInmiyg6wOIg=
|
||||
sigs.k8s.io/controller-runtime v0.4.0 h1:wATM6/m+3w8lj8FXNaO6Fs/rq/vqoOjO1Q116Z9NPsg=
|
||||
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1 h1:Ih6SJPvfKYfZaIFWUa2YAyg/0ZSTpA3LFjR/hv7+8ao=
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1/go.mod h1:ne3F9JPhW2wrVaLslxBsEe6MQJQ9YK5rUutrdhBWXwI=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1 h1:mwffj5vt3MPdbWV3fZnnwol8SO7sUoGdgejBlvseyak=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1/go.mod h1:bEzbO5pN9OvlEeCLvFHo8Pu7SA26Herc2m60UeWZBdI=
|
||||
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-20190817042607-6149e4549fca/go.mod h1:IIgPezJWb76P0hotTxzDbWsMYB8APh18qZnxkomBpxA=
|
||||
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06 h1:zD2IemQ4LmOcAumeiyDWXKUI2SO0NYDe3H6QGvPOVgU=
|
||||
|
||||
@@ -2,4 +2,4 @@ module sigs.k8s.io/kustomize/functions/examples/injection-tshirt-sizes
|
||||
|
||||
go 1.14
|
||||
|
||||
require sigs.k8s.io/kustomize/kyaml v0.7.1
|
||||
require sigs.k8s.io/kustomize/kyaml v0.6.1
|
||||
|
||||
@@ -217,8 +217,6 @@ golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR
|
||||
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
|
||||
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 h1:rjwSpXsdiK0dV8/Naq3kAw9ymfAeJIyd0upUIElB+lI=
|
||||
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -265,5 +263,5 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 h1:Xe2gvTZUJpsvOWUnvmL/tmhVBZUmHSvLbMjRj6NUUKo=
|
||||
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=
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1 h1:Ih6SJPvfKYfZaIFWUa2YAyg/0ZSTpA3LFjR/hv7+8ao=
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1/go.mod h1:ne3F9JPhW2wrVaLslxBsEe6MQJQ9YK5rUutrdhBWXwI=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1 h1:mwffj5vt3MPdbWV3fZnnwol8SO7sUoGdgejBlvseyak=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1/go.mod h1:bEzbO5pN9OvlEeCLvFHo8Pu7SA26Herc2m60UeWZBdI=
|
||||
|
||||
@@ -2,4 +2,4 @@ module sigs.k8s.io/kustomize/functions/examples/template-go-nginx
|
||||
|
||||
go 1.14
|
||||
|
||||
require sigs.k8s.io/kustomize/kyaml v0.7.1
|
||||
require sigs.k8s.io/kustomize/kyaml v0.6.1
|
||||
|
||||
@@ -218,8 +218,6 @@ golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR
|
||||
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
|
||||
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 h1:rjwSpXsdiK0dV8/Naq3kAw9ymfAeJIyd0upUIElB+lI=
|
||||
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -266,5 +264,5 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 h1:Xe2gvTZUJpsvOWUnvmL/tmhVBZUmHSvLbMjRj6NUUKo=
|
||||
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=
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1 h1:Ih6SJPvfKYfZaIFWUa2YAyg/0ZSTpA3LFjR/hv7+8ao=
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1/go.mod h1:ne3F9JPhW2wrVaLslxBsEe6MQJQ9YK5rUutrdhBWXwI=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1 h1:mwffj5vt3MPdbWV3fZnnwol8SO7sUoGdgejBlvseyak=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1/go.mod h1:bEzbO5pN9OvlEeCLvFHo8Pu7SA26Herc2m60UeWZBdI=
|
||||
|
||||
@@ -4,5 +4,5 @@ go 1.14
|
||||
|
||||
require (
|
||||
github.com/instrumenta/kubeval v0.0.0-20190918223246-8d013ec9fc56
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1
|
||||
)
|
||||
|
||||
@@ -244,8 +244,6 @@ golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR
|
||||
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
|
||||
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 h1:rjwSpXsdiK0dV8/Naq3kAw9ymfAeJIyd0upUIElB+lI=
|
||||
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -296,7 +294,7 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 h1:Xe2gvTZUJpsvOWUnvmL/tmhVBZUmHSvLbMjRj6NUUKo=
|
||||
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=
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1 h1:Ih6SJPvfKYfZaIFWUa2YAyg/0ZSTpA3LFjR/hv7+8ao=
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1/go.mod h1:ne3F9JPhW2wrVaLslxBsEe6MQJQ9YK5rUutrdhBWXwI=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1 h1:mwffj5vt3MPdbWV3fZnnwol8SO7sUoGdgejBlvseyak=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1/go.mod h1:bEzbO5pN9OvlEeCLvFHo8Pu7SA26Herc2m60UeWZBdI=
|
||||
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
||||
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||
|
||||
@@ -2,4 +2,4 @@ module sigs.k8s.io/kustomize/functions/examples/validator-resource-requests
|
||||
|
||||
go 1.14
|
||||
|
||||
require sigs.k8s.io/kustomize/kyaml v0.7.1
|
||||
require sigs.k8s.io/kustomize/kyaml v0.6.1
|
||||
|
||||
@@ -211,8 +211,6 @@ golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR
|
||||
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
|
||||
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 h1:rjwSpXsdiK0dV8/Naq3kAw9ymfAeJIyd0upUIElB+lI=
|
||||
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -259,5 +257,5 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 h1:Xe2gvTZUJpsvOWUnvmL/tmhVBZUmHSvLbMjRj6NUUKo=
|
||||
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=
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1 h1:Ih6SJPvfKYfZaIFWUa2YAyg/0ZSTpA3LFjR/hv7+8ao=
|
||||
sigs.k8s.io/kustomize/kyaml v0.7.1/go.mod h1:ne3F9JPhW2wrVaLslxBsEe6MQJQ9YK5rUutrdhBWXwI=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1 h1:mwffj5vt3MPdbWV3fZnnwol8SO7sUoGdgejBlvseyak=
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.1/go.mod h1:bEzbO5pN9OvlEeCLvFHo8Pu7SA26Herc2m60UeWZBdI=
|
||||
|
||||
@@ -8,19 +8,19 @@ require (
|
||||
github.com/spf13/cobra v1.0.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
k8s.io/client-go v0.17.3
|
||||
sigs.k8s.io/kustomize/api v0.6.0
|
||||
sigs.k8s.io/kustomize/cmd/config v0.6.0
|
||||
sigs.k8s.io/kustomize/api v0.5.1
|
||||
sigs.k8s.io/kustomize/cmd/config v0.5.0
|
||||
sigs.k8s.io/yaml v1.2.0
|
||||
)
|
||||
|
||||
replace (
|
||||
sigs.k8s.io/kustomize/api v0.5.1 => ../api
|
||||
sigs.k8s.io/kustomize/cmd/config v0.5.0 => ../cmd/config
|
||||
sigs.k8s.io/kustomize/kyaml => ../kyaml
|
||||
)
|
||||
|
||||
exclude (
|
||||
github.com/russross/blackfriday v2.0.0+incompatible
|
||||
sigs.k8s.io/kustomize/api v0.2.0
|
||||
sigs.k8s.io/kustomize/cmd/config v0.2.0
|
||||
)
|
||||
|
||||
replace sigs.k8s.io/kustomize/api v0.6.0 => ../api
|
||||
|
||||
replace sigs.k8s.io/kustomize/cmd/config v0.6.0 => ../cmd/config
|
||||
|
||||
replace sigs.k8s.io/kustomize/kyaml v0.7.1 => ../kyaml
|
||||
|
||||
@@ -358,8 +358,6 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
|
||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/2gBQ3RWajuToeY6ZtZTIKv2v7ThUy5KKusIT0yc0=
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4=
|
||||
github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
|
||||
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
@@ -629,7 +627,6 @@ golang.org/x/tools v0.0.0-20190910044552-dd2b5c81c578/go.mod h1:b+2E5dAYhXwXZwtn
|
||||
golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20190930201159-7c411dea38b0/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
@@ -745,14 +742,12 @@ modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
|
||||
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/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
||||
sigs.k8s.io/cli-utils v0.20.1 h1:S3IM4Rmely9oWNAeytx46PhngTwdRxsJ4ixtoGWPc3o=
|
||||
sigs.k8s.io/cli-utils v0.20.1/go.mod h1:fQkEMmLXduMNmUf1+dgQ5GRJa4OrrLMw3qzAN8YXAU8=
|
||||
sigs.k8s.io/cli-utils v0.19.0 h1:lAoR5okhSV/dIusodaQp5VbDpHMcKnvjqKYHU+AB3a4=
|
||||
sigs.k8s.io/cli-utils v0.19.0/go.mod h1:B7KdqkSkHNIUn3cFbaR4aKUZMKtr+Benboi1w/HW/Fg=
|
||||
sigs.k8s.io/controller-runtime v0.4.0 h1:wATM6/m+3w8lj8FXNaO6Fs/rq/vqoOjO1Q116Z9NPsg=
|
||||
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
|
||||
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
|
||||
sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU=
|
||||
sigs.k8s.io/kustomize/kyaml v0.8.0 h1:/MqPML99XAm2pbrD/eTpePh5rnU5bpnuTPqb29LpSz4=
|
||||
sigs.k8s.io/kustomize/kyaml v0.8.0/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-20190817042607-6149e4549fca/go.mod h1:IIgPezJWb76P0hotTxzDbWsMYB8APh18qZnxkomBpxA=
|
||||
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06/go.mod h1:/ULNhyfzRopfcjskuui0cTITekDduZ7ycKN3oUT9R18=
|
||||
|
||||
@@ -81,11 +81,6 @@ func newCmdAddConfigMap(
|
||||
"from-env-file",
|
||||
"",
|
||||
"Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).")
|
||||
cmd.Flags().BoolVar(
|
||||
&flags.DisableNameSuffixHash,
|
||||
"disableNameSuffixHash",
|
||||
false,
|
||||
"Disable the name suffix for the configmap")
|
||||
|
||||
return cmd
|
||||
}
|
||||
@@ -131,9 +126,4 @@ func mergeFlagsIntoCmArgs(args *types.ConfigMapArgs, flags flagsAndArgs) {
|
||||
args.EnvSources = append(
|
||||
args.EnvSources, flags.EnvFileSource)
|
||||
}
|
||||
if flags.DisableNameSuffixHash {
|
||||
args.Options = &types.GeneratorOptions{
|
||||
DisableNameSuffixHash: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,6 @@ type flagsAndArgs struct {
|
||||
Type string
|
||||
// Namespace of secret
|
||||
Namespace string
|
||||
// Disable name suffix
|
||||
DisableNameSuffixHash bool
|
||||
}
|
||||
|
||||
// Validate validates required fields are set to support structured generation.
|
||||
|
||||
@@ -91,11 +91,6 @@ func newCmdAddSecret(
|
||||
"namespace",
|
||||
"",
|
||||
"Specify the namespace of the secret")
|
||||
cmd.Flags().BoolVar(
|
||||
&flags.DisableNameSuffixHash,
|
||||
"disableNameSuffixHash",
|
||||
false,
|
||||
"Disable the name suffix for the secret")
|
||||
|
||||
return cmd
|
||||
}
|
||||
@@ -144,9 +139,4 @@ func mergeFlagsIntoGeneratorArgs(args *types.GeneratorArgs, flags flagsAndArgs)
|
||||
args.EnvSources = append(
|
||||
args.EnvSources, flags.EnvFileSource)
|
||||
}
|
||||
if flags.DisableNameSuffixHash {
|
||||
args.Options = &types.GeneratorOptions{
|
||||
DisableNameSuffixHash: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,14 +114,3 @@ func TestMergeFlagsIntoSecretArgs_EnvSource(t *testing.T) {
|
||||
t.Fatalf("expected env2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeFlagsIntoSecretArgs_DisableNameSuffixHash(t *testing.T) {
|
||||
k := &types.Kustomization{}
|
||||
args := findOrMakeSecretArgs(k, "foo", "bar", "forbidden")
|
||||
mergeFlagsIntoGeneratorArgs(
|
||||
&args.GeneratorArgs,
|
||||
flagsAndArgs{DisableNameSuffixHash: true})
|
||||
if k.SecretGenerator[0].Options.DisableNameSuffixHash != true {
|
||||
t.Fatalf("expected true")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,5 @@ func RunFix(fSys filesys.FileSystem) error {
|
||||
return err
|
||||
}
|
||||
|
||||
m.FixKustomizationPreMarshalling()
|
||||
return mf.Write(m)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"sigs.k8s.io/kustomize/api/filesys"
|
||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
|
||||
)
|
||||
@@ -32,104 +31,3 @@ func TestFix(t *testing.T) {
|
||||
t.Errorf("expected kind in kustomization")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFixOutdatedPatchesFieldTitle(t *testing.T) {
|
||||
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(`
|
||||
patchesJson6902:
|
||||
- path: patch1.yaml
|
||||
target:
|
||||
kind: Service
|
||||
- path: patch2.yaml
|
||||
target:
|
||||
group: apps
|
||||
kind: Deployment
|
||||
version: v1
|
||||
`)
|
||||
|
||||
expected := []byte(`
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
patches:
|
||||
- path: patch1.yaml
|
||||
target:
|
||||
kind: Service
|
||||
- path: patch2.yaml
|
||||
target:
|
||||
group: apps
|
||||
kind: Deployment
|
||||
version: v1
|
||||
`)
|
||||
fSys := filesys.MakeFsInMemory()
|
||||
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle)
|
||||
cmd := NewCmdFix(fSys)
|
||||
err := cmd.RunE(cmd, nil)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected cmd error: %v", err)
|
||||
}
|
||||
content, err := testutils_test.ReadTestKustomization(fSys)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected read error: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(content), "apiVersion: ") {
|
||||
t.Errorf("expected apiVersion in kustomization")
|
||||
}
|
||||
if !strings.Contains(string(content), "kind: Kustomization") {
|
||||
t.Errorf("expected kind in kustomization")
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(expected, content); diff != "" {
|
||||
t.Errorf("Mismatch (-expected, +actual):\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenameAndKeepOutdatedPatchesField(t *testing.T) {
|
||||
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(`
|
||||
patchesJson6902:
|
||||
- path: patch1.yaml
|
||||
target:
|
||||
kind: Deployment
|
||||
patches:
|
||||
- path: patch2.yaml
|
||||
target:
|
||||
kind: Deployment
|
||||
- path: patch3.yaml
|
||||
target:
|
||||
kind: Service
|
||||
`)
|
||||
|
||||
expected := []byte(`
|
||||
patches:
|
||||
- path: patch2.yaml
|
||||
target:
|
||||
kind: Deployment
|
||||
- path: patch3.yaml
|
||||
target:
|
||||
kind: Service
|
||||
- path: patch1.yaml
|
||||
target:
|
||||
kind: Deployment
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
`)
|
||||
fSys := filesys.MakeFsInMemory()
|
||||
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle)
|
||||
cmd := NewCmdFix(fSys)
|
||||
err := cmd.RunE(cmd, nil)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected cmd error: %v", err)
|
||||
}
|
||||
content, err := testutils_test.ReadTestKustomization(fSys)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected read error: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(content), "apiVersion: ") {
|
||||
t.Errorf("expected apiVersion in kustomization")
|
||||
}
|
||||
if !strings.Contains(string(content), "kind: Kustomization") {
|
||||
t.Errorf("expected kind in kustomization")
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(expected, content); diff != "" {
|
||||
t.Errorf("Mismatch (-expected, +actual):\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,47 +145,6 @@ func TestSetImage(t *testing.T) {
|
||||
" newTag: my-tag2",
|
||||
}},
|
||||
},
|
||||
{
|
||||
description: "override file with patch",
|
||||
given: given{
|
||||
args: []string{"image1=foo.bar.foo:8800/foo/image1:foo-bar"},
|
||||
infileImages: []string{
|
||||
"images:",
|
||||
"- name: image1",
|
||||
" newName: my-image1",
|
||||
" newTag: my-tag",
|
||||
"- name: image2",
|
||||
" newName: my-image2",
|
||||
" newTag: my-tag2",
|
||||
"patchesJson6902:",
|
||||
"- patch: |-",
|
||||
" - op: remove",
|
||||
" path: /spec/selector",
|
||||
" target:",
|
||||
" kind: Service",
|
||||
" name: foo",
|
||||
" version: v1",
|
||||
},
|
||||
},
|
||||
expected: expected{
|
||||
fileOutput: []string{
|
||||
"images:",
|
||||
"- name: image1",
|
||||
" newName: foo.bar.foo:8800/foo/image1",
|
||||
" newTag: foo-bar",
|
||||
"- name: image2",
|
||||
" newName: my-image2",
|
||||
" newTag: my-tag2",
|
||||
"patchesJson6902:",
|
||||
"- patch: |-",
|
||||
" - op: remove",
|
||||
" path: /spec/selector",
|
||||
" target:",
|
||||
" kind: Service",
|
||||
" name: foo",
|
||||
" version: v1",
|
||||
}},
|
||||
},
|
||||
{
|
||||
description: "override new tag and new name with just a new tag",
|
||||
given: given{
|
||||
|
||||
@@ -170,6 +170,7 @@ func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error {
|
||||
if kustomization == nil {
|
||||
return errors.New("util: kustomization file arg is nil")
|
||||
}
|
||||
kustomization.FixKustomizationPreMarshalling()
|
||||
data, err := mf.marshal(kustomization)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -338,6 +338,103 @@ kind: Kustomization
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenameAndKeepOutdatedPatchesField(t *testing.T) {
|
||||
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(`
|
||||
patchesJson6902:
|
||||
- path: patch1.yaml
|
||||
target:
|
||||
kind: Deployment
|
||||
patches:
|
||||
- path: patch2.yaml
|
||||
target:
|
||||
kind: Deployment
|
||||
- path: patch3.yaml
|
||||
target:
|
||||
kind: Service
|
||||
`)
|
||||
|
||||
expected := []byte(`
|
||||
patches:
|
||||
- path: patch2.yaml
|
||||
target:
|
||||
kind: Deployment
|
||||
- path: patch3.yaml
|
||||
target:
|
||||
kind: Service
|
||||
- path: patch1.yaml
|
||||
target:
|
||||
kind: Deployment
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
`)
|
||||
fSys := filesys.MakeFsInMemory()
|
||||
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle)
|
||||
mf, err := NewKustomizationFile(fSys)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
|
||||
kustomization, err := mf.Read()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
if err = mf.Write(kustomization); err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
bytes, _ := fSys.ReadFile(mf.path)
|
||||
|
||||
if diff := cmp.Diff(expected, bytes); diff != "" {
|
||||
t.Errorf("Mismatch (-expected, +actual):\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFixOutdatedPatchesFieldTitle(t *testing.T) {
|
||||
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(`
|
||||
patchesJson6902:
|
||||
- path: patch1.yaml
|
||||
target:
|
||||
kind: Service
|
||||
- path: patch2.yaml
|
||||
target:
|
||||
group: apps
|
||||
kind: Deployment
|
||||
version: v1
|
||||
`)
|
||||
|
||||
expected := []byte(`
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
patches:
|
||||
- path: patch1.yaml
|
||||
target:
|
||||
kind: Service
|
||||
- path: patch2.yaml
|
||||
target:
|
||||
group: apps
|
||||
kind: Deployment
|
||||
version: v1
|
||||
`)
|
||||
fSys := filesys.MakeFsInMemory()
|
||||
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle)
|
||||
mf, err := NewKustomizationFile(fSys)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
|
||||
kustomization, err := mf.Read()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
if err = mf.Write(kustomization); err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
bytes, _ := fSys.ReadFile(mf.path)
|
||||
|
||||
if diff := cmp.Diff(expected, bytes); diff != "" {
|
||||
t.Errorf("Mismatch (-expected, +actual):\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownFieldInKustomization(t *testing.T) {
|
||||
kContent := []byte(`
|
||||
foo:
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package ext
|
||||
|
||||
// GetIgnoreFileName returns the name for ignore files in
|
||||
// packages. It can be overridden by tools using this library.
|
||||
var GetIgnoreFileName = func() string {
|
||||
return ".krmignore"
|
||||
}
|
||||
@@ -8,7 +8,6 @@ require (
|
||||
github.com/go-openapi/spec v0.19.5
|
||||
github.com/go-openapi/strfmt v0.19.5
|
||||
github.com/go-openapi/validate v0.19.8
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00
|
||||
github.com/qri-io/starlib v0.4.2-0.20200213133954-ff2e8cd5ef8d
|
||||
github.com/sergi/go-diff v1.1.0
|
||||
github.com/spf13/cobra v1.0.0
|
||||
|
||||
@@ -145,8 +145,6 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
|
||||
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/2gBQ3RWajuToeY6ZtZTIKv2v7ThUy5KKusIT0yc0=
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
|
||||
github.com/paulmach/orb v0.1.3/go.mod h1:VFlX/8C+IQ1p6FTRRKzKoOPJnvEtA5G0Veuqwbu//Vk=
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package kio
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/monochromegane/go-gitignore"
|
||||
"sigs.k8s.io/kustomize/kyaml/ext"
|
||||
)
|
||||
|
||||
// ignoreFilesMatcher handles `.krmignore` files, which allows for ignoring
|
||||
// files or folders in a package. The format of this file is a subset of the
|
||||
// gitignore format, with recursive patterns (like a/**/c) not supported. If a
|
||||
// file or folder matches any of the patterns in the .krmignore file for the
|
||||
// package, it will be excluded.
|
||||
//
|
||||
// It works as follows:
|
||||
//
|
||||
// * It will look for .krmignore file in the top folder and on the top level
|
||||
// of any subpackages. Subpackages are defined by the presence of a Krmfile
|
||||
// in the folder.
|
||||
// * `.krmignore` files only cover files and folders for the package in which
|
||||
// it is defined. So ignore patterns defined in a parent package does not
|
||||
// affect which files are ignored from a subpackage.
|
||||
// * An ignore pattern can not ignore a subpackage. So even if the parent
|
||||
// package contains a pattern that ignores the directory foo, if foo is a
|
||||
// subpackage, it will still be included if the IncludeSubpackages property
|
||||
// is set to true
|
||||
type ignoreFilesMatcher struct {
|
||||
matchers []matcher
|
||||
}
|
||||
|
||||
// readIgnoreFile checks whether there is a .krmignore file in the path, and
|
||||
// if it is, reads it in and turns it into a matcher. If we can't find a file,
|
||||
// we just add a matcher that match nothing.
|
||||
func (i *ignoreFilesMatcher) readIgnoreFile(path string) error {
|
||||
i.verifyPath(path)
|
||||
m, err := gitignore.NewGitIgnore(filepath.Join(path, ext.GetIgnoreFileName()))
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
i.matchers = append(i.matchers, matcher{
|
||||
matcher: gitignore.DummyIgnoreMatcher(false),
|
||||
basePath: path,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
i.matchers = append(i.matchers, matcher{
|
||||
matcher: m,
|
||||
basePath: path,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// verifyPath checks whether the top matcher on the stack
|
||||
// is correct for the provided filepath. Matchers are removed once
|
||||
// we encounter a filepath that is not a subpath of the basepath for
|
||||
// the matcher.
|
||||
func (i *ignoreFilesMatcher) verifyPath(path string) {
|
||||
for j := len(i.matchers) - 1; j >= 0; j-- {
|
||||
matcher := i.matchers[j]
|
||||
if strings.HasPrefix(path, matcher.basePath) || path == matcher.basePath {
|
||||
i.matchers = i.matchers[:j+1]
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// matchFile checks whether the file given by the provided path matches
|
||||
// any of the patterns in the .krmignore file for the package.
|
||||
func (i *ignoreFilesMatcher) matchFile(path string) bool {
|
||||
if len(i.matchers) == 0 {
|
||||
return false
|
||||
}
|
||||
i.verifyPath(filepath.Dir(path))
|
||||
return i.matchers[len(i.matchers)-1].matcher.Match(path, false)
|
||||
}
|
||||
|
||||
// matchFile checks whether the directory given by the provided path matches
|
||||
// any of the patterns in the .krmignore file for the package.
|
||||
func (i *ignoreFilesMatcher) matchDir(path string) bool {
|
||||
if len(i.matchers) == 0 {
|
||||
return false
|
||||
}
|
||||
i.verifyPath(path)
|
||||
return i.matchers[len(i.matchers)-1].matcher.Match(path, true)
|
||||
}
|
||||
|
||||
// matcher wraps the gitignore matcher and the path to the folder
|
||||
// where the file was found.
|
||||
type matcher struct {
|
||||
matcher gitignore.IgnoreMatcher
|
||||
|
||||
basePath string
|
||||
}
|
||||
@@ -1,249 +0,0 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package kio
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIgnoreFilesMatcher_readIgnoreFile(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
writeIgnoreFile bool
|
||||
isMatch bool
|
||||
}{
|
||||
{
|
||||
name: "has .krmignore file",
|
||||
writeIgnoreFile: true,
|
||||
isMatch: true,
|
||||
},
|
||||
{
|
||||
name: "no .krmignore file",
|
||||
writeIgnoreFile: false,
|
||||
isMatch: false,
|
||||
},
|
||||
}
|
||||
|
||||
for i := range testCases {
|
||||
test := testCases[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "kyaml-test")
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
|
||||
if test.writeIgnoreFile {
|
||||
ignoreFilePath := filepath.Join(dir, ".krmignore")
|
||||
err = ioutil.WriteFile(ignoreFilePath, []byte(`
|
||||
testfile.yaml
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
}
|
||||
testFilePath := filepath.Join(dir, "testfile.yaml")
|
||||
err = ioutil.WriteFile(testFilePath, []byte{}, 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
|
||||
ignoreFilesMatcher := ignoreFilesMatcher{}
|
||||
err = ignoreFilesMatcher.readIgnoreFile(dir)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.Equal(t, test.isMatch, ignoreFilesMatcher.matchFile(testFilePath))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
readFileA = []byte(`
|
||||
a: a
|
||||
---
|
||||
c: c
|
||||
`)
|
||||
readFileB = []byte(`
|
||||
b: b
|
||||
`)
|
||||
)
|
||||
|
||||
func TestLocalPackageReader_Read_ignoreFile(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
directories []string
|
||||
files map[string][]byte
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "ignore file",
|
||||
directories: []string{
|
||||
filepath.Join("a", "b"),
|
||||
filepath.Join("a", "c"),
|
||||
},
|
||||
files: map[string][]byte{
|
||||
filepath.Join("pkgFile"): {},
|
||||
filepath.Join("a", "b", "a_test.yaml"): readFileA,
|
||||
filepath.Join("a", "c", "c_test.yaml"): readFileB,
|
||||
filepath.Join(".krmignore"): []byte(`
|
||||
a/c/c_test.yaml
|
||||
`,
|
||||
),
|
||||
},
|
||||
expected: []string{
|
||||
`a: a`,
|
||||
`c: c`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "ignore folder",
|
||||
directories: []string{
|
||||
filepath.Join("a", "b"),
|
||||
filepath.Join("a", "c"),
|
||||
},
|
||||
files: map[string][]byte{
|
||||
filepath.Join("pkgFile"): {},
|
||||
filepath.Join("a", "b", "a_test.yaml"): readFileA,
|
||||
filepath.Join("a", "c", "c_test.yaml"): readFileB,
|
||||
filepath.Join(".krmignore"): []byte(`
|
||||
a/c
|
||||
`,
|
||||
),
|
||||
},
|
||||
expected: []string{
|
||||
`a: a`,
|
||||
`c: c`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "krmignore file in subpackage",
|
||||
directories: []string{
|
||||
filepath.Join("a", "c"),
|
||||
},
|
||||
files: map[string][]byte{
|
||||
filepath.Join("pkgFile"): {},
|
||||
filepath.Join("a", "c", "a_test.yaml"): readFileA,
|
||||
filepath.Join("a", "c", "c_test.yaml"): readFileB,
|
||||
filepath.Join(".krmignore"): []byte(`
|
||||
d/e/f.yaml
|
||||
`,
|
||||
),
|
||||
filepath.Join("a", "c", "pkgFile"): {},
|
||||
filepath.Join("a", "c", ".krmignore"): []byte(`
|
||||
a_test.yaml
|
||||
`),
|
||||
},
|
||||
expected: []string{
|
||||
`b: b`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "krmignore files does not affect subpackages",
|
||||
directories: []string{
|
||||
filepath.Join("a", "c"),
|
||||
},
|
||||
files: map[string][]byte{
|
||||
filepath.Join("pkgFile"): {},
|
||||
filepath.Join("a", "c", "a_test.yaml"): readFileA,
|
||||
filepath.Join("a", "c", "c_test.yaml"): readFileB,
|
||||
filepath.Join(".krmignore"): []byte(`
|
||||
a/c/c_test.yaml
|
||||
`,
|
||||
),
|
||||
filepath.Join("a", "c", "pkgFile"): {},
|
||||
filepath.Join("a", "c", ".krmignore"): []byte(`
|
||||
a_test.yaml
|
||||
`),
|
||||
},
|
||||
expected: []string{
|
||||
`b: b`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "handles a combination of packages and directories",
|
||||
directories: []string{
|
||||
filepath.Join("a"),
|
||||
filepath.Join("d", "e"),
|
||||
filepath.Join("f"),
|
||||
},
|
||||
files: map[string][]byte{
|
||||
filepath.Join("pkgFile"): {},
|
||||
filepath.Join("d", "pkgFile"): {},
|
||||
filepath.Join("d", "e", "pkgFile"): {},
|
||||
filepath.Join("f", "pkgFile"): {},
|
||||
filepath.Join("manifest.yaml"): []byte(`root: root`),
|
||||
filepath.Join("a", "manifest.yaml"): []byte(`a: a`),
|
||||
filepath.Join("d", "manifest.yaml"): []byte(`d: d`),
|
||||
filepath.Join("d", "e", "manifest.yaml"): []byte(`e: e`),
|
||||
filepath.Join("f", "manifest.yaml"): []byte(`f: f`),
|
||||
filepath.Join("d", ".krmignore"): []byte(`
|
||||
manifest.yaml
|
||||
`),
|
||||
},
|
||||
expected: []string{
|
||||
`a: a`,
|
||||
`e: e`,
|
||||
`f: f`,
|
||||
`root: root`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "ignore file can exclude subpackages",
|
||||
directories: []string{
|
||||
filepath.Join("a"),
|
||||
},
|
||||
files: map[string][]byte{
|
||||
filepath.Join("pkgFile"): {},
|
||||
filepath.Join("a", "pkgFile"): {},
|
||||
filepath.Join("manifest.yaml"): []byte(`root: root`),
|
||||
filepath.Join("a", "manifest.yaml"): []byte(`a: a`),
|
||||
filepath.Join(".krmignore"): []byte(`
|
||||
a
|
||||
`),
|
||||
},
|
||||
expected: []string{
|
||||
`root: root`,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i := range testCases {
|
||||
test := testCases[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
s := SetupDirectories(t, test.directories...)
|
||||
defer s.Clean()
|
||||
for path, content := range test.files {
|
||||
s.WriteFile(t, path, content)
|
||||
}
|
||||
|
||||
// empty path
|
||||
rfr := LocalPackageReader{
|
||||
PackagePath: s.Root,
|
||||
IncludeSubpackages: true,
|
||||
PackageFileName: "pkgFile",
|
||||
OmitReaderAnnotations: true,
|
||||
}
|
||||
nodes, err := rfr.Read()
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
|
||||
if !assert.Len(t, nodes, len(test.expected)) {
|
||||
assert.FailNow(t, "wrong number items")
|
||||
}
|
||||
|
||||
for i, node := range nodes {
|
||||
val, err := node.String()
|
||||
assert.NoError(t, err)
|
||||
want := strings.ReplaceAll(test.expected[i], "${SEP}", string(filepath.Separator))
|
||||
assert.Equal(t, strings.TrimSpace(want), strings.TrimSpace(val))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -80,7 +80,6 @@ func (r *LocalPackageReadWriter) Read() ([]*yaml.RNode, error) {
|
||||
IncludeSubpackages: r.IncludeSubpackages,
|
||||
ErrorIfNonResources: r.ErrorIfNonResources,
|
||||
SetAnnotations: r.SetAnnotations,
|
||||
PackageFileName: r.PackageFileName,
|
||||
}.Read()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
@@ -185,13 +184,8 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
|
||||
|
||||
var operand ResourceNodeSlice
|
||||
var pathRelativeTo string
|
||||
var err error
|
||||
ignoreFilesMatcher := &ignoreFilesMatcher{}
|
||||
r.PackagePath, err = filepath.Abs(r.PackagePath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
err = filepath.Walk(r.PackagePath, func(
|
||||
r.PackagePath = filepath.Clean(r.PackagePath)
|
||||
err := filepath.Walk(r.PackagePath, func(
|
||||
path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
@@ -200,10 +194,9 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
|
||||
// is this the user specified path?
|
||||
if path == r.PackagePath {
|
||||
if info.IsDir() {
|
||||
// skip the root package directory, but check for a
|
||||
// .krmignore file first.
|
||||
// skip the root package directory
|
||||
pathRelativeTo = r.PackagePath
|
||||
return ignoreFilesMatcher.readIgnoreFile(path)
|
||||
return nil
|
||||
}
|
||||
|
||||
// user specified path is a file rather than a directory.
|
||||
@@ -213,9 +206,9 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
|
||||
|
||||
// check if we should skip the directory or file
|
||||
if info.IsDir() {
|
||||
return r.shouldSkipDir(path, ignoreFilesMatcher)
|
||||
return r.ShouldSkipDir(path)
|
||||
}
|
||||
if match, err := r.shouldSkipFile(path, ignoreFilesMatcher); err != nil {
|
||||
if match, err := r.ShouldSkipFile(info); err != nil {
|
||||
return err
|
||||
} else if !match {
|
||||
// skip this file
|
||||
@@ -257,16 +250,11 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode
|
||||
return rr.Read()
|
||||
}
|
||||
|
||||
// shouldSkipFile returns true if the file should be skipped
|
||||
func (r *LocalPackageReader) shouldSkipFile(path string, matcher *ignoreFilesMatcher) (bool, error) {
|
||||
// check if the file is covered by a .krmignore file.
|
||||
if matcher.matchFile(path) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// ShouldSkipFile returns true if the file should be skipped
|
||||
func (r *LocalPackageReader) ShouldSkipFile(info os.FileInfo) (bool, error) {
|
||||
// check if the files are in scope
|
||||
for _, g := range r.MatchFilesGlob {
|
||||
if match, err := filepath.Match(g, filepath.Base(path)); err != nil {
|
||||
if match, err := filepath.Match(g, info.Name()); err != nil {
|
||||
return false, errors.Wrap(err)
|
||||
} else if match {
|
||||
return true, nil
|
||||
@@ -285,12 +273,8 @@ func (r *LocalPackageReader) initReaderAnnotations(path string, _ os.FileInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
// shouldSkipDir returns a filepath.SkipDir if the directory should be skipped
|
||||
func (r *LocalPackageReader) shouldSkipDir(path string, matcher *ignoreFilesMatcher) error {
|
||||
if matcher.matchDir(path) {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
|
||||
// ShouldSkipDir returns a filepath.SkipDir if the directory should be skipped
|
||||
func (r *LocalPackageReader) ShouldSkipDir(path string) error {
|
||||
if r.PackageFileName == "" {
|
||||
return nil
|
||||
}
|
||||
@@ -304,5 +288,5 @@ func (r *LocalPackageReader) shouldSkipDir(path string, matcher *ignoreFilesMatc
|
||||
if !r.IncludeSubpackages {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return matcher.readIgnoreFile(path)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,14 +4,59 @@
|
||||
package kio_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
. "sigs.k8s.io/kustomize/kyaml/kio"
|
||||
// "sigs.k8s.io/kustomize/kyaml/testutil"
|
||||
)
|
||||
|
||||
// setup creates directories and files for testing
|
||||
type setup struct {
|
||||
// root is the tmp directory
|
||||
root string
|
||||
}
|
||||
|
||||
// setupDirectories creates directories for reading test configuration from
|
||||
func setupDirectories(t *testing.T, dirs ...string) setup {
|
||||
d, err := ioutil.TempDir("", "kyaml-test")
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
err = os.Chdir(d)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
for _, s := range dirs {
|
||||
err = os.MkdirAll(s, 0700)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
}
|
||||
return setup{root: d}
|
||||
}
|
||||
|
||||
// writeFile writes a file under the test directory
|
||||
func (s setup) writeFile(t *testing.T, path string, value []byte) {
|
||||
err := os.MkdirAll(filepath.Dir(filepath.Join(s.root, path)), 0700)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(s.root, path), value, 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// clean deletes the test config
|
||||
func (s setup) clean() {
|
||||
os.RemoveAll(s.root)
|
||||
}
|
||||
|
||||
var readFileA = []byte(`---
|
||||
a: b #first
|
||||
---
|
||||
@@ -49,18 +94,18 @@ func TestLocalPackageReader_Read_empty(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_pkg(t *testing.T) {
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
s.WriteFile(t, filepath.Join("c_test.yaml"), readFileC)
|
||||
s.WriteFile(t, filepath.Join("d_test.yaml"), readFileD)
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
s.writeFile(t, filepath.Join("c_test.yaml"), readFileC)
|
||||
s.writeFile(t, filepath.Join("d_test.yaml"), readFileD)
|
||||
|
||||
paths := []struct {
|
||||
path string
|
||||
}{
|
||||
{path: "./"},
|
||||
{path: s.Root},
|
||||
{path: s.root},
|
||||
}
|
||||
for _, p := range paths {
|
||||
rfr := LocalPackageReader{PackagePath: p.path}
|
||||
@@ -122,13 +167,13 @@ metadata:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_JSON(t *testing.T) {
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
|
||||
s.WriteFile(t, filepath.Join("a_test.json"), []byte(`{
|
||||
s.writeFile(t, filepath.Join("a_test.json"), []byte(`{
|
||||
"a": "b"
|
||||
}`))
|
||||
s.WriteFile(t, filepath.Join("b_test.json"), []byte(`{
|
||||
s.writeFile(t, filepath.Join("b_test.json"), []byte(`{
|
||||
"e": "f",
|
||||
"g": {
|
||||
"h": ["i", "j"]
|
||||
@@ -139,7 +184,7 @@ func TestLocalPackageReader_Read_JSON(t *testing.T) {
|
||||
path string
|
||||
}{
|
||||
{path: "./"},
|
||||
{path: s.Root},
|
||||
{path: s.root},
|
||||
}
|
||||
for _, p := range paths {
|
||||
rfr := LocalPackageReader{PackagePath: p.path, MatchFilesGlob: []string{"*.json"}}
|
||||
@@ -172,16 +217,16 @@ func TestLocalPackageReader_Read_JSON(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_file(t *testing.T) {
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
|
||||
paths := []struct {
|
||||
path string
|
||||
}{
|
||||
{path: "./"},
|
||||
{path: s.Root},
|
||||
{path: s.root},
|
||||
}
|
||||
for _, p := range paths {
|
||||
rfr := LocalPackageReader{PackagePath: filepath.Join(p.path, "a_test.yaml")}
|
||||
@@ -220,16 +265,16 @@ metadata:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_pkgOmitAnnotations(t *testing.T) {
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
|
||||
paths := []struct {
|
||||
path string
|
||||
}{
|
||||
{path: "./"},
|
||||
{path: s.Root},
|
||||
{path: s.root},
|
||||
}
|
||||
for _, p := range paths {
|
||||
// empty path
|
||||
@@ -268,16 +313,16 @@ g:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_nestedDirs(t *testing.T) {
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
||||
|
||||
paths := []struct {
|
||||
path string
|
||||
}{
|
||||
{path: "./"},
|
||||
{path: s.Root},
|
||||
{path: s.root},
|
||||
}
|
||||
for _, p := range paths {
|
||||
// empty path
|
||||
@@ -329,13 +374,13 @@ metadata:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_matchRegex(t *testing.T) {
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
||||
|
||||
// empty path
|
||||
rfr := LocalPackageReader{PackagePath: s.Root, MatchFilesGlob: []string{`a*.yaml`}}
|
||||
rfr := LocalPackageReader{PackagePath: s.root, MatchFilesGlob: []string{`a*.yaml`}}
|
||||
nodes, err := rfr.Read()
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
@@ -369,14 +414,14 @@ metadata:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_skipSubpackage(t *testing.T) {
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
||||
s.WriteFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
||||
s.writeFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
||||
|
||||
// empty path
|
||||
rfr := LocalPackageReader{PackagePath: s.Root, PackageFileName: "pkgFile"}
|
||||
rfr := LocalPackageReader{PackagePath: s.root, PackageFileName: "pkgFile"}
|
||||
nodes, err := rfr.Read()
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
@@ -410,14 +455,14 @@ metadata:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_includeSubpackage(t *testing.T) {
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
||||
s.WriteFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
||||
s.writeFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
||||
|
||||
// empty path
|
||||
rfr := LocalPackageReader{PackagePath: s.Root, IncludeSubpackages: true, PackageFileName: "pkgFile"}
|
||||
rfr := LocalPackageReader{PackagePath: s.root, IncludeSubpackages: true, PackageFileName: "pkgFile"}
|
||||
nodes, err := rfr.Read()
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package kio
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Setup creates directories and files for testing
|
||||
type Setup struct {
|
||||
// root is the tmp directory
|
||||
Root string
|
||||
}
|
||||
|
||||
// setupDirectories creates directories for reading test configuration from
|
||||
func SetupDirectories(t *testing.T, dirs ...string) Setup {
|
||||
d, err := ioutil.TempDir("", "kyaml-test")
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
err = os.Chdir(d)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
for _, s := range dirs {
|
||||
err = os.MkdirAll(s, 0700)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
}
|
||||
return Setup{Root: d}
|
||||
}
|
||||
|
||||
// writeFile writes a file under the test directory
|
||||
func (s Setup) WriteFile(t *testing.T, path string, value []byte) {
|
||||
err := os.MkdirAll(filepath.Dir(filepath.Join(s.Root, path)), 0700)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(s.Root, path), value, 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// clean deletes the test config
|
||||
func (s Setup) Clean() {
|
||||
os.RemoveAll(s.Root)
|
||||
}
|
||||
@@ -6,7 +6,6 @@ package kio
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -34,11 +33,10 @@ var GraphStructures = []string{string(TreeStructureGraph), string(TreeStructureP
|
||||
// TODO(pwittrock): test this package better. it is lower-risk since it is only
|
||||
// used for printing rather than updating or editing.
|
||||
type TreeWriter struct {
|
||||
Writer io.Writer
|
||||
Root string
|
||||
Fields []TreeWriterField
|
||||
Structure TreeStructure
|
||||
OpenAPIFileName string
|
||||
Writer io.Writer
|
||||
Root string
|
||||
Fields []TreeWriterField
|
||||
Structure TreeStructure
|
||||
}
|
||||
|
||||
// TreeWriterField configures a Resource field to be included in the tree
|
||||
@@ -74,7 +72,7 @@ func (p TreeWriter) packageStructure(nodes []*yaml.RNode) error {
|
||||
// create a new branch for the package
|
||||
createOk := pkg != "." // special edge case logic for tree on current working dir
|
||||
if createOk {
|
||||
branch = branch.AddBranch(branchName(p.Root, pkg, p.OpenAPIFileName))
|
||||
branch = branch.AddBranch(pkg)
|
||||
}
|
||||
|
||||
// cache the branch for this package
|
||||
@@ -93,19 +91,6 @@ func (p TreeWriter) packageStructure(nodes []*yaml.RNode) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// branchName takes the root directory and relative path to the directory
|
||||
// and returns the branch name
|
||||
func branchName(root, dirRelPath, openAPIFileName string) string {
|
||||
name := filepath.Base(dirRelPath)
|
||||
_, err := os.Stat(filepath.Join(root, dirRelPath, openAPIFileName))
|
||||
if !os.IsNotExist(err) {
|
||||
// add Pkg: prefix indicating that it is a separate package as it has
|
||||
// openAPIFile
|
||||
return fmt.Sprintf("Pkg: %s", name)
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// Write writes the ascii tree to p.Writer
|
||||
func (p TreeWriter) Write(nodes []*yaml.RNode) error {
|
||||
switch p.Structure {
|
||||
|
||||
@@ -6,6 +6,7 @@ package kio_test
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -75,9 +76,9 @@ spec:
|
||||
└── foo-package
|
||||
├── [f1.yaml] Deployment default/foo
|
||||
├── [f1.yaml] Service default/foo
|
||||
└── 3
|
||||
└── foo-package%s3
|
||||
└── [f3.yaml] Deployment default/foo
|
||||
`), out.String()) {
|
||||
`, string(filepath.Separator)), out.String()) {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@@ -95,7 +95,7 @@ func openapiKustomizationapiSwaggerJson() (*asset, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := bindataFileInfo{name: "openapi/kustomizationapi/swagger.json", size: 3127, mode: os.FileMode(420), modTime: time.Unix(1586909905, 0)}
|
||||
info := bindataFileInfo{name: "openapi/kustomizationapi/swagger.json", size: 3127, mode: os.FileMode(420), modTime: time.Unix(1586844916, 0)}
|
||||
a := &asset{bytes: bytes, info: info}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/go-openapi/spec"
|
||||
@@ -24,11 +23,10 @@ var globalSchema openapiData
|
||||
// openapiData contains the parsed openapi state. this is in a struct rather than
|
||||
// a list of vars so that it can be reset from tests.
|
||||
type openapiData struct {
|
||||
setup sync.Once
|
||||
schema spec.Schema
|
||||
schemaByResourceType map[yaml.TypeMeta]*spec.Schema
|
||||
namespaceabilityByResourceType map[yaml.TypeMeta]bool
|
||||
noUseBuiltInSchema bool
|
||||
setup sync.Once
|
||||
schema spec.Schema
|
||||
schemaByResourceType map[yaml.TypeMeta]*spec.Schema
|
||||
noUseBuiltInSchema bool
|
||||
}
|
||||
|
||||
// ResourceSchema wraps the OpenAPI Schema.
|
||||
@@ -68,41 +66,6 @@ func AddSchemaFromFile(path string) error {
|
||||
return AddSchemaFromFileUsingField(path, SupplementaryOpenAPIFieldName)
|
||||
}
|
||||
|
||||
// DeleteSchemaInFile reads the file at path and removes all the openAPI definitions
|
||||
// present in file from global schema
|
||||
func DeleteSchemaInFile(path string) error {
|
||||
b, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
object, err := yaml.Parse(string(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
definitions, err := object.Pipe(yaml.Lookup(SupplementaryOpenAPIFieldName, "definitions"))
|
||||
if definitions == nil {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fields, err := definitions.Fields()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, field := range fields {
|
||||
_, ok := globalSchema.schema.Definitions[field]
|
||||
if ok {
|
||||
delete(globalSchema.schema.Definitions, field)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddSchemaFromFileUsingField reads the file at path and parses the OpenAPI definitions
|
||||
// from the specified field. If field is the empty string, use the whole document as
|
||||
// OpenAPI.
|
||||
@@ -146,7 +109,7 @@ func AddSchemaFromFileUsingField(path, field string) error {
|
||||
}
|
||||
|
||||
// add the json schema to the global schema
|
||||
err = AddSchema(j)
|
||||
_, err = AddSchema(j)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -154,7 +117,7 @@ func AddSchemaFromFileUsingField(path, field string) error {
|
||||
}
|
||||
|
||||
// AddSchema parses s, and adds definitions from s to the global schema.
|
||||
func AddSchema(s []byte) error {
|
||||
func AddSchema(s []byte) (*spec.Schema, error) {
|
||||
return parse(s)
|
||||
}
|
||||
|
||||
@@ -190,27 +153,19 @@ func AddDefinitions(definitions spec.Definitions) {
|
||||
if !ok || len(exts) != 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
typeMeta, ok := toTypeMeta(exts[0])
|
||||
m, ok := exts[0].(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
globalSchema.schemaByResourceType[typeMeta] = &d
|
||||
}
|
||||
}
|
||||
|
||||
func toTypeMeta(ext interface{}) (yaml.TypeMeta, bool) {
|
||||
m, ok := ext.(map[string]interface{})
|
||||
if !ok {
|
||||
return yaml.TypeMeta{}, false
|
||||
// build the index key and save it
|
||||
g := m[groupKey].(string)
|
||||
apiVersion := m[versionKey].(string)
|
||||
if g != "" {
|
||||
apiVersion = g + "/" + apiVersion
|
||||
}
|
||||
globalSchema.schemaByResourceType[yaml.TypeMeta{Kind: m[kindKey].(string), APIVersion: apiVersion}] = &d
|
||||
}
|
||||
|
||||
g := m[groupKey].(string)
|
||||
apiVersion := m[versionKey].(string)
|
||||
if g != "" {
|
||||
apiVersion = g + "/" + apiVersion
|
||||
}
|
||||
return yaml.TypeMeta{Kind: m[kindKey].(string), APIVersion: apiVersion}, true
|
||||
}
|
||||
|
||||
// Resolve resolves the reference against the global schema
|
||||
@@ -241,19 +196,6 @@ func GetSchema(s string) (*ResourceSchema, error) {
|
||||
return &ResourceSchema{Schema: &sc}, nil
|
||||
}
|
||||
|
||||
// IsNamespaceScoped determines whether a resource is namespace or
|
||||
// cluster-scoped by looking at the information in the openapi schema.
|
||||
// The second return value tells whether the provided type could be found
|
||||
// in the openapi schema. If the value is false here, the scope of the
|
||||
// resource is not known. If the type if found, the first return value will
|
||||
// be true if the resource is namespace-scoped, and false if the type is
|
||||
// cluster-scoped.
|
||||
func IsNamespaceScoped(typeMeta yaml.TypeMeta) (bool, bool) {
|
||||
initSchema()
|
||||
isNamespaceScoped, found := globalSchema.namespaceabilityByResourceType[typeMeta]
|
||||
return isNamespaceScoped, found
|
||||
}
|
||||
|
||||
// SuppressBuiltInSchemaUse can be called to prevent using the built-in Kubernetes
|
||||
// schema as part of the global schema.
|
||||
// Must be called before the schema is used.
|
||||
@@ -391,12 +333,12 @@ func initSchema() {
|
||||
}
|
||||
|
||||
// parse the swagger, this should never fail
|
||||
if err := parse(kubernetesapi.MustAsset(kubernetesAPIAssetName)); err != nil {
|
||||
if _, err := parse(kubernetesapi.MustAsset(kubernetesAPIAssetName)); err != nil {
|
||||
// this should never happen
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := parse(kustomizationapi.MustAsset(kustomizationAPIAssetName)); err != nil {
|
||||
if _, err := parse(kustomizationapi.MustAsset(kustomizationAPIAssetName)); err != nil {
|
||||
// this should never happen
|
||||
panic(err)
|
||||
}
|
||||
@@ -404,55 +346,14 @@ func initSchema() {
|
||||
}
|
||||
|
||||
// parse parses and indexes a single json schema
|
||||
func parse(b []byte) error {
|
||||
var swagger spec.Swagger
|
||||
func parse(b []byte) (*spec.Schema, error) {
|
||||
var sc spec.Schema
|
||||
|
||||
if err := swagger.UnmarshalJSON(b); err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
AddDefinitions(swagger.Definitions)
|
||||
findNamespaceability(swagger.Paths)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// findNamespaceability looks at the api paths for the resource to determine
|
||||
// if it is cluster-scoped or namespace-scoped. The gvk of the resource
|
||||
// for each path is found by looking at the x-kubernetes-group-version-kind
|
||||
// extension. If a path exists for the resource that contains a namespace path
|
||||
// parameter, the resource is namespace-scoped.
|
||||
func findNamespaceability(paths *spec.Paths) {
|
||||
if globalSchema.namespaceabilityByResourceType == nil {
|
||||
globalSchema.namespaceabilityByResourceType = make(map[yaml.TypeMeta]bool)
|
||||
}
|
||||
|
||||
if paths == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for path, pathInfo := range paths.Paths {
|
||||
if pathInfo.Get == nil {
|
||||
continue
|
||||
}
|
||||
gvk, found := pathInfo.Get.VendorExtensible.Extensions[kubernetesGVKExtensionKey]
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
typeMeta, found := toTypeMeta(gvk)
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.Contains(path, "namespaces/{namespace}") {
|
||||
// if we find a namespace path parameter, we just update the map
|
||||
// directly
|
||||
globalSchema.namespaceabilityByResourceType[typeMeta] = true
|
||||
} else if _, found := globalSchema.namespaceabilityByResourceType[typeMeta]; !found {
|
||||
// if the resource doesn't have the namespace path parameter, we
|
||||
// only add it to the map if it doesn't already exist.
|
||||
globalSchema.namespaceabilityByResourceType[typeMeta] = false
|
||||
}
|
||||
if err := sc.UnmarshalJSON(b); err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
AddDefinitions(sc.Definitions)
|
||||
return &sc, nil
|
||||
}
|
||||
|
||||
func resolve(root interface{}, ref *spec.Ref) (*spec.Schema, error) {
|
||||
|
||||
@@ -16,7 +16,7 @@ func TestAddSchema(t *testing.T) {
|
||||
// reset package vars
|
||||
globalSchema = openapiData{}
|
||||
|
||||
err := AddSchema(additionalSchema)
|
||||
_, err := AddSchema(additionalSchema)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func TestNoUseBuiltInSchema_AddSchema(t *testing.T) {
|
||||
globalSchema = openapiData{}
|
||||
|
||||
SuppressBuiltInSchemaUse()
|
||||
err := AddSchema(additionalSchema)
|
||||
_, err := AddSchema(additionalSchema)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -160,82 +160,6 @@ openAPI:
|
||||
fmt.Sprintf("%v", s.Schema.Extensions))
|
||||
}
|
||||
|
||||
func TestDeleteSchemaInFile(t *testing.T) {
|
||||
ResetOpenAPI()
|
||||
inputyaml := `
|
||||
openAPI:
|
||||
definitions:
|
||||
io.k8s.cli.setters.image-name:
|
||||
x-k8s-cli:
|
||||
setter:
|
||||
name: image-name
|
||||
value: "nginx"
|
||||
`
|
||||
f, err := ioutil.TempFile("", "openapi-")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
if !assert.NoError(t, ioutil.WriteFile(f.Name(), []byte(inputyaml), 0600)) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
err = AddSchemaFromFile(f.Name())
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
s, err := GetSchema(`{"$ref": "#/definitions/io.k8s.cli.setters.image-name"}`)
|
||||
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
if !assert.Greater(t, len(globalSchema.schema.Definitions), 200) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.Equal(t, `map[x-k8s-cli:map[setter:map[name:image-name value:nginx]]]`,
|
||||
fmt.Sprintf("%v", s.Schema.Extensions))
|
||||
|
||||
err = DeleteSchemaInFile(f.Name())
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
_, err = GetSchema(`{"$ref": "#/definitions/io.k8s.cli.setters.image-name"}`)
|
||||
|
||||
if !assert.Error(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if !assert.Equal(t, `object has no key "io.k8s.cli.setters.image-name"`, err.Error()) {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSchemaInFileNoDefs(t *testing.T) {
|
||||
ResetOpenAPI()
|
||||
inputyaml := `
|
||||
openAPI:
|
||||
definitions:
|
||||
`
|
||||
f, err := ioutil.TempFile("", "openapi-")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
if !assert.NoError(t, ioutil.WriteFile(f.Name(), []byte(inputyaml), 0600)) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
err = AddSchemaFromFile(f.Name())
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
err = DeleteSchemaInFile(f.Name())
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestPopulateDefsInOpenAPI_Substitution(t *testing.T) {
|
||||
ResetOpenAPI()
|
||||
inputyaml := `
|
||||
@@ -313,100 +237,3 @@ kind: Example
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsNamespaceScoped_builtin(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
typeMeta yaml.TypeMeta
|
||||
expectIsFound bool
|
||||
expectIsNamespaced bool
|
||||
}{
|
||||
{
|
||||
name: "namespacescoped resource",
|
||||
typeMeta: yaml.TypeMeta{
|
||||
APIVersion: "apps/v1",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
expectIsFound: true,
|
||||
expectIsNamespaced: true,
|
||||
},
|
||||
{
|
||||
name: "clusterscoped resource",
|
||||
typeMeta: yaml.TypeMeta{
|
||||
APIVersion: "v1",
|
||||
Kind: "Namespace",
|
||||
},
|
||||
expectIsFound: true,
|
||||
expectIsNamespaced: false,
|
||||
},
|
||||
{
|
||||
name: "unknown resource",
|
||||
typeMeta: yaml.TypeMeta{
|
||||
APIVersion: "custom.io/v1",
|
||||
Kind: "Custom",
|
||||
},
|
||||
expectIsFound: false,
|
||||
},
|
||||
}
|
||||
|
||||
for i := range testCases {
|
||||
test := testCases[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ResetOpenAPI()
|
||||
isNamespaceable, isFound := IsNamespaceScoped(test.typeMeta)
|
||||
|
||||
if !test.expectIsFound {
|
||||
assert.False(t, isFound)
|
||||
return
|
||||
}
|
||||
assert.True(t, isFound)
|
||||
assert.Equal(t, test.expectIsNamespaced, isNamespaceable)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsNamespaceScoped_custom(t *testing.T) {
|
||||
SuppressBuiltInSchemaUse()
|
||||
err := AddSchema([]byte(`
|
||||
{
|
||||
"definitions": {},
|
||||
"paths": {
|
||||
"/apis/custom.io/v1/namespaces/{namespace}/customs/{name}": {
|
||||
"get": {
|
||||
"x-kubernetes-action": "get",
|
||||
"x-kubernetes-group-version-kind": {
|
||||
"group": "custom.io",
|
||||
"kind": "Custom",
|
||||
"version": "v1"
|
||||
}
|
||||
}
|
||||
},
|
||||
"/apis/custom.io/v1/clustercustoms": {
|
||||
"get": {
|
||||
"x-kubernetes-action": "get",
|
||||
"x-kubernetes-group-version-kind": {
|
||||
"group": "custom.io",
|
||||
"kind": "ClusterCustom",
|
||||
"version": "v1"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`))
|
||||
assert.NoError(t, err)
|
||||
|
||||
isNamespaceable, isFound := IsNamespaceScoped(yaml.TypeMeta{
|
||||
APIVersion: "custom.io/v1",
|
||||
Kind: "ClusterCustom",
|
||||
})
|
||||
assert.True(t, isFound)
|
||||
assert.False(t, isNamespaceable)
|
||||
|
||||
isNamespaceable, isFound = IsNamespaceScoped(yaml.TypeMeta{
|
||||
APIVersion: "custom.io/v1",
|
||||
Kind: "Custom",
|
||||
})
|
||||
assert.True(t, isFound)
|
||||
assert.True(t, isNamespaceable)
|
||||
}
|
||||
|
||||
@@ -6,29 +6,23 @@ package pathutil
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DirsWithFile takes the root directory path and returns all the paths of
|
||||
// sub-directories(including itself) which contain file with input fileName
|
||||
// at top level if recurse is true
|
||||
func DirsWithFile(root, fileName string, recurse bool) ([]string, error) {
|
||||
// SubDirsWithFile takes the root directory path and returns all the paths of
|
||||
// sub-directories which contain file with input fileName including itself
|
||||
func SubDirsWithFile(root, fileName string) ([]string, error) {
|
||||
var res []string
|
||||
if !recurse {
|
||||
// check if the file with fileName is present in root and return it
|
||||
// else return empty list
|
||||
_, err := os.Stat(filepath.Join(root, fileName))
|
||||
if !os.IsNotExist(err) {
|
||||
res = append(res, filepath.Clean(root))
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
err := filepath.Walk(root,
|
||||
func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if filepath.Base(path) == fileName {
|
||||
res = append(res, filepath.Dir(path))
|
||||
if strings.HasSuffix(path, fileName) {
|
||||
if root == "." {
|
||||
path = root + "/" + path
|
||||
}
|
||||
res = append(res, path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -13,32 +13,6 @@ import (
|
||||
)
|
||||
|
||||
func TestSubDirsWithFile(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
fileName string
|
||||
recurse bool
|
||||
outFilesCount int
|
||||
}{
|
||||
{
|
||||
name: "dirs-with-file-recurse",
|
||||
fileName: "Krmfile",
|
||||
outFilesCount: 3,
|
||||
recurse: true,
|
||||
},
|
||||
{
|
||||
name: "dirs-with-non-existent-file-recurse",
|
||||
fileName: "non-existent-file.txt",
|
||||
outFilesCount: 0,
|
||||
recurse: true,
|
||||
},
|
||||
{
|
||||
name: "dir-with-file-no-recurse",
|
||||
fileName: "Krmfile",
|
||||
outFilesCount: 1,
|
||||
recurse: false,
|
||||
},
|
||||
}
|
||||
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
@@ -49,17 +23,28 @@ func TestSubDirsWithFile(t *testing.T) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
for i := range tests {
|
||||
test := tests[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
res, err := DirsWithFile(dir, test.fileName, test.recurse)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
if !assert.Equal(t, test.outFilesCount, len(res)) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
res, err := SubDirsWithFile(dir, "Krmfile")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
if !assert.Equal(t, 3, len(res)) {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubDirsWithFileNoMatch(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
res, err := SubDirsWithFile(dir, "non-existent-file.txt")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
var expected []string
|
||||
if !assert.Equal(t, expected, res) {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -135,18 +135,18 @@ func (dd DeleterDefinition) Filter(object *yaml.RNode) (*yaml.RNode, error) {
|
||||
}
|
||||
|
||||
definitions, err := object.Pipe(yaml.Lookup(openapi.SupplementaryOpenAPIFieldName, "definitions"))
|
||||
if err != nil {
|
||||
if err != nil || definitions == nil {
|
||||
return nil, err
|
||||
}
|
||||
// return error if the setter to be deleted doesn't exist
|
||||
if definitions == nil || definitions.Field(key) == nil {
|
||||
return nil, errors.Errorf("%s %q does not exist", defType, dd.Name)
|
||||
if definitions.Field(key) == nil {
|
||||
return nil, errors.Errorf("%s with name %s does not exist", defType, dd.Name)
|
||||
}
|
||||
|
||||
subst := SubstReferringDefinition(definitions, key)
|
||||
|
||||
if subst != "" {
|
||||
return nil, errors.Errorf("%s %q is used in substitution %q, please delete the parent substitution first", defType, dd.Name, subst)
|
||||
return nil, errors.Errorf("%s is used in substitution %s, please delete the parent substitution first", defType, subst)
|
||||
}
|
||||
|
||||
_, err = definitions.Pipe(yaml.FieldClearer{Name: key})
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user