doswy02440 2018-07-31 16:49
浏览 106

如何在k8s.io/code-generator中使用切片和地图

I'm writing a Kubernetes Controller listening on a Environment Custom Resource.

The pkg/apis/environment/v1alpha1/types.go has the following content:

package v1alpha1

import (
    meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// Environment describes an Environment resource
type Environment struct {
    meta_v1.TypeMeta   `json:",inline"`
    meta_v1.ObjectMeta `json:"metadata,omitempty"`
    Spec               EnvironmentSpec `json:"spec"`
}

// EnvironmentSpec contains the specs for an Environment resource
type EnvironmentSpec struct {
    Services []Service `json:"services"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// EnvironmentList is a list of Environment resources
type EnvironmentList struct {
    meta_v1.TypeMeta `json:",inline"`
    meta_v1.ListMeta `json:"metadata"`

    Items []Environment `json:"items"`
}

// Service describes a Service in the Environment Custom Resource
type Service struct {
    Code       string `json:"code"`
    Parameters struct {
        Foo map[string]string `json:"foo"`
        Bar int               `json:"bar"`
    } `json:"parameters"`
}

After running the k8s.io/code-generator/generate-groups.sh script, I end up with a faulty pkg/apis/environment/v1alpha1/zz_generated.deepcopy.go file. The problem comes from this generated method:

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Service) DeepCopyInto(out *Service) {
    *out = *in
    in.Parameters.DeepCopyInto(&out.Parameters)
    return
}

Trying to build or run this code will give me the following error

pkg/apis/environment/v1alpha1/zz_generated.deepcopy.go:113:15: in.Parameters.DeepCopyInto undefined (type struct { Foo map[string]string "json:\"foo\""; Bar int "json:\"bar\"" } has no field or method DeepCopyInto)

As soon as I have a Map or a Slice inside an anonymous struct included in the Parameters struct, I encounter this error.

WORKAROUND

A workaround to this is to create a named type that will contain the map. For example I refactored the Service struct like this:

// Service describes a Service in the Environment Custom Resource
type Service struct {
    Code       string `json:"code"`
    Parameters FooBar `json:"parameters"`
}

type FooBar struct {
    Foo map[string]string `json:"foo"`
    Bar int               `json:"bar"`
}

The generated func (in *Service) DeepCopyInto(out *Service) does not change, but the following 2 new methods are created:

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *FooBar) DeepCopyInto(out *FooBar) {
    *out = *in
    if in.Foo != nil {
        in, out := &in.Foo, &out.Foo
        *out = make(map[string]string, len(*in))
        for key, val := range *in {
            (*out)[key] = val
        }
    }
    return
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FooBar.
func (in *FooBar) DeepCopy() *FooBar {
    if in == nil {
        return nil
    }
    out := new(FooBar)
    in.DeepCopyInto(out)
    return out
}

And now, I don't have any issues building and running the code.

This workaround is painful because my real Service struct is much bigger than in this example.

Is there a way to use maps and slices inside of anonymous func with the code-generator?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算