Golang code-generators used to implement Kubernetes-style API types.

Overview

code-generator

Golang code-generators used to implement Kubernetes-style API types.

Purpose

These code-generators can be used

  • in the context of CustomResourceDefinition to build native, versioned clients, informers and other helpers
  • in the context of User-provider API Servers to build conversions between internal and versioned types, defaulters, protobuf codecs, internal and versioned clients and informers.

Resources

Compatibility

HEAD of this repo will match HEAD of k8s.io/apiserver, k8s.io/apimachinery, and k8s.io/client-go.

Where does it come from?

code-generator is synced from https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/code-generator. Code changes are made in that location, merged into k8s.io/kubernetes and later synced here.

Comments
  • code-generator don't generate clientset, informer and lister

    code-generator don't generate clientset, informer and lister

    Question: When I use code-generator try to generate the clienset, informer and lister, it does not tell me any issue, but when I check the generate folder, it only have clientset, and the content is almost empty image

    My *_type.go file as below: package v1beta1

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

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

    // +k8s:openapi-gen=true // +resource:path=containersets

    // +kubebuilder:object:root=true

    // Appmgmt is the Schema for the appmgmts API type Appmgmt struct { metav1.TypeMeta json:",inline" metav1.ObjectMeta json:"metadata,omitempty"

    Spec   AppmgmtSpec   `json:"spec,omitempty"`
    Status AppmgmtStatus `json:"status,omitempty"`
    

    }

    // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! // NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

    // AppmgmtSpec defines the desired state of Appmgmt type AppmgmtSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "make" to regenerate code after modifying this file

    // Foo is an example field of Appmgmt. Edit Appmgmt_types.go to remove/update
    // Foo string `json:"foo,omitempty"`
    FindNamespace					string		`json:"findnamespace"`	
    Image							string  	`json:"image"`
    Env	    						[]Env		`json:"env"`	
    Ports    						int 		`json:"ports"`
    ImagePullSecret 			 	string	`json:"imagePullSecret"`
    

    }

    type Env struct { Name string json:"name" } // AppmgmtStatus defines the observed state of Appmgmt type AppmgmtStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file TestStatus string json:"testStatus" }

    // +kubebuilder:object:root=true

    // AppmgmtList contains a list of Appmgmt type AppmgmtList struct { metav1.TypeMeta json:",inline" metav1.ListMeta json:"metadata,omitempty" Items []Appmgmt json:"items" }

    var (
    // SchemeGroupVersion is group version used to register these objects SchemeGroupVersion = schema.GroupVersion{Group: "appmgmt", Version: "v1beta1"}

    // SchemeBuilder is used to add go types to the GroupVersionKind scheme
    SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion})
    

    func init() { SchemeBuilder.Register(&Appmgmt{}, &AppmgmtList{}) }

    Environment as below: $go version go version go1.13.8 linux/amd64

    $ kubectl version Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.4", GitCommit:"224be7bdce5a9dd0c2fd0d46b83865648e2fe0ba", GitTreeState:"clean", BuildDate:"2019-12-11T12:47:40Z", GoVersion:"go1.12.12", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.7", GitCommit:"20c697423de11d4d5d08c6832a513217ca11a6aa", GitTreeState:"clean", BuildDate:"2020-02-21T10:47:45Z", GoVersion:"go1.13.6", Compiler:"gc", Platform:"linux/amd64"}

    $go env GO111MODULE="on" GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/root/workspace/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/root/installation/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/root/installation/go/pkg/tool/linux_amd64" GCCGO="gccgo" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/dev/null" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build867010754=/tmp/go-build -gno-record-gcc-switches"

    lifecycle/rotten 
    opened by JaneLiuL 57
  • go modules support

    go modules support

    The generators insist on putting generated code into the GOPATH, rather than relative to the local directory the tool is running in.

    For the simplest use case of generating code for a single controller, it seems like it should be possible to support building/generating outsisde of a GOPATH, using go modules. It's not clear to me what other use cases this tool supports, and/or if using it outside of GOPATH makes sense (hopefully it can, as I think GOPATH's long term support is in question)

    lifecycle/rotten 
    opened by tcolgate 22
  • Hyphen as last subdomain in groupName breaks client generation

    Hyphen as last subdomain in groupName breaks client generation

    When using groupName as example.code-generation.k8s.io the client generation works. Changing it to code-generation.k8s.io causes the generated clients to be broken:

    https://github.com/mvladev/code-generation:

    Generating deepcopy funcs
    Generating clientset for example:v1 at github.com/mvladev/code-generation/client/clientset
    ERROR: logging before flag.Parse: F1106 07:49:42.418067   80131 main.go:69] Error: Failed executing generator: some packages had errors:
    errors in package "github.com/mvladev/code-generation/client/clientset/versioned":
    unable to format file "/Users/i068969/git/go/src/github.com/mvladev/code-generation/client/clientset/versioned/clientset.go" (6:6: expected 'STRING', found '-' (and 10 more errors)).
    
    errors in package "github.com/mvladev/code-generation/client/clientset/versioned/scheme":
    unable to format file "/Users/i068969/git/go/src/github.com/mvladev/code-generation/client/clientset/versioned/scheme/register.go" (8:6: expected 'STRING', found '-' (and 4 more errors)).
    
    errors in package "github.com/mvladev/code-generation/client/clientset/versioned/fake":
    unable to format file "/Users/i068969/git/go/src/github.com/mvladev/code-generation/client/clientset/versioned/fake/clientset_generated.go" (4:10: expected 'STRING', found '-' (and 9 more errors)).
    unable to format file "/Users/i068969/git/go/src/github.com/mvladev/code-generation/client/clientset/versioned/fake/register.go" (4:6: expected 'STRING', found '-' (and 4 more errors)).
    
    errors in package "github.com/mvladev/code-generation/client/clientset/versioned/typed/code-generator/v1":
    unable to format file "/Users/i068969/git/go/src/github.com/mvladev/code-generation/client/clientset/versioned/typed/code-generator/v1/code-generator_client.go" (11:10: expected type, found '-' (and 2 mor
    e errors)).
    unable to format file "/Users/i068969/git/go/src/github.com/mvladev/code-generation/client/clientset/versioned/typed/code-generator/v1/testtype.go" (40:26: missing ',' in parameter list).
    
    errors in package "github.com/mvladev/code-generation/client/clientset/versioned/typed/code-generator/v1/fake":
    unable to format file "/Users/i068969/git/go/src/github.com/mvladev/code-generation/client/clientset/versioned/typed/code-generator/v1/fake/fake_testtype.go" (16:16: expected ';', found '-' (and 1 more er
    rors)).
    unable to format file "/Users/i068969/git/go/src/github.com/mvladev/code-generation/client/clientset/versioned/typed/code-generator/v1/fake/fake_code-generator_client.go" (10:14: expected type, found '-'
    (and 2 more errors)).
    
    • bad imports code-generatorv1 "github.com/mvladev/code-generation/client/clientset/versioned/typed/code-generator/v1"
    • interfaces have hyphen in them Code-generatorV1() code-generatorv1.Code-generatorV1Interface
    kind/bug lifecycle/rotten 
    opened by mvladev 21
  • Failed to generate informers with goupName containing a dash

    Failed to generate informers with goupName containing a dash

    Hello, I'm using a group name containing a dash (ca-gip.github.com). I did not have any problems generating while using codegen v0.15.0. But since I upgraded to newer version it's not possible anymore, I've tested with: v0.16.8, v0.17.0 and v0.17.4. The error seems to be only associate to informers as clienset and listers are properly generated.

    The log I get while using the update-codegen.sh script :

    resourcequotas-operator $ bash hack/update-codegen.sh 
    Generating deepcopy funcs
    Generating clientset for ca-gip:v1 at github.com/ca-gip/resourcequotas-operator/pkg/generated/clientset
    Generating listers for ca-gip:v1 at github.com/ca-gip/resourcequotas-operator/pkg/generated/listers
    Generating informers for ca-gip:v1 at github.com/ca-gip/resourcequotas-operator/pkg/generated/informers
    F0320 14:10:32.749413   27566 main.go:60] Error: Failed executing generator: some packages had errors:
    errors in package "github.com/ca-gip/resourcequotas-operator/pkg/generated/informers/externalversions/ca-gip":
    unable to format file "/Users/clementblaise/go/src/github.com/ca-gip/resourcequotas-operator/pkg/generated/informers/externalversions/ca-gip/interface.go" (3:11: expected ';', found '-').
    
    lifecycle/rotten 
    opened by clementblaise 16
  • Generator only works if k8s.io/apimachinery is in the $GOPATH

    Generator only works if k8s.io/apimachinery is in the $GOPATH

    Whenever I attempt to run update-codegen.sh , but constantly get the below stacktrace.

    Generating deepcopy funcs
    F1104 02:57:44.419529      35 main.go:79] Error: Failed executing generator: some packages had errors:
    type "k8s.io/apimachinery/pkg/runtime.Object" in k8s:deepcopy-gen:interfaces tag of type k8s.io/apimachinery/pkg/runtime.Object is not an interface, but: ""
    goroutine 1 [running]:
    k8s.io/code-generator/vendor/github.com/golang/glog.stacks(0xc420136b00, 0xc42012b080, 0x104, 0x15a)
            /go/src/k8s.io/code-generator/vendor/github.com/golang/glog/glog.go:766 +0xa7
    k8s.io/code-generator/vendor/github.com/golang/glog.(*loggingT).output(0x83eb60, 0xc400000003, 0xc4200cf1e0, 0x81ea9f, 0x7, 0x4f, 0x0)
            /go/src/k8s.io/code-generator/vendor/github.com/golang/glog/glog.go:717 +0x348
    k8s.io/code-generator/vendor/github.com/golang/glog.(*loggingT).printf(0x83eb60, 0x3, 0x6ebfc7, 0x9, 0xc4200e7f48, 0x1, 0x1)
            /go/src/k8s.io/code-generator/vendor/github.com/golang/glog/glog.go:655 +0x14f
    k8s.io/code-generator/vendor/github.com/golang/glog.Fatalf(0x6ebfc7, 0x9, 0xc4200e7f48, 0x1, 0x1)
            /go/src/k8s.io/code-generator/vendor/github.com/golang/glog/glog.go:1145 +0x67
    main.main()
            /go/src/k8s.io/code-generator/cmd/deepcopy-gen/main.go:79 +0x253
    

    I created a simple test case using a Dockerfile below, using the sample-controller example, which replicates the problem.

    FROM golang:1.9.2
    
    ENV PATH=$PATH:/go/bin
    RUN go get -u k8s.io/code-generator/...
    RUN go get -d github.com/kubernetes/sample-controller
    RUN mkdir -p /go/src/k8s.io/kubernetes/hack/boilerplate && touch /go/src/k8s.io/kubernetes/hack/boilerplate/boilerplate.go.txt
    WORKDIR /
    RUN CODEGEN_PKG=/go/src/k8s.io/code-generator /go/src/github.com/kubernetes/sample-controller/hack/update-codegen.sh
    

    Help?

    lifecycle/rotten 
    opened by markmandel 15
  • Make generators consumable as Bazel rules

    Make generators consumable as Bazel rules

    It'd be nice to be able to use generators in a Bazel-based build as rules. E.g. a rule for client generation, a rule for deep copy functions generation, etc. Like e.g. rules_go.

    /cc @ixdy

    lifecycle/rotten 
    opened by ash2k 13
  • Is this a bug?

    Is this a bug?

    I want to update my controller to new version. But I get this error when to produce the code:

    Generating deepcopy funcs
    Generating clientset for mycontroller:v1 at github.com/dongwangdw/mycontroller/pkg/client/clientset
    panic: runtime error: index out of range [1] with length 0
    
    goroutine 95 [running]:
    golang.org/x/tools/internal/imports.(*ModuleResolver).scanDirForPackage(0xc001580ae0, 0xc005846440, 0x10, 0x4, 0xc006d518c0, 0x47, 0x0, 0x0, 0x0, 0x0, ...)
    	/root/go/pkg/mod/golang.org/x/[email protected]/internal/imports/mod.go:346 +0x990
    golang.org/x/tools/internal/imports.(*ModuleResolver).scan.func1(0xc005846440, 0x10, 0x4, 0xc006d518c0, 0x47)
    	/root/go/pkg/mod/golang.org/x/[email protected]/internal/imports/mod.go:263 +0x73e
    golang.org/x/tools/internal/gopathwalk.(*walker).walk(0xc008f3e100, 0xc006d518c0, 0x52, 0x0, 0xc006d518c0, 0x52)
    	/root/go/pkg/mod/golang.org/x/[email protected]/internal/gopathwalk/walk.go:175 +0xf3
    golang.org/x/tools/internal/fastwalk.(*walker).onDirEnt(0xc008278060, 0xc00691a5a0, 0x47, 0xc0052d11c0, 0xa, 0x0, 0x0, 0xc0058c9de0)
    	/root/go/pkg/mod/golang.org/x/[email protected]/internal/fastwalk/fastwalk.go:168 +0xbb
    golang.org/x/tools/internal/fastwalk.readDir(0xc00691a5a0, 0x47, 0xc0058cbe50, 0x0, 0x0)
    	/root/go/pkg/mod/golang.org/x/[email protected]/internal/fastwalk/fastwalk_unix.go:68 +0x228
    golang.org/x/tools/internal/fastwalk.(*walker).walk(0xc008278060, 0xc00691a5a0, 0x47, 0x1, 0x1, 0x0)
    	/root/go/pkg/mod/golang.org/x/[email protected]/internal/fastwalk/fastwalk.go:195 +0x6a
    golang.org/x/tools/internal/fastwalk.(*walker).doWork(0xc008278060, 0xc0059c8bf0)
    	/root/go/pkg/mod/golang.org/x/[email protected]/internal/fastwalk/fastwalk.go:134 +0x175
    created by golang.org/x/tools/internal/fastwalk.Walk
    	/root/go/pkg/mod/golang.org/x/[email protected]/internal/fastwalk/fastwalk.go:81 +0x248```
    lifecycle/rotten 
    opened by dongwangdw 11
  • Support for different plural forms (feature request)

    Support for different plural forms (feature request)

    We have a custom resource with a non-s plural form (Lets say a Bison CRD) - the plural of Bison is Bison.

    The API server etc all seem to handle this fine, but I can't see a tag to control this in code-gen at the moment. Right now I'm handling that by post-processing the generated code (sed -e /bisons/bison/) but this is suboptimal.

    kind/feature lifecycle/rotten 
    opened by rbtcollins 11
  • EvalSymlinks: too many links

    EvalSymlinks: too many links

    I'm getting an EvalSymlinks: too many links error when trying to generate a client:

    CODEGEN_PKG=../../../k8s.io/code-generator hack/update-codegen.sh
    

    on this repo.

    While looking into this, it seems that the error happens when generating code for the fake package:

    ❯ env CODEGEN_PKG=../../../k8s.io/code-generator hack/update-codegen.sh
    Generating deepcopy funcs
    path /home/asymmetric/code/go/src/github.com/habitat-sh/habitat-operator/pkg/apis/habitat/v1beta1
    Generating clientset for habitat:v1beta1 at github.com/habitat-sh/habitat-operator/pkg/client/clientset
    path /home/asymmetric/code/go/src/github.com/habitat-sh/habitat-operator/pkg/client/clientset/versioned
    path /home/asymmetric/code/go/src/github.com/habitat-sh/habitat-operator/pkg/client/clientset/versioned/scheme
    path /home/asymmetric/code/go/src/github.com/habitat-sh/habitat-operator/pkg/client/clientset/versioned/fake
    EvalSymlinks: too many links
    

    (Note: I added the debugging statement that prints path).

    This seems to be happening here, which is called indirectly as part of the client code generation.

    ❯ uname -omr
    4.13.0-37-generic x86_64 GNU/Linux
    

    Not sure if this helps, let me know if you need additional info or if I should open the issue on gengo or kubernetes.

    lifecycle/rotten 
    opened by asymmetric 11
  • Autogenerate register.go files

    Autogenerate register.go files

    Looking at register.go - it looks like these could be generated via this tool as well.

    When implementing a CRD it's not very clear what to put in these files, and a simple copy paste from the sample-controller with minor edits works, but it seems like this could be easily solved via code generation, which would also solve the documentation issue.

    Extra context: https://kubernetes.slack.com/archives/C0EG7JC6T/p1511820482000365

    Friction log: https://docs.google.com/a/google.com/document/d/1N1pBov7Odpg_u-kILHcswYh4VO-aMlYQdFIH64DYeek/edit?disco=AAAABhkn5r4

    opened by markmandel 11
  • Imports lowercased

    Imports lowercased

    Several packages are lowercased during package generation. This causes import errors if, for instance, your Github account uses uppercase characters.

    Some packages are also generated under lowercased paths so these packages are generated under a different directory than your project. For example my project might be in $GOPATH/src/github.com/IanLewis/myapp and code-generator will generate some files under $GOPATH/src/github.com/ianlewis/myapp

    opened by ianlewis 11
  • go-to-protobuf generated.proto reference k8s.* packages might clash with local package name

    go-to-protobuf generated.proto reference k8s.* packages might clash with local package name

    I'm using API Aggregation over sample-apiserver extension with go.company.name.net/k8s/api-server package name. Running go-to-protobuf works well, but it creates the following generated.proto code:

    package go.company.name.net.k8s.api_server.pkg.apis.ksm.v1alpha1;
    
    import "github.com/gogo/protobuf/gogoproto/gogo.proto";
    import "k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto";
    import "k8s.io/apimachinery/pkg/runtime/generated.proto";
    import "k8s.io/apimachinery/pkg/runtime/schema/generated.proto";
    
    // Package-wide variables from generator "generated".
    option (gogoproto.marshaler_all) = true;
    option (gogoproto.stable_marshaler_all) = true;
    option (gogoproto.sizer_all) = true;
    option (gogoproto.goproto_stringer_all) = false;
    option (gogoproto.stringer_all) = true;
    option (gogoproto.unmarshaler_all) = true;
    option (gogoproto.goproto_unrecognized_all) = false;
    option (gogoproto.goproto_enum_prefix_all) = false;
    option (gogoproto.goproto_getters_all) = false;
    option go_package = "go.company.name.net/k8s/api-server/pkg/apis/ksm/v1alpha1";
    
    // SecretReference is the Schema for the cloud fetch API objects
    message SecretReference {
      optional k8s.io.apimachinery.pkg.apis.meta.v1.ObjectMeta metadata = 1 [(gogoproto.customname) = "ObjectMeta", (gogoproto.nullable) = false];
    
      optional SecretReferenceSpec spec = 2 [(gogoproto.customname) = "Spec", (gogoproto.nullable) = false];
    
      optional SecretReferenceStatus status = 3 [(gogoproto.customname) = "Status", (gogoproto.nullable) = false];
    }
    

    I've managed a workaround way to fix it using one of the following ways:

    1. replace optional k8s.io... with optional .k8s.io...
    2. or replace package go.company.name.net.k8s.api_server.pkg.apis.ksm.v1alpha1; with package go.company.name.net._k8s.api_server.pkg.apis.ksm.v1alpha1;

    Still those variants don't work well, because the following generation chain is broken (so I've just made a replacement for package in a generator code itself to enforce it generate _k8s for my package).

    P.S. non k8s related packages which does not even use k8s dependencies works nicely with the tool ;)

    opened by tarvitz 0
  • Generated register.go (scheme and fake in clientset) spacing is different on macOS (darwin) vs RHEL (linux)

    Generated register.go (scheme and fake in clientset) spacing is different on macOS (darwin) vs RHEL (linux)

    Code-gen version k8s.io/code-generator v0.21.3

    When I generate to clienset on a mac there's a "tab" instead of spaces on linux in register.go . It's expected to be uniform across OSs.

    Note that other files are working as expected.

     M pkg/generated/clientset/versioned/scheme/register.go
    diff --git a/pkg/generated/clientset/versioned/fake/register.go b/pkg/generated/clientset/versioned/fake/register.go
    index 2e81944..0fb5d00 100644
    --- a/pkg/generated/clientset/versioned/fake/register.go
    +++ b/pkg/generated/clientset/versioned/fake/register.go
    @@ -36,14 +36,14 @@ var localSchemeBuilder = runtime.SchemeBuilder{
     // AddToScheme adds all types of this clientset into the given scheme. This allows composition
     // of clientsets, like in:
     //
    -//	import (
    -//	  "k8s.io/client-go/kubernetes"
    -//	  clientsetscheme "k8s.io/client-go/kubernetes/scheme"
    -//	  aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme"
    -//	)
    +//   import (
    +//     "k8s.io/client-go/kubernetes"
    +//     clientsetscheme "k8s.io/client-go/kubernetes/scheme"
    +//     aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme"
    +//   )
     //
    -//	kclientset, _ := kubernetes.NewForConfig(c)
    -//	_ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme)
    +//   kclientset, _ := kubernetes.NewForConfig(c)
    +//   _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme)
     //
     // After this, RawExtensions in Kubernetes types will serialize kube-aggregator types
     // correctly.
    diff --git a/pkg/generated/clientset/versioned/scheme/register.go b/pkg/generated/clientset/versioned/scheme/register.go
    index d92563b..b4090e5 100644
    --- a/pkg/generated/clientset/versioned/scheme/register.go
    +++ b/pkg/generated/clientset/versioned/scheme/register.go
    @@ -36,14 +36,14 @@ var localSchemeBuilder = runtime.SchemeBuilder{
     // AddToScheme adds all types of this clientset into the given scheme. This allows composition
     // of clientsets, like in:
     //
    -//	import (
    -//	  "k8s.io/client-go/kubernetes"
    -//	  clientsetscheme "k8s.io/client-go/kubernetes/scheme"
    -//	  aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme"
    -//	)
    +//   import (
    +//     "k8s.io/client-go/kubernetes"
    +//     clientsetscheme "k8s.io/client-go/kubernetes/scheme"
    +//     aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme"
    +//   )
     //
    -//	kclientset, _ := kubernetes.NewForConfig(c)
    -//	_ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme)
    +//   kclientset, _ := kubernetes.NewForConfig(c)
    +//   _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme)
     //
     // After this, RawExtensions in Kubernetes types will serialize kube-aggregator types
     // correctly.
    
    diff kind/bug 
    opened by pixelsoccupied 1
  • generated `With<MemberName> func implies shallow copy

    generated `With func implies shallow copy

    We would like to use struct types generated by protoc as .Spec field in CRD. The problem is that protobuf messages are not copyable while applyconfiguration-gen generates code that implies shallow copy.

    This code: https://github.com/kubernetes/code-generator/blob/a8a213c9be7b5d38a6b76abea283eecb9f4784c2/cmd/applyconfiguration-gen/generators/applyconfiguration.go#L244

    generates:

    func (b *CacheApplyConfiguration) WithSpec(value v1alpha1.CacheSpec) *CacheApplyConfiguration {
    	b.Spec = &value
    	return b
    }
    

    it would be great to have something like:

    func (b *CacheApplyConfiguration) WithSpec(value *v1alpha1.CacheSpec) *CacheApplyConfiguration {
    	b.Spec = value
    	return b
    }
    
    opened by rigazilla 0
  • applyconfiguration-gen fails on a map to a structure

    applyconfiguration-gen fails on a map to a structure

    Running k8s.io/code-generator/cmd/applyconfiguration-gen on types that contains a map to a structure fail with a templating error. (Maps to native type work fine.) Best shown on a simple reproducer:

    type Foo struct {
    	Bar string `json:"bar"`
    }
    
    // +kubebuilder:object:root=true
    // +kubebuilder:storageversion
    // +genclient
    // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    
    type MyType struct {
    	Foo Foo `json:"foo,omitempty"`
    
    	FooMap map[string]Foo `json:"fooMap,omitempty"`
    }
    
    
    $ GOPATH=/home/tnozicka/go go run "./vendor/k8s.io/code-generator/cmd/applyconfiguration-gen" --go-header-file='/dev/null' --input-dirs='github.com/scylladb/scylla-operator/pkg/api/test/v1' --output-package='github.com/scylladb/scylla-operator/pkg/client/scylla/applyconfigurations'
    F0830 19:24:58.455689  843949 main.go:53] Error: Failed executing generator: some packages had errors:
    template: /home/tnozicka/go/src/github.com/scylladb/scylla-operator/vendor/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/applyconfiguration.go:220:1:28: executing "/home/tnozicka/go/src/github.com/scylladb/scylla-operator/vendor/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/applyconfiguration.go:220" at <raw>: error calling raw: runtime error: invalid memory address or nil pointer dereference
    

    when I remove the map it all works fine

    type Foo struct {
    	Bar string `json:"bar"`
    }
    
    // +kubebuilder:object:root=true
    // +kubebuilder:storageversion
    // +genclient
    // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    
    type MyType struct {
    	Foo Foo `json:"foo,omitempty"`
    
    	// FooMap map[string]Foo `json:"fooMap,omitempty"`
    }
    
    kind/bug lifecycle/rotten 
    opened by tnozicka 4
  • infomer code gen happens erros

    infomer code gen happens erros

    bash generate-groups.sh informer ./pkg huachaoGoTest/api/ "samplecontroller.k8s:v1" --go-header-file=./boilerplate.go.txt -v 5 --output-base ./ 2>&1

    lifecycle/rotten 
    opened by huchaogithup 4
  • the default value of GoHeaderFilePath is wrong

    the default value of GoHeaderFilePath is wrong

    the problem is in this commit https://github.com/kubernetes/code-generator/commit/5bb9f2e1767e5cb98bb373649a1da739aa7cb4ff

    before: the default GoHeaderFilePath is filepath.Join(args.DefaultSourceTree(), path.Join(reflect.TypeOf(empty{}).PkgPath(), "/../../hack/boilerplate.go.txt"))

    after: the default GoHeaderFilePath is just filepath.Join(reflect.TypeOf(empty{}).PkgPath(), "/../../hack/boilerplate.go.txt") the args.DefaultSourceTree() is missing

    use the latest code,the generate-groups.sh can only run in ${GOPATH}/k8s.io/code-generator/

    otherwise will panic like this

    F0812 02:47:30.269045   68831 deepcopy.go:131] Failed loading boilerplate: open : no such file or directory
    goroutine 1 [running]:
    k8s.io/klog/v2.stacks(0xc000010001, 0xc000aff680, 0x6c, 0xbc)
            /Users/zhaohui/workspace/project/go/pkg/mod/k8s.io/klog/[email protected]/klog.go:1026 +0xb9
    k8s.io/klog/v2.(*loggingT).output(0x1619ba0, 0xc000000003, 0x0, 0x0, 0xc0000129a0, 0x0, 0x15f080c, 0xb, 0x83, 0x0)
            /Users/zhaohui/workspace/project/go/pkg/mod/k8s.io/klog/[email protected]/klog.go:975 +0x1f1
    k8s.io/klog/v2.(*loggingT).printf(0x1619ba0, 0xc000000003, 0x0, 0x0, 0x0, 0x0, 0x13d8852, 0x1e, 0xc0026122c0, 0x1, ...)
            /Users/zhaohui/workspace/project/go/pkg/mod/k8s.io/klog/[email protected]/klog.go:753 +0x19a
    k8s.io/klog/v2.Fatalf(...)
            /Users/zhaohui/workspace/project/go/pkg/mod/k8s.io/klog/[email protected]/klog.go:1514
    k8s.io/gengo/examples/deepcopy-gen/generators.Packages(0xc0002e1810, 0xc00011d040, 0x13b4e22, 0x6, 0xc0002e1810)
            /Users/zhaohui/workspace/project/go/pkg/mod/k8s.io/[email protected]/examples/deepcopy-gen/generators/deepcopy.go:131 +0x135
    k8s.io/gengo/args.(*GeneratorArgs).Execute(0xc00011d040, 0xc0023bfe38, 0x13b4e22, 0x6, 0x13e4650, 0x0, 0x0)
            /Users/zhaohui/workspace/project/go/pkg/mod/k8s.io/[email protected]/args/args.go:206 +0x1a9
    main.main()
            /Users/zhaohui/workspace/project/go/src/k8s.io/code-generator/cmd/deepcopy-gen/main.go:75 +0x42b
    
    goroutine 6 [chan receive]:
    k8s.io/klog/v2.(*loggingT).flushDaemon(0x1619ba0)
            /Users/zhaohui/workspace/project/go/pkg/mod/k8s.io/klog/[email protected]/klog.go:1169 +0x8b
    created by k8s.io/klog/v2.init.0
            /Users/zhaohui/workspace/project/go/pkg/mod/k8s.io/klog/[email protected]/klog.go:420 +0xdf
    
    

    because the method util.BoilerplatePath() just return an empty string.

    kind/bug 
    opened by 2581543189 14
Owner
Kubernetes
Kubernetes
Kubernetes
An implement of CNI depending on neutron API

An implement of CNI depending on neutron API

null 2 Dec 27, 2021
Use Golang to implement PHP's common built-in functions.

PHP2Go Use Golang to implement PHP's common built-in functions. About 140+ functions have been implemented. Install go get github.com/syyongx/php2go R

syyong.x 1.5k Dec 28, 2022
golang implement gossip algorithm

golang implement gossip algorithm

Gabriella Munger 4 Dec 1, 2022
Experimenting with golang generics to implement functional favorites like filter, map, && reduce.

funcy Experimenting with golang generics to implement functional favorites like filter, map, && reduce. 2021-12 To run the tests, you need to install

null 0 Dec 29, 2021
A go1.18+ package to (maybe) simplify performing operations on slices in a fluent-like style.

sop ✨ W.I.P. ✨ sop (slices operation) is a go1.18+ package to (maybe) simplify performing operations on slices in a fluent-like style with common oper

Ringo Hoffmann 3 Oct 1, 2022
Govalid is a data validation library that can validate most data types supported by golang

Govalid is a data validation library that can validate most data types supported by golang. Custom validators can be used where the supplied ones are not enough.

null 61 Apr 22, 2022
cuckoo-filter go implement. config by you 布谷鸟过滤器的Go实现,可以定制化过滤器参数

Cuckoo filter is a Bloom filter replacement for approximated set-membership queries. While Bloom filters are well-known space-efficient data structures to serve queries like "if item x is in a set?", they do not support deletion. Their variances to enable deletion (like counting Bloom filters) usually require much more space.

Linvon 244 Jan 1, 2023
Go implement D*Lite with SDL2

dstarlite-gosdl Go implement D*Lite with SDL2 使用Go SDL2库实现的D*Lite算法 注意,尽可能使用 go mod ,自动下载依赖库(确保已经安装了SDL2库) 推荐使用Linux或者macOS运行 git clone https://github

joxrays 2 Apr 2, 2022
Slice conversion between primitive types

sliceconv Sliceconv implements conversions to and from string representations of primitive types on entire slices. The package supports types int, flo

Henry Sarabia 8 Sep 27, 2022
Some utility functions for generic types in Go.

GOUF - Utility Functions for generic types Go team released Go 1.18 beta recently with support for Generics(a.k.a type parameters). This package provi

Akshay Bharambe 18 Apr 13, 2022
Clean-Swift source and test code auto-generator. It can save you time typing 500-600 lines of code.

Clean-Swift source & test code auto generator Overview Run Output Basic Usage make config.yaml target_project_name: Miro // target project name copyri

David Ha 20 Apr 13, 2022
Code generator that generates boilerplate code for a go http server

http-bootstrapper This is a code generator that uses go templates to generate a bootstrap code for a go http server. Usage Generate go http server cod

Jijo Thomas John 1 Nov 20, 2021
MNA - stands for mobile number assignment - a small zero external dependency golang library that is used to identify mobile number assignment in tanzania

MNA - stands for mobile number assignment - a small zero external dependency golang library that is used to identify mobile number assignment in tanzania

TECHCRAFT TECHNOLOGIES LIMITED 8 Nov 29, 2021
this is an api that execute your deno code and send you the output

this a simple api that execute your deno code and send you the output, has not limit per request example request: in deno: const rawResponse = await f

pai 13 Dec 23, 2022
a decision & trigger framework backed by Google's Common Expression Language used in graphikDB

a decision & trigger framework backed by Google's Common Expression Language used in graphikDB

null 62 Nov 15, 2022
流媒体NetFlix解锁检测脚本 / A script used to determine whether your network can watch native Netflix movies or not

netflix-verify 流媒体NetFlix解锁检测脚本,使用Go语言编写 在VPS网络正常的情况下,哪怕是双栈网络也可在几秒内快速完成IPv4/IPv6的解锁判断 鸣谢 感谢 @CoiaPrant 指出对于地域检测更简便的方法 感谢 @XmJwit 解决了IPV6 Only VPS无法下载脚

null 2k Dec 29, 2022
This is an open source project for commonly used functions for the Go programming language.

Common Functions This is an open source project for commonly used functions for the Go programming language. This package need >= go 1.3 Code Conventi

ᴜɴᴋɴᴡᴏɴ 741 Jan 8, 2023
sigbypass4xx is a utility to automate well-know techniques used to bypass access control restrictions.

sigbypass4xx sigbypass4xx is a utility to automate well-know techniques used to bypass access control restrictions. Resources Usage Installation From

Signed Security 8 Nov 9, 2022
Package strnaming is used to Convert string to camelCase, snake_case, kebab-case.

strnaming Package strnaming is used to Convert string to camelCase, snake_case, kebab-case. Contents strnaming Contents API Examples Install Quick sta

null 2 Oct 24, 2021