Skip to content

Commit 510a0d5

Browse files
committed
Refactor the whole app with a Go application
Signed-off-by: Xabier Larrakoetxea <[email protected]>
1 parent 71a884d commit 510a0d5

Some content is hidden

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

76 files changed

+2164
-781
lines changed

.github/dependabot.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "gomod"
4+
directory: "/"
5+
schedule:
6+
interval: "daily"
7+
ignore:
8+
# Ignore Kubernetes dependencies to have full control on them.
9+
- dependency-name: "k8s.io/*"
10+
- package-ecosystem: "github-actions"
11+
directory: "/"
12+
schedule:
13+
interval: "daily"
14+
- package-ecosystem: "docker"
15+
directory: "/docker/dev"
16+
schedule:
17+
interval: "daily"
18+
- package-ecosystem: "docker"
19+
directory: "/docker/prod"
20+
schedule:
21+
interval: "daily"

.github/workflows/ci.yaml

+30-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,40 @@ name: CI
33
on: [push]
44

55
jobs:
6+
check:
7+
name: Check
8+
runs-on: ubuntu-latest
9+
container: golangci/golangci-lint:v1.56.2-alpine
10+
steps:
11+
- run: echo "machine github.com login ${CW_GO_DEPS_LOGIN} password ${CW_GO_DEPS_TOKEN}" > ~/.netrc
12+
- uses: actions/checkout@v4
13+
- run: |
14+
# We need this go flag because it started to error after golangci-lint is using Go 1.21.
15+
# TODO(slok): Remove it on next (>1.54.1) golangci-lint upgrade to check if this problem has gone.
16+
export GOFLAGS="-buildvcs=false"
17+
./scripts/check/check.sh
18+
19+
unit-test:
20+
name: Unit test
21+
runs-on: ubuntu-latest
22+
steps:
23+
- run: echo "machine github.com login ${CW_GO_DEPS_LOGIN} password ${CW_GO_DEPS_TOKEN}" > ~/.netrc
24+
- uses: actions/checkout@v4
25+
- uses: actions/setup-go@v5
26+
with:
27+
go-version-file: go.mod
28+
- run: make ci-test
629

730
release-images:
831
# Only on main branch.
932
if: startsWith(github.ref, 'refs/heads/main')
1033
env:
11-
IMAGE_NAME: ghcr.io/${GITHUB_REPOSITORY}
34+
TAG_IMAGE_LATEST: "true"
35+
PROD_IMAGE_NAME: ghcr.io/${GITHUB_REPOSITORY}
1236
VERSION: ${GITHUB_SHA}
1337
name: Release images
1438
runs-on: ubuntu-latest
39+
needs: [check, unit-test]
1540
steps:
1641
- uses: actions/checkout@v3
1742
- uses: docker/login-action@v2
@@ -20,15 +45,16 @@ jobs:
2045
username: ${{ github.actor }}
2146
password: ${{ secrets.GITHUB_TOKEN }}
2247
- name: Build and publish docker images
23-
run: make build-publish-image
48+
run: make build-publish-image-all
2449

2550
tagged-release-images:
2651
# Only on tags.
2752
if: startsWith(github.ref, 'refs/tags/')
2853
env:
29-
IMAGE_NAME: ghcr.io/${GITHUB_REPOSITORY}
54+
PROD_IMAGE_NAME: ghcr.io/${GITHUB_REPOSITORY}
3055
name: Tagged release images
3156
runs-on: ubuntu-latest
57+
needs: [check, unit-test]
3258
steps:
3359
- run: echo "VERSION=${GITHUB_REF#refs/*/}" >> ${GITHUB_ENV} # Sets VERSION env var.
3460
- uses: actions/checkout@v3
@@ -38,4 +64,4 @@ jobs:
3864
username: ${{ github.actor }}
3965
password: ${{ secrets.GITHUB_TOKEN }}
4066
- name: Build and publish docker images
41-
run: make build-publish-image
67+
run: make build-publish-image-all

.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Editors
2+
.idea
3+
.vscode
4+
5+
# Binaries for programs and plugins
6+
*.exe
7+
*.exe~
8+
*.dll
9+
*.so
10+
*.dylib
11+
12+
# Test binary, built with `go test -c`
13+
*.test
14+
15+
# Output of the go coverage tool, specifically when used with LiteIDE
16+
*.out
17+
18+
# Vendor directory
19+
vendor/
20+
21+
# Test coverage.
22+
.test_coverage.txt
23+
24+
# Binaries
25+
/bin

