Skip to content

Commit

Permalink
Refactor the whole app with a Go application
Browse files Browse the repository at this point in the history
Signed-off-by: Xabier Larrakoetxea <[email protected]>
  • Loading branch information
slok committed Mar 27, 2024
1 parent 71a884d commit 3d1620b
Show file tree
Hide file tree
Showing 76 changed files with 2,164 additions and 781 deletions.
21 changes: 21 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
ignore:
# Ignore Kubernetes dependencies to have full control on them.
- dependency-name: "k8s.io/*"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "docker"
directory: "/docker/dev"
schedule:
interval: "daily"
- package-ecosystem: "docker"
directory: "/docker/prod"
schedule:
interval: "daily"
34 changes: 30 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,40 @@ name: CI
on: [push]

jobs:
check:
name: Check
runs-on: ubuntu-latest
container: golangci/golangci-lint:v1.56.2-alpine
steps:
- run: echo "machine github.com login ${CW_GO_DEPS_LOGIN} password ${CW_GO_DEPS_TOKEN}" > ~/.netrc
- uses: actions/checkout@v4
- run: |
# We need this go flag because it started to error after golangci-lint is using Go 1.21.
# TODO(slok): Remove it on next (>1.54.1) golangci-lint upgrade to check if this problem has gone.
export GOFLAGS="-buildvcs=false"
./scripts/check/check.sh
unit-test:
name: Unit test
runs-on: ubuntu-latest
steps:
- run: echo "machine github.com login ${CW_GO_DEPS_LOGIN} password ${CW_GO_DEPS_TOKEN}" > ~/.netrc
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: make ci-test

release-images:
# Only on main branch.
if: startsWith(github.ref, 'refs/heads/main')
env:
IMAGE_NAME: ghcr.io/${GITHUB_REPOSITORY}
TAG_IMAGE_LATEST: "true"
PROD_IMAGE_NAME: ghcr.io/${GITHUB_REPOSITORY}
VERSION: ${GITHUB_SHA}
name: Release images
runs-on: ubuntu-latest
needs: [check, unit-test]
steps:
- uses: actions/checkout@v3
- uses: docker/login-action@v2
Expand All @@ -20,15 +45,16 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and publish docker images
run: make build-publish-image
run: make build-publish-image-all

tagged-release-images:
# Only on tags.
if: startsWith(github.ref, 'refs/tags/')
env:
IMAGE_NAME: ghcr.io/${GITHUB_REPOSITORY}
PROD_IMAGE_NAME: ghcr.io/${GITHUB_REPOSITORY}
name: Tagged release images
runs-on: ubuntu-latest
needs: [check, unit-test]
steps:
- run: echo "VERSION=${GITHUB_REF#refs/*/}" >> ${GITHUB_ENV} # Sets VERSION env var.
- uses: actions/checkout@v3
Expand All @@ -38,4 +64,4 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and publish docker images
run: make build-publish-image
run: make build-publish-image-all
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Editors
.idea
.vscode

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Vendor directory
vendor/

# Test coverage.
.test_coverage.txt

# Binaries
/bin
21 changes: 21 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
run:
timeout: 3m
build-tags:
- integration

linters:
enable:
- misspell
- goimports
- revive
- gofmt
#- depguard
- godot

linters-settings:
revive:
rules:
# Spammy linter and complex to fix on lots of parameters. Makes more harm that it solves.
- name: unused-parameter
disabled: true
51 changes: 0 additions & 51 deletions Dockerfile

This file was deleted.

87 changes: 77 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,88 @@


SHELL := $(shell which bash)
OSTYPE := $(shell uname)
DOCKER := $(shell command -v docker)
GID := $(shell id -g)
UID := $(shell id -u)
VERSION ?= $(shell git describe --tags --always)

default: build-image
UNIT_TEST_CMD := ./scripts/check/unit-test.sh
INTEGRATION_TEST_CMD := ./scripts/check/integration-test.sh
CHECK_CMD := ./scripts/check/check.sh

DEV_IMAGE_NAME := localdev/kube-code-generator-dev
PROD_IMAGE_NAME ?= ghcr.io/slok/kube-code-generator

