parsing K8s yaml spec into client-go data structures

10/23/2019

I wrote some code but it doesn't work, it seems that client-go doesn't support parsing of K8s yaml spec into client-go data structures, could anyone tell me how to do it.

func GetDeploymentFromYamlString(str string) (*apps.Deployment, error) {
    decode := scheme.Codecs.UniversalDeserializer().Decode
    obj, _, err := decode([]byte(str), nil, nil)
    if err != nil {
        return nil, err
    }
    return obj.(*apps.Deployment), nil
}
-- edselwang
client-go
kubernetes

1 Answer

10/23/2019

You can use github.com/ghodss/yaml package to unmarshal the deployment yaml.

Try as following:

import "github.com/ghodss/yaml"

func GetDeploymentFromYamlString(str string) (*apps.Deployment, error) {
        deploy := new(apps.Deployment)
        if err := yaml.Unmarshal([]byte(str), deploy); err != nil {
                return nil, err
        }
        return deploy, nil
}
-- Masudur Rahman
Source: StackOverflow