diff --git a/1.txt b/1.txt deleted file mode 100644 index d00491f..0000000 --- a/1.txt +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..29a33db --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +# Build the manager binary +FROM golang:1.18.7-alpine3.15 as builder + +WORKDIR /app +# Copy the Go Modules manifests +COPY go.mod go.mod +COPY go.sum go.sum +# 坑: +# 报错 go mod download: google.golang.org/api@v0.44.0: read tcp 172.17.0.3:60862->14.204.51.154:443: read: connection reset by peer +# The command '/bin/sh -c go mod download' returned a non-zero code: 1 +# make: *** [docker-build] 错误 1 +ENV GOPROXY=https://goproxy.cn,direct +ENV GO111MODULE=on +# cache deps before building and copying source so that we don't need to re-download as much +# and so that source changes don't invalidate our downloaded layer +RUN go mod download + +# Copy the go source +# # 需要把该放入的都copy进去,如果报出 package xxxxx is not in GOROOT => 就是这个问题。 +COPY main.go main.go +COPY pkg/ pkg/ +# Build +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o taskflowoperator main.go + + +FROM alpine:3.12 +WORKDIR /app +# 需要的文件需要复制过来 +COPY --from=builder /app/taskflowoperator . +USER 65532:65532 + +ENTRYPOINT ["./taskflowoperator"] \ No newline at end of file diff --git a/README.md b/README.md index 911e87e..58a4171 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,136 @@ -## task-operator 简易型工作流控制器 +## taskflow-operator 简易型任务流控制器 ### 项目思路与设计 -![]() -设计背景:。 +设计背景:k8s 当中原生的 Pod 资源对象执行 container 容器时,并没有相互依赖的编排特性(ex: 容器 a 完成后 -> 再执行容器 b ...)。 +本项目在此需求上,基于 k8s 的扩展功能,实现 Task 的自定义资源控制器,实现一个能顺序执行容器的 operator 应用。 -思路: +```yaml +apiVersion: api.practice.com/v1alpha1 +kind: Task +metadata: + name: example-taskflow +spec: + # 设置多个 step 步骤,会按照填入的 container 顺序执行 + steps: + # 每项都是一个 container 对象 + - name: step1 + image: busybox:1.28 + command: [ "sh","-c" ] + args: [ "echo step1" ] + - name: step2 + image: busybox:1.28 + command: [ "sh","-c" ] + args: [ "echo step2" ] + - name: step3 + image: devopscube/kubernetes-job-demo:latest + args: [ "100" ] +``` + + +### 项目功能 +1. 支持任务中的 container 顺序执行 + +### 项目部署与使用 +1. 打成镜像或是使用编译二进制。 +```bash +# 项目根目录执行 +[root@VM-0-16-centos taskflowoperator]# pwd +/root/taskflowoperator +# 下列命令会得到一个二进制文件,服务启动时需要使用。 +# 可以直接使用 docker 镜像部署 +[root@VM-0-16-centos taskflowoperator]# docker build -t taskflowoperator:v1 . +Sending build context to Docker daemon 194.6kB +Step 1/15 : FROM golang:1.18.7-alpine3.15 as builder + ---> 33c97f935029 +Step 2/15 : WORKDIR /app +... +``` +2. 构建 container agent 镜像 +```bash +[root@VM-0-16-centos taskflowoperator]# chmod +x docker_build.sh +[root@VM-0-16-centos taskflowoperator]# ./docker_build.sh +镜像 docker.io/taskflow/agent:v1.0 不存在,开始构建... +Sending build context to Docker daemon 16.38kB +Step 1/17 : FROM golang:1.18.7-alpine3.15 as builder + ---> 33c97f935029 +Step 2/17 : RUN #mkdir /src + ---> Using cache + ---> abd15a740ce7 +Step 3/17 : WORKDIR /app + ---> Using cache + ---> b25bc39a9a40 +Step 4/17 : COPY go.mod go.mod + ---> 6576edb8ae05 +Step 5/17 : COPY go.sum go.sum + ---> 3e30c14b75a4 +Step 6/17 : ENV GOPROXY=https://goproxy.cn,direct + ---> Running in d98da0a5296e +Removing intermediate container d98da0a5296e + ---> a6263b47b54d +Step 7/17 : ENV GO111MODULE=on + ---> Running in a6da28b7982d +Removing intermediate container a6da28b7982d + ---> 613ebff35ca3 +Step 8/17 : RUN go mod download + ---> Running in 3b8e3931b18b +``` + +3. apply crd 资源 +```bash +[root@VM-0-16-centos taskflowoperator]# +[root@VM-0-16-centos taskflowoperator]# kubectl apply -f deploy/task.yaml +customresourcedefinition.apiextensions.k8s.io/tasks.api.practice.com unchanged +``` +4. 启动 controller 服务(需要先执行 rbac.yaml,否则服务会报错) +```bash +[root@VM-0-16-centos taskflowoperator]# kubectl apply -f deploy/task.yaml +customresourcedefinition.apiextensions.k8s.io/tasks.api.practice.com unchanged +[root@VM-0-16-centos taskflowoperator]# kubectl apply -f deploy/rbac.yaml +serviceaccount/taskflow-sa unchanged +clusterrole.rbac.authorization.k8s.io/taskflow-clusterrole unchanged +clusterrolebinding.rbac.authorization.k8s.io/taskflow-ClusterRoleBinding unchanged +[root@VM-0-16-centos taskflowoperator]# kubectl apply -f deploy/deploy.yaml +deployment.apps/taskflow-controller unchanged +``` +5. 查看 operator 服务 +```bash +[root@VM-0-16-centos deploy]# kubectl logs -f taskflow-controller-846ccc5bbb-w748q +I0101 05:14:59.301232 1 init_config.go:22] run in cluster! +{"level":"info","ts":"2024-01-01T05:14:59Z","logger":"controller-runtime.metrics","msg":"Metrics server is starting to listen","addr":":8080"} +{"level":"info","ts":"2024-01-01T05:14:59Z","logger":"task-flow operator","msg":"Starting server","path":"/metrics","kind":"metrics","addr":"[::]:8080"} +{"level":"info","ts":"2024-01-01T05:14:59Z","logger":"task-flow operator","msg":"Starting EventSource","controller":"task","controllerGroup":"api.practice.com","controllerKind":"Task","source":"kind source: *v1alpha1.Task"} +{"level":"info","ts":"2024-01-01T05:14:59Z","logger":"task-flow operator","msg":"Starting EventSource","controller":"task","controllerGroup":"api.practice.com","controllerKind":"Task","source":"kind source: *v1.Pod"} +{"level":"info","ts":"2024-01-01T05:14:59Z","logger":"task-flow operator","msg":"Starting Controller","controller":"task","controllerGroup":"api.practice.com","controllerKind":"Task"} +{"level":"info","ts":"2024-01-01T05:14:59Z","logger":"task-flow operator","msg":"Starting workers","controller":"task","controllerGroup":"api.practice.com","controllerKind":"Task","worker count":1} + +I0101 05:15:21.106836 1 helper.go:130] step1 use normal mode..... +I0101 05:15:21.106857 1 helper.go:184] [--wait /etc/podinfo/order --waitcontent 1 --out stdout --command sh -c echo step1] +I0101 05:15:21.106875 1 helper.go:130] step2 use normal mode..... +I0101 05:15:21.106882 1 helper.go:184] [--wait /etc/podinfo/order --waitcontent 2 --out stdout --command sh -c echo step2] +I0101 05:15:21.106889 1 helper.go:130] step3 use normal mode..... + +I0101 05:17:03.329371 1 image_helper.go:72] [devopscube/kubernetes-job-demo:latest] image is Image type image +I0101 05:17:07.804184 1 image_helper.go:85] Image Name: [devopscube/kubernetes-job-demo:latest], type: Image, os: [linux], Architecture: [amd64], Entrypoint: [[/script.sh]], Cmd: [[]] +I0101 05:17:07.804220 1 helper.go:184] [--wait /etc/podinfo/order --waitcontent 3 --out stdout --command /script.sh 100] +I0101 05:17:07.812416 1 task_controller.go:49] successful reconcile +I0101 05:17:07.824050 1 helper.go:241] pod status: Pending +I0101 05:17:07.824068 1 helper.go:242] annotation order: 0 +I0101 05:17:07.824076 1 task_controller.go:49] successful reconcile +I0101 05:17:07.847943 1 helper.go:241] pod status: Pending +I0101 05:17:07.847963 1 helper.go:242] annotation order: 0 +I0101 05:17:07.847971 1 task_controller.go:49] successful reconcile +I0101 05:17:09.756468 1 helper.go:241] pod status: Pending +I0101 05:17:09.756487 1 helper.go:242] annotation order: 0 +I0101 05:17:09.756493 1 task_controller.go:49] successful reconcile +I0101 05:17:10.873731 1 task_controller.go:49] successful reconcile +I0101 05:17:10.873931 1 helper.go:241] pod status: Running +I0101 05:17:10.873942 1 helper.go:242] annotation order: 1 +I0101 05:17:10.873949 1 task_controller.go:49] successful reconcile +I0101 05:17:12.141029 1 helper.go:241] pod status: Running +I0101 05:17:12.141048 1 helper.go:242] annotation order: 2 +I0101 05:17:12.141057 1 task_controller.go:49] successful reconcile +I0101 05:17:12.141302 1 helper.go:241] pod status: Running +``` + + +### RoadMap diff --git a/entrypoint/Dockerfile b/container-agent/Dockerfile similarity index 82% rename from entrypoint/Dockerfile rename to container-agent/Dockerfile index 8a4ec69..947cc41 100644 --- a/entrypoint/Dockerfile +++ b/container-agent/Dockerfile @@ -23,8 +23,8 @@ RUN go mod download COPY main.go main.go COPY cmd/ cmd/ -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o entrypoint main.go -RUN chmod +x entrypoint +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o container-agent main.go +RUN chmod +x container-agent FROM alpine:3.12 @@ -34,5 +34,5 @@ WORKDIR /app #COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /app -COPY --from=builder /app/entrypoint . -ENTRYPOINT ["./entrypoint"] +COPY --from=builder /app/container-agent . +ENTRYPOINT ["./container-agent"] diff --git a/container-agent/README.md b/container-agent/README.md new file mode 100644 index 0000000..18f6d97 --- /dev/null +++ b/container-agent/README.md @@ -0,0 +1,4 @@ +## container-agent 通用容器代理 + +### 设计功能: +- 用于代替并执行其他容器的代理入口 diff --git a/entrypoint/cmd/flags.go b/container-agent/cmd/flags.go similarity index 100% rename from entrypoint/cmd/flags.go rename to container-agent/cmd/flags.go diff --git a/entrypoint/cmd/helper.go b/container-agent/cmd/helper.go similarity index 90% rename from entrypoint/cmd/helper.go rename to container-agent/cmd/helper.go index b71513a..5955898 100644 --- a/entrypoint/cmd/helper.go +++ b/container-agent/cmd/helper.go @@ -14,8 +14,8 @@ import ( "time" ) -// CheckFlags 检查参数 -func CheckFlags() { +// ValidateFlags 检查参数 +func ValidateFlags() { if encodeFile == "" { if waitFile == "" || out == "" || command == "" { log.Println("error input param...") @@ -54,7 +54,7 @@ func GenEncodeFile(encodefile string) error { return nil } -// getWaitContent 读取waitfile内容 +// getWaitContent 读取 waitfile 内容 func getWaitContent() string { f, err := os.Open(waitFile) if err != nil { @@ -84,7 +84,7 @@ func CheckWaitFile() { } // 此时程序要退出 if getContent == quitContent { - log.Println("任务被取消") + log.Println("task exit") os.Exit(1) //停止程序 } } @@ -94,7 +94,7 @@ func CheckWaitFile() { continue } else { // 其他未知错误 - log.Fatal(err) + log.Fatal("unknown error: ", err) } } @@ -119,7 +119,7 @@ func ExecCmdAndArgs(args []string) { cmd := "" // 非加密文件模式 if encodeFile == "" { - cmdList := strings.Split(command, " ") // 譬如 sh -c 这种字符串,要切割,取出第一个作为command + cmdList := strings.Split(command, " ") // 如 sh -c 这种字符串,要切割,取出第一个作为command cmd = cmdList[0] //肯定有值 ,所以不用判断 if len(cmdList) > 1 { //把剩余的合并到argList 前面 argList = append(argList, cmdList[1:]...) @@ -152,7 +152,9 @@ func Gzip(str string) string { log.Println(err) return "" } - err = gz.Close() //这里要关掉,否则取不到数据 也可手工flush.但依然要关掉gz + + // 需要关闭,否则获取不到 data 也可手动 flush 最后依然需要关闭 + err = gz.Close() if err != nil { log.Println(err) return "" @@ -178,6 +180,5 @@ func UnGzip(str string) string { log.Println("read gzip error:", err) return "" } - return string(ret) } diff --git a/container-agent/cmd/rootcmd.go b/container-agent/cmd/rootcmd.go new file mode 100644 index 0000000..d34c0d4 --- /dev/null +++ b/container-agent/cmd/rootcmd.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "github.com/spf13/cobra" + "log" +) + +// 测试方式 +// go run cmd/container-agent/main.go --wait ./1.txt --out out.log --command go version +// ./container-agent --out stdout --wait 1.txt --command="sh -c" "echo 123" +// ./container-agent --encodefile /xxx/xxx/1.txt +var rootCmd = &cobra.Command{ + Use: "container-agent", + Short: "container agent", + Run: func(cmd *cobra.Command, args []string) { + + ValidateFlags() // 检查参数合法性 + CheckWaitFile() // 检查等待文件是否存在 + ExecCmdAndArgs(args) + + }, +} + +// InitCmd 初始化 +func InitCmd() { + rootCmd.Flags().StringVar(&waitFile, "wait", "", "container-agent --wait /var/run/1") + // 增加了一个参数。 如果有这个参数,那么还得判断 内容是否匹配 + // 如果没有这个参数,则只判断是否 有 wait 对应的文件 + rootCmd.Flags().StringVar(&waitFileContent, "waitcontent", "", "container-agent --wait /var/run/1 --waitcontent 2 ") + rootCmd.Flags().StringVar(&out, "out", "", "container-agent --out /var/run/out") + rootCmd.Flags().StringVar(&command, "command", "", "container-agent --command bash") + rootCmd.Flags().StringVar(&quitContent, "quit", "-1", "container-agent --quit -2") + rootCmd.Flags().StringVar(&encodeFile, "encodefile", "", "container-agent --encodefile /var/run/abc") + + if err := rootCmd.Execute(); err != nil { + log.Fatalln(err) + } +} diff --git a/entrypoint/go.mod b/container-agent/go.mod similarity index 78% rename from entrypoint/go.mod rename to container-agent/go.mod index be43265..ac330e7 100644 --- a/entrypoint/go.mod +++ b/container-agent/go.mod @@ -1,4 +1,4 @@ -module github.com/myoperator/entrypoint +module github.com/myoperator/container-agent go 1.18 diff --git a/entrypoint/go.sum b/container-agent/go.sum similarity index 100% rename from entrypoint/go.sum rename to container-agent/go.sum diff --git a/container-agent/main.go b/container-agent/main.go new file mode 100644 index 0000000..cc98d3c --- /dev/null +++ b/container-agent/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/myoperator/container-agent/cmd" + +func main() { + cmd.InitCmd() +} diff --git a/deploy/deploy.yaml b/deploy/deploy.yaml new file mode 100644 index 0000000..a8bebac --- /dev/null +++ b/deploy/deploy.yaml @@ -0,0 +1,29 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: taskflow-controller + namespace: default +spec: + selector: + matchLabels: + app: taskflow-controller + replicas: 1 + template: + metadata: + labels: + app: taskflow-controller + spec: + nodeName: vm-0-16-centos # 只调度到这个节点上 + serviceAccountName: taskflow-sa # 配置service account + containers: + - name: taskflow # 控制器镜像 + image: taskflowoperator:v1 + imagePullPolicy: IfNotPresent + env: + - name: "Release" + value: "1" + workingDir: "/app" + command: ["./taskflowoperator"] + ports: + - containerPort: 80 + diff --git a/deploy/rbac.yaml b/deploy/rbac.yaml new file mode 100644 index 0000000..0a35899 --- /dev/null +++ b/deploy/rbac.yaml @@ -0,0 +1,126 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: taskflow-sa + namespace: default +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: taskflow-clusterrole +rules: + - apiGroups: + - "" + resources: + - namespaces + verbs: + - get + - apiGroups: + - "" + resources: + - configmaps + - pods + - secrets + - endpoints + verbs: + - get + - list + - watch + - create + - update + - delete + - patch + - apiGroups: + - "" + resources: + - services + verbs: + - get + - list + - watch + - apiGroups: + - apps + resources: + - deployments + verbs: + - get + - list + - watch + - apiGroups: + - networking.k8s.io + resources: + - ingresses + verbs: + - get + - list + - watch + - apiGroups: + - networking.k8s.io + resources: + - ingresses/status + verbs: + - update + - apiGroups: + - networking.k8s.io + resources: + - ingressclasses + verbs: + - get + - list + - watch + - apiGroups: + - "" + resources: + - configmaps + - secrets + verbs: + - create + - delete + - update + - patch + - apiGroups: + - "" + resources: + - events + verbs: + - create + - patch + - apiGroups: + - api.practice.com + resources: + - tasks + verbs: + - create + - delete + - get + - list + - patch + - update + - watch + - apiGroups: + - api.practice.com + resources: + - tasks/finalizers + verbs: + - update + - apiGroups: + - api.practice.com + resources: + - tasks/status + verbs: + - get + - patch + - update +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: taskflow-ClusterRoleBinding +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: taskflow-clusterrole +subjects: + - kind: ServiceAccount + name: taskflow-sa + namespace: default \ No newline at end of file diff --git a/yaml/task.yaml b/deploy/task.yaml similarity index 100% rename from yaml/task.yaml rename to deploy/task.yaml diff --git a/entrypoint/docker_build.sh b/docker_build.sh similarity index 61% rename from entrypoint/docker_build.sh rename to docker_build.sh index 135d2a4..5162761 100755 --- a/entrypoint/docker_build.sh +++ b/docker_build.sh @@ -1,9 +1,14 @@ #!/bin/bash # 设置变量 -DOCKERFILE_PATH="./entrypoint/Dockerfile" # Dockerfile 的路径 -IMAGE_NAME="docker.io/taskflow/entrypoint" # 镜像名称 -TAG="v1.0" # 镜像标签 +BUILD_CONTEXT="./container-agent" # 构建上下文目录 + +# 切换到构建上下文目录 +cd "${BUILD_CONTEXT}" || exit +DOCKERFILE_PATH="Dockerfile" # Dockerfile 的路径 +IMAGE_NAME="docker.io/taskflow/agent" # 镜像名称 +TAG="v1.0" # 镜像标签 + # 检查镜像是否已经存在 if docker inspect "${IMAGE_NAME}:${TAG}" &> /dev/null; then diff --git a/entrypoint/cmd/rootcmd.go b/entrypoint/cmd/rootcmd.go deleted file mode 100644 index dfee4d8..0000000 --- a/entrypoint/cmd/rootcmd.go +++ /dev/null @@ -1,39 +0,0 @@ -package cmd - -import ( - "github.com/spf13/cobra" - "log" -) - -// 测试方式 -// go run cmd/entrypoint/main.go --wait ./1.txt --out out.log --command go version -//./entrypoint --out stdout --wait 1.txt --command="sh -c" "echo 123" -//./entrypoint --encodefile /home/shenyi/mycicd/ep/1.txt -var rootCmd = &cobra.Command{ - Use: "entrypoint", - Short: "container entrypoint", - Run: func(cmd *cobra.Command, args []string) { - - CheckFlags() //检查参数合法性 - CheckWaitFile() //检查 等待文件是否存在 - // 业务逻辑 - ExecCmdAndArgs(args) - - }, -} - -// InitCmd 初始化 -func InitCmd() { - rootCmd.Flags().StringVar(&waitFile, "wait", "", "entrypoint --wait /var/run/1") - // 增加了一个参数。 如果有这个参数,那么还得判断 内容是否匹配 - // 如果没有这个参数,则只判断是否 有 wait 对应的文件 - rootCmd.Flags().StringVar(&waitFileContent, "waitcontent", "", "entrypoint --wait /var/run/1 --waitcontent 2 ") - rootCmd.Flags().StringVar(&out, "out", "", "entrypoint --out /var/run/out") - rootCmd.Flags().StringVar(&command, "command", "", "entrypoint --command bash") - rootCmd.Flags().StringVar(&quitContent, "quit", "-1", "entrypoint --quit -2") - rootCmd.Flags().StringVar(&encodeFile, "encodefile", "", "entrypoint --encodefile /var/run/abc") - - if err := rootCmd.Execute(); err != nil { - log.Fatalln(err) - } -} diff --git a/entrypoint/entrypoint b/entrypoint/entrypoint deleted file mode 100644 index 2707700..0000000 Binary files a/entrypoint/entrypoint and /dev/null differ diff --git a/entrypoint/main.go b/entrypoint/main.go deleted file mode 100644 index a1e4734..0000000 --- a/entrypoint/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "github.com/myoperator/entrypoint/cmd" - -func main() { - cmd.InitCmd() -} diff --git "a/image/\346\227\240\346\240\207\351\242\230-2023-08-10-2343.png" "b/image/\346\227\240\346\240\207\351\242\230-2023-08-10-2343.png" new file mode 100644 index 0000000..4675f3d Binary files /dev/null and "b/image/\346\227\240\346\240\207\351\242\230-2023-08-10-2343.png" differ diff --git a/out.log b/out.log deleted file mode 100644 index 9a1ee1c..0000000 --- a/out.log +++ /dev/null @@ -1,3 +0,0 @@ -go version go1.18.4 darwin/arm64 -go version go1.18.4 darwin/arm64 -go version go1.18.4 darwin/arm64 diff --git a/pkg/apis/task/v1alpha1/types.go b/pkg/apis/task/v1alpha1/types.go index c720d41..7c12c65 100644 --- a/pkg/apis/task/v1alpha1/types.go +++ b/pkg/apis/task/v1alpha1/types.go @@ -15,6 +15,9 @@ type Task struct { metav1.ObjectMeta `json:"metadata,omitempty"` Spec TaskSpec `json:"spec,omitempty"` + // TODO: 加入 TaskStatus 用于查看每个工作流的状态 Running Complete Failed ... + // TODO: 记录运行时间,需要在 status加入一个时间值字段 开始时记录时间 结束或错误退出时记录时间 + // 在调协中修改 status } type TaskSpec struct { @@ -23,7 +26,6 @@ type TaskSpec struct { type TaskStep struct { corev1.Container `json:",inline"` // 容器对象 - JobStatus bool `json:"job_status",omitempty", default:"false"` // 支持脚本命令 Script string `json:"script,omitempty"` } diff --git a/pkg/controller/helper.go b/pkg/controller/helper.go index bc46bb3..c78bd43 100644 --- a/pkg/controller/helper.go +++ b/pkg/controller/helper.go @@ -17,18 +17,18 @@ import ( // setInitContainer 设置 InitContainer func (r *TaskController) setInitContainer(pod *v1.Pod) { - // entrypoint 容器 + // container-agent 容器 // 用意:把通用入口镜像挂载到 Pod 中,使所有容器都共享 pod.Spec.InitContainers = []v1.Container{ { Name: pod.Name + "init", Image: EntryPointImage, ImagePullPolicy: v1.PullIfNotPresent, - Command: []string{"cp", "/app/entrypoint", "/entrypoint/bin"}, + Command: []string{"cp", "/app/container-agent", "/container-agent/bin"}, VolumeMounts: []v1.VolumeMount{ { - Name: "entrypoint-volume", - MountPath: "/entrypoint/bin", + Name: "container-agent-volume", + MountPath: "/container-agent/bin", }, }, }, @@ -53,7 +53,7 @@ func (r *TaskController) setPodVolumes(pod *v1.Pod) { { Path: "order", FieldRef: &v1.ObjectFieldSelector{ - FieldPath: "metadata.annotations['taskorder']", + FieldPath: "metadata.annotations['taskflowoperaotr/taskOrder']", }, }, }, @@ -71,11 +71,11 @@ func (r *TaskController) setPodVolumes(pod *v1.Pod) { } const ( - EntryPointImage = "docker.io/taskflow/entrypoint:v1.0" - TaskPodPrefix = "task-pod-" - AnnotationTaskOrderKey = "taskorder" - AnnotationTaskOrderInitValue = "0" - AnnotationExitOrder = "-1" // 退出 step 用的标识 + EntryPointImage = "docker.io/taskflow/agent:v1.0" // 内部自定义通用镜像 + TaskPodPrefix = "task-pod-" // taskflow 创建的 Pod 名规范 + AnnotationTaskOrderKey = "taskflowoperaotr/taskOrder" // annotation + AnnotationTaskOrderInitValue = "0" // 初始 step 标识 + AnnotationExitOrder = "-1" // 退出 step 标识 ) // setPodMeta 设置 Pod 信息 @@ -170,11 +170,11 @@ func (r *TaskController) setContainer(index int, step v1alpha1.TaskStep) (v1.Con args := step.Args // 强迫设置拉取策略 step.Container.ImagePullPolicy = v1.PullIfNotPresent - step.Container.Command = []string{"/entrypoint/bin/entrypoint"} + step.Container.Command = []string{"/container-agent/bin/container-agent"} step.Container.Args = []string{ "--wait", "/etc/podinfo/order", "--waitcontent", strconv.Itoa(index + 1), - // entrypoint 中写上 stdout 就会定向到标准输出 + // container-agent 中写上 stdout 就会定向到标准输出 "--out", "stdout", "--command", } @@ -190,7 +190,7 @@ func (r *TaskController) setContainer(index int, step v1alpha1.TaskStep) (v1.Con step.Container.Command = []string{"sh"} // 使用sh命令 // 使用脚本:1.找到文件夹,2.创建文件并修改权限,3.写入文件 4.解密 - step.Container.Args = []string{"-c", fmt.Sprintf(`scriptfile="/execute/scripts/%s";touch ${scriptfile} && chmod +x ${scriptfile};echo "%s" > ${scriptfile};/entrypoint/bin/entrypoint --wait /etc/podinfo/order --waitcontent %d --out stdout --encodefile ${scriptfile};`, + step.Container.Args = []string{"-c", fmt.Sprintf(`scriptfile="/execute/scripts/%s";touch ${scriptfile} && chmod +x ${scriptfile};echo "%s" > ${scriptfile};/container-agent/bin/container-agent --wait /etc/podinfo/order --waitcontent %d --out stdout --encodefile ${scriptfile};`, step.Name, common.EncodeScript(step.Script), index+1)} } @@ -198,7 +198,7 @@ func (r *TaskController) setContainer(index int, step v1alpha1.TaskStep) (v1.Con step.Container.VolumeMounts = []v1.VolumeMount{ { Name: EntryPointVolume, - MountPath: "/entrypoint/bin", + MountPath: "/container-agent/bin", }, { Name: ExecuteScriptsVolume, //设置 script挂载卷,不管有没有设置 @@ -213,9 +213,9 @@ func (r *TaskController) setContainer(index int, step v1alpha1.TaskStep) (v1.Con } const ( - EntryPointVolume = "entrypoint-volume" // 入口程序挂载 - ExecuteScriptsVolume = "execute-inner-scripts" // script属性 存储卷 - PodInfoVolume = "podinfo" // 存储Pod信息用于 downwardAPI + EntryPointVolume = "container-agent-volume" // 入口程序挂载 + ExecuteScriptsVolume = "execute-inner-scripts" // script 属性存储卷 + PodInfoVolume = "podinfo" // 存储 Pod 信息用于 downwardAPI ) // deployTaskFlow 调协 task flow @@ -287,7 +287,7 @@ func (r *TaskController) forward(ctx context.Context, pod *v1.Pod, task *v1alpha // AnnotationTaskOrderKey = "-1" 表示流程有错误,直接退出 if pod.Annotations[AnnotationTaskOrderKey] == AnnotationExitOrder { - r.event.Eventf(task, v1.EventTypeWarning, "TaskFlow Failed", "TaskFlow exit") + r.event.Eventf(task, v1.EventTypeWarning, "TaskFlow Exited", "TaskFlow exit") return nil } diff --git a/pkg/image/image_helper.go b/pkg/image/image_helper.go index 87c3c99..2fe7f22 100644 --- a/pkg/image/image_helper.go +++ b/pkg/image/image_helper.go @@ -11,7 +11,7 @@ import ( // ImageCommand 镜像内部 command args type ImageCommand struct { - Command []string // 对应 docker 的 entrypoint + Command []string // 对应 docker 的 container-agent Args []string // 对应 docker 的 cmd } diff --git a/pkg/k8sconfig/init_config.go b/pkg/k8sconfig/init_config.go index fabdeb0..4eab078 100644 --- a/pkg/k8sconfig/init_config.go +++ b/pkg/k8sconfig/init_config.go @@ -18,7 +18,7 @@ func K8sRestConfigInPod() *rest.Config { } func K8sRestConfig() *rest.Config { - if os.Getenv("release") == "1" { + if os.Getenv("Release") == "1" { klog.Info("run in cluster!") return K8sRestConfigInPod() } diff --git a/yaml/example1.yaml b/yaml/task_customize_image_example.yaml similarity index 86% rename from yaml/example1.yaml rename to yaml/task_customize_image_example.yaml index e848238..a32042f 100644 --- a/yaml/example1.yaml +++ b/yaml/task_customize_image_example.yaml @@ -1,7 +1,7 @@ apiVersion: api.practice.com/v1alpha1 kind: Task metadata: - name: mytask-test-entrypoint + name: mytask-test-container-agent spec: steps: - name: step1 @@ -17,7 +17,7 @@ spec: command: [ "sh","-c" ] args: [ "echo step33333" ] - name: step4 - image: shenyisyn/task:v1 + image: <填入自定义镜像> args: - echo - "step test2" \ No newline at end of file diff --git a/yaml/example-error.yaml b/yaml/task_error_example.yaml similarity index 67% rename from yaml/example-error.yaml rename to yaml/task_error_example.yaml index e25bd5b..90548eb 100644 --- a/yaml/example-error.yaml +++ b/yaml/task_error_example.yaml @@ -1,13 +1,14 @@ apiVersion: api.practice.com/v1alpha1 kind: Task metadata: - name: mytask-error-test + name: example-taskflow-error-test spec: steps: - name: step1 image: busybox:1.28 command: [ "sh","-c" ] args: [ "echo step1" ] + # 这个 step2 会出错退出,不会执行 step3 - name: step2 image: busybox:1.28 command: ["sh", "-c"] @@ -16,7 +17,5 @@ spec: echo error; exit 1; - name: step3 - image: shenyisyn/task:v1 - args: - - echo - - "step test3" \ No newline at end of file + image: devopscube/kubernetes-job-demo:latest + args: [ "10" ] \ No newline at end of file diff --git a/yaml/example.yaml b/yaml/task_example.yaml similarity index 63% rename from yaml/example.yaml rename to yaml/task_example.yaml index af40233..bd2bc6c 100644 --- a/yaml/example.yaml +++ b/yaml/task_example.yaml @@ -1,7 +1,7 @@ apiVersion: api.practice.com/v1alpha1 kind: Task metadata: - name: mytask + name: example-taskflow spec: steps: - name: step1 @@ -11,4 +11,7 @@ spec: - name: step2 image: busybox:1.28 command: [ "sh","-c" ] - args: [ "echo step2" ] \ No newline at end of file + args: [ "echo step2" ] + - name: step3 + image: devopscube/kubernetes-job-demo:latest + args: [ "100" ] \ No newline at end of file diff --git a/yaml/example-script.yaml b/yaml/task_script_example.yaml similarity index 75% rename from yaml/example-script.yaml rename to yaml/task_script_example.yaml index cb8c46b..c303751 100644 --- a/yaml/example-script.yaml +++ b/yaml/task_script_example.yaml @@ -1,7 +1,7 @@ apiVersion: api.practice.com/v1alpha1 kind: Task metadata: - name: mytask + name: example-taskflow-script-test spec: steps: - name: step1 @@ -16,10 +16,10 @@ spec: echo error; exit 0; - name: step3 - image: shenyisyn/task:v1 - args: ["echo step3"] - - name: step4 image: busybox:1.28 script: | echo step4 - echo "step4-2" \ No newline at end of file + echo "step4-2" + - name: step4 + image: devopscube/kubernetes-job-demo:latest + args: [ "100" ] \ No newline at end of file