Skip to content

Commit 5c2c20a

Browse files
committed
implement for Definition of cloud robot CRD and Operator
Signed-off-by: zsj <[email protected]>
1 parent f4d50ea commit 5c2c20a

File tree

58 files changed

+3979
-1
lines changed

Some content is hidden

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

58 files changed

+3979
-1
lines changed
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: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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.27.1
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 commands 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:allowDangerousTypes=true 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+
##@ Build
68+
69+
.PHONY: build
70+
build: manifests generate fmt vet ## Build manager binary.
71+
go build -o bin/manager cmd/main.go
72+
73+
.PHONY: run
74+
run: manifests generate fmt vet ## Run a controller from your host.
75+
go run ./cmd/main.go
76+
77+
# If you wish built the manager image targeting other platforms you can use the --platform flag.
78+
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
79+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
80+
.PHONY: docker-build
81+
docker-build: test ## Build docker image with the manager.
82+
$(CONTAINER_TOOL) build -t ${IMG} .
83+
84+
.PHONY: docker-push
85+
docker-push: ## Push docker image with the manager.
86+
$(CONTAINER_TOOL) push ${IMG}
87+
88+
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
89+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
90+
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
91+
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
92+
# - 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)
93+
# To properly provided solutions that supports more than one platform you should use this option.
94+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
95+
.PHONY: docker-buildx
96+
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
97+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
98+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
99+
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
100+
$(CONTAINER_TOOL) buildx use project-v3-builder
101+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
102+
- $(CONTAINER_TOOL) buildx rm project-v3-builder
103+
rm Dockerfile.cross
104+
105+
##@ Deployment
106+
107+
ifndef ignore-not-found
108+
ignore-not-found = false
109+
endif
110+
111+
.PHONY: install
112+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
113+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
114+
115+
.PHONY: uninstall
116+
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.
117+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
118+
119+
.PHONY: deploy
120+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
121+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
122+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
123+
124+
.PHONY: undeploy
125+
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.
126+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
127+
128+
##@ Build Dependencies
129+
130+
## Location to install dependencies to
131+
LOCALBIN ?= $(shell pwd)/bin
132+
$(LOCALBIN):
133+
mkdir -p $(LOCALBIN)
134+
135+
## Tool Binaries
136+
KUBECTL ?= kubectl
137+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
138+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
139+
ENVTEST ?= $(LOCALBIN)/setup-envtest
140+
141+
## Tool Versions
142+
KUSTOMIZE_VERSION ?= v5.0.1
143+
CONTROLLER_TOOLS_VERSION ?= v0.12.0
144+
145+
.PHONY: kustomize
146+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
147+
$(KUSTOMIZE): $(LOCALBIN)
148+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
149+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
150+
rm -rf $(LOCALBIN)/kustomize; \
151+
fi
152+
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
153+
154+
.PHONY: controller-gen
155+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
156+
$(CONTROLLER_GEN): $(LOCALBIN)
157+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
158+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
159+
160+
.PHONY: envtest
161+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
162+
$(ENVTEST): $(LOCALBIN)
163+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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: cloudrobot.kubeedge
6+
layout:
7+
- go.kubebuilder.io/v4
8+
projectName: ospp2023
9+
repo: github.com/ospp2023
10+
resources:
11+
- api:
12+
crdVersion: v1
13+
namespaced: true
14+
controller: true
15+
domain: cloudrobot.kubeedge
16+
group: cloudrobot.kubeedge
17+
kind: Robot
18+
path: github.com/ospp2023/api/v1beta1
19+
version: v1beta1
20+
- api:
21+
crdVersion: v1
22+
namespaced: true
23+
controller: true
24+
domain: cloudrobot.kubeedge
25+
group: cloudrobot.kubeedge
26+
kind: RobotSync
27+
path: github.com/ospp2023/api/v1beta1
28+
version: v1beta1
29+
- api:
30+
crdVersion: v1
31+
namespaced: true
32+
controller: true
33+
domain: cloudrobot.kubeedge
34+
group: cloudrobot.kubeedge
35+
kind: Task
36+
path: github.com/ospp2023/api/v1beta1
37+
version: v1beta1
38+
version: "3"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# ospp2023
2+
3+
项目简述:通过定义一套通用的云原生机器人的CRD和Operator是区别于IOT和机器人的重要设计,提高机器人的可扩展性、灵活性、可用性等。
4+
5+
定义CRD可以实现以下功能:
6+
7+
* 定义机器人资源:可以定义自定义资源来表示机器人的各种属性和状态,如机器人的位置、传感器数据、任务状态等,从而方便进行机器人的调度和管理。
8+
9+
* 扩展机器人API:可以通过定义CRD来扩展机器人API,使API更加符合实际需求,从而提高机器人的可扩展性和灵活性。
10+
11+
* 实现机器人控制:可以通过定义CRD来实现机器人的控制,如控制机器人的移动、执行任务等操作。
12+
13+
定义Operator可以实现以下功能:
14+
15+
* 自动化机器人操作:可以定义Operator来实现自动化机器人操作,如自动控制机器人移动、执行任务等操作。
16+
17+
* 监控机器人状态:可以通过定义Operator来监控机器人的状态,如机器人的位置、传感器数据、任务状态等,从而方便进行机器人的调度和管理。
18+
19+
* 实现机器人故障处理:可以通过定义Operator来实现机器人故障处理,如自动重启机器人、重新执行任务等操作,从而提高机器人的可用性和稳定性。
20+
21+
* 实现机器人自适应控制:可以通过定义Operator来实现机器人的自适应控制,如根据机器人的传感器数据自动调整机器人的移动方向、速度等参数,从而提高机器人的智能化和自适应性。
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2022 The KubeEdge Authors.
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 v1beta1 contains API Schema definitions for the cloudrobot.kubeedge v1beta1 API group
18+
// +kubebuilder:object:generate=true
19+
// +groupName=cloudrobot.kubeedge.cloudrobot.kubeedge
20+
package v1beta1
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: "cloudrobot.kubeedge.cloudrobot.kubeedge", Version: "v1beta1"}
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)