From 676d14688c9d1a0b115857c98390ac9934323a58 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Tue, 14 Jun 2022 18:02:23 +0300 Subject: [PATCH] Containerized auto-generation Run code auto-generation inside a container instead of the host system. Our auto-generation depends on specific versions of a multitude of tools (like k8s code-generator, controller-gen, protoc, mockery etc). This made it really awkward (and error-prone) to run in the host environment, especially if/when you needed different versions of those tools for other projects. Making it even more unwieldy, the required versions of tools were not neatly documented anywhere (except for git commits, perhaps). With this patch we have a "fixed environment", as we build a special auto-generate-builder container which has correct versions of all the dependencies. Using the container makes auto-generation easy to run anywhere, independent of the host system, giving reproducibility and reliability. Also, the patch moves the auto-generation steps out from the makefile into a separate script, making the makefile cleaner and the script easier to maintain. --- Dockerfile_generator | 22 ++++++++++++++++++++++ Makefile | 28 +++++++++++----------------- hack/generate.sh | 30 ++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 Dockerfile_generator create mode 100755 hack/generate.sh diff --git a/Dockerfile_generator b/Dockerfile_generator new file mode 100644 index 000000000..ab347b4d3 --- /dev/null +++ b/Dockerfile_generator @@ -0,0 +1,22 @@ +FROM golang:1.18-buster as builder + +# Install tools +RUN go install github.com/vektra/mockery@v1.0.0 && \ + go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0 && \ + git clone https://github.com/kubernetes/code-generator -b v0.20.7 --depth 1 && \ + go install k8s.io/code-generator/cmd/...@v0.20.7 && \ + go install golang.org/x/tools/cmd/goimports@v0.1.1 && \ + go install github.com/golang/protobuf/protoc-gen-go@v1.4.3 + +RUN apt-get update && apt-get install unzip + +RUN curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip && \ + unzip protoc-3.17.3-linux-x86_64.zip -d /usr/local && \ + rm protoc-3.17.3-linux-x86_64.zip && \ + chmod a+x /usr/local/bin/protoc && \ + find /usr/local -type d | xargs chmod 755 && \ + find /usr/local -type f | xargs chmod a+r + + +# Expect to be working with nfd +WORKDIR /go/node-feature-discovery diff --git a/Makefile b/Makefile index 2c5819d4d..4f8562e39 100644 --- a/Makefile +++ b/Makefile @@ -14,8 +14,6 @@ BASE_IMAGE_MINIMAL ?= gcr.io/distroless/base MDL ?= mdl -K8S_CODE_GENERATOR ?= ../code-generator - # Docker base command for working with html documentation. # Use host networking because 'jekyll serve' is stupid enough to use the # same site url than the "host" it binds to. Thus, all the links will be @@ -129,21 +127,17 @@ templates: -e "}; /$$end/p; d }" -i deployment/helm/node-feature-discovery/values.yaml @rm nfd-worker.conf.tmp -generate: - go mod vendor - go generate ./cmd/... ./pkg/... ./source/... - rm -rf vendor/ - controller-gen object crd output:crd:stdout paths=./pkg/apis/... > deployment/base/nfd-crds/nodefeaturerule-crd.yaml - cp deployment/base/nfd-crds/nodefeaturerule-crd.yaml deployment/helm/node-feature-discovery/manifests/ - rm -rf sigs.k8s.io - $(K8S_CODE_GENERATOR)/generate-groups.sh client,informer,lister \ - sigs.k8s.io/node-feature-discovery/pkg/generated \ - sigs.k8s.io/node-feature-discovery/pkg/apis \ - "nfd:v1alpha1" --output-base=. \ - --go-header-file hack/boilerplate.go.txt - rm -rf pkg/generated - mv sigs.k8s.io/node-feature-discovery/pkg/generated pkg/ - rm -rf sigs.k8s.io +.generator.image.stamp: Dockerfile_generator + $(IMAGE_BUILD_CMD) -t nfd-generator -f Dockerfile_generator . + +generate: .generator.image.stamp + $(CONTAINER_RUN_CMD) --rm \ + -v "`pwd`:/go/node-feature-discovery" \ + -v "`go env GOCACHE`:/.cache" \ + -v "`go env GOMODCACHE`:/go/pkg/mod" \ + --user=`id -u`:`id -g`\ + nfd-generator \ + ./hack/generate.sh gofmt: @$(GO_FMT) -w -l $$(find . -name '*.go') diff --git a/hack/generate.sh b/hack/generate.sh new file mode 100755 index 000000000..e89e01f9c --- /dev/null +++ b/hack/generate.sh @@ -0,0 +1,30 @@ +#!/bin/bash -e +set -o pipefail + +# Default path for code-generator repo +K8S_CODE_GENERATOR=${K8S_CODE_GENERATOR:-../code-generator} + +go mod vendor + +go generate ./cmd/... ./pkg/... ./source/... + +rm -rf vendor/ + +controller-gen object crd output:crd:stdout paths=./pkg/apis/... > deployment/base/nfd-crds/nodefeaturerule-crd.yaml + +cp deployment/base/nfd-crds/nodefeaturerule-crd.yaml deployment/helm/node-feature-discovery/manifests/ + +rm -rf sigs.k8s.io + +${K8S_CODE_GENERATOR}/generate-groups.sh client,informer,lister \ + sigs.k8s.io/node-feature-discovery/pkg/generated \ + sigs.k8s.io/node-feature-discovery/pkg/apis \ + "nfd:v1alpha1" --output-base=. \ + --go-header-file hack/boilerplate.go.txt + +rm -rf pkg/generated + +mv sigs.k8s.io/node-feature-discovery/pkg/generated pkg/ + +rm -rf sigs.k8s.io +