diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 0875058fb..2ece946cc 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -27,7 +27,7 @@ type ByteReadWriter struct { // Writer is where ResourceNodes are encoded. 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. OmitReaderAnnotations bool @@ -68,13 +68,13 @@ func (rw *ByteReadWriter) Write(nodes []*yaml.RNode) error { } // 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. type ByteReader struct { // Reader is where ResourceNodes are decoded from. 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. OmitReaderAnnotations bool diff --git a/kyaml/kio/byteio_reader_test.go b/kyaml/kio/byteio_reader_test.go index 61edfb8e9..cb4604dec 100644 --- a/kyaml/kio/byteio_reader_test.go +++ b/kyaml/kio/byteio_reader_test.go @@ -151,7 +151,7 @@ func TestByteReader_Read(t *testing.T) { c: d metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 + config.kubernetes.io/index: 0 `, `# second resource e: f @@ -159,12 +159,12 @@ g: - h metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 + config.kubernetes.io/index: 1 `, `i: j metadata: annotations: - kyaml.kustomize.dev/kio/index: 2 + config.kubernetes.io/index: 2 `, } for i := range nodes { @@ -279,8 +279,8 @@ func TestByteReader_Read_setAnnotations(t *testing.T) { c: d metadata: annotations: + config.kubernetes.io/index: 0 foo: bar - kyaml.kustomize.dev/kio/index: 0 `, `# second resource e: f @@ -288,14 +288,14 @@ g: - h metadata: annotations: + config.kubernetes.io/index: 1 foo: bar - kyaml.kustomize.dev/kio/index: 1 `, `i: j metadata: annotations: + config.kubernetes.io/index: 2 foo: bar - kyaml.kustomize.dev/kio/index: 2 `, } for i := range nodes { diff --git a/kyaml/kio/byteio_writer_test.go b/kyaml/kio/byteio_writer_test.go index 391d2ddae..e0d9647b8 100644 --- a/kyaml/kio/byteio_writer_test.go +++ b/kyaml/kio/byteio_writer_test.go @@ -107,8 +107,8 @@ func TestByteWriter_Write_withAnnotationsKeepAnnotations(t *testing.T) { node1, err := yaml.Parse(`a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/index: 0 + config.kubernetes.io/path: "a/b/a_test.yaml" `) if !assert.NoError(t, err) { return @@ -116,8 +116,8 @@ metadata: node2, err := yaml.Parse(`c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/index: 1 + config.kubernetes.io/path: "a/b/a_test.yaml" `) if !assert.NoError(t, err) { return @@ -129,8 +129,8 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" + config.kubernetes.io/index: 0 + config.kubernetes.io/path: "a/b/b_test.yaml" `) if !assert.NoError(t, err) { return @@ -145,14 +145,14 @@ metadata: assert.Equal(t, `a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/index: 0 + config.kubernetes.io/path: "a/b/a_test.yaml" --- c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/index: 1 + config.kubernetes.io/path: "a/b/a_test.yaml" --- e: f g: @@ -161,8 +161,8 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" + config.kubernetes.io/index: 0 + config.kubernetes.io/path: "a/b/b_test.yaml" `, buff.String()) } @@ -173,8 +173,8 @@ func TestByteWriter_Write_withAnnotations(t *testing.T) { node1, err := yaml.Parse(`a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/index: 0 + config.kubernetes.io/path: "a/b/a_test.yaml" `) if !assert.NoError(t, err) { return @@ -182,8 +182,8 @@ metadata: node2, err := yaml.Parse(`c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/index: 1 + config.kubernetes.io/path: "a/b/a_test.yaml" `) if !assert.NoError(t, err) { return @@ -195,8 +195,8 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" + config.kubernetes.io/index: 0 + config.kubernetes.io/path: "a/b/b_test.yaml" `) if !assert.NoError(t, err) { return @@ -211,12 +211,12 @@ metadata: assert.Equal(t, `a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/path: "a/b/a_test.yaml" --- c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/path: "a/b/a_test.yaml" --- e: f g: @@ -225,7 +225,7 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" + config.kubernetes.io/path: "a/b/b_test.yaml" `, buff.String()) } @@ -235,7 +235,7 @@ func TestByteWriter_Write_partialAnnotations(t *testing.T) { node1, err := yaml.Parse(`a: b #first metadata: 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) { return @@ -243,8 +243,8 @@ metadata: node2, err := yaml.Parse(`c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/index: 1 + config.kubernetes.io/path: "a/b/a_test.yaml" `) if !assert.NoError(t, err) { return @@ -274,11 +274,11 @@ g: a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/path: "a/b/a_test.yaml" --- c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/path: "a/b/a_test.yaml" `, buff.String()) } diff --git a/kyaml/kio/doc.go b/kyaml/kio/doc.go index b0fe4a4b1..9c11a1463 100644 --- a/kyaml/kio/doc.go +++ b/kyaml/kio/doc.go @@ -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. // // Reading Resources diff --git a/kyaml/kio/filters/container_test.go b/kyaml/kio/filters/container_test.go index 0a294ee3a..76c46a853 100644 --- a/kyaml/kio/filters/container_test.go +++ b/kyaml/kio/filters/container_test.go @@ -100,13 +100,13 @@ items: metadata: name: deployment-foo annotations: - kyaml.kustomize.dev/kio/index: 0 + config.kubernetes.io/index: 0 - apiVersion: v1 kind: Service metadata: name: service-foo annotations: - kyaml.kustomize.dev/kio/index: 1 + config.kubernetes.io/index: 1 functionConfig: {apiVersion: apps/v1, kind: Deployment, metadata: {name: foo}} `, s) { t.FailNow() @@ -131,14 +131,14 @@ kind: StatefulSet metadata: name: deployment-foo annotations: - kyaml.kustomize.dev/kio/index: 0 + config.kubernetes.io/index: 0 --- apiVersion: v1 kind: Service metadata: name: service-foo annotations: - kyaml.kustomize.dev/kio/index: 1 + config.kubernetes.io/index: 1 `, b.String()) } @@ -182,13 +182,13 @@ items: metadata: name: deployment-foo annotations: - kyaml.kustomize.dev/kio/index: 0 + config.kubernetes.io/index: 0 - apiVersion: v1 kind: Service metadata: name: service-foo annotations: - kyaml.kustomize.dev/kio/index: 1 + config.kubernetes.io/index: 1 functionConfig: {apiversion: apps/v1, kind: Deployment, metadata: {name: foo}} `, s) { t.FailNow() @@ -213,14 +213,14 @@ kind: Deployment metadata: name: deployment-foo annotations: - kyaml.kustomize.dev/kio/index: 0 + config.kubernetes.io/index: 0 --- apiVersion: v1 kind: Service metadata: name: service-foo annotations: - kyaml.kustomize.dev/kio/index: 1 + config.kubernetes.io/index: 1 `, b.String()) } diff --git a/kyaml/kio/filters/filters.go b/kyaml/kio/filters/filters.go index 5902d4e20..77f335483 100644 --- a/kyaml/kio/filters/filters.go +++ b/kyaml/kio/filters/filters.go @@ -41,7 +41,8 @@ func (t *KFilter) UnmarshalYAML(unmarshal func(interface{}) error) error { if err := unmarshal(meta); err != nil { return err } - if filter, found := Filters[meta.Kind]; !found { + filter, found := Filters[meta.Kind] + if !found { var knownFilters []string for k := range Filters { knownFilters = append(knownFilters, k) @@ -49,9 +50,9 @@ func (t *KFilter) UnmarshalYAML(unmarshal func(interface{}) error) error { sort.Strings(knownFilters) return fmt.Errorf("unsupported filter Kind %v: may be one of: [%s]", meta, strings.Join(knownFilters, ",")) - } else { - t.Filter = filter() } + t.Filter = filter() + return unmarshal(t.Filter) } diff --git a/kyaml/kio/filters/filters_test.go b/kyaml/kio/filters/filters_test.go index d4e2eb4e6..720874572 100644 --- a/kyaml/kio/filters/filters_test.go +++ b/kyaml/kio/filters/filters_test.go @@ -53,21 +53,21 @@ metadata: name: foo1 namespace: bar annotations: - kyaml.kustomize.dev/kio/path: foo1_deployment.yaml + config.kubernetes.io/path: foo1_deployment.yaml --- apiVersion: v1 kind: Service metadata: name: foo1 annotations: - kyaml.kustomize.dev/kio/path: foo1_service.yaml + config.kubernetes.io/path: foo1_service.yaml --- apiVersion: apps/v1 kind: Deployment metadata: name: foo2 annotations: - kyaml.kustomize.dev/kio/path: foo2_deployment.yaml + config.kubernetes.io/path: foo2_deployment.yaml --- apiVersion: v1 kind: Service @@ -75,7 +75,7 @@ metadata: name: foo2 namespace: bar annotations: - kyaml.kustomize.dev/kio/path: foo2_service.yaml + config.kubernetes.io/path: foo2_service.yaml `, out.String()) } @@ -97,7 +97,7 @@ kind: Service metadata: name: foo1 annotations: - kyaml.kustomize.dev/kio/path: foo1__service.yaml + config.kubernetes.io/path: foo1__service.yaml --- apiVersion: apps/v1 kind: Deployment @@ -105,14 +105,14 @@ metadata: name: foo1 namespace: bar annotations: - kyaml.kustomize.dev/kio/path: foo1_bar_deployment.yaml + config.kubernetes.io/path: foo1_bar_deployment.yaml --- apiVersion: apps/v1 kind: Deployment metadata: name: foo2 annotations: - kyaml.kustomize.dev/kio/path: foo2__deployment.yaml + config.kubernetes.io/path: foo2__deployment.yaml --- apiVersion: v1 kind: Service @@ -120,7 +120,7 @@ metadata: name: foo2 namespace: bar annotations: - kyaml.kustomize.dev/kio/path: foo2_bar_service.yaml + config.kubernetes.io/path: foo2_bar_service.yaml `, out.String()) } @@ -143,14 +143,14 @@ metadata: name: foo1 namespace: bar annotations: - kyaml.kustomize.dev/kio/path: resource.yaml + config.kubernetes.io/path: resource.yaml --- apiVersion: apps/v1 kind: Deployment metadata: name: foo2 annotations: - kyaml.kustomize.dev/kio/path: resource.yaml + config.kubernetes.io/path: resource.yaml --- apiVersion: v1 kind: Service @@ -158,13 +158,13 @@ metadata: name: foo2 namespace: bar annotations: - kyaml.kustomize.dev/kio/path: resource.yaml + config.kubernetes.io/path: resource.yaml --- apiVersion: v1 kind: Service metadata: name: foo1 annotations: - kyaml.kustomize.dev/kio/path: resource.yaml + config.kubernetes.io/path: resource.yaml `, out.String()) } diff --git a/kyaml/kio/filters/stripcomments.go b/kyaml/kio/filters/stripcomments.go index 23e1a4541..8e9ecb3f2 100644 --- a/kyaml/kio/filters/stripcomments.go +++ b/kyaml/kio/filters/stripcomments.go @@ -1,16 +1,5 @@ -// Copyright 2019 Google LLC -// -// 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. +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package filters diff --git a/kyaml/kio/kio.go b/kyaml/kio/kio.go index 7b6c6dc67..0ce76bc3f 100644 --- a/kyaml/kio/kio.go +++ b/kyaml/kio/kio.go @@ -37,6 +37,12 @@ func (fn WriterFunc) Write(o []*yaml.RNode) error { 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. // When possible, Filters should be serializable to yaml so that they can be described // as either data or code. @@ -106,3 +112,16 @@ func (p Pipeline) Execute() error { } 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 + }) +} diff --git a/kyaml/kio/kioutil/kioutil.go b/kyaml/kio/kioutil/kioutil.go index 5027c799f..f307c48ab 100644 --- a/kyaml/kio/kioutil/kioutil.go +++ b/kyaml/kio/kioutil/kioutil.go @@ -15,13 +15,13 @@ type AnnotationKey = string const ( // 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 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 AnnotationKey = "kyaml.kustomize.dev/kio/package" + PackageAnnotation AnnotationKey = "config.kubernetes.io/package" ) func GetFileAnnotations(rn *yaml.RNode) (string, string, error) { @@ -50,6 +50,21 @@ func ErrorIfMissingAnnotation(nodes []*yaml.RNode, keys ...AnnotationKey) error 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: // - by PathAnnotation annotation // - by IndexAnnotation annotation @@ -92,12 +107,12 @@ func SortNodes(nodes []*yaml.RNode) error { var iIndex, jIndex int iIndex, err = strconv.Atoi(iValue) 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 } jIndex, err = strconv.Atoi(jValue) 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 } if iIndex != jIndex { diff --git a/kyaml/kio/pkgio_reader.go b/kyaml/kio/pkgio_reader.go index f56b48339..93a113536 100644 --- a/kyaml/kio/pkgio_reader.go +++ b/kyaml/kio/pkgio_reader.go @@ -200,9 +200,9 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) { // check if we should skip the directory or file 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 } else if !match { // 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 -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 for _, g := range r.MatchFilesGlob { 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 -func (r *LocalPackageReader) shouldSkipDir(path string, info os.FileInfo) error { +func (r *LocalPackageReader) shouldSkipDir(path string) error { if r.PackageFileName == "" { return nil } diff --git a/kyaml/kio/pkgio_reader_test.go b/kyaml/kio/pkgio_reader_test.go index 77cd88e8a..64ad089cc 100644 --- a/kyaml/kio/pkgio_reader_test.go +++ b/kyaml/kio/pkgio_reader_test.go @@ -108,16 +108,16 @@ func TestLocalPackageReader_Read_pkg(t *testing.T) { `a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: a_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/package: . + config.kubernetes.io/path: a_test.yaml `, `c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: a_test.yaml + config.kubernetes.io/index: 1 + config.kubernetes.io/package: . + config.kubernetes.io/path: a_test.yaml `, `# second thing e: f @@ -127,9 +127,9 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: b_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/package: . + config.kubernetes.io/path: b_test.yaml `, } for i := range nodes { @@ -170,16 +170,16 @@ func TestLocalPackageReader_Read_file(t *testing.T) { `a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: a_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/package: . + config.kubernetes.io/path: a_test.yaml `, `c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: a_test.yaml + config.kubernetes.io/index: 1 + config.kubernetes.io/package: . + config.kubernetes.io/path: a_test.yaml `, } for i := range nodes { @@ -270,16 +270,16 @@ func TestLocalPackageReader_Read_nestedDirs(t *testing.T) { `a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/package: a/b - kyaml.kustomize.dev/kio/path: a/b/a_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/package: a/b + config.kubernetes.io/path: a/b/a_test.yaml `, `c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/package: a/b - kyaml.kustomize.dev/kio/path: a/b/a_test.yaml + config.kubernetes.io/index: 1 + config.kubernetes.io/package: a/b + config.kubernetes.io/path: a/b/a_test.yaml `, `# second thing e: f @@ -289,9 +289,9 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/package: a/b - kyaml.kustomize.dev/kio/path: a/b/b_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/package: a/b + config.kubernetes.io/path: a/b/b_test.yaml `, } for i := range nodes { @@ -328,9 +328,9 @@ func TestLocalPackageReader_Read_matchRegex(t *testing.T) { assert.Equal(t, `a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/package: a/b - kyaml.kustomize.dev/kio/path: a/b/a_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/package: a/b + config.kubernetes.io/path: a/b/a_test.yaml `, val) val, err = nodes[1].String() @@ -338,9 +338,9 @@ metadata: assert.Equal(t, `c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/package: a/b - kyaml.kustomize.dev/kio/path: a/b/a_test.yaml + config.kubernetes.io/index: 1 + config.kubernetes.io/package: a/b + config.kubernetes.io/path: a/b/a_test.yaml `, val) } @@ -367,9 +367,9 @@ func TestLocalPackageReader_Read_skipSubpackage(t *testing.T) { assert.Equal(t, `a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/package: a/b - kyaml.kustomize.dev/kio/path: a/b/a_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/package: a/b + config.kubernetes.io/path: a/b/a_test.yaml `, val) val, err = nodes[1].String() @@ -377,9 +377,9 @@ metadata: assert.Equal(t, `c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/package: a/b - kyaml.kustomize.dev/kio/path: a/b/a_test.yaml + config.kubernetes.io/index: 1 + config.kubernetes.io/package: a/b + config.kubernetes.io/path: a/b/a_test.yaml `, val) } @@ -405,9 +405,9 @@ func TestLocalPackageReader_Read_includeSubpackage(t *testing.T) { assert.Equal(t, `a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/package: a/b - kyaml.kustomize.dev/kio/path: a/b/a_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/package: a/b + config.kubernetes.io/path: a/b/a_test.yaml `, val) val, err = nodes[1].String() @@ -415,9 +415,9 @@ metadata: assert.Equal(t, `c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/package: a/b - kyaml.kustomize.dev/kio/path: a/b/a_test.yaml + config.kubernetes.io/index: 1 + config.kubernetes.io/package: a/b + config.kubernetes.io/path: a/b/a_test.yaml `, val) val, err = nodes[2].String() @@ -430,9 +430,9 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/package: a/c - kyaml.kustomize.dev/kio/path: a/c/c_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/package: a/c + config.kubernetes.io/path: a/c/c_test.yaml `, val) } diff --git a/kyaml/kio/pkgio_writer.go b/kyaml/kio/pkgio_writer.go index 50c0c3302..33d920fd6 100644 --- a/kyaml/kio/pkgio_writer.go +++ b/kyaml/kio/pkgio_writer.go @@ -69,7 +69,7 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error { return err } 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) } } diff --git a/kyaml/kio/pkgio_writer_test.go b/kyaml/kio/pkgio_writer_test.go index 228c46238..b6bf6ce30 100644 --- a/kyaml/kio/pkgio_writer_test.go +++ b/kyaml/kio/pkgio_writer_test.go @@ -67,14 +67,14 @@ func TestLocalPackageWriter_Write_keepReaderAnnotations(t *testing.T) { assert.Equal(t, `a: b #first metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: a/b/a_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/path: a/b/a_test.yaml --- c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/path: a/b/a_test.yaml + config.kubernetes.io/index: 1 + config.kubernetes.io/path: a/b/a_test.yaml `, string(b)) b, err = ioutil.ReadFile(filepath.Join(d, "a", "b", "b_test.yaml")) @@ -88,8 +88,8 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: a/b/b_test.yaml + config.kubernetes.io/index: 0 + config.kubernetes.io/path: a/b/b_test.yaml `, string(b)) } @@ -99,7 +99,7 @@ func TestLocalPackageWriter_Write_clearAnnotations(t *testing.T) { d, node1, node2, node3 := getWriterInputs(t) 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}) if !assert.NoError(t, err) { assert.FailNow(t, err.Error()) @@ -139,8 +139,8 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: "a/b/../../../b_test.yaml" + config.kubernetes.io/index: 0 + config.kubernetes.io/path: "a/b/../../../b_test.yaml" `) if !assert.NoError(t, err) { assert.FailNow(t, err.Error()) @@ -166,8 +166,8 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: a - kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" # use a different path, should still collide + config.kubernetes.io/index: a + config.kubernetes.io/path: "a/b/b_test.yaml" # use a different path, should still collide `) if !assert.NoError(t, err) { assert.FailNow(t, err.Error()) @@ -176,12 +176,12 @@ metadata: w := LocalPackageWriter{PackagePath: d} err = w.Write([]*yaml.RNode{node2, node1, node3, node4}) 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: -// - If kyaml.kustomize.dev/kio/path is absolute, fail +// - If config.kubernetes.io/path is absolute, fail func TestLocalPackageWriter_Write_absPath(t *testing.T) { d, node1, node2, node3 := getWriterInputs(t) defer os.RemoveAll(d) @@ -193,8 +193,8 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: a - kyaml.kustomize.dev/kio/path: "%s/a/b/b_test.yaml" # use a different path, should still collide + config.kubernetes.io/index: a + config.kubernetes.io/path: "%s/a/b/b_test.yaml" # use a different path, should still collide `, d)) if !assert.NoError(t, err) { assert.FailNow(t, err.Error()) @@ -208,7 +208,7 @@ metadata: } // 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) { d, node1, node2, node3 := getWriterInputs(t) defer os.RemoveAll(d) @@ -220,7 +220,7 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: a + config.kubernetes.io/index: a `) if !assert.NoError(t, err) { assert.FailNow(t, err.Error()) @@ -229,12 +229,12 @@ metadata: w := LocalPackageWriter{PackagePath: d} err = w.Write([]*yaml.RNode{node2, node1, node3, node4}) 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: -// - If kyaml.kustomize.dev/kio/index is missing, fail +// - If config.kubernetes.io/index is missing, fail func TestLocalPackageWriter_Write_missingIndex(t *testing.T) { d, node1, node2, node3 := getWriterInputs(t) defer os.RemoveAll(d) @@ -246,7 +246,7 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/path: a/a.yaml + config.kubernetes.io/path: a/a.yaml `) if !assert.NoError(t, err) { assert.FailNow(t, err.Error()) @@ -255,12 +255,12 @@ metadata: w := LocalPackageWriter{PackagePath: d} err = w.Write([]*yaml.RNode{node2, node1, node3, node4}) 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: -// - 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) { d, node1, node2, node3 := getWriterInputs(t) defer os.RemoveAll(d) @@ -272,8 +272,8 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/path: a/ - kyaml.kustomize.dev/kio/index: 0 + config.kubernetes.io/path: a/ + config.kubernetes.io/index: 0 `) if !assert.NoError(t, err) { assert.FailNow(t, err.Error()) @@ -282,7 +282,7 @@ metadata: w := LocalPackageWriter{PackagePath: d} err = w.Write([]*yaml.RNode{node2, node1, node3, node4}) 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 metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/index: 0 + config.kubernetes.io/path: "a/b/a_test.yaml" `) if !assert.NoError(t, err) { assert.FailNow(t, err.Error()) @@ -299,8 +299,8 @@ metadata: node2, err := yaml.Parse(`c: d # second metadata: annotations: - kyaml.kustomize.dev/kio/index: 1 - kyaml.kustomize.dev/kio/path: "a/b/a_test.yaml" + config.kubernetes.io/index: 1 + config.kubernetes.io/path: "a/b/a_test.yaml" `) if !assert.NoError(t, err) { assert.FailNow(t, err.Error()) @@ -312,8 +312,8 @@ g: - j metadata: annotations: - kyaml.kustomize.dev/kio/index: 0 - kyaml.kustomize.dev/kio/path: "a/b/b_test.yaml" + config.kubernetes.io/index: 0 + config.kubernetes.io/path: "a/b/b_test.yaml" `) if !assert.NoError(t, err) { assert.FailNow(t, err.Error()) diff --git a/kyaml/kio/tree_test.go b/kyaml/kio/tree_test.go index e1eec5f68..e5779face 100644 --- a/kyaml/kio/tree_test.go +++ b/kyaml/kio/tree_test.go @@ -21,8 +21,8 @@ metadata: namespace: default annotations: app: nginx3 - kyaml.kustomize.dev/kio/package: foo-package/3 - kyaml.kustomize.dev/kio/path: f3.yaml + config.kubernetes.io/package: foo-package/3 + config.kubernetes.io/path: f3.yaml spec: replicas: 1 --- @@ -34,8 +34,8 @@ metadata: namespace: default annotations: app: nginx2 - kyaml.kustomize.dev/kio/package: foo-package - kyaml.kustomize.dev/kio/path: f1.yaml + config.kubernetes.io/package: foo-package + config.kubernetes.io/path: f1.yaml spec: replicas: 1 --- @@ -45,8 +45,8 @@ metadata: app: nginx annotations: app: nginx - kyaml.kustomize.dev/kio/package: bar-package - kyaml.kustomize.dev/kio/path: f2.yaml + config.kubernetes.io/package: bar-package + config.kubernetes.io/path: f2.yaml name: bar spec: replicas: 3 @@ -57,8 +57,8 @@ metadata: namespace: default annotations: app: nginx - kyaml.kustomize.dev/kio/package: foo-package - kyaml.kustomize.dev/kio/path: f1.yaml + config.kubernetes.io/package: foo-package + config.kubernetes.io/path: f1.yaml spec: selector: app: nginx @@ -94,8 +94,8 @@ metadata: namespace: default annotations: app: nginx3 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: f3.yaml + config.kubernetes.io/package: . + config.kubernetes.io/path: f3.yaml spec: replicas: 1 --- @@ -107,8 +107,8 @@ metadata: namespace: default annotations: app: nginx2 - kyaml.kustomize.dev/kio/package: foo-package - kyaml.kustomize.dev/kio/path: f1.yaml + config.kubernetes.io/package: foo-package + config.kubernetes.io/path: f1.yaml spec: replicas: 1 --- @@ -118,8 +118,8 @@ metadata: app: nginx annotations: app: nginx - kyaml.kustomize.dev/kio/package: bar-package - kyaml.kustomize.dev/kio/path: f2.yaml + config.kubernetes.io/package: bar-package + config.kubernetes.io/path: f2.yaml name: bar spec: replicas: 3 @@ -130,8 +130,8 @@ metadata: namespace: default annotations: app: nginx - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: f1.yaml + config.kubernetes.io/package: . + config.kubernetes.io/path: f1.yaml spec: selector: app: nginx @@ -167,8 +167,8 @@ metadata: namespace: default annotations: app: nginx2 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: f1.yaml + config.kubernetes.io/package: . + config.kubernetes.io/path: f1.yaml spec: replicas: 1 --- @@ -181,8 +181,8 @@ metadata: namespace: default annotations: app: nginx2 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: f1.yaml + config.kubernetes.io/package: . + config.kubernetes.io/path: f1.yaml spec: replicas: 1 --- @@ -195,8 +195,8 @@ metadata: namespace: default annotations: app: nginx2 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: f1.yaml + config.kubernetes.io/package: . + config.kubernetes.io/path: f1.yaml spec: replicas: 1 --- @@ -209,8 +209,8 @@ metadata: namespace: default2 annotations: app: nginx2 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: f1.yaml + config.kubernetes.io/package: . + config.kubernetes.io/path: f1.yaml spec: replicas: 1 --- @@ -223,8 +223,8 @@ metadata: namespace: default annotations: app: nginx3 - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: f1.yaml + config.kubernetes.io/package: . + config.kubernetes.io/path: f1.yaml spec: replicas: 1 --- @@ -234,8 +234,8 @@ metadata: app: nginx annotations: app: nginx - kyaml.kustomize.dev/kio/package: bar-package - kyaml.kustomize.dev/kio/path: f2.yaml + config.kubernetes.io/package: bar-package + config.kubernetes.io/path: f2.yaml name: bar spec: replicas: 3 @@ -246,8 +246,8 @@ metadata: namespace: default annotations: app: nginx - kyaml.kustomize.dev/kio/package: . - kyaml.kustomize.dev/kio/path: f1.yaml + config.kubernetes.io/package: . + config.kubernetes.io/path: f1.yaml spec: selector: app: nginx diff --git a/kyaml/yaml/alias.go b/kyaml/yaml/alias.go index 175775287..ee0d5cbb1 100644 --- a/kyaml/yaml/alias.go +++ b/kyaml/yaml/alias.go @@ -22,7 +22,7 @@ type TypeError = yaml.TypeError type Unmarshaler = yaml.Unmarshaler var Marshal = yaml.Marshal -var UnMarshal = yaml.Unmarshal +var Unmarshal = yaml.Unmarshal var NewDecoder = yaml.NewDecoder var NewEncoder = func(w io.Writer) *yaml.Encoder { e := yaml.NewEncoder(w) diff --git a/kyaml/yaml/filters.go b/kyaml/yaml/filters.go index b47b4993c..671919f39 100644 --- a/kyaml/yaml/filters.go +++ b/kyaml/yaml/filters.go @@ -46,7 +46,8 @@ func (y *YFilter) UnmarshalYAML(unmarshal func(interface{}) error) error { if err := unmarshal(meta); err != nil { return err } - if filter, found := Filters[meta.Kind]; !found { + filter, found := Filters[meta.Kind] + if !found { var knownFilters []string for k := range Filters { knownFilters = append(knownFilters, k) @@ -54,9 +55,9 @@ func (y *YFilter) UnmarshalYAML(unmarshal func(interface{}) error) error { sort.Strings(knownFilters) return fmt.Errorf("unsupported GrepFilter Kind %s: may be one of: [%s]", meta.Kind, strings.Join(knownFilters, ",")) - } else { - y.Filter = filter() } + y.Filter = filter() + if err := unmarshal(y.Filter); err != nil { return err } diff --git a/kyaml/yaml/match.go b/kyaml/yaml/match.go index 3ba66c010..a92203c2d 100644 --- a/kyaml/yaml/match.go +++ b/kyaml/yaml/match.go @@ -82,10 +82,9 @@ func (p *PathMatcher) filter(rn *RNode) (*RNode, error) { if IsListIndex(p.Path[0]) { // match seq elements 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) { diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index d3ff92238..de426097d 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -240,7 +240,7 @@ type ObjectMeta struct { 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 func (rn *RNode) GetMeta() (ResourceMeta, error) { @@ -259,7 +259,7 @@ func (rn *RNode) GetMeta() (ResourceMeta, error) { return m, err } if reflect.DeepEqual(m, ResourceMeta{}) { - return m, MissingMetaError + return m, ErrMissingMetadata } return m, nil } @@ -298,6 +298,13 @@ func (rn *RNode) Pipe(functions ...Filter) (*RNode, error) { 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 // DocumentNodes func (rn *RNode) Document() *yaml.Node { @@ -445,7 +452,7 @@ func (rn *RNode) Elements() ([]*RNode, error) { return nil, err } 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])) } return elements, nil @@ -459,7 +466,7 @@ func (rn *RNode) ElementValues(key string) ([]string, error) { return nil, err } 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) if !IsFieldEmpty(field) { elements = append(elements, field.Value.YNode().Value) @@ -497,8 +504,7 @@ func (rn *RNode) VisitElements(fn func(node *RNode) error) error { return nil } -// AssociativeSequencePaths is a list of field names that may be used as associative keys -// when merge Resources. +// AssociativeSequenceKeys is a map of paths to sequences that have associative keys. // 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 // associative key for merging that list. diff --git a/kyaml/yaml/walk/associative_sequence.go b/kyaml/yaml/walk/associative_sequence.go index 479d8a22d..cd5946939 100644 --- a/kyaml/yaml/walk/associative_sequence.go +++ b/kyaml/yaml/walk/associative_sequence.go @@ -24,10 +24,7 @@ func (l *Walker) walkAssociativeSequence() (*yaml.RNode, error) { if err != nil { return nil, err } - values, err := l.elementValues(key) - if err != nil { - return nil, err - } + values := l.elementValues(key) // recursively set the elements in the list for _, value := range values { @@ -90,7 +87,7 @@ func (l Walker) elementKey() (string, error) { // from all sources. // Return value slice is ordered using the original ordering from the elements, where // 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 // dest node must be first var returnValues []string @@ -111,7 +108,7 @@ func (l Walker) elementValues(key string) ([]string, error) { seen.Insert(s) } } - return returnValues, nil + return returnValues } // fieldValue returns a slice containing each source's value for fieldName diff --git a/kyaml/yaml/walk/visitor.go b/kyaml/yaml/walk/visitor.go index 50982e553..eb1451bbf 100644 --- a/kyaml/yaml/walk/visitor.go +++ b/kyaml/yaml/walk/visitor.go @@ -1,16 +1,5 @@ -// Copyright 2019 Google LLC -// -// 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. +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package walk @@ -34,5 +23,5 @@ type Visitor interface { VisitList(nodes Sources, kind ListKind) (*yaml.RNode, error) } -// NoOp is returned if GrepFilter should do nothing after calling Set -var ClearNode *yaml.RNode = nil +// ClearNode is returned if GrepFilter should do nothing after calling Set +var ClearNode *yaml.RNode diff --git a/kyaml/yaml/walk/walk.go b/kyaml/yaml/walk/walk.go index 3441ae21a..20e4979b8 100644 --- a/kyaml/yaml/walk/walk.go +++ b/kyaml/yaml/walk/walk.go @@ -48,9 +48,9 @@ func (l Walker) Walk() (*yaml.RNode, error) { } if yaml.IsAssociative(l.Sources) { return l.walkAssociativeSequence() - } else { - return l.walkNonAssociativeSequence() } + return l.walkNonAssociativeSequence() + case yaml.ScalarNode: if err := yaml.ErrorIfAnyInvalidAndNonNull(yaml.ScalarNode, l.Sources...); err != nil { return nil, err