读取yaml文件,生成runtimeObject
package main
import (
"bytes"
"io/ioutil"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"log"
)
func main() {
yamlBytes, err := ioutil.ReadFile("D:/vali/github/k8s/yamls/dp-nginx.yaml")
if err != nil {
log.Printf("get yaml content error %s", err.Error())
}
objectsInYAML := bytes.Split(yamlBytes, []byte("---"))
if len(objectsInYAML) == 0 {
}
for _, objectInYAML := range objectsInYAML {
runtimeObject, groupVersionAndKind, err := yaml.NewDecodingSerializer(
unstructured.UnstructuredJSONScheme).Decode(objectInYAML, nil, nil)
if err != nil {
log.Fatal(err)
}
objKind := runtimeObject.GetObjectKind()
log.Printf("objKind is %s;groupVersionAndKind is %s, ", objKind, groupVersionAndKind)
}
}