Skip to content

Commit 2eefb6a

Browse files
Setup all CI jobs
1 parent a5b036d commit 2eefb6a

16 files changed

+1114
-109
lines changed

.github/workflows/golangci-lint.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: golangci-lint
2+
on:
3+
pull_request:
4+
types: [opened, edited, synchronize, reopened]
5+
jobs:
6+
golangci:
7+
name: lint
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
working-directory:
12+
- ""
13+
- test
14+
- hack/tools
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: golangci-lint
18+
uses: golangci/golangci-lint-action@v2
19+
with:
20+
version: v1.43.0
21+
working-directory: ${{matrix.working-directory}}

Dockerfile

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# syntax=docker/dockerfile:1.1-experimental
2+
3+
# Copyright 2018 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Build the manager binary
18+
FROM golang:1.17.0 as builder
19+
WORKDIR /workspace
20+
21+
# Run this with docker build --build-arg goproxy=$(go env GOPROXY) to override the goproxy
22+
ARG goproxy=https://proxy.golang.org
23+
# Run this with docker build --build-arg package=./controlplane/kubeadm or --build-arg package=./bootstrap/kubeadm
24+
ENV GOPROXY=$goproxy
25+
26+
# Copy the Go Modules manifests
27+
COPY go.mod go.mod
28+
COPY go.sum go.sum
29+
30+
# Cache deps before building and copying source so that we don't need to re-download as much
31+
# and so that source changes don't invalidate our downloaded layer
32+
RUN --mount=type=cache,target=/go/pkg/mod \
33+
go mod download
34+
35+
# Copy the sources
36+
COPY ./ ./
37+
38+
# Cache the go build into the the Go’s compiler cache folder so we take benefits of compiler caching across docker build calls
39+
RUN --mount=type=cache,target=/root/.cache/go-build \
40+
--mount=type=cache,target=/go/pkg/mod \
41+
go build .
42+
43+
# Build
44+
ARG package=.
45+
ARG ARCH
46+
ARG ldflags
47+
48+
# Do not force rebuild of up-to-date packages (do not use -a) and use the compiler cache folder
49+
RUN --mount=type=cache,target=/root/.cache/go-build \
50+
--mount=type=cache,target=/go/pkg/mod \
51+
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \
52+
go build -ldflags "${ldflags} -extldflags '-static'" \
53+
-o manager ${package}
54+
55+
# Production image
56+
FROM gcr.io/distroless/static:nonroot
57+
WORKDIR /
58+
COPY --from=builder /workspace/manager .
59+
# Use uid of nonroot user (65532) because kubernetes expects numeric user when applying pod security policies
60+
USER 65532
61+
ENTRYPOINT ["/manager"]

Makefile

+54-28
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export DOCKER_CLI_EXPERIMENTAL := enabled
4040
TOOLS_DIR := $(ROOT)/hack/tools
4141
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
4242
BIN_DIR := bin
43+
GO_APIDIFF_BIN := $(BIN_DIR)/go-apidiff
44+
GO_APIDIFF := $(TOOLS_DIR)/$(GO_APIDIFF_BIN)
4345

4446
export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH)
4547

@@ -54,6 +56,7 @@ CONTROLLER_GEN := $(abspath $(TOOLS_BIN_DIR)/controller-gen)
5456
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint
5557
KUSTOMIZE := $(abspath $(TOOLS_BIN_DIR)/kustomize)
5658
SETUP_ENVTEST := $(abspath $(TOOLS_BIN_DIR)/setup-envtest)
59+
GOTESTSUM := $(abspath $(TOOLS_BIN_DIR)/gotestsum)
5760

5861
# Define Docker related variables. Releases should modify and double check these vars.
5962
REGISTRY ?= gcr.io/$(shell gcloud config get-value project)
@@ -83,6 +86,8 @@ help: ## Display this help
8386
## Testing
8487
## --------------------------------------
8588

89+
ARTIFACTS ?= ${ROOT}/_artifacts
90+
8691
KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use --use-env -p path $(KUBEBUILDER_ENVTEST_KUBERNETES_VERSION))
8792

