From 18f8f13ffbf192fcde26f88241a090cd7824c524 Mon Sep 17 00:00:00 2001 From: richardxz Date: Mon, 17 Sep 2018 11:20:05 +0800 Subject: [PATCH] support job re-run --- pkg/apis/v1alpha/jobs/jobs.go | 63 +++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 pkg/apis/v1alpha/jobs/jobs.go diff --git a/pkg/apis/v1alpha/jobs/jobs.go b/pkg/apis/v1alpha/jobs/jobs.go new file mode 100644 index 000000000..b98c5e261 --- /dev/null +++ b/pkg/apis/v1alpha/jobs/jobs.go @@ -0,0 +1,63 @@ +/* +Copyright 2018 The KubeSphere 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. +*/ + +package jobs + +import ( + "net/http" + + "github.com/emicklei/go-restful" + + "github.com/emicklei/go-restful-openapi" + + "fmt" + + "kubesphere.io/kubesphere/pkg/constants" + "kubesphere.io/kubesphere/pkg/models/controllers" +) + +func Register(ws *restful.WebService, subPath string) { + + tags := []string{"jobs"} + + ws.Route(ws.POST(subPath).To(handleJob).Consumes("*/*").Metadata(restfulspec.KeyOpenAPITags, tags).Doc("Handle job" + + " operation").Param(ws.PathParameter("job", "job name").DataType("string")).Param(ws.PathParameter("namespace", + "job's namespace").DataType("string")).Param(ws.QueryParameter("a", + "action").DataType("string")).Writes("")) +} + +func handleJob(req *restful.Request, resp *restful.Response) { + var res interface{} + var err error + + job := req.PathParameter("job") + namespace := req.PathParameter("namespace") + action := req.QueryParameter("a") + + switch action { + case "rerun": + res, err = controllers.JobReRun(namespace, job) + default: + resp.WriteHeaderAndEntity(http.StatusForbidden, constants.MessageResponse{Message: fmt.Sprintf("invalid operation %s", action)}) + return + } + if err != nil { + resp.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()}) + return + } + + resp.WriteEntity(res) +}