Skip to content

Commit f474467

Browse files
authored
Merge pull request #3616 from camilamacedo86/add-lint-check-sample
🌱 fix and improve samples as add further coverage
2 parents c7c699a + c0d25b9 commit f474467

File tree

184 files changed

+7505
-3129
lines changed

Some content is hidden

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

184 files changed

+7505
-3129
lines changed

.github/workflows/lint-sample.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2023 The Caramello-io Authors.
2+
3+
name: Sample Testdata Tests
4+
5+
on:
6+
push:
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
name: "Testdata - make test"
12+
runs-on: ubuntu-latest
13+
if: (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository)
14+
steps:
15+
- name: Clone the code
16+
uses: actions/checkout@v3
17+
- name: Setup Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: '~1.20'
21+
- name: Remove pre-installed kustomize
22+
run: sudo rm -f /usr/local/bin/kustomize
23+
- name: Run make test for project-v4-with-deploy-image
24+
run: cd testdata/project-v4-with-deploy-image && go mod tidy && make all && make test
25+
- name: Run make test for project-v4-multigroup-with-deploy-image
26+
run: cd testdata/project-v4-multigroup-with-deploy-image && go mod tidy && make all && make test
27+
28+
lint:
29+
name: "Testdata - Lint"
30+
runs-on: ubuntu-latest
31+
# Pull requests from the same repository won't trigger this checks as they were already triggered by the push
32+
if: (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository)
33+
steps:
34+
- name: Setup Go
35+
uses: actions/setup-go@v4
36+
with:
37+
go-version: '~1.20'
38+
- name: Clone the code
39+
uses: actions/checkout@v4
40+
- name: Run linter
41+
uses: golangci/golangci-lint-action@v3
42+
with:
43+
version: v1.54
44+
working-directory: testdata/project-v4-with-deploy-image
45+
args: --config .golangci.yml ./...
46+
skip-cache: true # first lint action will handle
47+
- name: Run linter
48+
uses: golangci/golangci-lint-action@v3
49+
with:
50+
version: v1.54
51+
working-directory: testdata/project-v4-multigroup-with-deploy-image
52+
args: --config .golangci.yml ./...
53+
skip-cache: true # first lint action will handle
54+

.github/workflows/lint.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ jobs:
2222
uses: golangci/golangci-lint-action@v3
2323
with:
2424
version: v1.54
25-
only-new-issues: true # Show only new issues if it's a pull request
2625

2726
yamllint:
2827
runs-on: ubuntu-latest