DOCKER_RUN_CMD := docker run --env ostype=$(OSTYPE) -v ${PWD}:/src --rm ${DEV_IMAGE_NAME}
BUILD_BINARY_CMD := VERSION=${VERSION} ./scripts/build/bin/build.sh
BUILD_BINARY_ALL_CMD := VERSION=${VERSION} ./scripts/build/bin/build-all.sh
BUILD_DEV_IMAGE_CMD := IMAGE=${DEV_IMAGE_NAME} DOCKER_FILE_PATH=./docker/dev/Dockerfile VERSION=latest ./scripts/build/docker/build-image-dev.sh
BUILD_PROD_IMAGE_CMD := IMAGE=${PROD_IMAGE_NAME} DOCKER_FILE_PATH=./docker/prod/Dockerfile VERSION=${VERSION} ./scripts/build/docker/build-image.sh
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
PUBLISH_PROD_IMAGE_CMD := IMAGE=${PROD_IMAGE_NAME} VERSION=${VERSION} ./scripts/build/docker/publish-image.sh

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

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

.PHONY: default
default: help

.PHONY: build-image
build-image: ## Builds the docker image.
@$(BUILD_IMAGE_CMD)
build-image: ## Builds the production docker image.
@$(BUILD_PROD_IMAGE_CMD)

.PHONY: build-publish-image-all
build-publish-image-all: ## Builds and publishes all the production docker images (multiarch).
@$(BUILD_PUBLSIH_PROD_IMAGE_ALL_CMD)

.PHONY: build-dev-image
build-dev-image: ## Builds the development docker image.
@$(BUILD_DEV_IMAGE_CMD)

.PHONY: build
build: build-dev-image ## Builds the production binary.
@$(DOCKER_RUN_CMD) /bin/sh -c '$(BUILD_BINARY_CMD)'

.PHONY: build-all
build-all: build-dev-image ## Builds all archs production binaries.
@$(DOCKER_RUN_CMD) /bin/sh -c '$(BUILD_BINARY_ALL_CMD)'

.PHONY: test
test: build-dev-image ## Runs unit test.
@$(DOCKER_RUN_CMD) /bin/sh -c '$(UNIT_TEST_CMD)'
.PHONY: check
check: build-dev-image ## Runs checks.
@$(DOCKER_RUN_CMD) /bin/sh -c '$(CHECK_CMD)'

.PHONY: integration
integration: build-dev-image ## Runs integration test.
@$(DOCKER_RUN_CMD) /bin/sh -c '$(INTEGRATION_TEST_CMD)'

.PHONY: go-gen
go-gen: build-dev-image ## Generates go based code.
@$(DOCKER_RUN_CMD) /bin/sh -c './scripts/gogen.sh'

.PHONY: gen
gen: go-gen ## Generates all.

.PHONY: deps
deps: build-dev-image ## Fixes the dependencies.
@$(DOCKER_RUN_CMD) /bin/sh -c './scripts/deps.sh'

.PHONY: ci-build
ci-build: ## Builds the production binary in CI environment (without docker).
@$(BUILD_BINARY_CMD)

.PHONY: ci-unit-test
ci-test: ## Runs unit test in CI environment (without docker).
@$(UNIT_TEST_CMD)

.PHONY: ci-check
ci-check: ## Runs checks in CI environment (without docker).
@$(CHECK_CMD)

.PHONY: build-publish-image
build-publish-image: ## Builds and publishes docker images.
@$(BUILD_PUBLSIH_IMAGE_CMD)
.PHONY: ci-integration
ci-integration: ## Runs integraton test in CI environment (without docker).
@$(INTEGRATION_TEST_CMD)
76 changes: 41 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
# Kube code generator

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

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

Uses [official code-generator](https://github.com/kubernetes/code-generator) created by Kubernetes to autogenerate the code required for the CRDs.
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.

## Generation targets
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.

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

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

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

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

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

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

Some settings are optional so you can customize special cases:
## How to use it

- On CRD manifest YAML generation:
- `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])
The easiest way is to use the provided Docker image as it has all the required upstream dependencies.

[unsecure-float64]: https://github.com/kubernetes-sigs/controller-tools/issues/245
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:

```bash
docker run -it --rm -v ${PWD}:/app ghcr.io/slok/kube-code-generator \
--apis-in ./apis \
--go-gen-out ./gen \
--crd-gen-out ./gen/manifests
```

However, the best way to know how to use it is with a full example, you have it in [_example](_example/) dir.

## Kubernetes versions

| Kubernetes | Docker image |
| ---------- | ------------------------------------------------------- |
| v1.29 | `docker pull ghcr.io/slok/kube-code-generator:v1.29.0` |

### Versions <v1.29

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).
Loading

0 comments on commit 3d1620b

Please sign in to comment.