Skip to content

Commit 7459bdf

Browse files
authored
Merge pull request #107 from kcp-dev/rewrite-to-upstream
✨ v3: rewrite to work the same as kubernetes code-generator
2 parents bdce542 + 30815cd commit 7459bdf

File tree

538 files changed

+16842
-16262
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

538 files changed

+16842
-16262
lines changed

.gitignore

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7-
/bin
7+
/_build
88

99
# Test binary, built with `go test -c`
1010
*.test
@@ -14,17 +14,7 @@
1414

1515
# Dependency directories
1616
vendor/
17+
hack/tools/
1718

1819
# IDE files
1920
.idea
20-
21-
hack/tools
22-
23-
examples/bin/applyconfiguration-gen
24-
examples/bin/client-gen
25-
examples/bin/conversion-gen
26-
examples/bin/deepcopy-gen
27-
examples/bin/defaulter-gen
28-
examples/bin/informer-gen
29-
examples/bin/lister-gen
30-
examples/bin/openapi-gen

.golangci.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ linters:
1010
- bidichk
1111
- bodyclose
1212
- containedctx
13+
- copyloopvar
1314
- dupword
1415
- durationcheck
1516
- errcheck
1617
- errchkjson
17-
- exportloopref
1818
- gocritic
1919
- godot
2020
- gofmt
@@ -29,10 +29,17 @@ linters:
2929
- noctx
3030
- nolintlint
3131
- nosprintfhostport
32-
- prealloc
3332
- revive
3433
- staticcheck
3534
- unconvert
3635
- unused
3736
- usestdlibvars
3837
- whitespace
38+
39+
linters-settings:
40+
revive:
41+
rules:
42+
# That is just how the Kubernetes code-generators are written.
43+
# We'd rather stay closer to upstream than fixing this.
44+
- name: unexported-return
45+
disabled: true

Makefile

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The KCP Authors.
1+
# Copyright 2025 The KCP Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -15,38 +15,18 @@
1515
SHELL := /usr/bin/env bash
1616

