From 281231e07497a3324ac39c8e61772f521103962e Mon Sep 17 00:00:00 2001 From: Maciej Szulik Date: Fri, 10 Sep 2021 13:06:08 +0200 Subject: [PATCH] Move YamlPrinter to its own file --- .../k8s.io/cli-runtime/pkg/printers/json.go | 63 ------------- .../cli-runtime/pkg/printers/json_test.go | 9 -- .../k8s.io/cli-runtime/pkg/printers/yaml.go | 90 +++++++++++++++++++ .../cli-runtime/pkg/printers/yaml_test.go | 33 +++++++ 4 files changed, 123 insertions(+), 72 deletions(-) create mode 100644 staging/src/k8s.io/cli-runtime/pkg/printers/yaml.go create mode 100644 staging/src/k8s.io/cli-runtime/pkg/printers/yaml_test.go diff --git a/staging/src/k8s.io/cli-runtime/pkg/printers/json.go b/staging/src/k8s.io/cli-runtime/pkg/printers/json.go index 1c35b97d735..8ab2235f8b2 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/printers/json.go +++ b/staging/src/k8s.io/cli-runtime/pkg/printers/json.go @@ -22,12 +22,9 @@ import ( "fmt" "io" "reflect" - "sync/atomic" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - - "sigs.k8s.io/yaml" ) // JSONPrinter is an implementation of ResourcePrinter which outputs an object as JSON. @@ -80,63 +77,3 @@ func (p *JSONPrinter) PrintObj(obj runtime.Object, w io.Writer) error { _, err = w.Write(data) return err } - -// YAMLPrinter is an implementation of ResourcePrinter which outputs an object as YAML. -// The input object is assumed to be in the internal version of an API and is converted -// to the given version first. -// If PrintObj() is called multiple times, objects are separated with a '---' separator. -type YAMLPrinter struct { - printCount int64 -} - -// PrintObj prints the data as YAML. -func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error { - // we use reflect.Indirect here in order to obtain the actual value from a pointer. - // we need an actual value in order to retrieve the package path for an object. - // using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers. - if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) { - return fmt.Errorf(InternalObjectPrinterErr) - } - - count := atomic.AddInt64(&p.printCount, 1) - if count > 1 { - if _, err := w.Write([]byte("---\n")); err != nil { - return err - } - } - - switch obj := obj.(type) { - case *metav1.WatchEvent: - if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj.Object.Object)).Type().PkgPath()) { - return fmt.Errorf(InternalObjectPrinterErr) - } - data, err := json.Marshal(obj) - if err != nil { - return err - } - data, err = yaml.JSONToYAML(data) - if err != nil { - return err - } - _, err = w.Write(data) - return err - case *runtime.Unknown: - data, err := yaml.JSONToYAML(obj.Raw) - if err != nil { - return err - } - _, err = w.Write(data) - return err - } - - if obj.GetObjectKind().GroupVersionKind().Empty() { - return fmt.Errorf("missing apiVersion or kind; try GetObjectKind().SetGroupVersionKind() if you know the type") - } - - output, err := yaml.Marshal(obj) - if err != nil { - return err - } - _, err = fmt.Fprint(w, string(output)) - return err -} diff --git a/staging/src/k8s.io/cli-runtime/pkg/printers/json_test.go b/staging/src/k8s.io/cli-runtime/pkg/printers/json_test.go index f8f1277ec1c..902ef3f66eb 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/printers/json_test.go +++ b/staging/src/k8s.io/cli-runtime/pkg/printers/json_test.go @@ -27,7 +27,6 @@ import ( "k8s.io/apimachinery/pkg/util/diff" "k8s.io/apimachinery/pkg/util/json" "k8s.io/client-go/kubernetes/scheme" - "sigs.k8s.io/yaml" ) var testData = TestStruct{ @@ -51,10 +50,6 @@ func (in *TestStruct) DeepCopyObject() runtime.Object { panic("never called") } -func TestYAMLPrinter(t *testing.T) { - testPrinter(t, NewTypeSetter(scheme.Scheme).ToPrinter(&YAMLPrinter{}), yamlUnmarshal) -} - func TestJSONPrinter(t *testing.T) { testPrinter(t, NewTypeSetter(scheme.Scheme).ToPrinter(&JSONPrinter{}), json.Unmarshal) } @@ -106,10 +101,6 @@ func testPrinter(t *testing.T, printer ResourcePrinter, unmarshalFunc func(data } } -func yamlUnmarshal(data []byte, v interface{}) error { - return yaml.Unmarshal(data, v) -} - func TestPrintersSuccess(t *testing.T) { om := func(name string) metav1.ObjectMeta { return metav1.ObjectMeta{Name: name} } diff --git a/staging/src/k8s.io/cli-runtime/pkg/printers/yaml.go b/staging/src/k8s.io/cli-runtime/pkg/printers/yaml.go new file mode 100644 index 00000000000..1d636ba7404 --- /dev/null +++ b/staging/src/k8s.io/cli-runtime/pkg/printers/yaml.go @@ -0,0 +1,90 @@ +/* +Copyright 2021 The Kubernetes Authors. + +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 printers + +import ( + "encoding/json" + "fmt" + "io" + "reflect" + "sync/atomic" + + "sigs.k8s.io/yaml" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" +) + +// YAMLPrinter is an implementation of ResourcePrinter which outputs an object as YAML. +// The input object is assumed to be in the internal version of an API and is converted +// to the given version first. +// If PrintObj() is called multiple times, objects are separated with a '---' separator. +type YAMLPrinter struct { + printCount int64 +} + +// PrintObj prints the data as YAML. +func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error { + // we use reflect.Indirect here in order to obtain the actual value from a pointer. + // we need an actual value in order to retrieve the package path for an object. + // using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers. + if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) { + return fmt.Errorf(InternalObjectPrinterErr) + } + + count := atomic.AddInt64(&p.printCount, 1) + if count > 1 { + if _, err := w.Write([]byte("---\n")); err != nil { + return err + } + } + + switch obj := obj.(type) { + case *metav1.WatchEvent: + if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj.Object.Object)).Type().PkgPath()) { + return fmt.Errorf(InternalObjectPrinterErr) + } + data, err := json.Marshal(obj) + if err != nil { + return err + } + data, err = yaml.JSONToYAML(data) + if err != nil { + return err + } + _, err = w.Write(data) + return err + case *runtime.Unknown: + data, err := yaml.JSONToYAML(obj.Raw) + if err != nil { + return err + } + _, err = w.Write(data) + return err + } + + if obj.GetObjectKind().GroupVersionKind().Empty() { + return fmt.Errorf("missing apiVersion or kind; try GetObjectKind().SetGroupVersionKind() if you know the type") + } + + output, err := yaml.Marshal(obj) + if err != nil { + return err + } + _, err = fmt.Fprint(w, string(output)) + return err +} diff --git a/staging/src/k8s.io/cli-runtime/pkg/printers/yaml_test.go b/staging/src/k8s.io/cli-runtime/pkg/printers/yaml_test.go new file mode 100644 index 00000000000..157e9eff323 --- /dev/null +++ b/staging/src/k8s.io/cli-runtime/pkg/printers/yaml_test.go @@ -0,0 +1,33 @@ +/* +Copyright 2021 The Kubernetes Authors. + +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 printers + +import ( + "testing" + + "sigs.k8s.io/yaml" + + "k8s.io/client-go/kubernetes/scheme" +) + +func TestYAMLPrinter(t *testing.T) { + testPrinter(t, NewTypeSetter(scheme.Scheme).ToPrinter(&YAMLPrinter{}), yamlUnmarshal) +} + +func yamlUnmarshal(data []byte, v interface{}) error { + return yaml.Unmarshal(data, v) +}