Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Once you move main.go scaffolding can't find it to add webhook configs #1719

Closed
christopherhein opened this issue Oct 9, 2020 · 12 comments · Fixed by #1726
Closed

Once you move main.go scaffolding can't find it to add webhook configs #1719

christopherhein opened this issue Oct 9, 2020 · 12 comments · Fixed by #1726
Assignees
Labels
good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/bug Categorizes issue or PR as related to a bug.

Comments

@christopherhein
Copy link
Member

When trying to scaffold a new webhook if the main.go has moved it will fail to create anything even just the webhook file and ignoring updating of the main.go.

→ kubebuilder create webhook --defaulting --kind NestedControlPlane --group controlplane --version v1alpha3
Writing scaffold for you to edit...
apis/controlplane.cluster.x-k8s.io/v1alpha3/nestedcontrolplane_webhook.go
2020/10/09 11:35:40 failed to create webhook: failed to open main.go: open main.go: no such file or directory

I'm able to do the create command for new APIs and it will still succeed when the main.go is moved even though it errors. I'd expect that I can at least create the {resource}_webhook.go file at least.

What versions of software are you using? Specifically, the following are often useful:

→ go version
go version go1.15.2 darwin/amd64
→ kubebuilder version
Version: version.Version{KubeBuilderVersion:"2.3.1", KubernetesVendor:"1.16.4", GitCommit:"8b53abeb4280186e494b726edf8f54ca7aa64a49", BuildDate:"2020-03-26T16:42:00Z", GoOs:"unknown", GoArch:"unknown"}
sigs.k8s.io/controller-runtime v0.5.11
sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0
→ kubectl version
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.2", GitCommit:"f5743093fd1c663cb0cbc89748f730662345d44d", GitTreeState:"clean", BuildDate:"2020-09-16T21:51:49Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.2", GitCommit:"52c56ce7a8272c798dbc29846288d7cd9fbae032", GitTreeState:"clean", BuildDate:"2020-04-30T20:19:45Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

/kind bug

@k8s-ci-robot k8s-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label Oct 9, 2020
@camilamacedo86 camilamacedo86 added kind/bug Categorizes issue or PR as related to a bug. and removed kind/bug Categorizes issue or PR as related to a bug. labels Oct 11, 2020
@camilamacedo86
Copy link
Member

Hi @christopherhein,

The tool will scaffold data in the main.go so if you change the layout by moving the main.go it will no longer work. See that the create api also face the same;

Example

  • Steps to mock
$ cd kubebuilder/testdata/project-v3 
$ mkdir cmd
$ mv main.go cmd/main.go
  • Error:
 $ kubebuilder create api --group crew --version v1beta1 --kind Frigatec
Create Resource [y/n]
y
Create Controller [y/n]
y
Writing scaffold for you to edit...
api/v1beta1/frigatec_types.go
controllers/frigatec_controller.go
Error: failed to create API with version "3-alpha": error updating main.go: failed to open main.go: open main.go: no such file or directory
Usage:
  kubebuilder create api [flags]

Examples:
  # Create a frigates API with Group: ship, Version: v1beta1 and Kind: Frigate
  kubebuilder create api --group ship --version v1beta1 --kind Frigate

  # Edit the API Scheme
  nano api/v1beta1/frigate_types.go

  # Edit the Controller
  nano controllers/frigate/frigate_controller.go

  # Edit the Controller Test
  nano controllers/frigate/frigate_controller_test.go

  # Install CRDs into the Kubernetes cluster using kubectl apply
  make install

  # Regenerate code and run against the Kubernetes cluster configured by ~/.kube/config
  make run
	

Flags:
      --controller       if set, generate the controller without prompting the user (default true)
      --force            attempt to create resource even if it already exists
      --group string     resource Group
  -h, --help             help for api
      --kind string      resource Kind
      --make             if true, run make after generating files (default true)
      --namespaced       resource is namespaced (default true)
      --resource         if set, generate the resource without prompting the user (default true)
      --version string   resource Version

2020/10/11 12:46:49 failed to create API with version "3-alpha": error updating main.go: failed to open main.go: open main.go: no such file or directory
camilamacedo@Camilas-MacBook-Pro ~/go/src/sigs.k8s.io/kubebuilder/testdata/project-v3 (testissue) $ 

also, for both, it shows the expected behaviour. in this way, could you clarify why in your opinion it would be a bug?

@camilamacedo86 camilamacedo86 added the triage/needs-information Indicates an issue needs more information in order to work on it. label Oct 11, 2020
@christopherhein
Copy link
Member Author

Hey @camilamacedo86, sorry about that I should have been more clear.

The error messaging is fine, the interaction is different which is confusing. If you’ve moved the main.go and you use create api it will still create the other scaffolding besides updating the main.go where as when you use create webhook it will look like it wrote the scaffolded file into api/{kind}/{version}/{resource}_webhook.go and then error but none of the files are actually written.


