How to use slices and maps with the k8s.io/code-generator

7/31/2018

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?

-- Cocotton
go
kubernetes
kubernetes-apiserver
kubernetes-custom-resources

0 Answers