Skip to content

Commit 70c17c1

Browse files
author
yumaojun03
committed
补充controller
1 parent c5029ed commit 70c17c1

Some content is hidden

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

63 files changed

+1525
-2125
lines changed

.devcontainer/devcontainer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Kubebuilder DevContainer",
3+
"image": "golang:1.24",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
6+
"ghcr.io/devcontainers/features/git:1": {}
7+
},
8+
9+
"runArgs": ["--network=host"],
10+
11+
"customizations": {
12+
"vscode": {
13+
"settings": {
14+
"terminal.integrated.shell.linux": "/bin/bash"
15+
},
16+
"extensions": [
17+
"ms-kubernetes-tools.vscode-kubernetes-tools",
18+
"ms-azuretools.vscode-docker"
19+
]
20+
}
21+
},
22+
23+
"onCreateCommand": "bash .devcontainer/post-install.sh"
24+
}
25+

.devcontainer/post-install.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -x
3+
4+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
5+
chmod +x ./kind
6+
mv ./kind /usr/local/bin/kind
7+
8+
curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/linux/amd64
9+
chmod +x kubebuilder
10+
mv kubebuilder /usr/local/bin/
11+
12+
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
13+
curl -LO "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl"
14+
chmod +x kubectl
15+
mv kubectl /usr/local/bin/kubectl
16+
17+
docker network create -d=bridge --subnet=172.19.0.0/24 kind
18+
19+
kind version
20+
kubebuilder version
21+
docker --version
22+
go version
23+
kubectl version --client

.github/workflows/lint.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
lint:
9+
name: Run on Ubuntu
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Clone the code
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version-file: go.mod
19+
20+
- name: Run linter
21+
uses: golangci/golangci-lint-action@v8
22+
with:
23+
version: v2.3.0

.github/workflows/test-e2e.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: E2E Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test-e2e:
9+
name: Run on Ubuntu
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Clone the code
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version-file: go.mod
19+
20+
- name: Install the latest version of kind
21+
run: |
22+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
23+
chmod +x ./kind
24+
sudo mv ./kind /usr/local/bin/kind
25+
26+
- name: Verify kind installation
27+
run: kind version
28+
29+
- name: Running Test e2e
30+
run: |
31+
go mod tidy
32+
make test-e2e

.github/workflows/test.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
name: Run on Ubuntu
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Clone the code
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version-file: go.mod
19+
20+
- name: Running Tests
21+
run: |
22+
go mod tidy
23+
make test

.gitignore

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,3 @@ go.work
2525
*.swp
2626
*.swo
2727
*~
28-
.netrc
29-
30-
# 自定义需要忽略的文件
31-
config.env
32-
unit_test.env
33-
kube_config.yml
34-
mpaas

.golangci.yml

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
1+
version: "2"
12
run:
2-
timeout: 5m
33
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
194
linters:
20-
disable-all: true
5+
default: none
216
enable:
7+
- copyloopvar
228
- dupl
239
- errcheck
24-
- exportloopref
2510
- ginkgolinter
2611
- goconst
2712
- gocyclo
28-
- gofmt
29-
- goimports
30-
- gosimple
3113
- govet
3214
- ineffassign
3315
- lll
@@ -36,12 +18,35 @@ linters:
3618
- prealloc
3719
- revive
3820
- staticcheck
39-
- typecheck
4021
- unconvert
4122
- unparam
4223
- unused
43-
44-
linters-settings:
45-
revive:
24+
settings:
25+
revive:
26+
rules:
27+
- name: comment-spacings
28+
- name: import-shadowing
29+
exclusions:
30+
generated: lax
4631
rules:
47-
- name: comment-spacings
32+
- linters:
33+
- lll
34+
path: api/*
35+
- linters:
36+
- dupl
37+
- lll
38+
path: internal/*
39+
paths:
40+
- third_party$
41+
- builtin$
42+
- examples$
43+
formatters:
44+
enable:
45+
- gofmt
46+
- goimports
47+
exclusions:
48+
generated: lax
49+
paths:
50+
- third_party$
51+
- builtin$
52+
- examples$

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.22 AS builder
2+
FROM golang:1.24 AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
55

@@ -14,10 +14,10 @@ RUN go mod download
1414
# Copy the go source
1515
COPY cmd/main.go cmd/main.go
1616
COPY api/ api/
17-
COPY internal/controller/ internal/controller/
17+
COPY internal/ internal/
1818

1919
# Build
20-
# the GOARCH has not a default value to allow the binary be built according to the host where the command
20+
# the GOARCH has no default value to allow the binary to be built according to the host where the command
2121
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2222
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2323
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.

Makefile

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Image URL to use all building/pushing image targets
22
IMG ?= controller:latest
3-
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
4-
ENVTEST_K8S_VERSION = 1.31.0
53

64
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
75
ifeq (,$(shell go env GOBIN))
@@ -60,13 +58,37 @@ vet: ## Run go vet against code.
6058
go vet ./...
6159

6260
.PHONY: test
63-
test: manifests generate fmt vet envtest ## Run tests.
61+
test: manifests generate fmt vet setup-envtest ## Run tests.
6462
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
6563

66-
# Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
67-
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
68-
test-e2e:
69-
go test ./test/e2e/ -v -ginkgo.v
64+
# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
65+
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
66+
# CertManager is installed by default; skip with:
67+
# - CERT_MANAGER_INSTALL_SKIP=true
68+
KIND_CLUSTER ?= moperator-test-e2e
69+
70+
.PHONY: setup-test-e2e
71+
setup-test-e2e: ## Set up a Kind cluster for e2e tests if it does not exist
72+
@command -v $(KIND) >/dev/null 2>&1 || { \
73+
echo "Kind is not installed. Please install Kind manually."; \
74+
exit 1; \
75+
}
76+
@case "$$($(KIND) get clusters)" in \
77+
*"$(KIND_CLUSTER)"*) \
78+
echo "Kind cluster '$(KIND_CLUSTER)' already exists. Skipping creation." ;; \
79+
*) \
80+
echo "Creating Kind cluster '$(KIND_CLUSTER)'..."; \
81+
$(KIND) create cluster --name $(KIND_CLUSTER) ;; \
82+
esac
83+
84+
.PHONY: test-e2e
85+
test-e2e: setup-test-e2e manifests generate fmt vet ## Run the e2e tests. Expected an isolated environment using Kind.
86+
KIND=$(KIND) KIND_CLUSTER=$(KIND_CLUSTER) go test -tags=e2e ./test/e2e/ -v -ginkgo.v
87+
$(MAKE) cleanup-test-e2e
88+
89+
.PHONY: cleanup-test-e2e
90+
cleanup-test-e2e: ## Tear down the Kind cluster used for e2e tests
91+
@$(KIND) delete cluster --name $(KIND_CLUSTER)
7092

7193
.PHONY: lint
7294
lint: golangci-lint ## Run golangci-lint linter
@@ -76,6 +98,10 @@ lint: golangci-lint ## Run golangci-lint linter
7698
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
7799
$(GOLANGCI_LINT) run --fix
78100

101+
.PHONY: lint-config
102+
lint-config: golangci-lint ## Verify golangci-lint linter configuration
103+
$(GOLANGCI_LINT) config verify
104+
79105
##@ Build
80106

81107
.PHONY: build
@@ -84,7 +110,7 @@ build: manifests generate fmt vet ## Build manager binary.
84110

85111
.PHONY: run
86112
run: manifests generate fmt vet ## Run a controller from your host.
87-
source etc/config.env && go run ./cmd/main.go
113+
go run ./cmd/main.go
88114

89115
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
90116
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
@@ -152,16 +178,20 @@ $(LOCALBIN):
152178

153179
## Tool Binaries
154180
KUBECTL ?= kubectl
181+
KIND ?= kind
155182
KUSTOMIZE ?= $(LOCALBIN)/kustomize
156183
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
157184
ENVTEST ?= $(LOCALBIN)/setup-envtest
158185
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
159186

160187
## Tool Versions
161-
KUSTOMIZE_VERSION ?= v5.4.3
162-
CONTROLLER_TOOLS_VERSION ?= v0.16.1
163-
ENVTEST_VERSION ?= release-0.19
164-
GOLANGCI_LINT_VERSION ?= v1.59.1
188+
KUSTOMIZE_VERSION ?= v5.6.0
189+
CONTROLLER_TOOLS_VERSION ?= v0.18.0
190+
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
191+
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
192+
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
193+
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
194+
GOLANGCI_LINT_VERSION ?= v2.3.0
165195

166196
.PHONY: kustomize
167197
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
@@ -173,6 +203,14 @@ controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessar
173203
$(CONTROLLER_GEN): $(LOCALBIN)
174204
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
175205

206+
.PHONY: setup-envtest
207+
setup-envtest: envtest ## Download the binaries required for ENVTEST in the local bin directory.
208+
@echo "Setting up envtest binaries for Kubernetes version $(ENVTEST_K8S_VERSION)..."
209+
@$(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path || { \
210+
echo "Error: Failed to set up envtest binaries for version $(ENVTEST_K8S_VERSION)."; \
211+
exit 1; \
212+
}
213+
176214
.PHONY: envtest
177215
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
178216
$(ENVTEST): $(LOCALBIN)
@@ -181,20 +219,20 @@ $(ENVTEST): $(LOCALBIN)
181219
.PHONY: golangci-lint
182220
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
183221
$(GOLANGCI_LINT): $(LOCALBIN)
184-
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
222+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
185223

186224
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
187225
# $1 - target path with name of binary
188226
# $2 - package url which can be installed
189227
# $3 - specific version of package
190228
define go-install-tool
191-
@[ -f "$(1)-$(3)" ] || { \
229+
@[ -f "$(1)-$(3)" ] && [ "$$(readlink -- "$(1)" 2>/dev/null)" = "$(1)-$(3)" ] || { \
192230
set -e; \
193231
package=$(2)@$(3) ;\
194232
echo "Downloading $${package}" ;\
195-
rm -f $(1) || true ;\
233+
rm -f $(1) ;\
196234
GOBIN=$(LOCALBIN) go install $${package} ;\
197235
mv $(1) $(1)-$(3) ;\
198236
} ;\
199-
ln -sf $(1)-$(3) $(1)
237+
ln -sf $$(realpath $(1)-$(3)) $(1)
200238
endef

0 commit comments

Comments
 (0)