1717
GO_INSTALL = ./hack/go-install.sh
18+
BUILD_DEST ?= _build
19+
BUILDFLAGS ?=
20+
CMD ?= $(notdir $(wildcard ./cmd/*))
1821

1922
TOOLS_DIR=hack/tools
2023
GOBIN_DIR := $(abspath $(TOOLS_DIR))
2124
TMPDIR := $(shell mktemp -d)
2225

23-
CONTROLLER_GEN_VER := v0.17.0
24-
CONTROLLER_GEN_BIN := controller-gen
25-
CONTROLLER_GEN := $(GOBIN_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER)
26-
export CONTROLLER_GEN
27-
2826
GOLANGCI_LINT_VER := v1.62.2
2927
GOLANGCI_LINT_BIN := golangci-lint
3028
GOLANGCI_LINT := $(GOBIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)
3129

32-
KUBE_CLIENT_GEN_VER := v0.32.3
33-
KUBE_CLIENT_GEN_BIN := client-gen
34-
KUBE_LISTER_GEN_VER := v0.32.3
35-
KUBE_LISTER_GEN_BIN := lister-gen
36-
KUBE_INFORMER_GEN_VER := v0.32.3
37-
KUBE_INFORMER_GEN_BIN := informer-gen
38-
KUBE_APPLYCONFIGURATION_GEN_VER := v0.32.3
39-
KUBE_APPLYCONFIGURATION_GEN_BIN := applyconfiguration-gen
40-
41-
KUBE_CLIENT_GEN := $(GOBIN_DIR)/$(KUBE_CLIENT_GEN_BIN)-$(KUBE_CLIENT_GEN_VER)
42-
export KUBE_CLIENT_GEN
43-
KUBE_LISTER_GEN := $(GOBIN_DIR)/$(KUBE_LISTER_GEN_BIN)-$(KUBE_LISTER_GEN_VER)
44-
export KUBE_LISTER_GEN
45-
KUBE_INFORMER_GEN := $(GOBIN_DIR)/$(KUBE_INFORMER_GEN_BIN)-$(KUBE_INFORMER_GEN_VER)
46-
export KUBE_INFORMER_GEN
47-
KUBE_APPLYCONFIGURATION_GEN := $(GOBIN_DIR)/$(KUBE_APPLYCONFIGURATION_GEN_BIN)-$(KUBE_APPLYCONFIGURATION_GEN_VER)
48-
export KUBE_APPLYCONFIGURATION_GEN
49-
5030
OPENSHIFT_GOIMPORTS_VER := c70783e636f2213cac683f6865d88c5edace3157
5131
OPENSHIFT_GOIMPORTS_BIN := openshift-goimports
5232
OPENSHIFT_GOIMPORTS := $(TOOLS_DIR)/$(OPENSHIFT_GOIMPORTS_BIN)-$(OPENSHIFT_GOIMPORTS_VER)
@@ -60,30 +40,26 @@ imports: $(OPENSHIFT_GOIMPORTS)
6040
$(OPENSHIFT_GOIMPORTS) --path ./examples -m acme.corp
6141
.PHONY: imports
6242

63-
$(CONTROLLER_GEN):
64-
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/$(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)
43+
.PHONY: clean
44+
clean:
45+
rm -rf $(BUILD_DEST)
46+
@echo "Cleaned $(BUILD_DEST)"
6547

66-
$(KUBE_CLIENT_GEN):
67-
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) k8s.io/code-generator/cmd/$(KUBE_CLIENT_GEN_BIN) $(KUBE_CLIENT_GEN_BIN) $(KUBE_CLIENT_GEN_VER)
68-
$(KUBE_LISTER_GEN):
69-
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) k8s.io/code-generator/cmd/$(KUBE_LISTER_GEN_BIN) $(KUBE_LISTER_GEN_BIN) $(KUBE_LISTER_GEN_VER)
70-
$(KUBE_INFORMER_GEN):
71-
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) k8s.io/code-generator/cmd/$(KUBE_INFORMER_GEN_BIN) $(KUBE_INFORMER_GEN_BIN) $(KUBE_INFORMER_GEN_VER)
72-
$(KUBE_APPLYCONFIGURATION_GEN):
73-
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) k8s.io/code-generator/cmd/$(KUBE_APPLYCONFIGURATION_GEN_BIN) $(KUBE_APPLYCONFIGURATION_GEN_BIN) $(KUBE_APPLYCONFIGURATION_GEN_VER)
48+
.PHONY: build
49+
build: $(CMD)
7450

51+
.PHONY: $(CMD)
52+
$(CMD): %: $(BUILD_DEST)/%
7553

76-
.PHONY: build
77-
build: ## Build the project
78-
mkdir -p bin
79-
go build -o bin
54+
$(BUILD_DEST)/%: cmd/%
55+
go build $(BUILDFLAGS) -o $@ ./cmd/$*
8056

8157
.PHONY: install
8258
install:
8359
go install
8460

8561
.PHONY: codegen
86-
codegen: $(CONTROLLER_GEN) $(KUBE_CLIENT_GEN) $(KUBE_LISTER_GEN) $(KUBE_INFORMER_GEN) $(KUBE_APPLYCONFIGURATION_GEN) build
62+
codegen: build
8763
./hack/update-codegen.sh
8864
$(MAKE) imports
8965

@@ -124,4 +100,5 @@ $(TOOLS_DIR)/verify_boilerplate.py:
124100

125101
.PHONY: verify-boilerplate
126102
verify-boilerplate: $(TOOLS_DIR)/verify_boilerplate.py
127-
$(TOOLS_DIR)/verify_boilerplate.py --boilerplate-dir=hack/boilerplate
103+
$(TOOLS_DIR)/verify_boilerplate.py --boilerplate-dir=hack/boilerplate --skip examples
104+
$(TOOLS_DIR)/verify_boilerplate.py --boilerplate-dir=hack/boilerplate/examples examples

README.md

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,77 @@
1-
## Code Generators for KCP-aware informers and listers
1+
## Code Generators for KCP-aware clients, informers and listers
22

3-
### Usage:
3+
This repository contains code generation tools analogous to the Kubernetes
4+
code-generator. It contains:
45

5-
```
6-
code-generator <command> [input-flags]
7-
```
8-
9-
where `<command>` can be one of:
10-
- lister
11-
- informer
6+
* `cluster-client-gen` to generate a cluster-aware clientset,
7+
* `cluster-informer-gen` to generate cluster-aware informers and
8+
* `cluster-lister-gen` to do the same for listers.
129

13-
It is possible to run the code generation for multiple components at once `code-generator lister,informer [input-flags]`
10+
Note that you need to have generated the versioned Kubernetes clientset and
11+
applyconfiguration packages already in order to generate and use cluster-aware
12+
code. Single-cluster listers and informers however are optional and the
13+
generator here can generate the necessary interfaces itself.
1414

15-
#### Input flags:
15+
### Usage
1616

17-
1. `--input-dir` - The directory path where APIs are defined. Make sure that the types are defined in `<inputDir>/pkg/apis/{$GROUP}/{$VERSION}`. For example, if your input apis are defined in `types.go` inside `examples/pkg/apis/apps/v1/types.go`, the input directory should be specified as `examples/pkg`. `{$GROUP}/{$VERSION}` is appended in the input path.
18-
**Note**: This is the relative path to the input directory where APIs live.
17+
It is strongly recommended to use the provided `cluster_codegen.sh`, which works
18+
very much like Kubernetes' `kube_codegen.sh`. A common way to acquire it is to
19+
have a synthetic Go dependency on `github.com/kcp-dev/code-generator/v3/cmd/cluster-client-gen`
20+
(often done in a `hack/tools.go`) and then call it like so in your project:
1921

20-
2. `--output-dir` - The directory where output clients are to be generated. It defaults to the `output` folder under current working directory.
21-
- Listers are output to `<outputDir>/listers/${GROUP}/${VERSION}/${TYPE}.go`
22-
- Individual informers are output to `<outputDir>/informers/externalversions/${GROUP}/${VERSION}/${TYPE}.go`
22+
```bash
23+
# Often you would want to generate both the regular Kubernetes clientset and
24+
# the cluster-aware clienset.
2325

24-
3. `--clientset-api-path` - The path to where `clientset` generated by `k8s.io/code-gen` is present.
26+
CODEGEN_PKG="$(go list -f '{{.Dir}}' -m k8s.io/code-generator)"
27+
CLUSTER_CODEGEN_PKG="$(go list -f '{{.Dir}}' -m github.com/kcp-dev/code-generator/v3)"
2528

26-
4. `--group-versions` - List of group versions in the format `group:version`. Define multiple groups by specifying the flag again. For example, the inputs can be:
27-
- `--group-version="apps:v1"`
28-
- `--group-versions="rbac:v1" --group-versions="apps:v1"`
29-
- `--group-version="rbac:v1,v2"`
29+
source "$CODEGEN_PKG/kube_codegen.sh"
30+
source "$CLUSTER_CODEGEN_PKG/cluster_codegen.sh"
3031

31-
5. `--go-header-file` - Path to the header file.
32+
# Now you can call kube::codegen:: and cluster::codegen:: functions.
3233

33-
Example:
34-
To run it locally and see how it works, use the following command:
34+
kube::codegen::gen_client \
35+
--boilerplate hack/boilerplate/examples/boilerplate.generatego.txt \
36+
--output-dir pkg/generated \
37+
--output-pkg acme.corp/pkg/generated \
38+
--with-applyconfig \
39+
--applyconfig-name applyconfigurations \
40+
--with-watch \
41+
./pkg/apis
3542

43+
cluster::codegen::gen_client \
44+
--boilerplate hack/boilerplate/examples/boilerplate.generatego.txt \
45+
--output-dir pkg/clients \
46+
--output-pkg acme.corp/pkg/clients \
47+
--with-watch \
48+
--single-cluster-versioned-clientset-pkg acme.corp/pkg/generated/clientset/versioned \
49+
--single-cluster-applyconfigurations-pkg acme.corp/pkg/generated/applyconfigurations \
50+
--single-cluster-listers-pkg acme.corp/pkg/generated/listers \
51+
--single-cluster-informers-pkg acme.corp/pkg/generated/informers/externalversions \
52+
pkg/apis
3653
```
37-
go run main.go informer,lister --go-header-file testdata/header.txt
38-
--clientset-api-path=github.com/kcp-dev/code-generator/testdata/pkg/generated/clientset/versioned
39-
--input-dir testdata/pkg/apis
40-
--output-dir testdata/pkg --group-versions example:v1
41-
```
4254

43-
creates output folders in `testdata/pkg/informers` and `testdata/pkg/listers`.
55+
Please refer to the [cluster_codegen.sh](./cluster_codegen.sh) for more information
56+
on the available command line flags.
57+
58+
## Contributing
59+
60+
We ❤️ our contributors! If you're interested in helping us out, please check out [contributing to kcp](https://docs.kcp.io/kcp/main/contributing/).
61+
62+
This community has a [Code of Conduct](./code-of-conduct.md). Please make sure to follow it.
63+
64+
## Getting in touch
65+
66+
There are several ways to communicate with us:
67+
68+
- The [`#kcp-dev` channel](https://app.slack.com/client/T09NY5SBT/C021U8WSAFK) in the [Kubernetes Slack workspace](https://slack.k8s.io).
69+
- Our mailing lists:
70+
- [kcp-dev](https://groups.google.com/g/kcp-dev) for development discussions.
71+
- [kcp-users](https://groups.google.com/g/kcp-users) for discussions among users and potential users.
72+
- By joining the kcp-dev mailing list, you should receive an invite to our bi-weekly community meetings.
73+
- See recordings of past community meetings on [YouTube](https://www.youtube.com/channel/UCfP_yS5uYix0ppSbm2ltS5Q).
74+
- The next community meeting dates are available via our [CNCF community group](https://community.cncf.io/kcp/).
75+
- Check the [community meeting notes document](https://docs.google.com/document/d/1PrEhbmq1WfxFv1fTikDBZzXEIJkUWVHdqDFxaY1Ply4) for future and past meeting agendas.
76+
- Browse the [shared Google Drive](https://drive.google.com/drive/folders/1FN7AZ_Q1CQor6eK0gpuKwdGFNwYI517M?usp=sharing) to share design docs, notes, etc.
77+
- Members of the kcp-dev mailing list can view this drive.

0 commit comments

Comments
 (0)