Skip to content

Commit 085e888

Browse files
committed
init vpn gw
1 parent c289152 commit 085e888

Some content is hidden

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

49 files changed

+4393
-1
lines changed

Dockerfile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Build the manager binary
2+
FROM golang:1.19 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"]

Makefile

+260
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
# VERSION defines the project version for the bundle.
2+
# Update this value when you upgrade the version of your project.
3+
# To re-generate a bundle for another specific version without changing the standard setup, you can:
4+
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
5+
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
6+
VERSION ?= 0.0.1
7+
8+
# CHANNELS define the bundle channels used in the bundle.
9+
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "candidate,fast,stable")
10+
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
11+
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=candidate,fast,stable)
12+
# - use environment variables to overwrite this value (e.g export CHANNELS="candidate,fast,stable")
13+
ifneq ($(origin CHANNELS), undefined)
14+
BUNDLE_CHANNELS := --channels=$(CHANNELS)
15+
endif
16+
17+
# DEFAULT_CHANNEL defines the default channel used in the bundle.
18+
# Add a new line here if you would like to change its default config. (E.g DEFAULT_CHANNEL = "stable")
19+
# To re-generate a bundle for any other default channel without changing the default setup, you can:
20+
# - use the DEFAULT_CHANNEL as arg of the bundle target (e.g make bundle DEFAULT_CHANNEL=stable)
21+
# - use environment variables to overwrite this value (e.g export DEFAULT_CHANNEL="stable")
22+
ifneq ($(origin DEFAULT_CHANNEL), undefined)
23+
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
24+
endif
25+
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
26+
27+
# IMAGE_TAG_BASE defines the docker.io namespace and part of the image name for remote images.
28+
# This variable is used to construct full image tags for bundle and catalog images.
29+
#
30+
# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
31+
# kube-ovn-operator.com/vpn-gw-bundle:$VERSION and kube-ovn-operator.com/vpn-gw-catalog:$VERSION.
32+
IMAGE_TAG_BASE ?= kube-ovn-operator.com/vpn-gw
33+
34+
# BUNDLE_IMG defines the image:tag used for the bundle.
35+
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
36+
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
37+
38+
# BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command
39+
BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
40+
41+
# USE_IMAGE_DIGESTS defines if images are resolved via tags or digests
42+
# You can enable this value if you would like to use SHA Based Digests
43+
# To enable set flag to true
44+
USE_IMAGE_DIGESTS ?= false
45+
ifeq ($(USE_IMAGE_DIGESTS), true)
46+
BUNDLE_GEN_FLAGS += --use-image-digests
47+
endif
48+
49+
# Image URL to use all building/pushing image targets
50+
IMG ?= controller:latest
51+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
52+
ENVTEST_K8S_VERSION = 1.26.0
53+
54+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
55+
ifeq (,$(shell go env GOBIN))
56+
GOBIN=$(shell go env GOPATH)/bin
57+
else
58+
GOBIN=$(shell go env GOBIN)
59+
endif
60+
61+
# Setting SHELL to bash allows bash commands to be executed by recipes.
62+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
63+
SHELL = /usr/bin/env bash -o pipefail
64+
.SHELLFLAGS = -ec
65+
66+
.PHONY: all
67+
all: build
68+
69+
##@ General
70+
71+
# The help target prints out all targets with their descriptions organized
72+
# beneath their categories. The categories are represented by '##@' and the
73+
# target descriptions by '##'. The awk commands is responsible for reading the
74+
# entire set of makefiles included in this invocation, looking for lines of the
75+
# file as xyz: ## something, and then pretty-format the target and help. Then,
76+
# if there's a line with ##@ something, that gets pretty-printed as a category.
77+
# More info on the usage of ANSI control characters for terminal formatting:
78+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
79+
# More info on the awk command:
80+
# http://linuxcommand.org/lc3_adv_awk.php
81+
82+
.PHONY: help
83+
help: ## Display this help.
84+
@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)
85+
86+
##@ Development
87+
88+
.PHONY: manifests
89+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
90+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
91+
92+
.PHONY: generate
93+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
94+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
95+
96+
.PHONY: fmt
97+
fmt: ## Run go fmt against code.
98+
go fmt ./...
99+
100+
.PHONY: vet
101+
vet: ## Run go vet against code.
102+
go vet ./...
103+
104+
.PHONY: test
105+
test: manifests generate fmt vet envtest ## Run tests.
106+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
107+
108+
##@ Build
109+
110+
.PHONY: build
111+
build: manifests generate fmt vet ## Build manager binary.
112+
go build -o bin/manager cmd/main.go
113+
114+
.PHONY: run
115+
run: manifests generate fmt vet ## Run a controller from your host.
116+
go run ./cmd/main.go
117+
118+
# If you wish built the manager image targeting other platforms you can use the --platform flag.
119+
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
120+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
121+
.PHONY: docker-build
122+
docker-build: test ## Build docker image with the manager.
123+
docker build -t ${IMG} .
124+
125+
.PHONY: docker-push
126+
docker-push: ## Push docker image with the manager.
127+
docker push ${IMG}
128+
129+
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
130+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
131+
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
132+
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
133+
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
134+
# To properly provided solutions that supports more than one platform you should use this option.
135+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
136+
.PHONY: docker-buildx
137+
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
138+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
139+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
140+
- docker buildx create --name project-v3-builder
141+
docker buildx use project-v3-builder
142+
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
143+
- docker buildx rm project-v3-builder
144+
rm Dockerfile.cross
145+
146+
##@ Deployment
147+
148+
ifndef ignore-not-found
149+
ignore-not-found = false
150+
endif
151+
152+
.PHONY: install
153+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
154+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
155+
156+
.PHONY: uninstall
157+
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.
158+
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
159+
160+
.PHONY: deploy
161+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
162+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
163+
$(KUSTOMIZE) build config/default | kubectl apply -f -
164+
165+
.PHONY: undeploy
166+
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.
167+
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
168+
169+
##@ Build Dependencies
170+
171+
## Location to install dependencies to
172+
LOCALBIN ?= $(shell pwd)/bin
173+
$(LOCALBIN):
174+
mkdir -p $(LOCALBIN)
175+
176+
## Tool Binaries
177+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
178+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
179+
ENVTEST ?= $(LOCALBIN)/setup-envtest
180+
181+
## Tool Versions
182+
KUSTOMIZE_VERSION ?= v4.5.7
183+
CONTROLLER_TOOLS_VERSION ?= v0.11.1
184+
185+
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
186+
.PHONY: kustomize
187+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
188+
$(KUSTOMIZE): $(LOCALBIN)
189+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
190+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
191+
rm -rf $(LOCALBIN)/kustomize; \
192+
fi
193+
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
194+
195+
.PHONY: controller-gen
196+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
197+
$(CONTROLLER_GEN): $(LOCALBIN)
198+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
199+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
200+
201+
.PHONY: envtest
202+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
203+
$(ENVTEST): $(LOCALBIN)
204+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
205+
206+
.PHONY: bundle
207+
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
208+
operator-sdk generate kustomize manifests -q
209+
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
210+
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle $(BUNDLE_GEN_FLAGS)
211+
operator-sdk bundle validate ./bundle
212+
213+
.PHONY: bundle-build
214+
bundle-build: ## Build the bundle image.
215+
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
216+
217+
.PHONY: bundle-push
218+
bundle-push: ## Push the bundle image.
219+
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
220+
221+
.PHONY: opm
222+
OPM = ./bin/opm
223+
opm: ## Download opm locally if necessary.
224+
ifeq (,$(wildcard $(OPM)))
225+
ifeq (,$(shell which opm 2>/dev/null))
226+
@{ \
227+
set -e ;\
228+
mkdir -p $(dir $(OPM)) ;\
229+
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
230+
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.23.0/$${OS}-$${ARCH}-opm ;\
231+
chmod +x $(OPM) ;\
232+
}
233+
else
234+
OPM = $(shell which opm)
235+
endif
236+
endif
237+
238+
# A comma-separated list of bundle images (e.g. make catalog-build BUNDLE_IMGS=example.com/operator-bundle:v0.1.0,example.com/operator-bundle:v0.2.0).
239+
# These images MUST exist in a registry and be pull-able.
240+
BUNDLE_IMGS ?= $(BUNDLE_IMG)
241+
242+
# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
243+
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION)
244+
245+
# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
246+
ifneq ($(origin CATALOG_BASE_IMG), undefined)
247+
FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG)
248+
endif
249+
250+
# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'.
251+
# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
252+
# https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator
253+
.PHONY: catalog-build
254+
catalog-build: opm ## Build a catalog image.
255+
$(OPM) index add --container-tool docker --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
256+
257+
# Push the catalog image.
258+
.PHONY: catalog-push
259+
catalog-push: ## Push a catalog image.
260+
$(MAKE) docker-push IMG=$(CATALOG_IMG)

