import ( "fmt" "k8s.io/apimachinery/pkg/util/yaml" "k8s.io/kubernetes/pkg/kubectl/cmd/util" ) inputYaml := ` apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: my-container image: nginx:1.7.9 ` decoder := util.NewFactory(yaml.Unmarshal) pod := &v1.Pod{} err := decoder.Decode([]byte(inputYaml), &pod) if err != nil { panic(err) } fmt.Println(pod.Name)
import ( "fmt" "encoding/json" "k8s.io/kubernetes/pkg/kubectl/cmd/util" ) inputJson := ` { "apiVersion": "v1", "kind": "Pod", "metadata": { "name": "my-pod" }, "spec": { "containers": [{ "name": "my-container", "image": "nginx:1.7.9" }] } } ` decoder := util.NewFactory(json.Unmarshal) pod := &v1.Pod{} err := decoder.Decode([]byte(inputJson), &pod) if err != nil { panic(err) } fmt.Println(pod.Name)In both examples, the Factory function is used to create a new decoder that can handle either YAML or JSON input data. The decoder is then used to decode the input data into a Kubernetes Pod object.