8893
.PHONY: test
@@ -93,38 +98,77 @@ test: $(SETUP_ENVTEST) ## Run unit and integration tests
9398
test-verbose: ## Run tests with verbose settings.
9499
TEST_ARGS="$(TEST_ARGS) -v" $(MAKE) test
95100

101+
.PHONY: test-junit
102+
test-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run tests with verbose setting and generate a junit report
103+
set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.exitcode) | tee $(ARTIFACTS)/junit.stdout
104+
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.xml --raw-command cat $(ARTIFACTS)/junit.stdout
105+
exit $$(cat $(ARTIFACTS)/junit.exitcode)
106+
96107
## --------------------------------------
97108
## Binaries
98109
## --------------------------------------
99110

100111
.PHONY: operator
101112
operator: ## Build operator binary
102-
go build -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/operator sigs.k8s.io/cluster-api-operator
113+
go build -trimpath -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/operator sigs.k8s.io/cluster-api-operator
103114

104115
$(CONTROLLER_GEN): $(TOOLS_DIR)/go.mod # Build controller-gen from tools folder.
105116
cd $(TOOLS_DIR); go build -tags=tools -o $(BIN_DIR)/controller-gen sigs.k8s.io/controller-tools/cmd/controller-gen
106117

107-
$(GOLANGCI_LINT): $(TOOLS_DIR)/go.mod # Build golangci-lint from tools folder.
108-
cd $(TOOLS_DIR); go build -tags=tools -o $(BIN_DIR)/golangci-lint github.com/golangci/golangci-lint/cmd/golangci-lint
109-
110118
$(KUSTOMIZE): # Build kustomize from tools folder.
111119
$(ROOT)/hack/ensure-kustomize.sh
112120

113121
$(SETUP_ENVTEST): $(TOOLS_DIR)/go.mod # Build setup-envtest from tools folder.
114122
cd $(TOOLS_DIR); go build -tags=tools -o $(BIN_DIR)/setup-envtest sigs.k8s.io/controller-runtime/tools/setup-envtest
115123

124+
$(GOTESTSUM): $(TOOLS_DIR)/go.mod # Build gotestsum from tools folder.
125+
cd $(TOOLS_DIR); go build -tags=tools -o $(BIN_DIR)/gotestsum gotest.tools/gotestsum
126+
127+
$(GOLANGCI_LINT): .github/workflows/golangci-lint.yml # Download golanci-lint using hack script into tools folder.
128+
hack/ensure-golangci-lint.sh \
129+
-b $(TOOLS_DIR)/$(BIN_DIR) \
130+
$(shell cat .github/workflows/golangci-lint.yml | grep version | sed 's/.*version: //')
131+
132+
$(GO_APIDIFF): $(TOOLS_DIR)/go.mod # Build go-apidiff from tools folder.
133+
cd $(TOOLS_DIR) && go build -tags=tools -o $(GO_APIDIFF_BIN) github.com/joelanford/go-apidiff
134+
116135
kustomize: $(KUSTOMIZE) ## Build a local copy of kustomize.
117136

118137
## --------------------------------------
119-
## Linting
138+
## Lint / Verify
120139
## --------------------------------------
121140

122141
.PHONY: lint lint-full
123-
lint: $(GOLANGCI_LINT) ## Lint codebase
124-
$(GOLANGCI_LINT) run -v
142+
lint: $(GOLANGCI_LINT) ## Lint the codebase
143+
$(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
144+
cd $(TOOLS_DIR); $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
145+
146+
.PHONY: lint-fix
147+
lint-fix: $(GOLANGCI_LINT) ## Lint the codebase and run auto-fixers if supported by the linter
148+
GOLANGCI_LINT_EXTRA_ARGS=--fix $(MAKE) lint
149+
150+
.PHONY: apidiff
151+
apidiff: $(GO_APIDIFF) ## Check for API differences
152+
$(GO_APIDIFF) $(shell git rev-parse origin/main) --print-compatible
153+
154+
.PHONY: verify
155+
verify:
156+
$(MAKE) verify-modules
157+
$(MAKE) verify-gen
125158

126-
lint-full: $(GOLANGCI_LINT) ## Run slower linters to detect possible issues
127-
$(GOLANGCI_LINT) run -v --fast=false
159+
.PHONY: verify-modules
160+
verify-modules: modules
161+
@if !(git diff --quiet HEAD -- go.sum go.mod $(TOOLS_DIR)/go.mod $(TOOLS_DIR)/go.sum); then \
162+
git diff; \
163+
echo "go module files are out of date"; exit 1; \
164+
fi
165+
166+
.PHONY: verify-gen
167+
verify-gen: generate
168+
@if !(git diff --quiet HEAD); then \
169+
git diff; \
170+
echo "generated files are out of date, run make generate"; exit 1; \
171+
fi
128172

129173
## --------------------------------------
130174
## Generate / Manifests
@@ -170,7 +214,7 @@ docker-pull-prerequisites:
170214

171215
.PHONY: docker-build
172216
docker-build: ## Build the docker image for management cluster operator
173-
DOCKER_BUILDKIT=1 docker build --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg ldflags="$(LDFLAGS)" . -f $(ROOT)Dockerfile -t $(CONTROLLER_IMG)-$(ARCH):$(TAG)
217+
DOCKER_BUILDKIT=1 docker build --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg ldflags="$(LDFLAGS)" . -t $(CONTROLLER_IMG)-$(ARCH):$(TAG)
174218
$(MAKE) set-manifest-image MANIFEST_IMG=$(CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./config/default/manager_image_patch.yaml"
175219
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="./config/default/manager_pull_policy.yaml"
176220

@@ -281,21 +325,3 @@ clean-bin: ## Remove all generated binaries
281325
clean-release: ## Remove the release folder
282326
rm -rf $(RELEASE_DIR)
283327

284-
.PHONY: verify
285-
verify:
286-
$(MAKE) verify-modules
287-
$(MAKE) verify-gen
288-
289-
.PHONY: verify-modules
290-
verify-modules: modules
291-
@if !(git diff --quiet HEAD -- go.sum go.mod $(TOOLS_DIR)/go.mod $(TOOLS_DIR)/go.sum); then \
292-
git diff; \
293-
echo "go module files are out of date"; exit 1; \
294-
fi
295-
296-
.PHONY: verify-gen
297-
verify-gen: generate
298-
@if !(git diff --quiet HEAD); then \
299-
git diff; \
300-
echo "generated files are out of date, run make generate"; exit 1; \
301-
fi

config/default/manager_image_patch.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ spec:
77
template:
88
spec:
99
containers:
10-
- image: gcr.io/k8s-staging-cluster-api/operator-controller-amd64:dev
10+
- image: gcr.io/openshift-gce-devel/cluster-api-operator-amd64:dev
1111
name: manager

config/default/manager_pull_policy.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ spec:
88
spec:
99
containers:
1010
- name: manager
11-
imagePullPolicy: Always
11+
imagePullPolicy:

hack/ensure-go.sh

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2022 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
# Ensure the go tool exists and is a viable version.
22+
verify_go_version() {
23+
if [[ -z "$(command -v go)" ]]; then
24+
cat <<EOF
25+
Can't find 'go' in PATH, please fix and retry.
26+
See http://golang.org/doc/install for installation instructions.
27+
EOF
28+
return 2
29+
fi
30+
31+
local go_version
32+
IFS=" " read -ra go_version <<< "$(go version)"
33+
local minimum_go_version
34+
minimum_go_version=go1.16.0
35+
if [[ "${minimum_go_version}" != $(echo -e "${minimum_go_version}\n${go_version[2]}" | sort -s -t. -k 1,1 -k 2,2n -k 3,3n | head -n1) && "${go_version[2]}" != "devel" ]]; then
36+
cat <<EOF
37+
Detected go version: ${go_version[*]}.
38+
Kubernetes requires ${minimum_go_version} or greater.
39+
Please install ${minimum_go_version} or later.
40+
EOF
41+
return 2
42+
fi
43+
}
44+
45+
verify_go_version
46+
47+
# Explicitly opt into go modules, even though we're inside a GOPATH directory
48+
export GO111MODULE=on

0 commit comments

Comments
 (0)