code-generator icon indicating copy to clipboard operation
code-generator copied to clipboard

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

Open JaneLiuL opened this issue 4 years ago • 52 comments

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"

JaneLiuL avatar Apr 25 '20 06:04 JaneLiuL

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"

JaneLiuL avatar Apr 25 '20 06:04 JaneLiuL

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

antoineco avatar Apr 28 '20 17:04 antoineco

I guess the question is integrate with the operator-sdk. If I only write a types.go and use the code-generate, that is success, the informer and lister and clientset generate correct.

JaneLiuL avatar May 03 '20 00:05 JaneLiuL

Is your code available somewhere so I can try to reproduce?

antoineco avatar May 03 '20 09:05 antoineco

I had the same problem, using an operator-sdk.

wubo0067 avatar May 13 '20 09:05 wubo0067

If you use operator-sdk, and use the code-generate to generate the clientset, informers, then you will get fail. But if you only write apis_types.go by yourself in a empty project, then you can use the code-generate to generate the clientset, informers and so on

JaneLiuL avatar May 13 '20 10:05 JaneLiuL

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle stale

fejta-bot avatar Aug 11 '20 10:08 fejta-bot

Stale issues rot after 30d of inactivity. Mark the issue as fresh with /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle rotten

fejta-bot avatar Sep 10 '20 11:09 fejta-bot

Rotten issues close after 30d of inactivity. Reopen the issue with /reopen. Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /close

fejta-bot avatar Oct 10 '20 11:10 fejta-bot

@fejta-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity. Reopen the issue with /reopen. Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Oct 10 '20 11:10 k8s-ci-robot

@gabrik because by default code generators try to read ${GOPATH}/src/k8s.io/code-generator/hack/boilerplate.go.txt. Notice the $GOPATH in there. That means you have to manually clone the code-generator repository at this location on your machine.

There is a better and simpler approach though:

I see you have your own boilerplate file in the repo you linked. That means you have to reference it manually by passing --go-header-file hack/boilerplate.go.txt at the end of this line.

Here is an example: https://github.com/knative-sandbox/eventing-gitlab/blob/602b94319985c830fa2a9ba978e3f3c5886a760b/hack/update-codegen.sh#L34-L37

antoineco avatar Feb 03 '21 16:02 antoineco

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

gabrik avatar Feb 03 '21 16:02 gabrik

/remove-lifecycle stale

chrislovecnm avatar Mar 09 '21 14:03 chrislovecnm

/remove-lifecycle rotten

chrislovecnm avatar Mar 09 '21 14:03 chrislovecnm

/open

chrislovecnm avatar Mar 09 '21 14:03 chrislovecnm

/reopen

chrislovecnm avatar Mar 09 '21 14:03 chrislovecnm

@chrislovecnm: Reopened this issue.

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Mar 09 '21 14:03 k8s-ci-robot

@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

chrislovecnm avatar Mar 09 '21 14:03 chrislovecnm

@chrislovecnm: The label(s) area/documentation cannot be applied, because the repository doesn't have them.

In response to this:

@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.

/area documentation

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Mar 09 '21 14:03 k8s-ci-robot

/open

onkarbanerjee avatar Apr 07 '21 18:04 onkarbanerjee

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

onkarbanerjee avatar Apr 07 '21 19:04 onkarbanerjee

@onkarbanerjee yes, if you describe your issue I can try to help.

Please provide snippets of your scripts, the structure of your project, and the errors you're seeing.

antoineco avatar Apr 07 '21 19:04 antoineco

@antoineco Thanks for your response. Here is what I am trying to do.

This is my code directory structure image

This is my types.go

// +genclient

// +genclient:noStatus

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

type Database struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`
	Spec DatabaseSpec `json:"spec"`
}

// DatabaseSpec is the spec for a Foo resource
type DatabaseSpec struct {
	User     string `json:"user"`
	Password string `json:"password"`
	Encoding string `json:"encoding,omitempty"`
}

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

type DatabaseList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata"`
	Items []Database `json:"items"`
}

This is my doc.go

// +k8s:deepcopy-gen=package
// Package v1 is the v1 version of the API.
// +groupName=example.com
package v1

I am running the script like below: image

I am not seeing any error as you can see above , but there are no files created ever

onkarbanerjee avatar Apr 07 '21 20:04 onkarbanerjee

@onkarbanerjee chances are that your code was generated in $GOPATH/src/github.com/onkarbanerjee/crd-code, and not in your current project's directory.

antoineco avatar Apr 07 '21 20:04 antoineco

@antoineco No code generated. image

onkarbanerjee avatar Apr 08 '21 06:04 onkarbanerjee

Your screenshot doesn't show pkg/client.

antoineco avatar Apr 08 '21 07:04 antoineco

@antoineco It only has the clientset folder and the files in them are mostly empty. image

onkarbanerjee avatar Apr 08 '21 07:04 onkarbanerjee

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.......

zq-david-wang avatar Apr 08 '21 10:04 zq-david-wang

@zq-david-wang It is unable to import the package it seems. image

onkarbanerjee avatar Apr 08 '21 11:04 onkarbanerjee

@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 avatar Apr 08 '21 11:04 antoineco