sigs.k8s.io/kb-test: k8s:kind-kind                                                                                                                                                               
→ kubebuilder init --domain example.com                                                                                                                                                          
Writing scaffold for you to edit...                                                                                                                                                              
Get controller runtime:                                                                                                                                                                          
$ go get sigs.k8s.io/controller-runtime@v0.5.0                                                                                                                                                   
Update go.mod:                                                                                                                                                                                   
$ go mod tidy                                                                                                                                                                                    
Running make:                                                                                                                                                                                   
$ make                                                                                                                                                                                           
/Users/chris/Code/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."                                                                                                   
go fmt ./...                                                                                                                                                                                
go vet ./...                                                                                                                                                                                    
                                                                                                                                                                                              
go build -o bin/manager main.go
Next: define a resource with:
$ kubebuilder create api
insigs.k8s.io/kb-test: k8s:kind-kind
→ mkdir cmd
sigs.k8s.io/kb-test: k8s:kind-kind
→ mv main.go cmd
sigs.k8s.io/kb-test: k8s:kind-kind
→ kubebuilder create api --group crew --version v1beta1 --kind Frigate                           
Create Resource [y/n]
y
Create Controller [y/n]
y
Writing scaffold for you to edit...
api/v1beta1/frigate_types.go
controllers/frigate_controller.go
2020/10/11 14:41:14 failed to create API: error updating main.go: failed to open main.go: open main.go: no such file or directory
sigs.k8s.io/kb-test: k8s:kind-kind
→ ls -al api/v1beta1/
  rwx------  chris  staff   128 B    Sun Oct 11 14:41:14 2020    ./
  rwx------  chris  staff    96 B    Sun Oct 11 14:41:14 2020    ../
  rw-------  chris  staff     1 KiB  Sun Oct 11 14:41:14 2020    frigate_types.go 
  rw-------  chris  staff     1 KiB  Sun Oct 11 14:41:14 2020    groupversion_info.go 
sigs.k8s.io/kb-test: k8s:kind-kind
→ kubebuilder create webhook --defaulting --kind Frigate --group crew --version v1beta1          
Writing scaffold for you to edit...
api/v1beta1/frigate_webhook.go
2020/10/11 14:42:59 failed to create webhook: failed to open main.go: open main.go: no such file or directory
sigs.k8s.io/kb-test: k8s:kind-kind
→ ls -al api/v1beta1/                           
  rwx------  chris  staff   128 B    Sun Oct 11 14:41:14 2020    ./
  rwx------  chris  staff    96 B    Sun Oct 11 14:41:14 2020    ../
  rw-------  chris  staff     1 KiB  Sun Oct 11 14:41:14 2020    frigate_types.go 
  rw-------  chris  staff     1 KiB  Sun Oct 11 14:41:14 2020    groupversion_info.go 

It would be nice if it still created api/v1beta1/frigate_webhook.go in this example so that if you’re using other means to configure the controllers and webhooks and don’t care as much about the kubebuilder scaffolding in main.go you still can.

@camilamacedo86
Copy link
Member

Hi @christopherhein,

It happens because the tool goes until face the issue in both cases. The tool has no rollback in failures case. IHMO the correct approach would ensure in the tool that the required files are when should be in the validate func of the plugin which means that the error would be raised before to do any scaffold.

@christopherhein
Copy link
Member Author

I think that would be acceptable if you were able to turn off updating main.go with a flag or a config in PROJECT

@camilamacedo86
Copy link
Member

Hi @christopherhein,

Regards if you were able to turn off updating main.go with a flag or a config in PROJECT, it would be an RFE. Could you please raise an issue for that and what is the problem that it would solve and why you think that is important?

@christopherhein
Copy link
Member Author

Absolutely, I will get something started.

@christopherhein
Copy link
Member Author

/close

@k8s-ci-robot
Copy link
Contributor

@christopherhein: Closing this issue.

In response to this:

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

@camilamacedo86
Copy link
Member

Hi @christopherhein,

I think we can use this one to address the validation check to ensure that the main.go is in the root dir which is a very tiny change.
if nobody gets that, I will try to push it soon.

@camilamacedo86 camilamacedo86 added good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. and removed triage/needs-information Indicates an issue needs more information in order to work on it. labels Oct 15, 2020
@prafull01
Copy link
Contributor

/assign

@ArtieReus
Copy link

I am facing also this issue. I could run the cmd by moving temporary my main.go file to the root of the project. After running the cmd you can move back the main.go file to the place it is used to be.

@tengfeisun
Copy link

tengfeisun commented Nov 29, 2021

I also want to move my main.go into cmd package, bug it will be failed when create an API @camilamacedo86

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/bug Categorizes issue or PR as related to a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants