将* extensions.Deployment转换为* v1beta1.Deployment(Kubernetes client-go)

时间:2017-01-12 21:49:53

标签: go kubernetes

使用Kubernetes client-go,我如何使用Convert()进行* extensions.Deployment到* v1beta1.Deployment?当我运行下面的程序时,我收到此错误:

2017/01/12 22:40:42无法转换:转换(extensions.Deployment).CreationTimestamp.Time.sec为(v1beta1.Deployment).CreationTimestamp.Time.sec:无法设置dest。 (试图用未导出的字段深层复制某些东西?)

package main

import (
    "log"

    _ "k8s.io/client-go/1.4/kubernetes"
    "k8s.io/client-go/1.4/pkg/api"
    "k8s.io/client-go/1.4/pkg/apis/extensions"
    "k8s.io/client-go/1.4/pkg/apis/extensions/v1beta1"
    "k8s.io/client-go/1.4/pkg/runtime"
)

const yaml = `
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: push
`

func main() {

    d := api.Codecs.UniversalDecoder()
    obj, _, err := d.Decode([]byte(yaml), nil, nil)
    if err != nil {
        log.Fatalf("could not decode yaml: %s\n%s", yaml, err)
    }

    src := obj.(*extensions.Deployment)
    s := runtime.NewScheme()
    dst := &v1beta1.Deployment{}
    err = s.Convert(src, dst, 0)
    if err != nil {
        log.Fatalf("failed to convert: %s", err)
    }
}

我希望调用v1beta1.Convert_extensions_Deployment_To_v1beta1_Deployment()并处理未导出的字段。该函数在main_init()中注册,但不由Convert()调用。

我是否可以使用Convert()进行此类转换,还是应该使用json.Unmarshal()和json.Marshal()?

1 个答案:

答案 0 :(得分:0)

Use api.Scheme.Convert instead of runtime.NewScheme().Convert.

Found solution at https://github.com/kubernetes/client-go/issues/151