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

Question about how to run examples #491

Closed
devdattakulkarni opened this issue Jun 18, 2019 · 15 comments
Closed

Question about how to run examples #491

devdattakulkarni opened this issue Jun 18, 2019 · 15 comments
Labels
kind/support Categorizes issue or PR as a support question.

Comments

@devdattakulkarni
Copy link

I was wondering if there are any pointers on how to run examples from the examples folder.

@DirectXMan12
Copy link
Contributor

For the ones that rely on CRDs, you'll have to generate CRD YAML and then apply it to the cluster. In either case, go run should be sufficient to run the actual example.

Can you be more specific in what issues you were having?

@DirectXMan12 DirectXMan12 added the kind/support Categorizes issue or PR as a support question. label Jun 19, 2019
@devdattakulkarni
Copy link
Author

Thanks for the pointer. I generated CRD YAML and applied it. Then ran 'go run main.go'. Here is the error that I am seeing:

2019-06-19T06:03:27.484-0500 ERROR setup problem running manager {"error": "open /tmp/k8s-webhook-server/serving-certs/tls.crt: no such file or directory"}

exit status 1

The manager fails to start because seems like it expects a certificate.

@devdattakulkarni
Copy link
Author

@DirectXMan12 ^

Do we need to generate a certificate that the manager needs?

@kragniz
Copy link

kragniz commented Jun 26, 2019

@devdattakulkarni yes, see this pr for docs on how to do this using cert-manager kubernetes-sigs/kubebuilder#776

@functicons
Copy link

I have the same question, how do I actually run the example_test.go?

@DirectXMan12
Copy link
Contributor

If you want to serve webhooks, yes, you'll have to have certificates.

example_test.go cannot be run directly. It's a golang convention for providing example code in godocs and checking that it continues to compile.

@ggaurav10
Copy link

Is there a way to change the default CertDir /tmp/k8s-webhook-server/serving-certs?

@DirectXMan12
Copy link
Contributor

You can create the webhook server directly, and use the CertDir field in the webhook.Options struct. You should be able to set this for the webhook server returned from GetWebhookServer or override that server, but I think something got lost in the shuffle there.

@mengqiy

@mengqiy
Copy link
Member

mengqiy commented Sep 16, 2019

Should be fixed by #569.
With it, you can set CertDir in manager.Options and then use webhook builder, which underlying calling GetWebhookServer.

@desourav
Copy link

desourav commented Jan 3, 2020

How do I disable webhook altogether?

@Abhishek-Srivastava
Copy link

For me ENABLE_WEBHOOKS=false is not working

@yoichiwo7
Copy link

@desourav @Abhishek-Srivastava
ENABLE_WEBHOOKS check is only implemented in the kubebuilder's CronJob example. None of controller-runtime/controller-gen/kubebuilder adds or provides the check.

https://github.com/kubernetes-sigs/kubebuilder/blob/abb49b494e5da4f84c90146edeac5393422f514c/docs/book/src/cronjob-tutorial/testdata/project/main.go#L103

If you need a way to disable webhook altogether, you can just add the if statement like the example above.

@brightzheng100
Copy link

I checked out the master branch to carry on the tutorial -- I may have missed something so it didn't work by following the tutorial.

Firstly, I needed to apply this patch:
kubernetes-sigs/kubebuilder#1466 (comment)

Then I tried to run it locally but it wouldn't work -- please run it and wait for a couple of seconds to see it crash.

# I tried this
make run ENABLE_WEBHOOKS=false

# and this
ENABLE_WEBHOOKS=false make run

Please note that I'd printed out the ENABLE_WEBHOOKS setting, it was already false -- so setting it to false to avoid this code block sounds not enough:

	setupLog.Info("ENABLE_WEBHOOKS setting", "ENABLE_WEBHOOKS", os.Getenv("ENABLE_WEBHOOKS"))
	if os.Getenv("ENABLE_WEBHOOKS") != "false" {
		setupLog.Info("set up webhook")
		if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
			setupLog.Error(err, "unable to create webhook", "webhook", "Captain")
			os.Exit(1)
		}
	}