PROJECT

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Code generated by tool. DO NOT EDIT.
2+
# This file is used to track the info used to scaffold your project
3+
# and allow the plugins properly work.
4+
# More info: https://book.kubebuilder.io/reference/project-config.html
5+
domain: kube-ovn-operator.com
6+
layout:
7+
- go.kubebuilder.io/v4-alpha
8+
plugins:
9+
manifests.sdk.operatorframework.io/v2: {}
10+
scorecard.sdk.operatorframework.io/v2: {}
11+
projectName: vpn-gw
12+
repo: github.com/bobz965/kube-ovn-operator
13+
resources:
14+
- api:
15+
crdVersion: v1
16+
namespaced: true
17+
controller: true
18+
domain: kube-ovn-operator.com
19+
group: vpn-gw
20+
kind: VpnGw
21+
path: github.com/bobz965/kube-ovn-operator/api/v1
22+
version: v1
23+
version: "3"

README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
1-
# kube-ovn-operator
1+
# kube-ovn-operator
2+
3+
## 1.init
4+
5+
``` bash
6+
7+
operator-sdk init --domain kube-ovn-operator.com --repo github.com/bobz965/kube-ovn-operator --plugins=go/v4-alpha
8+
9+
# we'll use a domain of kube-ovn-operator.com
10+
# so all API groups will be <group>.kube-ovn-operator.com
11+
12+
# --plugins=go/v4-alpha mac arm 芯片需要指定
13+
14+
# 该步骤后可创建 api
15+
# operator-sdk create api
16+
operator-sdk create api --group vpn-gw --version v1 --kind VpnGw --resource --controller
17+
18+
# make generate 生成controller 相关的 informer clientset 等代码
19+
20+
## 下一步就是编写crd
21+
## 重新生成代码
22+
## 编写 reconcile 逻辑
23+
24+
### 最后就是生成部署文件
25+
make manifests
26+
27+
```

api/v1/groupversion_info.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package v1 contains API Schema definitions for the vpn-gw v1 API group
18+
// +kubebuilder:object:generate=true
19+
// +groupName=vpn-gw.kube-ovn-operator.com
20+
package v1
21+
22+
import (
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"sigs.k8s.io/controller-runtime/pkg/scheme"
25+
)
26+
27+
var (
28+
// GroupVersion is group version used to register these objects
29+
GroupVersion = schema.GroupVersion{Group: "vpn-gw.kube-ovn-operator.com", Version: "v1"}
30+
31+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
32+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
33+
34+
// AddToScheme adds the types in this group-version to the given scheme.
35+
AddToScheme = SchemeBuilder.AddToScheme
36+
)

0 commit comments

Comments
 (0)