Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 2 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*.dll
*.so
*.dylib
/bin
/_build

# Test binary, built with `go test -c`
*.test
Expand All @@ -14,17 +14,7 @@

# Dependency directories
vendor/
hack/tools/

# IDE files
.idea

hack/tools

examples/bin/applyconfiguration-gen
examples/bin/client-gen
examples/bin/conversion-gen
examples/bin/deepcopy-gen
examples/bin/defaulter-gen
examples/bin/informer-gen
examples/bin/lister-gen
examples/bin/openapi-gen
11 changes: 9 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ linters:
- bidichk
- bodyclose
- containedctx
- copyloopvar
- dupword
- durationcheck
- errcheck
- errchkjson
- exportloopref
- gocritic
- godot
- gofmt
Expand All @@ -29,10 +29,17 @@ linters:
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- revive
- staticcheck
- unconvert
- unused
- usestdlibvars
- whitespace

linters-settings:
revive:
rules:
# That is just how the Kubernetes code-generators are written.
# We'd rather stay closer to upstream than fixing this.
- name: unexported-return
disabled: true
57 changes: 17 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 The KCP Authors.
# Copyright 2025 The KCP Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,38 +15,18 @@
SHELL := /usr/bin/env bash

GO_INSTALL = ./hack/go-install.sh
BUILD_DEST ?= _build
BUILDFLAGS ?=
CMD ?= $(notdir $(wildcard ./cmd/*))

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

CONTROLLER_GEN_VER := v0.17.0
CONTROLLER_GEN_BIN := controller-gen
CONTROLLER_GEN := $(GOBIN_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER)
export CONTROLLER_GEN

GOLANGCI_LINT_VER := v1.62.2
GOLANGCI_LINT_BIN := golangci-lint
GOLANGCI_LINT := $(GOBIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)

KUBE_CLIENT_GEN_VER := v0.32.3
KUBE_CLIENT_GEN_BIN := client-gen
KUBE_LISTER_GEN_VER := v0.32.3
KUBE_LISTER_GEN_BIN := lister-gen
KUBE_INFORMER_GEN_VER := v0.32.3
KUBE_INFORMER_GEN_BIN := informer-gen
KUBE_APPLYCONFIGURATION_GEN_VER := v0.32.3
KUBE_APPLYCONFIGURATION_GEN_BIN := applyconfiguration-gen

KUBE_CLIENT_GEN := $(GOBIN_DIR)/$(KUBE_CLIENT_GEN_BIN)-$(KUBE_CLIENT_GEN_VER)
export KUBE_CLIENT_GEN
KUBE_LISTER_GEN := $(GOBIN_DIR)/$(KUBE_LISTER_GEN_BIN)-$(KUBE_LISTER_GEN_VER)
export KUBE_LISTER_GEN
KUBE_INFORMER_GEN := $(GOBIN_DIR)/$(KUBE_INFORMER_GEN_BIN)-$(KUBE_INFORMER_GEN_VER)
export KUBE_INFORMER_GEN
KUBE_APPLYCONFIGURATION_GEN := $(GOBIN_DIR)/$(KUBE_APPLYCONFIGURATION_GEN_BIN)-$(KUBE_APPLYCONFIGURATION_GEN_VER)
export KUBE_APPLYCONFIGURATION_GEN

OPENSHIFT_GOIMPORTS_VER := c70783e636f2213cac683f6865d88c5edace3157
OPENSHIFT_GOIMPORTS_BIN := openshift-goimports
OPENSHIFT_GOIMPORTS := $(TOOLS_DIR)/$(OPENSHIFT_GOIMPORTS_BIN)-$(OPENSHIFT_GOIMPORTS_VER)
Expand All @@ -60,30 +40,26 @@ imports: $(OPENSHIFT_GOIMPORTS)
$(OPENSHIFT_GOIMPORTS) --path ./examples -m acme.corp
.PHONY: imports

$(CONTROLLER_GEN):
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/$(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)
.PHONY: clean
clean:
rm -rf $(BUILD_DEST)
@echo "Cleaned $(BUILD_DEST)"

$(KUBE_CLIENT_GEN):
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) k8s.io/code-generator/cmd/$(KUBE_CLIENT_GEN_BIN) $(KUBE_CLIENT_GEN_BIN) $(KUBE_CLIENT_GEN_VER)
$(KUBE_LISTER_GEN):
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) k8s.io/code-generator/cmd/$(KUBE_LISTER_GEN_BIN) $(KUBE_LISTER_GEN_BIN) $(KUBE_LISTER_GEN_VER)
$(KUBE_INFORMER_GEN):
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) k8s.io/code-generator/cmd/$(KUBE_INFORMER_GEN_BIN) $(KUBE_INFORMER_GEN_BIN) $(KUBE_INFORMER_GEN_VER)
$(KUBE_APPLYCONFIGURATION_GEN):
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) k8s.io/code-generator/cmd/$(KUBE_APPLYCONFIGURATION_GEN_BIN) $(KUBE_APPLYCONFIGURATION_GEN_BIN) $(KUBE_APPLYCONFIGURATION_GEN_VER)
.PHONY: build
build: $(CMD)

.PHONY: $(CMD)
$(CMD): %: $(BUILD_DEST)/%

.PHONY: build
build: ## Build the project
mkdir -p bin
go build -o bin
$(BUILD_DEST)/%: cmd/%
go build $(BUILDFLAGS) -o $@ ./cmd/$*

.PHONY: install
install:
go install

.PHONY: codegen
codegen: $(CONTROLLER_GEN) $(KUBE_CLIENT_GEN) $(KUBE_LISTER_GEN) $(KUBE_INFORMER_GEN) $(KUBE_APPLYCONFIGURATION_GEN) build
codegen: build
./hack/update-codegen.sh
$(MAKE) imports

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

.PHONY: verify-boilerplate
verify-boilerplate: $(TOOLS_DIR)/verify_boilerplate.py
$(TOOLS_DIR)/verify_boilerplate.py --boilerplate-dir=hack/boilerplate
$(TOOLS_DIR)/verify_boilerplate.py --boilerplate-dir=hack/boilerplate --skip examples
$(TOOLS_DIR)/verify_boilerplate.py --boilerplate-dir=hack/boilerplate/examples examples
94 changes: 64 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,77 @@
## Code Generators for KCP-aware informers and listers
## Code Generators for KCP-aware clients, informers and listers

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

```
code-generator <command> [input-flags]
```

where `<command>` can be one of:
- lister
- informer
* `cluster-client-gen` to generate a cluster-aware clientset,
* `cluster-informer-gen` to generate cluster-aware informers and
* `cluster-lister-gen` to do the same for listers.

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

#### Input flags:
### Usage

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.
**Note**: This is the relative path to the input directory where APIs live.
It is strongly recommended to use the provided `cluster_codegen.sh`, which works
very much like Kubernetes' `kube_codegen.sh`. A common way to acquire it is to
have a synthetic Go dependency on `github.com/kcp-dev/code-generator/v3/cmd/cluster-client-gen`
(often done in a `hack/tools.go`) and then call it like so in your project:

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

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

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:
- `--group-version="apps:v1"`
- `--group-versions="rbac:v1" --group-versions="apps:v1"`
- `--group-version="rbac:v1,v2"`
source "$CODEGEN_PKG/kube_codegen.sh"
source "$CLUSTER_CODEGEN_PKG/cluster_codegen.sh"

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

Example:
To run it locally and see how it works, use the following command:
kube::codegen::gen_client \
--boilerplate hack/boilerplate/examples/boilerplate.generatego.txt \
--output-dir pkg/generated \
--output-pkg acme.corp/pkg/generated \
--with-applyconfig \
--applyconfig-name applyconfigurations \
--with-watch \
./pkg/apis

cluster::codegen::gen_client \
--boilerplate hack/boilerplate/examples/boilerplate.generatego.txt \
--output-dir pkg/clients \
--output-pkg acme.corp/pkg/clients \
--with-watch \
--single-cluster-versioned-clientset-pkg acme.corp/pkg/generated/clientset/versioned \
--single-cluster-applyconfigurations-pkg acme.corp/pkg/generated/applyconfigurations \
--single-cluster-listers-pkg acme.corp/pkg/generated/listers \
--single-cluster-informers-pkg acme.corp/pkg/generated/informers/externalversions \
pkg/apis
```
go run main.go informer,lister --go-header-file testdata/header.txt
--clientset-api-path=github.com/kcp-dev/code-generator/testdata/pkg/generated/clientset/versioned
--input-dir testdata/pkg/apis
--output-dir testdata/pkg --group-versions example:v1
```

creates output folders in `testdata/pkg/informers` and `testdata/pkg/listers`.
Please refer to the [cluster_codegen.sh](./cluster_codegen.sh) for more information
on the available command line flags.

## Contributing

We ❤️ our contributors! If you're interested in helping us out, please check out [contributing to kcp](https://docs.kcp.io/kcp/main/contributing/).

This community has a [Code of Conduct](./code-of-conduct.md). Please make sure to follow it.

## Getting in touch

There are several ways to communicate with us:

- The [`#kcp-dev` channel](https://app.slack.com/client/T09NY5SBT/C021U8WSAFK) in the [Kubernetes Slack workspace](https://slack.k8s.io).
- Our mailing lists:
- [kcp-dev](https://groups.google.com/g/kcp-dev) for development discussions.
- [kcp-users](https://groups.google.com/g/kcp-users) for discussions among users and potential users.
- By joining the kcp-dev mailing list, you should receive an invite to our bi-weekly community meetings.
- See recordings of past community meetings on [YouTube](https://www.youtube.com/channel/UCfP_yS5uYix0ppSbm2ltS5Q).
- The next community meeting dates are available via our [CNCF community group](https://community.cncf.io/kcp/).
- Check the [community meeting notes document](https://docs.google.com/document/d/1PrEhbmq1WfxFv1fTikDBZzXEIJkUWVHdqDFxaY1Ply4) for future and past meeting agendas.
- Browse the [shared Google Drive](https://drive.google.com/drive/folders/1FN7AZ_Q1CQor6eK0gpuKwdGFNwYI517M?usp=sharing) to share design docs, notes, etc.
- Members of the kcp-dev mailing list can view this drive.
Loading