Skip to content

Commit 6717899

Browse files
committed
scaffold: add support for envtest
1 parent 991be92 commit 6717899

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

scaffold/scaffold.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type Config struct {
5151
KubernetesVersion string `json:"kubernetesVersion,omitempty"`
5252
ControllerRuntimeVersion string `json:"controllerRuntimeVersion,omitempty"`
5353
AdmissionWebhookRuntimeVersion string `json:"admissionWebhookRuntimeVersion,omitempty"`
54+
EnvtestKubernetesVersion string `json:"envtestKubernetesVersion,omitempty"`
5455
Image string `json:"image,omitempty"`
5556
}
5657

@@ -81,6 +82,7 @@ var (
8182
kubernetesVersion = "v0.27.2"
8283
controllerRuntimeVersion = "v0.15.0"
8384
admissionWebhookRuntimeVersion = "v0.1.0"
85+
envtestKubernetesVersion = "1.26.1"
8486
)
8587

8688
func main() {
@@ -105,6 +107,7 @@ func main() {
105107
pflag.StringVar(&config.KubernetesVersion, "kubernetes-version", kubernetesVersion, "Kubernetes go-client version to be used")
106108
pflag.StringVar(&config.ControllerRuntimeVersion, "controller-runtime-version", controllerRuntimeVersion, "Controller-runtime version to be used")
107109
pflag.StringVar(&config.AdmissionWebhookRuntimeVersion, "admission-webhook-runtime-version", admissionWebhookRuntimeVersion, "Admission-webhook-runtime version to be used")
110+
pflag.StringVar(&config.EnvtestKubernetesVersion, "envtest-kubernetes-version", envtestKubernetesVersion, "Kubernetes version to be used by envtest")
108111
pflag.StringVar(&config.Image, "image", "controller:latest", "Name of the Docker/OCI image produced by this project")
109112
pflag.BoolVar(&skipPostProcessing, "skip-post-processing", false, "Skip post-processing")
110113
pflag.CommandLine.SortFlags = false
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"go.testEnvVars": {
3+
"KUBEBUILDER_ASSETS": "${workspaceFolder}/bin/k8s/current"
4+
}
5+
}

scaffold/templates/Dockerfile.tpl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ RUN go mod download
1515
COPY main.go main.go
1616
COPY api/ api/
1717
COPY pkg/ pkg/
18+
COPY hack/ hack/
19+
COPY Makefile Makefile
1820

1921
# Build
2022
# the GOARCH has not a default value to allow the binary be built according to the host where the command
2123
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2224
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2325
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
26+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} make test build
2527

2628
# Use distroless as minimal base image to package the manager binary
2729
# Refer to https://github.com/GoogleContainerTools/distroless for more details
2830
FROM gcr.io/distroless/static:nonroot
2931
WORKDIR /
30-
COPY --from=builder /workspace/manager .
32+
COPY --from=builder /workspace/bin/manager .
3133
USER 65532:65532
3234

3335
ENTRYPOINT ["/manager"]
34-

scaffold/templates/Makefile.tpl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Image URL to use all building/pushing image targets
22
IMG ?= {{ .image }}
3+
# K8s version used by envtest
4+
ENVTEST_K8S_VERSION = {{ .envtestKubernetesVersion }}
35

46
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
57
ifeq (,$(shell go env GOBIN))
@@ -51,6 +53,12 @@ fmt: ## Run go fmt against code.
5153
vet: ## Run go vet against code.
5254
go vet ./...
5355

56+
##@ Testing
57+
58+
.PHONY: test
59+
test: manifests generate fmt vet envtest ## Run tests.
60+
KUBEBUILDER_ASSETS="$(LOCALBIN)/k8s/current" go test ./... -coverprofile cover.out
61+
5462
##@ Build
5563

5664
.PHONY: build
@@ -74,7 +82,7 @@ docker-push: ## Push docker image with the manager.
7482
# Build and push docker image for all given platforms.
7583
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
7684
.PHONY: docker-buildx
77-
docker-buildx: ## Build and push docker image for the manager for cross-platform support
85+
docker-buildx: ## Build and push docker image for the manager for cross-platform support.
7886
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
7987
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
8088
- docker buildx create --name project-v3-builder
@@ -92,11 +100,24 @@ $(LOCALBIN):
92100

93101
## Tool Binaries
94102
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
103+
SETUP_ENVTEST ?= $(LOCALBIN)/setup-envtest
95104

96105
## Tool Versions
97106
CONTROLLER_TOOLS_VERSION ?= v0.9.2
107+
SETUP_ENVTEST_VERSION ?= latest
98108

99109
.PHONY: controller-gen
100110
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
101111
$(CONTROLLER_GEN): $(LOCALBIN)
102112
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
113+
114+
.PHONY: setup-envtest
115+
setup-envtest: $(SETUP_ENVTEST) ## Download setup-envtest locally if necessary.
116+
$(SETUP_ENVTEST): $(LOCALBIN)
117+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(SETUP_ENVTEST_VERSION)
118+
119+
.PHONY: envtest
120+
envtest: setup-envtest
121+
ENVTESTDIR=$$($(SETUP_ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path) ;\
122+
rm -f $(LOCALBIN)/k8s/current ;\
123+
ln -s $$ENVTESTDIR $(LOCALBIN)/k8s/current

0 commit comments

Comments
 (0)