.golangci.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
run:
3+
timeout: 3m
4+
build-tags:
5+
- integration
6+
7+
linters:
8+
enable:
9+
- misspell
10+
- goimports
11+
- revive
12+
- gofmt
13+
#- depguard
14+
- godot
15+
16+
linters-settings:
17+
revive:
18+
rules:
19+
# Spammy linter and complex to fix on lots of parameters. Makes more harm that it solves.
20+
- name: unused-parameter
21+
disabled: true

Dockerfile

-51
This file was deleted.

Makefile

+77-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,88 @@
11

2-
2+
SHELL := $(shell which bash)
3+
OSTYPE := $(shell uname)
4+
DOCKER := $(shell command -v docker)
5+
GID := $(shell id -g)
6+
UID := $(shell id -u)
37
VERSION ?= $(shell git describe --tags --always)
48

5-
default: build-image
9+
UNIT_TEST_CMD := ./scripts/check/unit-test.sh
10+
INTEGRATION_TEST_CMD := ./scripts/check/integration-test.sh
11+
CHECK_CMD := ./scripts/check/check.sh
12+
13+
DEV_IMAGE_NAME := localdev/kube-code-generator-dev
14+
PROD_IMAGE_NAME ?= ghcr.io/slok/kube-code-generator
15+
16+
DOCKER_RUN_CMD := docker run --env ostype=$(OSTYPE) -v ${PWD}:/src --rm ${DEV_IMAGE_NAME}
17+
BUILD_BINARY_CMD := VERSION=${VERSION} ./scripts/build/bin/build.sh
18+
BUILD_BINARY_ALL_CMD := VERSION=${VERSION} ./scripts/build/bin/build-all.sh
19+
BUILD_DEV_IMAGE_CMD := IMAGE=${DEV_IMAGE_NAME} DOCKER_FILE_PATH=./docker/dev/Dockerfile VERSION=latest ./scripts/build/docker/build-image-dev.sh
20+
BUILD_PROD_IMAGE_CMD := IMAGE=${PROD_IMAGE_NAME} DOCKER_FILE_PATH=./docker/prod/Dockerfile VERSION=${VERSION} ./scripts/build/docker/build-image.sh
21+
BUILD_PUBLSIH_PROD_IMAGE_ALL_CMD := IMAGE=${PROD_IMAGE_NAME} DOCKER_FILE_PATH=./docker/prod/Dockerfile VERSION=${VERSION} ./scripts/build/docker/build-publish-image-all.sh
22+
PUBLISH_PROD_IMAGE_CMD := IMAGE=${PROD_IMAGE_NAME} VERSION=${VERSION} ./scripts/build/docker/publish-image.sh
623

7-
IMAGE_NAME ?= ghcr.io/slok/kube-code-generator
8-
BUILD_IMAGE_CMD := IMAGE=${IMAGE_NAME} DOCKER_FILE_PATH=./Dockerfile VERSION=${VERSION} ./scripts/build-image.sh
9-
BUILD_PUBLSIH_IMAGE_CMD := IMAGE=${IMAGE_NAME} DOCKER_FILE_PATH=./Dockerfile VERSION=${VERSION} ./scripts/build-publish-image.sh
1024

