code-generator: 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”

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 57 (13 by maintainers)

Most upvoted comments

image This is what have been generate, it is very strange, and when I check file generated/clientset/versioned/clientset.go only have one word “package versioned”

@droslean I have solved this problem, add ”-v 10“ flag, and the following errors were found:

unable to import "api/crd.k8s.xxx.com/v1": open /Users/westos/Workspace/gopath/src/qpa/vendor/api/crd.k8s.xxx.com/v1: no such file or directory

so copy api/crd.k8s.xxx.com/v1 to ./vendor

➜  tree generated -L 1
generated
├── clientset
├── informers
└── listers

append “-v 10” at the end of your command, it should tell where is wrong.

The tool would exit with success code when it has done nothing…

Hello,there. I have the problem something similar. I use the shell script to generate code, but I got only one clientset folder and which is almost empty. There is no any Errors or Warnings.The code I used is just the same as the sample-controller.

My project is just like this:

root: /sameple-controller ls
apis  controller.go  go.mod  go.sum  hack  main.go  vendor
root: /sameple-controller# tree apis
apis
└── samplecontroller
    ├── register.go
    └── v1alpha1
        ├── doc.go
        ├── register.go
        └── types.go

2 directories, 4 files

the shell script to generate code:

root:/sameple-controller# cat hack/update-codegen.sh 
set -o errexit
set -o nounset
set -o pipefail

# generate the code with:
# --output-base    because this script should also be able to run inside the vendor dir of
#                  k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
#                  instead of the $GOPATH directly. For normal projects this can be dropped.
../vendor/k8s.io/code-generator/generate-groups.sh \
  "deepcopy,client,informer,lister" \
  sameple-controller/generated \
  sameple-controller/apis \
  samplecontroller:v1alpha1 \
  --go-header-file $(pwd)/boilerplate.go.txt \
  --output-base $(pwd)/../../

My go.mod file:

root:/sameple-controller# cat go.mod 
// This is a generated file. Do not edit directly.

module k8s.io/sample-controller

go 1.16

require (
        k8s.io/apimachinery v0.21.0
        k8s.io/code-generator v0.21.0
)

replace (
        k8s.io/apimachinery => k8s.io/apimachinery v0.21.0
        k8s.io/code-generator => k8s.io/code-generator v0.21.0
)

The response is no Erroers

root:/sameple-controller/hack# bash update-codegen.sh 
Generating deepcopy funcs
Generating clientset for samplecontroller:v1alpha1 at sameple-controller/generated/clientset
Generating listers for samplecontroller:v1alpha1 at sameple-controller/generated/listers
Generating informers for samplecontroller:v1alpha1 at sameple-controller/generated/informers

And the generated folder only has a clientset folder in it, and its just almost empty:

root@cld-dnode2-1091:/home/wb.yanghairui/cluster-inspector-go/cmd/sameple-controller# tree generated/
generated/
└── clientset
    └── versioned
        ├── clientset.go
        ├── doc.go
        ├── fake
        │   ├── clientset_generated.go
        │   ├── doc.go
        │   └── register.go
        ├── scheme
        │   ├── doc.go
        │   └── register.go
        └── typed
            └── samplecontroller
                └── v1alpha1
                    ├── doc.go
                    ├── fake
                    │   ├── doc.go
                    │   └── fake_samplecontroller_client.go
                    ├── generated_expansion.go
                    └── samplecontroller_client.go

8 directories, 12 files

I don’t realy know what’s the problem is.

I have the same problem.Only clientset was generated.

me too !!!

workdir:

├── go.mod
├── go.sum
├── hack
│   ├── boilerplate.go.txt
│   ├── tools.go
│   ├── update-codegen.sh
├── pkg
│   └── apis
│       └── samplecrd
│           ├── register.go
│           └── v1
│               ├── doc.go
│               ├── register.go
│               └── types.go
└── vendor
    ├── github.com
     ...

./vendor/k8s.io/code-generator/generate-groups.sh “all” “./pkg/generated” “./pkg/apis” “samplecrd:v1” -o “./” --go-header-file “./hack/boilerplate.go.txt”

this is ok for me

@harryyann I was facing a similar issue. Mine was because the tags were not set correctly in types.go. i had a few empty lines between the comment and the struct definition. The gengo parser was skipping this saying the doesn’t need a client code generation

The below is what is had initially.

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



type PolicyDefinition struct {
    metav1.TypeMeta             `json:",inline"`
    // +optional
    metav1.ObjectMeta           `json:"metadata"`
    Name string                 `json:"name"`
}

I change it as follows to get it working

// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type PolicyDefinition struct {
    metav1.TypeMeta             `json:",inline"`
    // +optional
    metav1.ObjectMeta           `json:"metadata"`
    Name string                 `json:"name"`
}

There were some issues with the directory structure also. I felt instead of ironing out the supported directory structure it is easier to make a directory structure which the generator expect.

I have the structure as follows from the workspace root

 github.com
        └── user
            └── repo
                ├── go.mod
                ├── go.sum
                └── pkg
                    └── apis
                        └── k8s.crd.io
                            └── v1alpha1
                                ├── doc.go
                                ├── register.go
                                └── types.go

The tree is mimicking the module name github.com/user/repo

The generator is executed from the module directory github.com/user/repo

$GEN/generate-groups.sh all $MODULE/pkg/client $MODULE/pkg/apis "k8s.crd.io:v1alpha1" -h $GEN/hack/boilerplate.go.txt -o $WSROOT --go-header-file $GEN/hack/boilerplate.go.txt


GEN=<path to code-generator>
MODULE=<module name = github.com/user/repo>
WSROOT=<root of the workspace>

I have uploaded a sample code here incase needed https://github.com/naduvat/k8s-codegen-basic

@harryyann I have the same problem.Only clientset was generated.

This issue is till there for me, can anyone help?

Code generators write to a directory relative to $GOPATH/src. In your case:

  • $GOPATH/src/appmgmt-operator/generated/appmgmt-operator/api/clientset
  • $GOPATH/src/appmgmt-operator/generated/appmgmt-operator/api/listers
  • $GOPATH/src/appmgmt-operator/generated/appmgmt-operator/api/informers

@onkarbanerjee this seems like a misconfigured Go project or Go toolchain to me. This goes beyond the topic of this discussion, but here are two ways to fix this:

  • If your Go project uses modules, make sure go.mod starts with the line module github.com/onkarbanerjee/crd-code
  • If it doesn’t, please refer to Module-aware commands in the Go documentation

@antoineco I am having the same problem. and I am using a custom template. Where is the shell script located that you are calling?

I am reopening this issue because it would be awesome if we can get some documentation around this. A lot of codebases are using the operator sdk and client-gen together. If I have time I am happy to document this problem, but I have no idea how to fix it yet.

/area documentation

Thank you @antoineco I indeed added that parameter and now it works.