test/testdata/generate.sh

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,23 @@ function scaffold_test_project {
9898
$kb create api --group foo --version v1 --kind Bar --controller=true --resource=true --make=false
9999
$kb create api --group fiz --version v1 --kind Bar --controller=true --resource=true --make=false
100100

101-
if [ $project == "project-v3-multigroup" ] || [ $project == "project-v4-multigroup" ]; then
101+
if [ $project == "project-v4-multigroup" ] || [ $project == "project-v4-multigroup-with-deploy-image" ] ; then
102102
$kb create api --version v1 --kind Lakers --controller=true --resource=true --make=false
103103
$kb create webhook --version v1 --kind Lakers --defaulting --programmatic-validation
104104
fi
105-
106-
# Call ALL optional plugins within multigroup layout to ensure that they can work within
107-
header_text 'Creating Memcached API with deploy-image plugin ...'
108-
$kb create api --group example.com --version v1alpha1 --kind Memcached --image=memcached:1.4.36-alpine --image-container-command="memcached,-m=64,-o,modern,-v" --image-container-port="11211" --run-as-user="1001" --plugins="deploy-image/v1-alpha" --make=false
109-
$kb create api --group example.com --version v1alpha1 --kind Busybox --image=busybox:1.28 --plugins="deploy-image/v1-alpha" --make=false
110-
header_text 'Creating Memcached webhook ...'
111-
$kb create webhook --group example.com --version v1alpha1 --kind Memcached --programmatic-validation
112-
113-
header_text 'Editing project with Grafana plugin ...'
114-
$kb edit --plugins=grafana.kubebuilder.io/v1-alpha
115-
116105
elif [[ $project =~ deploy-image ]]; then
117106
header_text 'Creating Memcached API with deploy-image plugin ...'
118107
$kb create api --group example.com --version v1alpha1 --kind Memcached --image=memcached:1.4.36-alpine --image-container-command="memcached,-m=64,-o,modern,-v" --image-container-port="11211" --run-as-user="1001" --plugins="deploy-image/v1-alpha" --make=false
119108
$kb create api --group example.com --version v1alpha1 --kind Busybox --image=busybox:1.28 --plugins="deploy-image/v1-alpha" --make=false
120109
header_text 'Creating Memcached webhook ...'
121110
$kb create webhook --group example.com --version v1alpha1 --kind Memcached --programmatic-validation
122-
elif [[ $project =~ "with-grafana" ]]; then
111+
fi
112+
113+
if [[ $project == project-v4-with-grafana ]]; then
123114
header_text 'Editing project with Grafana plugin ...'
124115
$kb edit --plugins=grafana.kubebuilder.io/v1-alpha
125116
fi
126-
117+
127118
make generate manifests
128119
rm -f go.sum
129120
go mod tidy
@@ -141,5 +132,6 @@ scaffold_test_project project-v3 --plugins="go/v3"
141132
# [Currently, default CLI plugin] - [Next version, alpha] Project version v4-alpha
142133
scaffold_test_project project-v4 --plugins="go/v4"
143134
scaffold_test_project project-v4-multigroup --plugins="go/v4"
135+
scaffold_test_project project-v4-multigroup-with-deploy-image --plugins="go/v4"
144136
scaffold_test_project project-v4-with-deploy-image --plugins="go/v4"
145137
scaffold_test_project project-v4-with-grafana --plugins="go/v4"

test/testdata/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ build_kb
3333
# Project version v4-alpha
3434
test_project project-v4
3535
test_project project-v4-multigroup
36+
test_project project-v4-multigroup-with-deploy-image
3637
test_project project-v4-with-deploy-image
3738
test_project project-v4-with-grafana
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore build and test binaries.
3+
bin/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
bin/*
9+
Dockerfile.cross
10+
11+
# Test binary, build with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Kubernetes Generated files - skip generated files, except for vendored files
18+
19+
!vendor/**/zz_generated.*
20+
21+
# editor and IDE paraphernalia
22+
.idea
23+
.vscode
24+
*.swp
25+
*.swo
26+
*~
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
run:
2+
deadline: 5m
3+
allow-parallel-runners: true
4+
5+
issues:
6+
# don't skip warning about doc comments
7+
# don't exclude the default set of lint
8+
exclude-use-default: false
9+
# restore some of the defaults
10+
# (fill in the rest as needed)
11+
exclude-rules:
12+
- path: "api/*"
13+
linters:
14+
- lll
15+
- path: "internal/*"
16+
linters:
17+
- dupl
18+
- lll
19+
linters:
20+
disable-all: true
21+
enable:
22+
- dupl
23+
- errcheck
24+
- exportloopref
25+
- goconst
26+
- gocyclo
27+
- gofmt
28+
- goimports
29+
- gosimple
30+
- govet
31+
- ineffassign
32+
- lll
33+
- misspell
34+
- nakedret
35+
- prealloc
36+
- staticcheck
37+
- typecheck
38+
- unconvert
39+
- unparam
40+
- unused
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Build the manager binary
2+
FROM golang:1.20 as builder
3+
ARG TARGETOS
4+
ARG TARGETARCH
5+
6+
WORKDIR /workspace
7+
# Copy the Go Modules manifests
8+
COPY go.mod go.mod
9+
COPY go.sum go.sum
10+
# cache deps before building and copying source so that we don't need to re-download as much
11+
# and so that source changes don't invalidate our downloaded layer
12+
RUN go mod download
13+
14+
# Copy the go source
15+
COPY cmd/main.go cmd/main.go
16+
COPY api/ api/
17+
COPY internal/controller/ internal/controller/
18+
19+
# Build
20+
# the GOARCH has not a default value to allow the binary be built according to the host where the command
21+
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
22+
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
23+
# 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 cmd/main.go
25+
26+
# Use distroless as minimal base image to package the manager binary
27+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
28+
FROM gcr.io/distroless/static:nonroot
29+
WORKDIR /
30+
COPY --from=builder /workspace/manager .
31+
USER 65532:65532
32+
33+
ENTRYPOINT ["/manager"]
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
2+
# Image URL to use all building/pushing image targets
3+
IMG ?= controller:latest
4+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5+
ENVTEST_K8S_VERSION = 1.28.0
6+
7+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
8+
ifeq (,$(shell go env GOBIN))
9+
GOBIN=$(shell go env GOPATH)/bin
10+
else
11+
GOBIN=$(shell go env GOBIN)
12+
endif
13+
14+
# CONTAINER_TOOL defines the container tool to be used for building images.
15+
# Be aware that the target commands are only tested with Docker which is
16+
# scaffolded by default. However, you might want to replace it to use other
17+
# tools. (i.e. podman)
18+
CONTAINER_TOOL ?= docker
19+
20+
# Setting SHELL to bash allows bash commands to be executed by recipes.
21+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
22+
SHELL = /usr/bin/env bash -o pipefail
23+
.SHELLFLAGS = -ec
24+
25+
.PHONY: all
26+
all: build
27+
28+
##@ General
29+
30+
# The help target prints out all targets with their descriptions organized
31+
# beneath their categories. The categories are represented by '##@' and the
32+
# target descriptions by '##'. The awk command is responsible for reading the
33+
# entire set of makefiles included in this invocation, looking for lines of the
34+
# file as xyz: ## something, and then pretty-format the target and help. Then,
35+
# if there's a line with ##@ something, that gets pretty-printed as a category.
36+
# More info on the usage of ANSI control characters for terminal formatting:
37+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
38+
# More info on the awk command:
39+
# http://linuxcommand.org/lc3_adv_awk.php
40+
41+
.PHONY: help
42+
help: ## Display this help.
43+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
44+
45+
##@ Development
46+
47+
.PHONY: manifests
48+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
49+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
50+
51+
.PHONY: generate
52+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
53+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
54+
55+
.PHONY: fmt
56+
fmt: ## Run go fmt against code.
57+
go fmt ./...
58+
59+
.PHONY: vet
60+
vet: ## Run go vet against code.
61+
go vet ./...
62+
63+
.PHONY: test
64+
test: manifests generate fmt vet envtest ## Run tests.
65+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
66+
67+
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
68+
golangci-lint:
69+
@[ -f $(GOLANGCI_LINT) ] || { \
70+
set -e ;\
71+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.54.2 ;\
72+
}
73+
74+
.PHONY: lint
75+
lint: golangci-lint ## Run golangci-lint linter & yamllint
76+
$(GOLANGCI_LINT) run
77+
78+
.PHONY: lint-fix
79+
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
80+
$(GOLANGCI_LINT) run --fix
81+
82+
##@ Build
83+
84+
.PHONY: build
85+
build: manifests generate fmt vet ## Build manager binary.
86+
go build -o bin/manager cmd/main.go
87+
88+
.PHONY: run
89+
run: manifests generate fmt vet ## Run a controller from your host.
90+
go run ./cmd/main.go
91+
92+
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
93+
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
94+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
95+
.PHONY: docker-build
96+
docker-build: ## Build docker image with the manager.
97+
$(CONTAINER_TOOL) build -t ${IMG} .
98+
99+
.PHONY: docker-push
100+
docker-push: ## Push docker image with the manager.
101+
$(CONTAINER_TOOL) push ${IMG}
102+
103+
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
104+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
105+
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
106+
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
107+
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
108+
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
109+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
110+
.PHONY: docker-buildx
111+
docker-buildx: ## Build and push docker image for the manager for cross-platform support
112+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
113+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
114+
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
115+
$(CONTAINER_TOOL) buildx use project-v3-builder
116+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
117+
- $(CONTAINER_TOOL) buildx rm project-v3-builder
118+
rm Dockerfile.cross
119+
120+
##@ Deployment
121+
122+
ifndef ignore-not-found
123+
ignore-not-found = false
124+
endif
125+
126+
.PHONY: install
127+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
128+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
129+
130+
.PHONY: uninstall
131+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
132+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
133+
134+
.PHONY: deploy
135+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
136+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
137+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
138+
139+
.PHONY: undeploy
140+
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
141+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
142+
143+
##@ Build Dependencies
144+
145+
## Location to install dependencies to
146+
LOCALBIN ?= $(shell pwd)/bin
147+
$(LOCALBIN):
148+
mkdir -p $(LOCALBIN)
149+
150+
## Tool Binaries
151+
KUBECTL ?= kubectl
152+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
153+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
154+
ENVTEST ?= $(LOCALBIN)/setup-envtest
155+
156+
## Tool Versions
157+
KUSTOMIZE_VERSION ?= v5.1.1
158+
CONTROLLER_TOOLS_VERSION ?= v0.13.0
159+
160+
.PHONY: kustomize
161+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
162+
$(KUSTOMIZE): $(LOCALBIN)
163+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
164+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
165+
rm -rf $(LOCALBIN)/kustomize; \
166+
fi
167+
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
168+
169+
.PHONY: controller-gen
170+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
171+
$(CONTROLLER_GEN): $(LOCALBIN)
172+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
173+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
174+
175+
.PHONY: envtest
176+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
177+
$(ENVTEST): $(LOCALBIN)
178+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

0 commit comments

Comments
 (0)