|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// package kyaml provides a printer for Kubernetes objects that formats them |
| 18 | +// as KYAML, a strict subset of YAML that is designed to be explicit and |
| 19 | +// unambiguous. KYAML is YAML. |
| 20 | +package printers |
| 21 | + |
| 22 | +import ( |
| 23 | + "bytes" |
| 24 | + "errors" |
| 25 | + "fmt" |
| 26 | + "io" |
| 27 | + "reflect" |
| 28 | + |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/apimachinery/pkg/runtime" |
| 31 | + "sigs.k8s.io/yaml/kyaml" |
| 32 | +) |
| 33 | + |
| 34 | +// KYAMLPrinter is an implementation of ResourcePrinter which formats data into |
| 35 | +// a specific dialect of YAML, known as KYAML. KYAML is halfway between YAML |
| 36 | +// and JSON, but is a strict subset of YAML, and so it should should be |
| 37 | +// readable by any YAML parser. It is designed to be explicit and unambiguous, |
| 38 | +// and eschews significant whitespace. |
| 39 | +type KYAMLPrinter struct { |
| 40 | + encoder kyaml.Encoder |
| 41 | +} |
| 42 | + |
| 43 | +// PrintObj prints the data as KYAML to the specified writer. |
| 44 | +func (p *KYAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error { |
| 45 | + // We use reflect.Indirect here in order to obtain the actual value from a pointer. |
| 46 | + // We need an actual value in order to retrieve the package path for an object. |
| 47 | + // Using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers. |
| 48 | + if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) { |
| 49 | + return errors.New(InternalObjectPrinterErr) |
| 50 | + } |
| 51 | + |
| 52 | + switch obj := obj.(type) { |
| 53 | + case *metav1.WatchEvent: |
| 54 | + if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj.Object.Object)).Type().PkgPath()) { |
| 55 | + return errors.New(InternalObjectPrinterErr) |
| 56 | + } |
| 57 | + case *runtime.Unknown: |
| 58 | + return p.encoder.FromYAML(bytes.NewReader(obj.Raw), w) |
| 59 | + } |
| 60 | + |
| 61 | + if obj.GetObjectKind().GroupVersionKind().Empty() { |
| 62 | + return fmt.Errorf("missing apiVersion or kind; try GetObjectKind().SetGroupVersionKind() if you know the type") |
| 63 | + } |
| 64 | + |
| 65 | + return p.encoder.FromObject(obj, w) |
| 66 | +} |
0 commit comments