$ ENABLE_WEBHOOKS=false make run
/Users/brightzheng/development/go/GOPATH/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
go fmt ./...
go vet ./...
/Users/brightzheng/development/go/GOPATH/bin/controller-gen "crd:trivialVersions=true" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
go run ./main.go
2020-11-24T20:48:19.447+0800	INFO	controller-runtime.metrics	metrics server is starting to listen	{"addr": ":8080"}
2020-11-24T20:48:19.448+0800	INFO	controller-runtime.builder	Registering a mutating webhook	{"GVK": "batch.tutorial.kubebuilder.io/v1, Kind=CronJob", "path": "/mutate-batch-tutorial-kubebuilder-io-v1-cronjob"}
2020-11-24T20:48:19.448+0800	INFO	controller-runtime.webhook	registering webhook	{"path": "/mutate-batch-tutorial-kubebuilder-io-v1-cronjob"}
2020-11-24T20:48:19.448+0800	INFO	controller-runtime.builder	Registering a validating webhook	{"GVK": "batch.tutorial.kubebuilder.io/v1, Kind=CronJob", "path": "/validate-batch-tutorial-kubebuilder-io-v1-cronjob"}
2020-11-24T20:48:19.448+0800	INFO	controller-runtime.webhook	registering webhook	{"path": "/validate-batch-tutorial-kubebuilder-io-v1-cronjob"}
2020-11-24T20:48:19.448+0800	INFO	setup	ENABLE_WEBHOOKS setting	{"ENABLE_WEBHOOKS": "false"}
2020-11-24T20:48:19.448+0800	INFO	setup	starting manager
2020-11-24T20:48:19.548+0800	INFO	controller-runtime.webhook.webhooks	starting webhook server
2020-11-24T20:48:19.548+0800	INFO	controller-runtime.manager	starting metrics server	{"path": "/metrics"}
2020-11-24T20:48:19.548+0800	INFO	controller	Starting EventSource	{"reconcilerGroup": "batch.tutorial.kubebuilder.io", "reconcilerKind": "CronJob", "controller": "cronjob", "source": "kind source: /, Kind="}
2020-11-24T20:48:49.548+0800	ERROR	setup	problem running manager	{"error": "[open /var/folders/0n/fwbj43n15v3g_gc74x682qgh0000gn/T/k8s-webhook-server/serving-certs/tls.crt: no such file or directory, failed waiting for all runnables to end within grace period of 30s: context deadline exceeded]", "errorCauses": [{"error": "open /var/folders/0n/fwbj43n15v3g_gc74x682qgh0000gn/T/k8s-webhook-server/serving-certs/tls.crt: no such file or directory"}, {"error": "failed waiting for all runnables to end within grace period of 30s: context deadline exceeded"}]}
github.com/go-logr/zapr.(*zapLogger).Error
	/Users/brightzheng/development/go/GOPATH/pkg/mod/github.com/go-logr/zapr@v0.1.0/zapr.go:128
main.main
	/Users/brightzheng/development/go/projects/cronjob-tutorial-repo/main.go:126
runtime.main
	/usr/local/go/src/runtime/proc.go:204
exit status 1
make: *** [run] Error 1

@brightzheng100
Copy link

Hmm, I saw the bug, here:

	if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
		setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
		os.Exit(1)
	}

	/*
		We'll also set up webhooks for our type, which we'll talk about next.
		We just need to add them to the manager.  Since we might want to run
		the webhooks separately, or not run them when testing our controller
		locally, we'll put them behind an environment variable.

		We'll just make sure to set `ENABLE_WEBHOOKS=false` when we run locally.
	*/
	setupLog.Info("ENABLE_WEBHOOKS setting", "ENABLE_WEBHOOKS", os.Getenv("ENABLE_WEBHOOKS"))
	if os.Getenv("ENABLE_WEBHOOKS") != "false" {
		setupLog.Info("set up webhook")
		if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
			setupLog.Error(err, "unable to create webhook", "webhook", "Captain")
			os.Exit(1)
		}
	}

@rufreakde
Copy link

@desourav @Abhishek-Srivastava ENABLE_WEBHOOKS check is only implemented in the kubebuilder's CronJob example. None of controller-runtime/controller-gen/kubebuilder adds or provides the check.

https://github.com/kubernetes-sigs/kubebuilder/blob/abb49b494e5da4f84c90146edeac5393422f514c/docs/book/src/cronjob-tutorial/testdata/project/main.go#L103

If you need a way to disable webhook altogether, you can just add the if statement like the example above.

@yoichiwo7
What would be against using this ENV variable in the generated code it just introduces the possibility to configure the deployment via environment. And it is used in the example anyways so there are benefits for beginners to have this setting directly in the boilerplate code anyways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Categorizes issue or PR as a support question.
Projects
None yet
Development

No branches or pull requests