kyaml: rename annotations and fix linting

This commit is contained in:
Phillip Wittrock
2019-11-07 11:46:42 -08:00
parent e0b46acf2f
commit 3db1111f8e
22 changed files with 242 additions and 223 deletions

View File

@@ -27,7 +27,7 @@ type ByteReadWriter struct {
// Writer is where ResourceNodes are encoded. // Writer is where ResourceNodes are encoded.
Writer io.Writer Writer io.Writer
// OmitReaderAnnotations will configures Read to skip setting the kyaml.kustomize.dev/kio/index // OmitReaderAnnotations will configures Read to skip setting the config.kubernetes.io/index
// annotation on Resources as they are Read. // annotation on Resources as they are Read.
OmitReaderAnnotations bool OmitReaderAnnotations bool
@@ -68,13 +68,13 @@ func (rw *ByteReadWriter) Write(nodes []*yaml.RNode) error {
} }
// ByteReader decodes ResourceNodes from bytes. // ByteReader decodes ResourceNodes from bytes.
// By default, Read will set the kyaml.kustomize.dev/kio/index annotation on each RNode as it // By default, Read will set the config.kubernetes.io/index annotation on each RNode as it
// is read so they can be written back in the same order. // is read so they can be written back in the same order.
type ByteReader struct { type ByteReader struct {
// Reader is where ResourceNodes are decoded from. // Reader is where ResourceNodes are decoded from.
Reader io.Reader Reader io.Reader
// OmitReaderAnnotations will configures Read to skip setting the kyaml.kustomize.dev/kio/index // OmitReaderAnnotations will configures Read to skip setting the config.kubernetes.io/index
// annotation on Resources as they are Read. // annotation on Resources as they are Read.
OmitReaderAnnotations bool OmitReaderAnnotations bool

View File

@@ -151,7 +151,7 @@ func TestByteReader_Read(t *testing.T) {
c: d c: d
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
`, `,
`# second resource `# second resource
e: f e: f
@@ -159,12 +159,12 @@ g:
- h - h
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
`, `,
`i: j `i: j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 2 config.kubernetes.io/index: 2
`, `,
} }
for i := range nodes { for i := range nodes {
@@ -279,8 +279,8 @@ func TestByteReader_Read_setAnnotations(t *testing.T) {
c: d c: d
metadata: metadata:
annotations: annotations:
config.kubernetes.io/index: 0
foo: bar foo: bar
kyaml.kustomize.dev/kio/index: 0
`, `,
`# second resource `# second resource
e: f e: f
@@ -288,14 +288,14 @@ g:
- h - h
metadata: metadata:
annotations: annotations:
config.kubernetes.io/index: 1
foo: bar foo: bar
kyaml.kustomize.dev/kio/index: 1
`, `,
`i: j `i: j
metadata: metadata:
annotations: annotations:
config.kubernetes.io/index: 2
foo: bar foo: bar
kyaml.kustomize.dev/kio/index: 2
`, `,
} }
for i := range nodes { for i := range nodes {

View File

@@ -107,8 +107,8 @@ func TestByteWriter_Write_withAnnotationsKeepAnnotations(t *testing.T) {
node1, err := yaml.Parse(`a: b #first node1, err := yaml.Parse(`a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
@@ -116,8 +116,8 @@ metadata:
node2, err := yaml.Parse(`c: d # second node2, err := yaml.Parse(`c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
@@ -129,8 +129,8 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" config.kubernetes.io/path: "a/b/b_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
@@ -145,14 +145,14 @@ metadata:
assert.Equal(t, `a: b #first assert.Equal(t, `a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
--- ---
c: d # second c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
--- ---
e: f e: f
g: g:
@@ -161,8 +161,8 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" config.kubernetes.io/path: "a/b/b_test.yaml"
`, buff.String()) `, buff.String())
} }
@@ -173,8 +173,8 @@ func TestByteWriter_Write_withAnnotations(t *testing.T) {
node1, err := yaml.Parse(`a: b #first node1, err := yaml.Parse(`a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
@@ -182,8 +182,8 @@ metadata:
node2, err := yaml.Parse(`c: d # second node2, err := yaml.Parse(`c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
@@ -195,8 +195,8 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" config.kubernetes.io/path: "a/b/b_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
@@ -211,12 +211,12 @@ metadata:
assert.Equal(t, `a: b #first assert.Equal(t, `a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
--- ---
c: d # second c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
--- ---
e: f e: f
g: g:
@@ -225,7 +225,7 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" config.kubernetes.io/path: "a/b/b_test.yaml"
`, buff.String()) `, buff.String())
} }
@@ -235,7 +235,7 @@ func TestByteWriter_Write_partialAnnotations(t *testing.T) {
node1, err := yaml.Parse(`a: b #first node1, err := yaml.Parse(`a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
@@ -243,8 +243,8 @@ metadata:
node2, err := yaml.Parse(`c: d # second node2, err := yaml.Parse(`c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
@@ -274,11 +274,11 @@ g:
a: b #first a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
--- ---
c: d # second c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
`, buff.String()) `, buff.String())
} }

View File

@@ -1,3 +1,6 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// Package kio contains libraries for reading and writing collections of Resources. // Package kio contains libraries for reading and writing collections of Resources.
// //
// Reading Resources // Reading Resources

View File

@@ -100,13 +100,13 @@ items:
metadata: metadata:
name: deployment-foo name: deployment-foo
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
- apiVersion: v1 - apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: service-foo name: service-foo
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
functionConfig: {apiVersion: apps/v1, kind: Deployment, metadata: {name: foo}} functionConfig: {apiVersion: apps/v1, kind: Deployment, metadata: {name: foo}}
`, s) { `, s) {
t.FailNow() t.FailNow()
@@ -131,14 +131,14 @@ kind: StatefulSet
metadata: metadata:
name: deployment-foo name: deployment-foo
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: service-foo name: service-foo
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
`, b.String()) `, b.String())
} }
@@ -182,13 +182,13 @@ items:
metadata: metadata:
name: deployment-foo name: deployment-foo
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
- apiVersion: v1 - apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: service-foo name: service-foo
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
functionConfig: {apiversion: apps/v1, kind: Deployment, metadata: {name: foo}} functionConfig: {apiversion: apps/v1, kind: Deployment, metadata: {name: foo}}
`, s) { `, s) {
t.FailNow() t.FailNow()
@@ -213,14 +213,14 @@ kind: Deployment
metadata: metadata:
name: deployment-foo name: deployment-foo
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: service-foo name: service-foo
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
`, b.String()) `, b.String())
} }

View File

@@ -41,7 +41,8 @@ func (t *KFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal(meta); err != nil { if err := unmarshal(meta); err != nil {
return err return err
} }
if filter, found := Filters[meta.Kind]; !found { filter, found := Filters[meta.Kind]
if !found {
var knownFilters []string var knownFilters []string
for k := range Filters { for k := range Filters {
knownFilters = append(knownFilters, k) knownFilters = append(knownFilters, k)
@@ -49,9 +50,9 @@ func (t *KFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
sort.Strings(knownFilters) sort.Strings(knownFilters)
return fmt.Errorf("unsupported filter Kind %v: may be one of: [%s]", return fmt.Errorf("unsupported filter Kind %v: may be one of: [%s]",
meta, strings.Join(knownFilters, ",")) meta, strings.Join(knownFilters, ","))
} else {
t.Filter = filter()
} }
t.Filter = filter()
return unmarshal(t.Filter) return unmarshal(t.Filter)
} }

View File

@@ -53,21 +53,21 @@ metadata:
name: foo1 name: foo1
namespace: bar namespace: bar
annotations: annotations:
kyaml.kustomize.dev/kio/path: foo1_deployment.yaml config.kubernetes.io/path: foo1_deployment.yaml
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: foo1 name: foo1
annotations: annotations:
kyaml.kustomize.dev/kio/path: foo1_service.yaml config.kubernetes.io/path: foo1_service.yaml
--- ---
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: foo2 name: foo2
annotations: annotations:
kyaml.kustomize.dev/kio/path: foo2_deployment.yaml config.kubernetes.io/path: foo2_deployment.yaml
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
@@ -75,7 +75,7 @@ metadata:
name: foo2 name: foo2
namespace: bar namespace: bar
annotations: annotations:
kyaml.kustomize.dev/kio/path: foo2_service.yaml config.kubernetes.io/path: foo2_service.yaml
`, out.String()) `, out.String())
} }
@@ -97,7 +97,7 @@ kind: Service
metadata: metadata:
name: foo1 name: foo1
annotations: annotations:
kyaml.kustomize.dev/kio/path: foo1__service.yaml config.kubernetes.io/path: foo1__service.yaml
--- ---
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
@@ -105,14 +105,14 @@ metadata:
name: foo1 name: foo1
namespace: bar namespace: bar
annotations: annotations:
kyaml.kustomize.dev/kio/path: foo1_bar_deployment.yaml config.kubernetes.io/path: foo1_bar_deployment.yaml
--- ---
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: foo2 name: foo2
annotations: annotations:
kyaml.kustomize.dev/kio/path: foo2__deployment.yaml config.kubernetes.io/path: foo2__deployment.yaml
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
@@ -120,7 +120,7 @@ metadata:
name: foo2 name: foo2
namespace: bar namespace: bar
annotations: annotations:
kyaml.kustomize.dev/kio/path: foo2_bar_service.yaml config.kubernetes.io/path: foo2_bar_service.yaml
`, out.String()) `, out.String())
} }
@@ -143,14 +143,14 @@ metadata:
name: foo1 name: foo1
namespace: bar namespace: bar
annotations: annotations:
kyaml.kustomize.dev/kio/path: resource.yaml config.kubernetes.io/path: resource.yaml
--- ---
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: foo2 name: foo2
annotations: annotations:
kyaml.kustomize.dev/kio/path: resource.yaml config.kubernetes.io/path: resource.yaml
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
@@ -158,13 +158,13 @@ metadata:
name: foo2 name: foo2
namespace: bar namespace: bar
annotations: annotations:
kyaml.kustomize.dev/kio/path: resource.yaml config.kubernetes.io/path: resource.yaml
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: foo1 name: foo1
annotations: annotations:
kyaml.kustomize.dev/kio/path: resource.yaml config.kubernetes.io/path: resource.yaml
`, out.String()) `, out.String())
} }

View File

@@ -1,16 +1,5 @@
// Copyright 2019 Google LLC // Copyright 2019 The Kubernetes Authors.
// // SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package filters package filters

View File

@@ -37,6 +37,12 @@ func (fn WriterFunc) Write(o []*yaml.RNode) error {
return fn(o) return fn(o)
} }
// ReaderWriter implements both Reader and Writer interfaces
type ReaderWriter interface {
Reader
Writer
}
// Filter modifies a collection of Resource Configuration by returning the modified slice. // Filter modifies a collection of Resource Configuration by returning the modified slice.
// When possible, Filters should be serializable to yaml so that they can be described // When possible, Filters should be serializable to yaml so that they can be described
// as either data or code. // as either data or code.
@@ -106,3 +112,16 @@ func (p Pipeline) Execute() error {
} }
return nil return nil
} }
// FilterAll runs the yaml.Filter against all inputs
func FilterAll(filter yaml.Filter) Filter {
return FilterFunc(func(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
for i := range nodes {
_, err := filter.Filter(nodes[i])
if err != nil {
return nil, err
}
}
return nodes, nil
})
}

View File

@@ -15,13 +15,13 @@ type AnnotationKey = string
const ( const (
// IndexAnnotation records the index of a specific resource in a file or input stream. // IndexAnnotation records the index of a specific resource in a file or input stream.
IndexAnnotation AnnotationKey = "kyaml.kustomize.dev/kio/index" IndexAnnotation AnnotationKey = "config.kubernetes.io/index"
// PathAnnotation records the path to the file the Resource was read from // PathAnnotation records the path to the file the Resource was read from
PathAnnotation AnnotationKey = "kyaml.kustomize.dev/kio/path" PathAnnotation AnnotationKey = "config.kubernetes.io/path"
// PackageAnnotation records the name of the package the Resource was read from // PackageAnnotation records the name of the package the Resource was read from
PackageAnnotation AnnotationKey = "kyaml.kustomize.dev/kio/package" PackageAnnotation AnnotationKey = "config.kubernetes.io/package"
) )
func GetFileAnnotations(rn *yaml.RNode) (string, string, error) { func GetFileAnnotations(rn *yaml.RNode) (string, string, error) {
@@ -50,6 +50,21 @@ func ErrorIfMissingAnnotation(nodes []*yaml.RNode, keys ...AnnotationKey) error
return nil return nil
} }
// Map invokes fn for each element in nodes.
func Map(nodes []*yaml.RNode, fn func(*yaml.RNode) (*yaml.RNode, error)) ([]*yaml.RNode, error) {
var returnNodes []*yaml.RNode
for i := range nodes {
n, err := fn(nodes[i])
if err != nil {
return nil, err
}
if n != nil {
returnNodes = append(returnNodes, n)
}
}
return returnNodes, nil
}
// SortNodes sorts nodes in place: // SortNodes sorts nodes in place:
// - by PathAnnotation annotation // - by PathAnnotation annotation
// - by IndexAnnotation annotation // - by IndexAnnotation annotation
@@ -92,12 +107,12 @@ func SortNodes(nodes []*yaml.RNode) error {
var iIndex, jIndex int var iIndex, jIndex int
iIndex, err = strconv.Atoi(iValue) iIndex, err = strconv.Atoi(iValue)
if err != nil { if err != nil {
err = fmt.Errorf("unable to parse kyaml.kustomize.dev/kio/index %s :%v", iValue, err) err = fmt.Errorf("unable to parse config.kubernetes.io/index %s :%v", iValue, err)
return false return false
} }
jIndex, err = strconv.Atoi(jValue) jIndex, err = strconv.Atoi(jValue)
if err != nil { if err != nil {
err = fmt.Errorf("unable to parse kyaml.kustomize.dev/kio/index %s :%v", jValue, err) err = fmt.Errorf("unable to parse config.kubernetes.io/index %s :%v", jValue, err)
return false return false
} }
if iIndex != jIndex { if iIndex != jIndex {

View File

@@ -200,9 +200,9 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
// check if we should skip the directory or file // check if we should skip the directory or file
if info.IsDir() { if info.IsDir() {
return r.shouldSkipDir(path, info) return r.shouldSkipDir(path)
} }
if match, err := r.shouldSkipFile(path, info); err != nil { if match, err := r.shouldSkipFile(info); err != nil {
return err return err
} else if !match { } else if !match {
// skip this file // skip this file
@@ -243,7 +243,7 @@ func (r *LocalPackageReader) readFile(path string, info os.FileInfo) ([]*yaml.RN
} }
// shouldSkipFile returns true if the file should be skipped // shouldSkipFile returns true if the file should be skipped
func (r *LocalPackageReader) shouldSkipFile(path string, info os.FileInfo) (bool, error) { func (r *LocalPackageReader) shouldSkipFile(info os.FileInfo) (bool, error) {
// check if the files are in scope // check if the files are in scope
for _, g := range r.MatchFilesGlob { for _, g := range r.MatchFilesGlob {
if match, err := filepath.Match(g, info.Name()); err != nil { if match, err := filepath.Match(g, info.Name()); err != nil {
@@ -267,7 +267,7 @@ func (r *LocalPackageReader) initReaderAnnotations(path string, info os.FileInfo
} }
// shouldSkipDir returns a filepath.SkipDir if the directory should be skipped // shouldSkipDir returns a filepath.SkipDir if the directory should be skipped
func (r *LocalPackageReader) shouldSkipDir(path string, info os.FileInfo) error { func (r *LocalPackageReader) shouldSkipDir(path string) error {
if r.PackageFileName == "" { if r.PackageFileName == "" {
return nil return nil
} }

View File

@@ -108,16 +108,16 @@ func TestLocalPackageReader_Read_pkg(t *testing.T) {
`a: b #first `a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: a_test.yaml config.kubernetes.io/path: a_test.yaml
`, `,
`c: d # second `c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: a_test.yaml config.kubernetes.io/path: a_test.yaml
`, `,
`# second thing `# second thing
e: f e: f
@@ -127,9 +127,9 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: b_test.yaml config.kubernetes.io/path: b_test.yaml
`, `,
} }
for i := range nodes { for i := range nodes {
@@ -170,16 +170,16 @@ func TestLocalPackageReader_Read_file(t *testing.T) {
`a: b #first `a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: a_test.yaml config.kubernetes.io/path: a_test.yaml
`, `,
`c: d # second `c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: a_test.yaml config.kubernetes.io/path: a_test.yaml
`, `,
} }
for i := range nodes { for i := range nodes {
@@ -270,16 +270,16 @@ func TestLocalPackageReader_Read_nestedDirs(t *testing.T) {
`a: b #first `a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/package: a/b config.kubernetes.io/package: a/b
kyaml.kustomize.dev/kio/path: a/b/a_test.yaml config.kubernetes.io/path: a/b/a_test.yaml
`, `,
`c: d # second `c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/package: a/b config.kubernetes.io/package: a/b
kyaml.kustomize.dev/kio/path: a/b/a_test.yaml config.kubernetes.io/path: a/b/a_test.yaml
`, `,
`# second thing `# second thing
e: f e: f
@@ -289,9 +289,9 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/package: a/b config.kubernetes.io/package: a/b
kyaml.kustomize.dev/kio/path: a/b/b_test.yaml config.kubernetes.io/path: a/b/b_test.yaml
`, `,
} }
for i := range nodes { for i := range nodes {
@@ -328,9 +328,9 @@ func TestLocalPackageReader_Read_matchRegex(t *testing.T) {
assert.Equal(t, `a: b #first assert.Equal(t, `a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/package: a/b config.kubernetes.io/package: a/b
kyaml.kustomize.dev/kio/path: a/b/a_test.yaml config.kubernetes.io/path: a/b/a_test.yaml
`, val) `, val)
val, err = nodes[1].String() val, err = nodes[1].String()
@@ -338,9 +338,9 @@ metadata:
assert.Equal(t, `c: d # second assert.Equal(t, `c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/package: a/b config.kubernetes.io/package: a/b
kyaml.kustomize.dev/kio/path: a/b/a_test.yaml config.kubernetes.io/path: a/b/a_test.yaml
`, val) `, val)
} }
@@ -367,9 +367,9 @@ func TestLocalPackageReader_Read_skipSubpackage(t *testing.T) {
assert.Equal(t, `a: b #first assert.Equal(t, `a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/package: a/b config.kubernetes.io/package: a/b
kyaml.kustomize.dev/kio/path: a/b/a_test.yaml config.kubernetes.io/path: a/b/a_test.yaml
`, val) `, val)
val, err = nodes[1].String() val, err = nodes[1].String()
@@ -377,9 +377,9 @@ metadata:
assert.Equal(t, `c: d # second assert.Equal(t, `c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/package: a/b config.kubernetes.io/package: a/b
kyaml.kustomize.dev/kio/path: a/b/a_test.yaml config.kubernetes.io/path: a/b/a_test.yaml
`, val) `, val)
} }
@@ -405,9 +405,9 @@ func TestLocalPackageReader_Read_includeSubpackage(t *testing.T) {
assert.Equal(t, `a: b #first assert.Equal(t, `a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/package: a/b config.kubernetes.io/package: a/b
kyaml.kustomize.dev/kio/path: a/b/a_test.yaml config.kubernetes.io/path: a/b/a_test.yaml
`, val) `, val)
val, err = nodes[1].String() val, err = nodes[1].String()
@@ -415,9 +415,9 @@ metadata:
assert.Equal(t, `c: d # second assert.Equal(t, `c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/package: a/b config.kubernetes.io/package: a/b
kyaml.kustomize.dev/kio/path: a/b/a_test.yaml config.kubernetes.io/path: a/b/a_test.yaml
`, val) `, val)
val, err = nodes[2].String() val, err = nodes[2].String()
@@ -430,9 +430,9 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/package: a/c config.kubernetes.io/package: a/c
kyaml.kustomize.dev/kio/path: a/c/c_test.yaml config.kubernetes.io/path: a/c/c_test.yaml
`, val) `, val)
} }

View File

@@ -69,7 +69,7 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error {
return err return err
} }
if st.IsDir() { if st.IsDir() {
return fmt.Errorf("kyaml.kustomize.dev/kio/path cannot be a directory: %s", path) return fmt.Errorf("config.kubernetes.io/path cannot be a directory: %s", path)
} }
} }

View File

@@ -67,14 +67,14 @@ func TestLocalPackageWriter_Write_keepReaderAnnotations(t *testing.T) {
assert.Equal(t, `a: b #first assert.Equal(t, `a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: a/b/a_test.yaml config.kubernetes.io/path: a/b/a_test.yaml
--- ---
c: d # second c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/path: a/b/a_test.yaml config.kubernetes.io/path: a/b/a_test.yaml
`, string(b)) `, string(b))
b, err = ioutil.ReadFile(filepath.Join(d, "a", "b", "b_test.yaml")) b, err = ioutil.ReadFile(filepath.Join(d, "a", "b", "b_test.yaml"))
@@ -88,8 +88,8 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: a/b/b_test.yaml config.kubernetes.io/path: a/b/b_test.yaml
`, string(b)) `, string(b))
} }
@@ -99,7 +99,7 @@ func TestLocalPackageWriter_Write_clearAnnotations(t *testing.T) {
d, node1, node2, node3 := getWriterInputs(t) d, node1, node2, node3 := getWriterInputs(t)
defer os.RemoveAll(d) defer os.RemoveAll(d)
w := LocalPackageWriter{PackagePath: d, ClearAnnotations: []string{"kyaml.kustomize.dev/kio/mode"}} w := LocalPackageWriter{PackagePath: d, ClearAnnotations: []string{"config.kubernetes.io/mode"}}
err := w.Write([]*yaml.RNode{node2, node1, node3}) err := w.Write([]*yaml.RNode{node2, node1, node3})
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
assert.FailNow(t, err.Error()) assert.FailNow(t, err.Error())
@@ -139,8 +139,8 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: "a/b/../../../b_test.yaml" config.kubernetes.io/path: "a/b/../../../b_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
assert.FailNow(t, err.Error()) assert.FailNow(t, err.Error())
@@ -166,8 +166,8 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: a config.kubernetes.io/index: a
kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" # use a different path, should still collide config.kubernetes.io/path: "a/b/b_test.yaml" # use a different path, should still collide
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
assert.FailNow(t, err.Error()) assert.FailNow(t, err.Error())
@@ -176,12 +176,12 @@ metadata:
w := LocalPackageWriter{PackagePath: d} w := LocalPackageWriter{PackagePath: d}
err = w.Write([]*yaml.RNode{node2, node1, node3, node4}) err = w.Write([]*yaml.RNode{node2, node1, node3, node4})
if assert.Error(t, err) { if assert.Error(t, err) {
assert.Contains(t, err.Error(), "unable to parse kyaml.kustomize.dev/kio/index") assert.Contains(t, err.Error(), "unable to parse config.kubernetes.io/index")
} }
} }
// TestLocalPackageWriter_Write_absPath tests: // TestLocalPackageWriter_Write_absPath tests:
// - If kyaml.kustomize.dev/kio/path is absolute, fail // - If config.kubernetes.io/path is absolute, fail
func TestLocalPackageWriter_Write_absPath(t *testing.T) { func TestLocalPackageWriter_Write_absPath(t *testing.T) {
d, node1, node2, node3 := getWriterInputs(t) d, node1, node2, node3 := getWriterInputs(t)
defer os.RemoveAll(d) defer os.RemoveAll(d)
@@ -193,8 +193,8 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: a config.kubernetes.io/index: a
kyaml.kustomize.dev/kio/path: "%s/a/b/b_test.yaml" # use a different path, should still collide config.kubernetes.io/path: "%s/a/b/b_test.yaml" # use a different path, should still collide
`, d)) `, d))
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
assert.FailNow(t, err.Error()) assert.FailNow(t, err.Error())
@@ -208,7 +208,7 @@ metadata:
} }
// TestLocalPackageWriter_Write_missingIndex tests: // TestLocalPackageWriter_Write_missingIndex tests:
// - If kyaml.kustomize.dev/kio/path is missing, fail // - If config.kubernetes.io/path is missing, fail
func TestLocalPackageWriter_Write_missingPath(t *testing.T) { func TestLocalPackageWriter_Write_missingPath(t *testing.T) {
d, node1, node2, node3 := getWriterInputs(t) d, node1, node2, node3 := getWriterInputs(t)
defer os.RemoveAll(d) defer os.RemoveAll(d)
@@ -220,7 +220,7 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: a config.kubernetes.io/index: a
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
assert.FailNow(t, err.Error()) assert.FailNow(t, err.Error())
@@ -229,12 +229,12 @@ metadata:
w := LocalPackageWriter{PackagePath: d} w := LocalPackageWriter{PackagePath: d}
err = w.Write([]*yaml.RNode{node2, node1, node3, node4}) err = w.Write([]*yaml.RNode{node2, node1, node3, node4})
if assert.Error(t, err) { if assert.Error(t, err) {
assert.Contains(t, err.Error(), "kyaml.kustomize.dev/kio/path") assert.Contains(t, err.Error(), "config.kubernetes.io/path")
} }
} }
// TestLocalPackageWriter_Write_missingIndex tests: // TestLocalPackageWriter_Write_missingIndex tests:
// - If kyaml.kustomize.dev/kio/index is missing, fail // - If config.kubernetes.io/index is missing, fail
func TestLocalPackageWriter_Write_missingIndex(t *testing.T) { func TestLocalPackageWriter_Write_missingIndex(t *testing.T) {
d, node1, node2, node3 := getWriterInputs(t) d, node1, node2, node3 := getWriterInputs(t)
defer os.RemoveAll(d) defer os.RemoveAll(d)
@@ -246,7 +246,7 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/path: a/a.yaml config.kubernetes.io/path: a/a.yaml
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
assert.FailNow(t, err.Error()) assert.FailNow(t, err.Error())
@@ -255,12 +255,12 @@ metadata:
w := LocalPackageWriter{PackagePath: d} w := LocalPackageWriter{PackagePath: d}
err = w.Write([]*yaml.RNode{node2, node1, node3, node4}) err = w.Write([]*yaml.RNode{node2, node1, node3, node4})
if assert.Error(t, err) { if assert.Error(t, err) {
assert.Contains(t, err.Error(), "kyaml.kustomize.dev/kio/index") assert.Contains(t, err.Error(), "config.kubernetes.io/index")
} }
} }
// TestLocalPackageWriter_Write_pathIsDir tests: // TestLocalPackageWriter_Write_pathIsDir tests:
// - If kyaml.kustomize.dev/kio/path is a directory, fail // - If config.kubernetes.io/path is a directory, fail
func TestLocalPackageWriter_Write_pathIsDir(t *testing.T) { func TestLocalPackageWriter_Write_pathIsDir(t *testing.T) {
d, node1, node2, node3 := getWriterInputs(t) d, node1, node2, node3 := getWriterInputs(t)
defer os.RemoveAll(d) defer os.RemoveAll(d)
@@ -272,8 +272,8 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/path: a/ config.kubernetes.io/path: a/
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
assert.FailNow(t, err.Error()) assert.FailNow(t, err.Error())
@@ -282,7 +282,7 @@ metadata:
w := LocalPackageWriter{PackagePath: d} w := LocalPackageWriter{PackagePath: d}
err = w.Write([]*yaml.RNode{node2, node1, node3, node4}) err = w.Write([]*yaml.RNode{node2, node1, node3, node4})
if assert.Error(t, err) { if assert.Error(t, err) {
assert.Contains(t, err.Error(), "kyaml.kustomize.dev/kio/path cannot be a directory") assert.Contains(t, err.Error(), "config.kubernetes.io/path cannot be a directory")
} }
} }
@@ -290,8 +290,8 @@ func getWriterInputs(t *testing.T) (string, *yaml.RNode, *yaml.RNode, *yaml.RNod
node1, err := yaml.Parse(`a: b #first node1, err := yaml.Parse(`a: b #first
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
assert.FailNow(t, err.Error()) assert.FailNow(t, err.Error())
@@ -299,8 +299,8 @@ metadata:
node2, err := yaml.Parse(`c: d # second node2, err := yaml.Parse(`c: d # second
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 1 config.kubernetes.io/index: 1
kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" config.kubernetes.io/path: "a/b/a_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
assert.FailNow(t, err.Error()) assert.FailNow(t, err.Error())
@@ -312,8 +312,8 @@ g:
- j - j
metadata: metadata:
annotations: annotations:
kyaml.kustomize.dev/kio/index: 0 config.kubernetes.io/index: 0
kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" config.kubernetes.io/path: "a/b/b_test.yaml"
`) `)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
assert.FailNow(t, err.Error()) assert.FailNow(t, err.Error())

View File

@@ -21,8 +21,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx3 app: nginx3
kyaml.kustomize.dev/kio/package: foo-package/3 config.kubernetes.io/package: foo-package/3
kyaml.kustomize.dev/kio/path: f3.yaml config.kubernetes.io/path: f3.yaml
spec: spec:
replicas: 1 replicas: 1
--- ---
@@ -34,8 +34,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx2 app: nginx2
kyaml.kustomize.dev/kio/package: foo-package config.kubernetes.io/package: foo-package
kyaml.kustomize.dev/kio/path: f1.yaml config.kubernetes.io/path: f1.yaml
spec: spec:
replicas: 1 replicas: 1
--- ---
@@ -45,8 +45,8 @@ metadata:
app: nginx app: nginx
annotations: annotations:
app: nginx app: nginx
kyaml.kustomize.dev/kio/package: bar-package config.kubernetes.io/package: bar-package
kyaml.kustomize.dev/kio/path: f2.yaml config.kubernetes.io/path: f2.yaml
name: bar name: bar
spec: spec:
replicas: 3 replicas: 3
@@ -57,8 +57,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx app: nginx
kyaml.kustomize.dev/kio/package: foo-package config.kubernetes.io/package: foo-package
kyaml.kustomize.dev/kio/path: f1.yaml config.kubernetes.io/path: f1.yaml
spec: spec:
selector: selector:
app: nginx app: nginx
@@ -94,8 +94,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx3 app: nginx3
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: f3.yaml config.kubernetes.io/path: f3.yaml
spec: spec:
replicas: 1 replicas: 1
--- ---
@@ -107,8 +107,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx2 app: nginx2
kyaml.kustomize.dev/kio/package: foo-package config.kubernetes.io/package: foo-package
kyaml.kustomize.dev/kio/path: f1.yaml config.kubernetes.io/path: f1.yaml
spec: spec:
replicas: 1 replicas: 1
--- ---
@@ -118,8 +118,8 @@ metadata:
app: nginx app: nginx
annotations: annotations:
app: nginx app: nginx
kyaml.kustomize.dev/kio/package: bar-package config.kubernetes.io/package: bar-package
kyaml.kustomize.dev/kio/path: f2.yaml config.kubernetes.io/path: f2.yaml
name: bar name: bar
spec: spec:
replicas: 3 replicas: 3
@@ -130,8 +130,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx app: nginx
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: f1.yaml config.kubernetes.io/path: f1.yaml
spec: spec:
selector: selector:
app: nginx app: nginx
@@ -167,8 +167,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx2 app: nginx2
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: f1.yaml config.kubernetes.io/path: f1.yaml
spec: spec:
replicas: 1 replicas: 1
--- ---
@@ -181,8 +181,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx2 app: nginx2
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: f1.yaml config.kubernetes.io/path: f1.yaml
spec: spec:
replicas: 1 replicas: 1
--- ---
@@ -195,8 +195,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx2 app: nginx2
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: f1.yaml config.kubernetes.io/path: f1.yaml
spec: spec:
replicas: 1 replicas: 1
--- ---
@@ -209,8 +209,8 @@ metadata:
namespace: default2 namespace: default2
annotations: annotations:
app: nginx2 app: nginx2
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: f1.yaml config.kubernetes.io/path: f1.yaml
spec: spec:
replicas: 1 replicas: 1
--- ---
@@ -223,8 +223,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx3 app: nginx3
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: f1.yaml config.kubernetes.io/path: f1.yaml
spec: spec:
replicas: 1 replicas: 1
--- ---
@@ -234,8 +234,8 @@ metadata:
app: nginx app: nginx
annotations: annotations:
app: nginx app: nginx
kyaml.kustomize.dev/kio/package: bar-package config.kubernetes.io/package: bar-package
kyaml.kustomize.dev/kio/path: f2.yaml config.kubernetes.io/path: f2.yaml
name: bar name: bar
spec: spec:
replicas: 3 replicas: 3
@@ -246,8 +246,8 @@ metadata:
namespace: default namespace: default
annotations: annotations:
app: nginx app: nginx
kyaml.kustomize.dev/kio/package: . config.kubernetes.io/package: .
kyaml.kustomize.dev/kio/path: f1.yaml config.kubernetes.io/path: f1.yaml
spec: spec:
selector: selector:
app: nginx app: nginx

View File

@@ -22,7 +22,7 @@ type TypeError = yaml.TypeError
type Unmarshaler = yaml.Unmarshaler type Unmarshaler = yaml.Unmarshaler
var Marshal = yaml.Marshal var Marshal = yaml.Marshal
var UnMarshal = yaml.Unmarshal var Unmarshal = yaml.Unmarshal
var NewDecoder = yaml.NewDecoder var NewDecoder = yaml.NewDecoder
var NewEncoder = func(w io.Writer) *yaml.Encoder { var NewEncoder = func(w io.Writer) *yaml.Encoder {
e := yaml.NewEncoder(w) e := yaml.NewEncoder(w)

View File

@@ -46,7 +46,8 @@ func (y *YFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal(meta); err != nil { if err := unmarshal(meta); err != nil {
return err return err
} }
if filter, found := Filters[meta.Kind]; !found { filter, found := Filters[meta.Kind]
if !found {
var knownFilters []string var knownFilters []string
for k := range Filters { for k := range Filters {
knownFilters = append(knownFilters, k) knownFilters = append(knownFilters, k)
@@ -54,9 +55,9 @@ func (y *YFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
sort.Strings(knownFilters) sort.Strings(knownFilters)
return fmt.Errorf("unsupported GrepFilter Kind %s: may be one of: [%s]", return fmt.Errorf("unsupported GrepFilter Kind %s: may be one of: [%s]",
meta.Kind, strings.Join(knownFilters, ",")) meta.Kind, strings.Join(knownFilters, ","))
} else {
y.Filter = filter()
} }
y.Filter = filter()
if err := unmarshal(y.Filter); err != nil { if err := unmarshal(y.Filter); err != nil {
return err return err
} }

View File

@@ -82,10 +82,9 @@ func (p *PathMatcher) filter(rn *RNode) (*RNode, error) {
if IsListIndex(p.Path[0]) { if IsListIndex(p.Path[0]) {
// match seq elements // match seq elements
return p.doSeq(rn) return p.doSeq(rn)
} else {
// match a field
return p.doField(rn)
} }
// match a field
return p.doField(rn)
} }
func (p *PathMatcher) doField(rn *RNode) (*RNode, error) { func (p *PathMatcher) doField(rn *RNode) (*RNode, error) {

View File

@@ -240,7 +240,7 @@ type ObjectMeta struct {
Annotations map[string]string `yaml:"annotations,omitempty"` Annotations map[string]string `yaml:"annotations,omitempty"`
} }
var MissingMetaError = errors.New("missing Resource metadata") var ErrMissingMetadata = errors.New("missing Resource metadata")
// GetMeta returns the ResourceMeta for a RNode // GetMeta returns the ResourceMeta for a RNode
func (rn *RNode) GetMeta() (ResourceMeta, error) { func (rn *RNode) GetMeta() (ResourceMeta, error) {
@@ -259,7 +259,7 @@ func (rn *RNode) GetMeta() (ResourceMeta, error) {
return m, err return m, err
} }
if reflect.DeepEqual(m, ResourceMeta{}) { if reflect.DeepEqual(m, ResourceMeta{}) {
return m, MissingMetaError return m, ErrMissingMetadata
} }
return m, nil return m, nil
} }
@@ -298,6 +298,13 @@ func (rn *RNode) Pipe(functions ...Filter) (*RNode, error) {
return v, err return v, err
} }
// PipeE runs Pipe, dropping the *RNode return value.
// Useful for directly returning the Pipe error value from functions.
func (rn *RNode) PipeE(functions ...Filter) error {
_, err := rn.Pipe(functions...)
return err
}
// Document returns the Node RNode for the value. Does not unwrap the node if it is a // Document returns the Node RNode for the value. Does not unwrap the node if it is a
// DocumentNodes // DocumentNodes
func (rn *RNode) Document() *yaml.Node { func (rn *RNode) Document() *yaml.Node {
@@ -445,7 +452,7 @@ func (rn *RNode) Elements() ([]*RNode, error) {
return nil, err return nil, err
} }
var elements []*RNode var elements []*RNode
for i := 0; i < len(rn.Content()); i += 1 { for i := 0; i < len(rn.Content()); i++ {
elements = append(elements, NewRNode(rn.Content()[i])) elements = append(elements, NewRNode(rn.Content()[i]))
} }
return elements, nil return elements, nil
@@ -459,7 +466,7 @@ func (rn *RNode) ElementValues(key string) ([]string, error) {
return nil, err return nil, err
} }
var elements []string var elements []string
for i := 0; i < len(rn.Content()); i += 1 { for i := 0; i < len(rn.Content()); i++ {
field := NewRNode(rn.Content()[i]).Field(key) field := NewRNode(rn.Content()[i]).Field(key)
if !IsFieldEmpty(field) { if !IsFieldEmpty(field) {
elements = append(elements, field.Value.YNode().Value) elements = append(elements, field.Value.YNode().Value)
@@ -497,8 +504,7 @@ func (rn *RNode) VisitElements(fn func(node *RNode) error) error {
return nil return nil
} }
// AssociativeSequencePaths is a list of field names that may be used as associative keys // AssociativeSequenceKeys is a map of paths to sequences that have associative keys.
// when merge Resources.
// The order sets the precedence of the merge keys -- if multiple keys are present // The order sets the precedence of the merge keys -- if multiple keys are present
// in Resources in a list, then the FIRST key which ALL elements in the list have is used as the // in Resources in a list, then the FIRST key which ALL elements in the list have is used as the
// associative key for merging that list. // associative key for merging that list.

View File

@@ -24,10 +24,7 @@ func (l *Walker) walkAssociativeSequence() (*yaml.RNode, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
values, err := l.elementValues(key) values := l.elementValues(key)
if err != nil {
return nil, err
}
// recursively set the elements in the list // recursively set the elements in the list
for _, value := range values { for _, value := range values {
@@ -90,7 +87,7 @@ func (l Walker) elementKey() (string, error) {
// from all sources. // from all sources.
// Return value slice is ordered using the original ordering from the elements, where // Return value slice is ordered using the original ordering from the elements, where
// elements missing from earlier sources appear later. // elements missing from earlier sources appear later.
func (l Walker) elementValues(key string) ([]string, error) { func (l Walker) elementValues(key string) []string {
// use slice to to keep elements in the original order // use slice to to keep elements in the original order
// dest node must be first // dest node must be first
var returnValues []string var returnValues []string
@@ -111,7 +108,7 @@ func (l Walker) elementValues(key string) ([]string, error) {
seen.Insert(s) seen.Insert(s)
} }
} }
return returnValues, nil return returnValues
} }
// fieldValue returns a slice containing each source's value for fieldName // fieldValue returns a slice containing each source's value for fieldName

View File

@@ -1,16 +1,5 @@
// Copyright 2019 Google LLC // Copyright 2019 The Kubernetes Authors.
// // SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package walk package walk
@@ -34,5 +23,5 @@ type Visitor interface {
VisitList(nodes Sources, kind ListKind) (*yaml.RNode, error) VisitList(nodes Sources, kind ListKind) (*yaml.RNode, error)
} }
// NoOp is returned if GrepFilter should do nothing after calling Set // ClearNode is returned if GrepFilter should do nothing after calling Set
var ClearNode *yaml.RNode = nil var ClearNode *yaml.RNode

View File

@@ -48,9 +48,9 @@ func (l Walker) Walk() (*yaml.RNode, error) {
} }
if yaml.IsAssociative(l.Sources) { if yaml.IsAssociative(l.Sources) {
return l.walkAssociativeSequence() return l.walkAssociativeSequence()
} else {
return l.walkNonAssociativeSequence()
} }
return l.walkNonAssociativeSequence()
case yaml.ScalarNode: case yaml.ScalarNode:
if err := yaml.ErrorIfAnyInvalidAndNonNull(yaml.ScalarNode, l.Sources...); err != nil { if err := yaml.ErrorIfAnyInvalidAndNonNull(yaml.ScalarNode, l.Sources...); err != nil {
return nil, err return nil, err