diff --git a/README.md b/README.md index 58a4171..e8db146 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ metadata: spec: # 设置多个 step 步骤,会按照填入的 container 顺序执行 steps: - # 每项都是一个 container 对象 + # 每项都是一个 container 对象,每个 container 都会顺序执行 - name: step1 image: busybox:1.28 command: [ "sh","-c" ] @@ -23,12 +23,27 @@ spec: args: [ "echo step2" ] - name: step3 image: devopscube/kubernetes-job-demo:latest - args: [ "100" ] + args: [ "10" ] ``` ### 项目功能 1. 支持任务中的 container 顺序执行 +2. 查看任务流状态 +```bash +[root@VM-0-16-centos yaml]# kubectl get tasks.api.practice.com +NAME STATUS START DURATION AGE +example-taskflow Successful 2024-01-06 22:19:24 31s 80s +example-taskflow-error-test Running 2024-01-06 22:20:36 6s +[root@VM-0-16-centos yaml]# kubectl get tasks.api.practice.com +NAME STATUS START DURATION AGE +example-taskflow Successful 2024-01-06 22:19:24 31s 84s +example-taskflow-error-test Failure 2024-01-06 22:20:36 6s 10s +[root@VM-0-16-centos yaml]# kubectl get tasks.api.practice.com +NAME STATUS START DURATION AGE +example-taskflow Successful 2024-01-06 22:19:24 31s 86s +example-taskflow-error-test Failure 2024-01-06 22:20:36 6s 12s +``` ### 项目部署与使用 1. 打成镜像或是使用编译二进制。 @@ -131,6 +146,6 @@ 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 ``` - +6. 使用 资源对象 ### RoadMap diff --git a/container-agent/cmd/helper.go b/container-agent/cmd/helper.go index 5955898..5da5872 100644 --- a/container-agent/cmd/helper.go +++ b/container-agent/cmd/helper.go @@ -17,7 +17,7 @@ import ( // ValidateFlags 检查参数 func ValidateFlags() { if encodeFile == "" { - if waitFile == "" || out == "" || command == "" { + if waitFile == "" || out == "" { log.Println("error input param...") os.Exit(1) } @@ -119,6 +119,9 @@ func ExecCmdAndArgs(args []string) { cmd := "" // 非加密文件模式 if encodeFile == "" { + if command == "" { + command = "sh" + } cmdList := strings.Split(command, " ") // 如 sh -c 这种字符串,要切割,取出第一个作为command cmd = cmdList[0] //肯定有值 ,所以不用判断 if len(cmdList) > 1 { //把剩余的合并到argList 前面 diff --git a/deploy/task.yaml b/deploy/task.yaml index ff7563c..365dbda 100644 --- a/deploy/task.yaml +++ b/deploy/task.yaml @@ -13,6 +13,19 @@ spec: served: true #是否是存储版本 storage: true + additionalPrinterColumns: + - name: Status + type: string + jsonPath: .status.status + - name: Start + type: string + jsonPath: .status.startAt + - name: Duration + type: string + jsonPath: .status.duration + - name: Age + type: date + jsonPath: .metadata.creationTimestamp schema: openAPIV3Schema: type: object diff --git a/docker_build.sh b/docker_build.sh index 5162761..5c4ba1d 100755 --- a/docker_build.sh +++ b/docker_build.sh @@ -10,15 +10,7 @@ IMAGE_NAME="docker.io/taskflow/agent" # 镜像名称 TAG="v1.0" # 镜像标签 -# 检查镜像是否已经存在 -if docker inspect "${IMAGE_NAME}:${TAG}" &> /dev/null; then - echo "镜像 ${IMAGE_NAME}:${TAG} 已存在,无需构建。" -else - echo "镜像 ${IMAGE_NAME}:${TAG} 不存在,开始构建..." - # 执行 Docker build 命令 - docker build -t "${IMAGE_NAME}:${TAG}" -f "${DOCKERFILE_PATH}" . - echo "镜像构建完成。" - - # 可选:推送镜像到 Docker 镜像仓库 - # docker push "${IMAGE_NAME}:${TAG}" -fi \ No newline at end of file +echo "镜像 ${IMAGE_NAME}:${TAG} 开始构建..." +# 执行 Docker build 命令 +docker build -t "${IMAGE_NAME}:${TAG}" -f "${DOCKERFILE_PATH}" . +echo "镜像构建完成。" \ No newline at end of file diff --git a/go.mod b/go.mod index df19ac9..37ac9a7 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/myoperator/cicdoperator +module github.com/myoperator/taskflowoperator go 1.18 diff --git a/main.go b/main.go index 19f4c87..74e0f2e 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,10 @@ package main import ( - taskv1alpha1 "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" - "github.com/myoperator/cicdoperator/pkg/controller" - builder2 "github.com/myoperator/cicdoperator/pkg/image" - "github.com/myoperator/cicdoperator/pkg/k8sconfig" + taskv1alpha1 "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" + "github.com/myoperator/taskflowoperator/pkg/controller" + builder2 "github.com/myoperator/taskflowoperator/pkg/image" + "github.com/myoperator/taskflowoperator/pkg/k8sconfig" corev1 "k8s.io/api/core/v1" _ "k8s.io/code-generator" "log" diff --git a/pkg/apis/task/v1alpha1/types.go b/pkg/apis/task/v1alpha1/types.go index 7c12c65..f0bf693 100644 --- a/pkg/apis/task/v1alpha1/types.go +++ b/pkg/apis/task/v1alpha1/types.go @@ -18,6 +18,7 @@ type Task struct { // TODO: 加入 TaskStatus 用于查看每个工作流的状态 Running Complete Failed ... // TODO: 记录运行时间,需要在 status加入一个时间值字段 开始时记录时间 结束或错误退出时记录时间 // 在调协中修改 status + Status TaskStatus `json:"status,omitempty"` } type TaskSpec struct { @@ -30,6 +31,15 @@ type TaskStep struct { Script string `json:"script,omitempty"` } +type TaskStatus struct { + // Status 任务状态 + Status string `json:"status"` + // StartAt 任务起始时间 + StartAt string `json:"startAt"` + // Duration 任务执行多久 + Duration string `json:"duration"` +} + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // TaskList .. diff --git a/pkg/apis/task/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/task/v1alpha1/zz_generated.deepcopy.go index 53be61b..43765d0 100644 --- a/pkg/apis/task/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/task/v1alpha1/zz_generated.deepcopy.go @@ -31,6 +31,7 @@ func (in *Task) DeepCopyInto(out *Task) { out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) in.Spec.DeepCopyInto(&out.Spec) + in.Status.DeepCopyInto(&out.Status) return } @@ -108,6 +109,22 @@ func (in *TaskSpec) DeepCopy() *TaskSpec { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TaskStatus) DeepCopyInto(out *TaskStatus) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TaskStatus. +func (in *TaskStatus) DeepCopy() *TaskStatus { + if in == nil { + return nil + } + out := new(TaskStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TaskStep) DeepCopyInto(out *TaskStep) { *out = *in diff --git a/pkg/client/clientset/versioned/clientset.go b/pkg/client/clientset/versioned/clientset.go index c4db81c..027e2d0 100644 --- a/pkg/client/clientset/versioned/clientset.go +++ b/pkg/client/clientset/versioned/clientset.go @@ -21,7 +21,7 @@ package versioned import ( "fmt" - apiv1alpha1 "github.com/myoperator/cicdoperator/pkg/client/clientset/versioned/typed/task/v1alpha1" + apiv1alpha1 "github.com/myoperator/taskflowoperator/pkg/client/clientset/versioned/typed/task/v1alpha1" discovery "k8s.io/client-go/discovery" rest "k8s.io/client-go/rest" flowcontrol "k8s.io/client-go/util/flowcontrol" diff --git a/pkg/client/clientset/versioned/fake/clientset_generated.go b/pkg/client/clientset/versioned/fake/clientset_generated.go index 2e5bd9b..c062faf 100644 --- a/pkg/client/clientset/versioned/fake/clientset_generated.go +++ b/pkg/client/clientset/versioned/fake/clientset_generated.go @@ -19,9 +19,9 @@ limitations under the License. package fake import ( - clientset "github.com/myoperator/cicdoperator/pkg/client/clientset/versioned" - apiv1alpha1 "github.com/myoperator/cicdoperator/pkg/client/clientset/versioned/typed/task/v1alpha1" - fakeapiv1alpha1 "github.com/myoperator/cicdoperator/pkg/client/clientset/versioned/typed/task/v1alpha1/fake" + clientset "github.com/myoperator/taskflowoperator/pkg/client/clientset/versioned" + apiv1alpha1 "github.com/myoperator/taskflowoperator/pkg/client/clientset/versioned/typed/task/v1alpha1" + fakeapiv1alpha1 "github.com/myoperator/taskflowoperator/pkg/client/clientset/versioned/typed/task/v1alpha1/fake" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/discovery" @@ -74,7 +74,10 @@ func (c *Clientset) Tracker() testing.ObjectTracker { return c.tracker } -var _ clientset.Interface = &Clientset{} +var ( + _ clientset.Interface = &Clientset{} + _ testing.FakeClient = &Clientset{} +) // ApiV1alpha1 retrieves the ApiV1alpha1Client func (c *Clientset) ApiV1alpha1() apiv1alpha1.ApiV1alpha1Interface { diff --git a/pkg/client/clientset/versioned/fake/register.go b/pkg/client/clientset/versioned/fake/register.go index f0c23b1..5025499 100644 --- a/pkg/client/clientset/versioned/fake/register.go +++ b/pkg/client/clientset/versioned/fake/register.go @@ -19,7 +19,7 @@ limitations under the License. package fake import ( - apiv1alpha1 "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" + apiv1alpha1 "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" schema "k8s.io/apimachinery/pkg/runtime/schema" diff --git a/pkg/client/clientset/versioned/scheme/register.go b/pkg/client/clientset/versioned/scheme/register.go index 5ff8123..ce06d61 100644 --- a/pkg/client/clientset/versioned/scheme/register.go +++ b/pkg/client/clientset/versioned/scheme/register.go @@ -19,7 +19,7 @@ limitations under the License. package scheme import ( - apiv1alpha1 "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" + apiv1alpha1 "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" schema "k8s.io/apimachinery/pkg/runtime/schema" diff --git a/pkg/client/clientset/versioned/typed/task/v1alpha1/fake/fake_task.go b/pkg/client/clientset/versioned/typed/task/v1alpha1/fake/fake_task.go index d54acfc..39002bc 100644 --- a/pkg/client/clientset/versioned/typed/task/v1alpha1/fake/fake_task.go +++ b/pkg/client/clientset/versioned/typed/task/v1alpha1/fake/fake_task.go @@ -21,7 +21,7 @@ package fake import ( "context" - v1alpha1 "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" + v1alpha1 "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" labels "k8s.io/apimachinery/pkg/labels" schema "k8s.io/apimachinery/pkg/runtime/schema" @@ -102,6 +102,18 @@ func (c *FakeTasks) Update(ctx context.Context, task *v1alpha1.Task, opts v1.Upd return obj.(*v1alpha1.Task), err } +// UpdateStatus was generated because the type contains a Status member. +// Add a +genclient:noStatus comment above the type to avoid generating UpdateStatus(). +func (c *FakeTasks) UpdateStatus(ctx context.Context, task *v1alpha1.Task, opts v1.UpdateOptions) (*v1alpha1.Task, error) { + obj, err := c.Fake. + Invokes(testing.NewUpdateSubresourceAction(tasksResource, "status", c.ns, task), &v1alpha1.Task{}) + + if obj == nil { + return nil, err + } + return obj.(*v1alpha1.Task), err +} + // Delete takes name of the task and deletes it. Returns an error if one occurs. func (c *FakeTasks) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { _, err := c.Fake. diff --git a/pkg/client/clientset/versioned/typed/task/v1alpha1/fake/fake_task_client.go b/pkg/client/clientset/versioned/typed/task/v1alpha1/fake/fake_task_client.go index 9696b57..fe93de8 100644 --- a/pkg/client/clientset/versioned/typed/task/v1alpha1/fake/fake_task_client.go +++ b/pkg/client/clientset/versioned/typed/task/v1alpha1/fake/fake_task_client.go @@ -19,7 +19,7 @@ limitations under the License. package fake import ( - v1alpha1 "github.com/myoperator/cicdoperator/pkg/client/clientset/versioned/typed/task/v1alpha1" + v1alpha1 "github.com/myoperator/taskflowoperator/pkg/client/clientset/versioned/typed/task/v1alpha1" rest "k8s.io/client-go/rest" testing "k8s.io/client-go/testing" ) diff --git a/pkg/client/clientset/versioned/typed/task/v1alpha1/task.go b/pkg/client/clientset/versioned/typed/task/v1alpha1/task.go index f75e492..f9276e5 100644 --- a/pkg/client/clientset/versioned/typed/task/v1alpha1/task.go +++ b/pkg/client/clientset/versioned/typed/task/v1alpha1/task.go @@ -22,8 +22,8 @@ import ( "context" "time" - v1alpha1 "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" - scheme "github.com/myoperator/cicdoperator/pkg/client/clientset/versioned/scheme" + v1alpha1 "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" + scheme "github.com/myoperator/taskflowoperator/pkg/client/clientset/versioned/scheme" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" types "k8s.io/apimachinery/pkg/types" watch "k8s.io/apimachinery/pkg/watch" @@ -40,6 +40,7 @@ type TasksGetter interface { type TaskInterface interface { Create(ctx context.Context, task *v1alpha1.Task, opts v1.CreateOptions) (*v1alpha1.Task, error) Update(ctx context.Context, task *v1alpha1.Task, opts v1.UpdateOptions) (*v1alpha1.Task, error) + UpdateStatus(ctx context.Context, task *v1alpha1.Task, opts v1.UpdateOptions) (*v1alpha1.Task, error) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error Get(ctx context.Context, name string, opts v1.GetOptions) (*v1alpha1.Task, error) @@ -135,6 +136,22 @@ func (c *tasks) Update(ctx context.Context, task *v1alpha1.Task, opts v1.UpdateO return } +// UpdateStatus was generated because the type contains a Status member. +// Add a +genclient:noStatus comment above the type to avoid generating UpdateStatus(). +func (c *tasks) UpdateStatus(ctx context.Context, task *v1alpha1.Task, opts v1.UpdateOptions) (result *v1alpha1.Task, err error) { + result = &v1alpha1.Task{} + err = c.client.Put(). + Namespace(c.ns). + Resource("tasks"). + Name(task.Name). + SubResource("status"). + VersionedParams(&opts, scheme.ParameterCodec). + Body(task). + Do(ctx). + Into(result) + return +} + // Delete takes name of the task and deletes it. Returns an error if one occurs. func (c *tasks) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { return c.client.Delete(). diff --git a/pkg/client/clientset/versioned/typed/task/v1alpha1/task_client.go b/pkg/client/clientset/versioned/typed/task/v1alpha1/task_client.go index b132bd6..eb2a9f0 100644 --- a/pkg/client/clientset/versioned/typed/task/v1alpha1/task_client.go +++ b/pkg/client/clientset/versioned/typed/task/v1alpha1/task_client.go @@ -19,8 +19,8 @@ limitations under the License. package v1alpha1 import ( - v1alpha1 "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" - "github.com/myoperator/cicdoperator/pkg/client/clientset/versioned/scheme" + v1alpha1 "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" + "github.com/myoperator/taskflowoperator/pkg/client/clientset/versioned/scheme" rest "k8s.io/client-go/rest" ) diff --git a/pkg/client/clientset/versioned/typed/tasks/v1alpha1/doc.go b/pkg/client/clientset/versioned/typed/tasks/v1alpha1/doc.go new file mode 100644 index 0000000..df51baa --- /dev/null +++ b/pkg/client/clientset/versioned/typed/tasks/v1alpha1/doc.go @@ -0,0 +1,20 @@ +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by client-gen. DO NOT EDIT. + +// This package has the automatically generated typed clients. +package v1alpha1 diff --git a/pkg/client/clientset/versioned/typed/tasks/v1alpha1/fake/doc.go b/pkg/client/clientset/versioned/typed/tasks/v1alpha1/fake/doc.go new file mode 100644 index 0000000..16f4439 --- /dev/null +++ b/pkg/client/clientset/versioned/typed/tasks/v1alpha1/fake/doc.go @@ -0,0 +1,20 @@ +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by client-gen. DO NOT EDIT. + +// Package fake has the automatically generated clients. +package fake diff --git a/pkg/client/clientset/versioned/typed/tasks/v1alpha1/fake/fake_tasks_client.go b/pkg/client/clientset/versioned/typed/tasks/v1alpha1/fake/fake_tasks_client.go new file mode 100644 index 0000000..33f03b0 --- /dev/null +++ b/pkg/client/clientset/versioned/typed/tasks/v1alpha1/fake/fake_tasks_client.go @@ -0,0 +1,19 @@ +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by client-gen. DO NOT EDIT. + +package fake diff --git a/pkg/client/clientset/versioned/typed/tasks/v1alpha1/generated_expansion.go b/pkg/client/clientset/versioned/typed/tasks/v1alpha1/generated_expansion.go new file mode 100644 index 0000000..86351db --- /dev/null +++ b/pkg/client/clientset/versioned/typed/tasks/v1alpha1/generated_expansion.go @@ -0,0 +1,19 @@ +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by client-gen. DO NOT EDIT. + +package v1alpha1 diff --git a/pkg/client/clientset/versioned/typed/tasks/v1alpha1/tasks_client.go b/pkg/client/clientset/versioned/typed/tasks/v1alpha1/tasks_client.go new file mode 100644 index 0000000..86351db --- /dev/null +++ b/pkg/client/clientset/versioned/typed/tasks/v1alpha1/tasks_client.go @@ -0,0 +1,19 @@ +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by client-gen. DO NOT EDIT. + +package v1alpha1 diff --git a/pkg/client/informers/externalversions/factory.go b/pkg/client/informers/externalversions/factory.go index db75c0c..5722c89 100644 --- a/pkg/client/informers/externalversions/factory.go +++ b/pkg/client/informers/externalversions/factory.go @@ -23,9 +23,9 @@ import ( sync "sync" time "time" - versioned "github.com/myoperator/cicdoperator/pkg/client/clientset/versioned" - internalinterfaces "github.com/myoperator/cicdoperator/pkg/client/informers/externalversions/internalinterfaces" - task "github.com/myoperator/cicdoperator/pkg/client/informers/externalversions/task" + versioned "github.com/myoperator/taskflowoperator/pkg/client/clientset/versioned" + internalinterfaces "github.com/myoperator/taskflowoperator/pkg/client/informers/externalversions/internalinterfaces" + task "github.com/myoperator/taskflowoperator/pkg/client/informers/externalversions/task" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" schema "k8s.io/apimachinery/pkg/runtime/schema" diff --git a/pkg/client/informers/externalversions/generic.go b/pkg/client/informers/externalversions/generic.go index d2cfe2d..402a391 100644 --- a/pkg/client/informers/externalversions/generic.go +++ b/pkg/client/informers/externalversions/generic.go @@ -21,7 +21,7 @@ package externalversions import ( "fmt" - v1alpha1 "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" + v1alpha1 "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" schema "k8s.io/apimachinery/pkg/runtime/schema" cache "k8s.io/client-go/tools/cache" ) diff --git a/pkg/client/informers/externalversions/internalinterfaces/factory_interfaces.go b/pkg/client/informers/externalversions/internalinterfaces/factory_interfaces.go index fd3aba3..17ec8f6 100644 --- a/pkg/client/informers/externalversions/internalinterfaces/factory_interfaces.go +++ b/pkg/client/informers/externalversions/internalinterfaces/factory_interfaces.go @@ -21,7 +21,7 @@ package internalinterfaces import ( time "time" - versioned "github.com/myoperator/cicdoperator/pkg/client/clientset/versioned" + versioned "github.com/myoperator/taskflowoperator/pkg/client/clientset/versioned" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" cache "k8s.io/client-go/tools/cache" diff --git a/pkg/client/informers/externalversions/task/interface.go b/pkg/client/informers/externalversions/task/interface.go index 94a9770..335256d 100644 --- a/pkg/client/informers/externalversions/task/interface.go +++ b/pkg/client/informers/externalversions/task/interface.go @@ -19,8 +19,8 @@ limitations under the License. package task import ( - internalinterfaces "github.com/myoperator/cicdoperator/pkg/client/informers/externalversions/internalinterfaces" - v1alpha1 "github.com/myoperator/cicdoperator/pkg/client/informers/externalversions/task/v1alpha1" + internalinterfaces "github.com/myoperator/taskflowoperator/pkg/client/informers/externalversions/internalinterfaces" + v1alpha1 "github.com/myoperator/taskflowoperator/pkg/client/informers/externalversions/task/v1alpha1" ) // Interface provides access to each of this group's versions. diff --git a/pkg/client/informers/externalversions/task/v1alpha1/interface.go b/pkg/client/informers/externalversions/task/v1alpha1/interface.go index 769b18f..5180bd3 100644 --- a/pkg/client/informers/externalversions/task/v1alpha1/interface.go +++ b/pkg/client/informers/externalversions/task/v1alpha1/interface.go @@ -19,7 +19,7 @@ limitations under the License. package v1alpha1 import ( - internalinterfaces "github.com/myoperator/cicdoperator/pkg/client/informers/externalversions/internalinterfaces" + internalinterfaces "github.com/myoperator/taskflowoperator/pkg/client/informers/externalversions/internalinterfaces" ) // Interface provides access to all the informers in this group version. diff --git a/pkg/client/informers/externalversions/task/v1alpha1/task.go b/pkg/client/informers/externalversions/task/v1alpha1/task.go index b6b0c6d..b9399c0 100644 --- a/pkg/client/informers/externalversions/task/v1alpha1/task.go +++ b/pkg/client/informers/externalversions/task/v1alpha1/task.go @@ -22,10 +22,10 @@ import ( "context" time "time" - taskv1alpha1 "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" - versioned "github.com/myoperator/cicdoperator/pkg/client/clientset/versioned" - internalinterfaces "github.com/myoperator/cicdoperator/pkg/client/informers/externalversions/internalinterfaces" - v1alpha1 "github.com/myoperator/cicdoperator/pkg/client/listers/task/v1alpha1" + taskv1alpha1 "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" + versioned "github.com/myoperator/taskflowoperator/pkg/client/clientset/versioned" + internalinterfaces "github.com/myoperator/taskflowoperator/pkg/client/informers/externalversions/internalinterfaces" + v1alpha1 "github.com/myoperator/taskflowoperator/pkg/client/listers/task/v1alpha1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" watch "k8s.io/apimachinery/pkg/watch" diff --git a/pkg/client/listers/task/v1alpha1/task.go b/pkg/client/listers/task/v1alpha1/task.go index 58e7d40..d84dcce 100644 --- a/pkg/client/listers/task/v1alpha1/task.go +++ b/pkg/client/listers/task/v1alpha1/task.go @@ -19,7 +19,7 @@ limitations under the License. package v1alpha1 import ( - v1alpha1 "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" + v1alpha1 "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/tools/cache" diff --git a/pkg/controller/helper.go b/pkg/controller/helper.go index c78bd43..07c0fbc 100644 --- a/pkg/controller/helper.go +++ b/pkg/controller/helper.go @@ -4,15 +4,16 @@ import ( "context" "fmt" "github.com/google/go-containerregistry/pkg/name" - "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" - "github.com/myoperator/cicdoperator/pkg/common" - "github.com/myoperator/cicdoperator/pkg/image" + "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" + "github.com/myoperator/taskflowoperator/pkg/common" + "github.com/myoperator/taskflowoperator/pkg/image" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" "strconv" "strings" + "time" ) // setInitContainer 设置 InitContainer @@ -78,6 +79,12 @@ const ( AnnotationExitOrder = "-1" // 退出 step 标识 ) +const ( + TaskStatusSuccess = "Successful" + TaskStatusRunning = "Running" + TaskStatusFailed = "Failure" +) + // setPodMeta 设置 Pod 信息 func (r *TaskController) setPodMeta(task *v1alpha1.Task, pod *v1.Pod) { pod.Namespace = task.Namespace @@ -225,22 +232,43 @@ func (r *TaskController) deployTaskFlow(ctx context.Context, task *v1alpha1.Task getPod, err := r.getChildPod(task) // 代表 Pod 已被创建,代表 taskFlow 正在执行 if err == nil { + klog.Info("pod status: ", getPod.Status.Phase) + klog.Info("annotation order: ", getPod.Annotations[AnnotationTaskOrderKey]) if getPod.Status.Phase == v1.PodRunning { // 如果为起始状态,先把 annotation 设置为1,让流水线可以执行,并更新 if getPod.Annotations[AnnotationTaskOrderKey] == AnnotationTaskOrderInitValue { getPod.Annotations[AnnotationTaskOrderKey] = "1" r.event.Event(task, v1.EventTypeNormal, "TaskFlow Start", "TaskFlow start to run step 1 container") - return r.client.Update(ctx, getPod) + err = r.client.Update(ctx, getPod) + if err != nil { + return err + } + // task start time + task.Status.StartAt = time.Now().Format("2006-01-02 15:04:05") + task.Status.Status = TaskStatusRunning + return r.client.Status().Update(ctx, task) } else { // 如果是其他状态,则调用forward方法前进 if err := r.forward(ctx, getPod, task); err != nil { return err } } - } - klog.Info("pod status: ", getPod.Status.Phase) - klog.Info("annotation order: ", getPod.Annotations[AnnotationTaskOrderKey]) + } else if getPod.Status.Phase == v1.PodSucceeded { + r.event.Eventf(task, v1.EventTypeNormal, "TaskFlow Completed", "TaskFlow completed successfully") + task.Status.Status = TaskStatusSuccess + st, err := time.Parse("2006-01-02 15:04:05", task.Status.StartAt) + if err != nil { + return err + } + end := time.Now().Format("2006-01-02 15:04:05") + et, err := time.Parse("2006-01-02 15:04:05", end) + if err != nil { + return err + } + task.Status.Duration = et.Sub(st).String() + return r.client.Status().Update(ctx, task) + } return nil } @@ -281,14 +309,26 @@ func (r *TaskController) deployTaskFlow(ctx context.Context, task *v1alpha1.Task func (r *TaskController) forward(ctx context.Context, pod *v1.Pod, task *v1alpha1.Task) error { // Pod 状态 Succeeded 代表整个 Pod 执行完毕 if pod.Status.Phase == v1.PodSucceeded { - r.event.Eventf(task, v1.EventTypeNormal, "TaskFlow Completed", "TaskFlow completed successfully") return nil } // AnnotationTaskOrderKey = "-1" 表示流程有错误,直接退出 if pod.Annotations[AnnotationTaskOrderKey] == AnnotationExitOrder { + // 触发 event 事件 r.event.Eventf(task, v1.EventTypeWarning, "TaskFlow Exited", "TaskFlow exit") - return nil + // 修改 task status + task.Status.Status = TaskStatusFailed + st, err := time.Parse("2006-01-02 15:04:05", task.Status.StartAt) + if err != nil { + return err + } + end := time.Now().Format("2006-01-02 15:04:05") + et, err := time.Parse("2006-01-02 15:04:05", end) + if err != nil { + return err + } + task.Status.Duration = et.Sub(st).String() + return r.client.Status().Update(ctx, task) } order, err := strconv.Atoi(pod.Annotations[AnnotationTaskOrderKey]) @@ -296,7 +336,8 @@ func (r *TaskController) forward(ctx context.Context, pod *v1.Pod, task *v1alpha klog.Error("AnnotationTaskOrderKey err: ", err) return err } - // 容器长度相等,代表已经到了最后一个 + + // 容器长度相等,代表执行到最后 if order == len(pod.Spec.Containers) { return nil } @@ -314,8 +355,7 @@ func (r *TaskController) forward(ctx context.Context, pod *v1.Pod, task *v1alpha r.event.Eventf(task, v1.EventTypeWarning, "TaskFlow Failed", "TaskFlow failed to run step %v container", order) pod.Annotations[AnnotationTaskOrderKey] = AnnotationExitOrder return r.client.Update(ctx, pod) - //pod.Status.Phase=v1.PodFailed - //return pb.Client.Status().Update(ctx,pod) + } } diff --git a/pkg/controller/task_controller.go b/pkg/controller/taskflow_controller.go similarity index 95% rename from pkg/controller/task_controller.go rename to pkg/controller/taskflow_controller.go index 4d4b9b0..583b07c 100644 --- a/pkg/controller/task_controller.go +++ b/pkg/controller/taskflow_controller.go @@ -2,8 +2,8 @@ package controller import ( "context" - "github.com/myoperator/cicdoperator/pkg/apis/task/v1alpha1" - "github.com/myoperator/cicdoperator/pkg/image" + "github.com/myoperator/taskflowoperator/pkg/apis/task/v1alpha1" + "github.com/myoperator/taskflowoperator/pkg/image" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" "k8s.io/client-go/util/workqueue" diff --git a/pkg/k8sconfig/init_config.go b/pkg/k8sconfig/init_config.go index 4eab078..998015c 100644 --- a/pkg/k8sconfig/init_config.go +++ b/pkg/k8sconfig/init_config.go @@ -1,7 +1,7 @@ package k8sconfig import ( - "github.com/myoperator/cicdoperator/pkg/common" + "github.com/myoperator/taskflowoperator/pkg/common" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/klog/v2" diff --git a/yaml/task_example.yaml b/yaml/task_example.yaml index bd2bc6c..cc9b053 100644 --- a/yaml/task_example.yaml +++ b/yaml/task_example.yaml @@ -14,4 +14,4 @@ spec: args: [ "echo step2" ] - name: step3 image: devopscube/kubernetes-job-demo:latest - args: [ "100" ] \ No newline at end of file + args: [ "10" ] \ No newline at end of file