1125
help: ## Show this help
1226
@echo "Help"
1327
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-20s\033[93m %s\n", $$1, $$2}'
1428

29+
.PHONY: default
30+
default: help
31+
1532
.PHONY: build-image
16-
build-image: ## Builds the docker image.
17-
@$(BUILD_IMAGE_CMD)
33+
build-image: ## Builds the production docker image.
34+
@$(BUILD_PROD_IMAGE_CMD)
35+
36+
.PHONY: build-publish-image-all
37+
build-publish-image-all: ## Builds and publishes all the production docker images (multiarch).
38+
@$(BUILD_PUBLSIH_PROD_IMAGE_ALL_CMD)
39+
40+
.PHONY: build-dev-image
41+
build-dev-image: ## Builds the development docker image.
42+
@$(BUILD_DEV_IMAGE_CMD)
43+
44+
.PHONY: build
45+
build: build-dev-image ## Builds the production binary.
46+
@$(DOCKER_RUN_CMD) /bin/sh -c '$(BUILD_BINARY_CMD)'
47+
48+
.PHONY: build-all
49+
build-all: build-dev-image ## Builds all archs production binaries.
50+
@$(DOCKER_RUN_CMD) /bin/sh -c '$(BUILD_BINARY_ALL_CMD)'
51+
52+
.PHONY: test
53+
test: build-dev-image ## Runs unit test.
54+
@$(DOCKER_RUN_CMD) /bin/sh -c '$(UNIT_TEST_CMD)'
55+
.PHONY: check
56+
check: build-dev-image ## Runs checks.
57+
@$(DOCKER_RUN_CMD) /bin/sh -c '$(CHECK_CMD)'
58+
59+
.PHONY: integration
60+
integration: build-dev-image ## Runs integration test.
61+
@$(DOCKER_RUN_CMD) /bin/sh -c '$(INTEGRATION_TEST_CMD)'
62+
63+
.PHONY: go-gen
64+
go-gen: build-dev-image ## Generates go based code.
65+
@$(DOCKER_RUN_CMD) /bin/sh -c './scripts/gogen.sh'
66+
67+
.PHONY: gen
68+
gen: go-gen ## Generates all.
69+
70+
.PHONY: deps
71+
deps: build-dev-image ## Fixes the dependencies.
72+
@$(DOCKER_RUN_CMD) /bin/sh -c './scripts/deps.sh'
73+
74+
.PHONY: ci-build
75+
ci-build: ## Builds the production binary in CI environment (without docker).
76+
@$(BUILD_BINARY_CMD)
77+
78+
.PHONY: ci-unit-test
79+
ci-test: ## Runs unit test in CI environment (without docker).
80+
@$(UNIT_TEST_CMD)
81+
82+
.PHONY: ci-check
83+
ci-check: ## Runs checks in CI environment (without docker).
84+
@$(CHECK_CMD)
1885

19-
.PHONY: build-publish-image
20-
build-publish-image: ## Builds and publishes docker images.
21-
@$(BUILD_PUBLSIH_IMAGE_CMD)
86+
.PHONY: ci-integration
87+
ci-integration: ## Runs integraton test in CI environment (without docker).
88+
@$(INTEGRATION_TEST_CMD)

README.md

+41-35
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,55 @@
11
# Kube code generator
22

3-
![Kubernetes release](https://img.shields.io/badge/Kubernetes-v1.27-green?logo=Kubernetes&style=flat&color=326CE5&logoColor=white)
3+
![Kubernetes release](https://img.shields.io/badge/Kubernetes-v1.<30-green?logo=Kubernetes&style=flat&color=326CE5&logoColor=white)
44

5-
A kubernetes code generator container that makes easier to generate CRD manifests and its Go clients.
5+
## Introduction
66

7-
Uses [official code-generator](https://github.com/kubernetes/code-generator) created by Kubernetes to autogenerate the code required for the CRDs.
7+
When we speak about Kubernetes operators or controllers, normally Go code, CR, CRDs... are required. To create all the autogenerated Kubernetes Go code (Clients, helpers...) and manifests (CRD), the process is a bit painful.
88

9-
## Generation targets
9+
This small project tries making easy this process, by creating a small layer between Kubernetes official tooling that are used to get all this autogenerated stuff, and abstract options and infer some others, making a better UX for the user.
1010

11-
- CRD based Go code (clients, lib...).
12-
- CRD manifest YAMLs to register your CRs on the cluster.
11+
The projects that are used under the hood are:
1312

14-
## Docker image versions
13+
- [code-generator](https://github.com/kubernetes/code-generator) for Go code autogeneration.
14+
- [controller-tools](https://github.com/kubernetes-sigs/controller-tools) for CRD autogeneration.
1515

16-
| | Docker image |
17-
| ---------------- | ------------------------------------------------------- |
18-
| Kubernetes v1.27 | `docker pull ghcr.io/slok/kube-code-generator:v1.27.0` |
19-
| Kubernetes v1.26 | `docker pull ghcr.io/slok/kube-code-generator:v1.26.0` |
20-
| Kubernetes v1.25 | `docker pull ghcr.io/slok/kube-code-generator:v1.25.0` |
21-
| Kubernetes v1.24 | `docker pull ghcr.io/slok/kube-code-generator:v1.24.0` |
22-
| Kubernetes v1.23 | `docker pull ghcr.io/slok/kube-code-generator:v1.23.0` |
23-
| Kubernetes v1.22 | `docker pull ghcr.io/slok/kube-code-generator:v1.22.0` |
24-
| Kubernetes v1.21 | `docker pull ghcr.io/slok/kube-code-generator:v1.21.1` |
25-
| Kubernetes v1.20 | `docker pull ghcr.io/slok/kube-code-generator:v1.20.1` |
26-
| Kubernetes v1.19 | `docker pull ghcr.io/slok/kube-code-generator:v1.19.2` |
27-
| Kubernetes v1.18 | `docker pull ghcr.io/slok/kube-code-generator:v1.18.0` |
28-
| Kubernetes v1.17 | `docker pull ghcr.io/slok/kube-code-generator:v1.17.3` |
29-
| Kubernetes v1.16 | `docker pull ghcr.io/slok/kube-code-generator:v1.16.7` |
30-
| Kubernetes v1.15 | `docker pull ghcr.io/slok/kube-code-generator:v1.15.10` |
31-
| Kubernetes v1.14 | `docker pull ghcr.io/slok/kube-code-generator:v1.14.2` |
32-
| Kubernetes v1.13 | `docker pull ghcr.io/slok/kube-code-generator:v1.13.5` |
33-
| Kubernetes v1.12 | `docker pull ghcr.io/slok/kube-code-generator:v1.12.4` |
34-
| Kubernetes v1.11 | `docker pull ghcr.io/slok/kube-code-generator:v1.11.3` |
35-
| Kubernetes v1.10 | `docker pull ghcr.io/slok/kube-code-generator:v1.10.0` |
36-
| Kubernetes v1.9 | `docker pull ghcr.io/slok/kube-code-generator:v1.9.1` |
16+
## Why and when use this
3717

38-
## Getting started
18+
- You don't like, need or use kubebuilder for your CRDs.
19+
- You want simple tooling to generate Kubernetes CRD Go clients and manifests.
20+
- You like safe standards and simple things.
21+
- You use CRDs for more/other things than operators (e.g: generating CLIs, storing state on k8s as APIs...).
22+
- You don't want to do hacky and ugly stuff to start creating Kubernetes tooling.
3923

40-
The best way to know how to use it is by checking the [example](example/) that will generate the required clients and CRD manifests.
24+
## Features
4125

42-
### Optional settings
26+
- Small API/configuration.
27+
- Safe standards
28+
- Ready to use Docker images.
29+
- Generates Go code like clients and informers (Used to implement operators, CLIs...).
30+
- Generates CRD manifests (Used for API registration on k8s clusters).
4331

44-
Some settings are optional so you can customize special cases:
32+
## How to use it
4533

46-
- On CRD manifest YAML generation:
47-
- `CRD_FLAG` env var to overwrite CRD flag with a custom one. (E.g: Use `allowDangerousTypes=true` to allow `float64` on generation, [more info here][unsecure-float64])
34+
The easiest way is to use the provided Docker image as it has all the required upstream dependencies.
4835

49-
[unsecure-float64]: https://github.com/kubernetes-sigs/controller-tools/issues/245
36+
Here is an example that mounts the current directory (a Go project) and generates the Go code and the CRDs by providing the APIs input directory and the generation output directories:
37+
38+
```bash
39+
docker run -it --rm -v ${PWD}:/app ghcr.io/slok/kube-code-generator \
40+
--apis-in ./apis \
41+
--go-gen-out ./gen \
42+
--crd-gen-out ./gen/manifests
43+
```
44+
45+
However, the best way to know how to use it is with a full example, you have it in [_example](_example/) dir.
46+
47+
## Kubernetes versions
48+
49+
| Kubernetes | Docker image |
50+
| ---------- | ------------------------------------------------------- |
51+
| v1.29 | `docker pull ghcr.io/slok/kube-code-generator:v1.29.0` |
52+
53+
### Versions <v1.29
54+
55+
With the release of Kubernetes v1.30, this app was rewritten from bash hacky scripts into a proper Go application, that is easier, more extendable and safer to use. This new refactor of the app works with Kubernetes `>=v1.29` and changes the usage of the app, so for other versions you will need to check the previous implementation I would suggest that you check the [Readme of `<v1.27`](https://github.com/slok/kube-code-generator/tree/v1.27.0).

0 commit comments

Comments
 (0)