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

Allow to use the code-gen in vendorless projects

Open cardil opened this issue 2 years ago • 7 comments

Background

At Knative project, we would like to get rid of the vendor directory (see knative/infra#134 for details). One of the things that block that is the extensive use of the Kubernetes code-generator, to be exact, the generate-groups.sh shell script.

Recently, the shell code in the Kubernetes has been rewritten a bit in the right direction (https://github.com/kubernetes/kubernetes/pull/117262).

However, it still depends on the non-go shell scripts as the entry points, thus still requiring the use of vendor in the code. The usage of shell scripts isn't cross-platform.

Expected behavior

The Kubernetes code-gen should allow projects to leverage the go run ... syntax. With it, projects could use the code-gen even without any connection to Kuberentes code:

$ go run k8s.io/[email protected] deepcopy \
  knative.dev/pkg/client knative.dev/pkg/apis \
  'duck:v1alpha1,v1beta1,v1' \
  --go-header-file "${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt"

Or by managing the code-gen version in the project's go.mod file:

module knative.dev/serving

go 1.20

require (
	k8s.io/code-generator v0.28.1
)

and then running withing that project (notice the lack of the package version):

$ go run k8s.io/code-generator deepcopy \
  knative.dev/pkg/client knative.dev/pkg/apis \
  'duck:v1alpha1,v1beta1,v1' \
  --go-header-file "${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt"

Ideas

Refactor into an entry point binary.

The K8s code-gen already has multiple binaries in the cmd/ directory, but we could add the entry point code-gen binary there, that will directly support the same arguments as the current kube_codegen.sh script. This approach is cross-platform.

Embed the shell script in Go

An easier solution would be to use an approach similar to knative/hack#222, so embedding the shell scripts into Go code (with //go:embed feature), and extracting it into some tmp/ directory.

cardil avatar Sep 01 '23 21:09 cardil