Add proxy devops APIs request to ks-devops
move devops controllers into ks-devops Signed-off-by: rick <1450685+LinuxSuRen@users.noreply.github.com>
This commit is contained in:
@@ -1,908 +0,0 @@
|
||||
/*
|
||||
Copyright 2020 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 v1alpha2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
"k8s.io/apiserver/pkg/authentication/user"
|
||||
log "k8s.io/klog"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"kubesphere.io/api/devops/v1alpha3"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/authorization/authorizer"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/request"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/models/devops"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
clientDevOps "kubesphere.io/kubesphere/pkg/simple/client/devops"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins"
|
||||
)
|
||||
|
||||
const jenkinsHeaderPre = "X-"
|
||||
|
||||
func (h *ProjectPipelineHandler) GetPipeline(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
|
||||
res, err := h.devopsOperator.GetPipeline(projectName, pipelineName, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) getPipelinesByRequest(req *restful.Request) (api.ListResult, error) {
|
||||
// this is a very trick way, but don't have a better solution for now
|
||||
var (
|
||||
start int
|
||||
limit int
|
||||
namespace string
|
||||
)
|
||||
|
||||
// parse query from the request
|
||||
pipelineFilter, namespace := parseNameFilterFromQuery(req.QueryParameter("q"))
|
||||
|
||||
// make sure we have an appropriate value
|
||||
limit, start = params.ParsePaging(req)
|
||||
return h.devopsOperator.ListPipelineObj(namespace, pipelineFilter, func(list []*v1alpha3.Pipeline, i int, j int) bool {
|
||||
return strings.Compare(strings.ToUpper(list[i].Name), strings.ToUpper(list[j].Name)) < 0
|
||||
}, limit, start)
|
||||
}
|
||||
|
||||
func parseNameFilterFromQuery(query string) (filter devops.PipelineFilter, namespace string) {
|
||||
var (
|
||||
nameFilter string
|
||||
)
|
||||
|
||||
for _, val := range strings.Split(query, ";") {
|
||||
if strings.HasPrefix(val, "pipeline:") {
|
||||
nsAndName := strings.TrimPrefix(val, "pipeline:")
|
||||
filterMeta := strings.Split(nsAndName, "/")
|
||||
if len(filterMeta) >= 2 {
|
||||
namespace = filterMeta[0]
|
||||
nameFilter = filterMeta[1] // the format is '*keyword*'
|
||||
nameFilter = strings.TrimSuffix(nameFilter, "*")
|
||||
nameFilter = strings.TrimPrefix(nameFilter, "*")
|
||||
} else if len(filterMeta) > 0 {
|
||||
namespace = filterMeta[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
filter = func(pipeline *v1alpha3.Pipeline) bool {
|
||||
return strings.Contains(pipeline.Name, nameFilter)
|
||||
}
|
||||
if nameFilter == "" {
|
||||
filter = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) ListPipelines(req *restful.Request, resp *restful.Response) {
|
||||
objs, err := h.getPipelinesByRequest(req)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
// get all pipelines which come from ks
|
||||
pipelineList := &clientDevOps.PipelineList{
|
||||
Total: objs.TotalItems,
|
||||
Items: make([]clientDevOps.Pipeline, len(objs.Items)),
|
||||
}
|
||||
pipelineMap := make(map[string]int, pipelineList.Total)
|
||||
for i := range objs.Items {
|
||||
if pipeline, ok := objs.Items[i].(v1alpha3.Pipeline); !ok {
|
||||
continue
|
||||
} else {
|
||||
pipelineMap[pipeline.Name] = i
|
||||
pipelineList.Items[i] = clientDevOps.Pipeline{
|
||||
Name: pipeline.Name,
|
||||
Annotations: pipeline.Annotations,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get all pipelines which come from Jenkins
|
||||
// fill out the rest fields
|
||||
if query, err := jenkins.ParseJenkinsQuery(req.Request.URL.RawQuery); err == nil {
|
||||
query.Set("limit", "10000")
|
||||
query.Set("start", "0")
|
||||
req.Request.URL.RawQuery = query.Encode()
|
||||
}
|
||||
res, err := h.devopsOperator.ListPipelines(req.Request)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
} else {
|
||||
for i := range res.Items {
|
||||
if index, ok := pipelineMap[res.Items[i].Name]; ok {
|
||||
// keep annotations field of pipelineList
|
||||
annotations := pipelineList.Items[index].Annotations
|
||||
pipelineList.Items[index] = res.Items[i]
|
||||
pipelineList.Items[index].Annotations = annotations
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(pipelineList)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetPipelineRun(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.GetPipelineRun(projectName, pipelineName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) ListPipelineRuns(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
|
||||
res, err := h.devopsOperator.ListPipelineRuns(projectName, pipelineName, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) StopPipeline(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.StopPipeline(projectName, pipelineName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) ReplayPipeline(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.ReplayPipeline(projectName, pipelineName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) RunPipeline(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
|
||||
res, err := h.devopsOperator.RunPipeline(projectName, pipelineName, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetArtifacts(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.GetArtifacts(projectName, pipelineName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetRunLog(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.GetRunLog(projectName, pipelineName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Write(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetStepLog(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
runId := req.PathParameter("run")
|
||||
nodeId := req.PathParameter("node")
|
||||
stepId := req.PathParameter("step")
|
||||
|
||||
res, header, err := h.devopsOperator.GetStepLog(projectName, pipelineName, runId, nodeId, stepId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
for k, v := range header {
|
||||
if strings.HasPrefix(k, jenkinsHeaderPre) {
|
||||
resp.AddHeader(k, v[0])
|
||||
}
|
||||
}
|
||||
resp.Write(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetNodeSteps(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
runId := req.PathParameter("run")
|
||||
nodeId := req.PathParameter("node")
|
||||
|
||||
res, err := h.devopsOperator.GetNodeSteps(projectName, pipelineName, runId, nodeId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetPipelineRunNodes(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.GetPipelineRunNodes(projectName, pipelineName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
// there're two situation here:
|
||||
// 1. the particular submitters exist
|
||||
// the users who are the owner of this Pipeline or the submitter of this Pipeline, or has the auth to create a DevOps project
|
||||
// 2. no particular submitters
|
||||
// only the owner of this Pipeline can approve or reject it
|
||||
func (h *ProjectPipelineHandler) approvableCheck(nodes []clientDevOps.NodesDetail, pipe pipelineParam) {
|
||||
var userInfo user.Info
|
||||
var ok bool
|
||||
var isAdmin bool
|
||||
// check if current user belong to the admin group, grant it if it's true
|
||||
if userInfo, ok = request.UserFrom(pipe.Context); ok {
|
||||
createAuth := authorizer.AttributesRecord{
|
||||
User: userInfo,
|
||||
Verb: authorizer.VerbDelete,
|
||||
Workspace: pipe.Workspace,
|
||||
DevOps: pipe.ProjectName,
|
||||
Resource: "devopsprojects",
|
||||
ResourceRequest: true,
|
||||
ResourceScope: request.DevOpsScope,
|
||||
}
|
||||
|
||||
if decision, _, err := h.authorizer.Authorize(createAuth); err == nil {
|
||||
isAdmin = decision == authorizer.DecisionAllow
|
||||
} else {
|
||||
// this is an expected case, printing the debug info for troubleshooting
|
||||
klog.V(8).Infof("authorize failed with '%v', error is '%v'",
|
||||
createAuth, err)
|
||||
}
|
||||
} else {
|
||||
klog.V(6).Infof("cannot get the current user when checking the approvable with pipeline '%s/%s'",
|
||||
pipe.ProjectName, pipe.Name)
|
||||
return
|
||||
}
|
||||
|
||||
var createdByCurrentUser bool // indicate if the current user is the owner
|
||||
if pipeline, err := h.devopsOperator.GetPipelineObj(pipe.ProjectName, pipe.Name); err == nil {
|
||||
if creator, ok := pipeline.GetAnnotations()[constants.CreatorAnnotationKey]; ok {
|
||||
createdByCurrentUser = userInfo.GetName() == creator
|
||||
} else {
|
||||
klog.V(6).Infof("annotation '%s' is necessary but it is missing from '%s/%s'",
|
||||
constants.CreatorAnnotationKey, pipe.ProjectName, pipe.Name)
|
||||
}
|
||||
} else {
|
||||
klog.V(6).Infof("cannot find pipeline '%s/%s', error is '%v'", pipe.ProjectName, pipe.Name, err)
|
||||
return
|
||||
}
|
||||
|
||||
// check every input steps if it's approvable
|
||||
for i, node := range nodes {
|
||||
if node.State != clientDevOps.StatePaused {
|
||||
continue
|
||||
}
|
||||
|
||||
for j, step := range node.Steps {
|
||||
if step.State != clientDevOps.StatePaused || step.Input == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
nodes[i].Steps[j].Approvable = isAdmin || createdByCurrentUser || step.Input.Approvable(userInfo.GetName())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) createdBy(projectName string, pipelineName string, currentUserName string) bool {
|
||||
if pipeline, err := h.devopsOperator.GetPipelineObj(projectName, pipelineName); err == nil {
|
||||
if creator, ok := pipeline.Annotations[constants.CreatorAnnotationKey]; ok {
|
||||
return creator == currentUserName
|
||||
}
|
||||
} else {
|
||||
log.V(4).Infof("cannot get pipeline %s/%s, error %#v", projectName, pipelineName, err)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) hasSubmitPermission(req *restful.Request) (hasPermit bool, err error) {
|
||||
pipeParam := parsePipelineParam(req)
|
||||
httpReq := &http.Request{
|
||||
URL: req.Request.URL,
|
||||
Header: req.Request.Header,
|
||||
Form: req.Request.Form,
|
||||
PostForm: req.Request.PostForm,
|
||||
}
|
||||
|
||||
runId := req.PathParameter("run")
|
||||
nodeId := req.PathParameter("node")
|
||||
stepId := req.PathParameter("step")
|
||||
branchName := req.PathParameter("branch")
|
||||
|
||||
// check if current user can approve this input
|
||||
var res []clientDevOps.NodesDetail
|
||||
|
||||
if branchName == "" {
|
||||
res, err = h.devopsOperator.GetNodesDetail(pipeParam.ProjectName, pipeParam.Name, runId, httpReq)
|
||||
} else {
|
||||
res, err = h.devopsOperator.GetBranchNodesDetail(pipeParam.ProjectName, pipeParam.Name, branchName, runId, httpReq)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
h.approvableCheck(res, parsePipelineParam(req))
|
||||
|
||||
for _, node := range res {
|
||||
if node.ID != nodeId {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, step := range node.Steps {
|
||||
if step.ID != stepId || step.Input == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
hasPermit = step.Approvable
|
||||
break
|
||||
}
|
||||
break
|
||||
}
|
||||
} else {
|
||||
log.V(4).Infof("cannot get nodes detail, error: %v", err)
|
||||
err = errors.New("cannot get the submitters of current pipeline run")
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) SubmitInputStep(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
runId := req.PathParameter("run")
|
||||
nodeId := req.PathParameter("node")
|
||||
stepId := req.PathParameter("step")
|
||||
|
||||
var response []byte
|
||||
var err error
|
||||
var ok bool
|
||||
|
||||
if ok, err = h.hasSubmitPermission(req); !ok || err != nil {
|
||||
msg := map[string]string{
|
||||
"allow": "false",
|
||||
"message": fmt.Sprintf("%v", err),
|
||||
}
|
||||
|
||||
response, _ = json.Marshal(msg)
|
||||
} else {
|
||||
response, err = h.devopsOperator.SubmitInputStep(projectName, pipelineName, runId, nodeId, stepId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
}
|
||||
resp.Write(response)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetNodesDetail(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.GetNodesDetail(projectName, pipelineName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
h.approvableCheck(res, parsePipelineParam(req))
|
||||
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetBranchPipeline(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
|
||||
res, err := h.devopsOperator.GetBranchPipeline(projectName, pipelineName, branchName, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetBranchPipelineRun(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.GetBranchPipelineRun(projectName, pipelineName, branchName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) StopBranchPipeline(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.StopBranchPipeline(projectName, pipelineName, branchName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) ReplayBranchPipeline(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.ReplayBranchPipeline(projectName, pipelineName, branchName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) RunBranchPipeline(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
|
||||
res, err := h.devopsOperator.RunBranchPipeline(projectName, pipelineName, branchName, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetBranchArtifacts(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.GetBranchArtifacts(projectName, pipelineName, branchName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetBranchRunLog(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.GetBranchRunLog(projectName, pipelineName, branchName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Write(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetBranchStepLog(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
runId := req.PathParameter("run")
|
||||
nodeId := req.PathParameter("node")
|
||||
stepId := req.PathParameter("step")
|
||||
|
||||
res, header, err := h.devopsOperator.GetBranchStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request)
|
||||
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
for k, v := range header {
|
||||
if strings.HasPrefix(k, jenkinsHeaderPre) {
|
||||
resp.AddHeader(k, v[0])
|
||||
}
|
||||
}
|
||||
resp.Write(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetBranchNodeSteps(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
runId := req.PathParameter("run")
|
||||
nodeId := req.PathParameter("node")
|
||||
|
||||
res, err := h.devopsOperator.GetBranchNodeSteps(projectName, pipelineName, branchName, runId, nodeId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetBranchPipelineRunNodes(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.GetBranchPipelineRunNodes(projectName, pipelineName, branchName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) SubmitBranchInputStep(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
runId := req.PathParameter("run")
|
||||
nodeId := req.PathParameter("node")
|
||||
stepId := req.PathParameter("step")
|
||||
|
||||
var response []byte
|
||||
var err error
|
||||
var ok bool
|
||||
|
||||
if ok, err = h.hasSubmitPermission(req); !ok || err != nil {
|
||||
msg := map[string]string{
|
||||
"allow": "false",
|
||||
"message": fmt.Sprintf("%v", err),
|
||||
}
|
||||
|
||||
response, _ = json.Marshal(msg)
|
||||
} else {
|
||||
response, err = h.devopsOperator.SubmitBranchInputStep(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
resp.Write(response)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetBranchNodesDetail(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
branchName := req.PathParameter("branch")
|
||||
runId := req.PathParameter("run")
|
||||
|
||||
res, err := h.devopsOperator.GetBranchNodesDetail(projectName, pipelineName, branchName, runId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
h.approvableCheck(res, parsePipelineParam(req))
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func parsePipelineParam(req *restful.Request) pipelineParam {
|
||||
return pipelineParam{
|
||||
Workspace: req.PathParameter("workspace"),
|
||||
ProjectName: req.PathParameter("devops"),
|
||||
Name: req.PathParameter("pipeline"),
|
||||
Context: req.Request.Context(),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetPipelineBranch(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
|
||||
res, err := h.devopsOperator.GetPipelineBranch(projectName, pipelineName, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) ScanBranch(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
|
||||
res, err := h.devopsOperator.ScanBranch(projectName, pipelineName, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Write(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetConsoleLog(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
|
||||
res, err := h.devopsOperator.GetConsoleLog(projectName, pipelineName, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Write(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetCrumb(req *restful.Request, resp *restful.Response) {
|
||||
res, err := h.devopsOperator.GetCrumb(req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetSCMServers(req *restful.Request, resp *restful.Response) {
|
||||
scmId := req.PathParameter("scm")
|
||||
|
||||
res, err := h.devopsOperator.GetSCMServers(scmId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetSCMOrg(req *restful.Request, resp *restful.Response) {
|
||||
scmId := req.PathParameter("scm")
|
||||
|
||||
res, err := h.devopsOperator.GetSCMOrg(scmId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetOrgRepo(req *restful.Request, resp *restful.Response) {
|
||||
scmId := req.PathParameter("scm")
|
||||
organizationId := req.PathParameter("organization")
|
||||
|
||||
res, err := h.devopsOperator.GetOrgRepo(scmId, organizationId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) CreateSCMServers(req *restful.Request, resp *restful.Response) {
|
||||
scmId := req.PathParameter("scm")
|
||||
|
||||
res, err := h.devopsOperator.CreateSCMServers(scmId, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) Validate(req *restful.Request, resp *restful.Response) {
|
||||
scmId := req.PathParameter("scm")
|
||||
|
||||
res, err := h.devopsOperator.Validate(scmId, req.Request)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
if jErr, ok := err.(*devops.JkError); ok {
|
||||
if jErr.Code != http.StatusUnauthorized {
|
||||
resp.WriteError(jErr.Code, err)
|
||||
} else {
|
||||
resp.WriteHeader(http.StatusPreconditionRequired)
|
||||
}
|
||||
} else {
|
||||
resp.WriteError(http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetNotifyCommit(req *restful.Request, resp *restful.Response) {
|
||||
res, err := h.devopsOperator.GetNotifyCommit(req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Write(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) PostNotifyCommit(req *restful.Request, resp *restful.Response) {
|
||||
res, err := h.devopsOperator.GetNotifyCommit(req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Write(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GithubWebhook(req *restful.Request, resp *restful.Response) {
|
||||
res, err := h.devopsOperator.GithubWebhook(req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Write(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) CheckScriptCompile(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
pipelineName := req.PathParameter("pipeline")
|
||||
|
||||
resBody, err := h.devopsOperator.CheckScriptCompile(projectName, pipelineName, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteAsJson(resBody)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) CheckCron(req *restful.Request, resp *restful.Response) {
|
||||
projectName := req.PathParameter("devops")
|
||||
|
||||
res, err := h.devopsOperator.CheckCron(projectName, req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) ToJenkinsfile(req *restful.Request, resp *restful.Response) {
|
||||
res, err := h.devopsOperator.ToJenkinsfile(req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) ToJson(req *restful.Request, resp *restful.Response) {
|
||||
res, err := h.devopsOperator.ToJson(req.Request)
|
||||
if err != nil {
|
||||
parseErr(err, resp)
|
||||
return
|
||||
}
|
||||
resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) GetProjectCredentialUsage(req *restful.Request, resp *restful.Response) {
|
||||
projectId := req.PathParameter("devops")
|
||||
credentialId := req.PathParameter("credential")
|
||||
response, err := h.projectCredentialGetter.GetProjectCredentialUsage(projectId, credentialId)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
resp.WriteAsJson(response)
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func parseErr(err error, resp *restful.Response) {
|
||||
log.Error(err)
|
||||
if jErr, ok := err.(*devops.JkError); ok {
|
||||
resp.WriteError(jErr.Code, err)
|
||||
} else {
|
||||
resp.WriteError(http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package v1alpha2
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"kubesphere.io/api/devops/v1alpha3"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/models/devops"
|
||||
)
|
||||
|
||||
func TestParseNameFilterFromQuery(t *testing.T) {
|
||||
table := []struct {
|
||||
query string
|
||||
pipeline *v1alpha3.Pipeline
|
||||
expectFilter devops.PipelineFilter
|
||||
expectNamespace string
|
||||
message string
|
||||
}{{
|
||||
query: "type:pipeline;organization:jenkins;pipeline:serverjkq4c/*",
|
||||
pipeline: &v1alpha3.Pipeline{},
|
||||
expectFilter: nil,
|
||||
expectNamespace: "serverjkq4c",
|
||||
message: "query all pipelines with filter *",
|
||||
}, {
|
||||
query: "type:pipeline;organization:jenkins;pipeline:cccc/*abc*",
|
||||
pipeline: &v1alpha3.Pipeline{},
|
||||
expectFilter: func(pipeline *v1alpha3.Pipeline) bool {
|
||||
return strings.Contains(pipeline.Name, "abc")
|
||||
},
|
||||
expectNamespace: "cccc",
|
||||
message: "query all pipelines with filter abc",
|
||||
}, {
|
||||
query: "type:pipeline;organization:jenkins;pipeline:pai-serverjkq4c/*",
|
||||
pipeline: &v1alpha3.Pipeline{},
|
||||
expectFilter: nil,
|
||||
expectNamespace: "pai-serverjkq4c",
|
||||
message: "query all pipelines with filter *",
|
||||
}, {
|
||||
query: "type:pipeline;organization:jenkins;pipeline:defdef",
|
||||
pipeline: &v1alpha3.Pipeline{},
|
||||
expectFilter: nil,
|
||||
expectNamespace: "defdef",
|
||||
message: "query all pipelines with filter *",
|
||||
}}
|
||||
|
||||
for i, item := range table {
|
||||
filter, ns := parseNameFilterFromQuery(item.query)
|
||||
if item.expectFilter == nil {
|
||||
if filter != nil {
|
||||
t.Fatalf("invalid filter, index: %d, message: %s", i, item.message)
|
||||
}
|
||||
} else {
|
||||
if filter == nil || filter(item.pipeline) != item.expectFilter(item.pipeline) {
|
||||
t.Fatalf("invalid filter, index: %d, message: %s", i, item.message)
|
||||
}
|
||||
}
|
||||
if ns != item.expectNamespace {
|
||||
t.Fatalf("invalid namespace, index: %d, message: %s", i, item.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
Copyright 2020 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 v1alpha2
|
||||
|
||||
import (
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/authorization/authorizer"
|
||||
"kubesphere.io/kubesphere/pkg/client/clientset/versioned"
|
||||
"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
|
||||
"kubesphere.io/kubesphere/pkg/models/devops"
|
||||
devopsClient "kubesphere.io/kubesphere/pkg/simple/client/devops"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/s3"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
|
||||
)
|
||||
|
||||
type ProjectPipelineHandler struct {
|
||||
devopsOperator devops.DevopsOperator
|
||||
projectCredentialGetter devops.ProjectCredentialGetter
|
||||
authorizer authorizer.Authorizer
|
||||
}
|
||||
|
||||
type PipelineSonarHandler struct {
|
||||
pipelineSonarGetter devops.PipelineSonarGetter
|
||||
}
|
||||
|
||||
func NewProjectPipelineHandler(devopsClient devopsClient.Interface, ksInformers externalversions.SharedInformerFactory,
|
||||
authorizer authorizer.Authorizer) ProjectPipelineHandler {
|
||||
return ProjectPipelineHandler{
|
||||
devopsOperator: devops.NewDevopsOperator(devopsClient, nil, nil, ksInformers, nil),
|
||||
projectCredentialGetter: devops.NewProjectCredentialOperator(devopsClient),
|
||||
authorizer: authorizer,
|
||||
}
|
||||
}
|
||||
|
||||
func NewPipelineSonarHandler(devopsClient devopsClient.Interface, sonarClient sonarqube.SonarInterface) PipelineSonarHandler {
|
||||
return PipelineSonarHandler{
|
||||
pipelineSonarGetter: devops.NewPipelineSonarGetter(devopsClient, sonarClient),
|
||||
}
|
||||
}
|
||||
|
||||
func NewS2iBinaryHandler(client versioned.Interface, informers externalversions.SharedInformerFactory, s3Client s3.Interface) S2iBinaryHandler {
|
||||
return S2iBinaryHandler{devops.NewS2iBinaryUploader(client, informers, s3Client)}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
Copyright 2020 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 v1alpha2
|
||||
|
||||
import (
|
||||
"github.com/emicklei/go-restful"
|
||||
"k8s.io/klog"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
)
|
||||
|
||||
func (h PipelineSonarHandler) GetPipelineSonarStatusHandler(request *restful.Request, resp *restful.Response) {
|
||||
projectId := request.PathParameter("devops")
|
||||
pipelineId := request.PathParameter("pipeline")
|
||||
sonarStatus, err := h.pipelineSonarGetter.GetPipelineSonar(projectId, pipelineId)
|
||||
if err != nil {
|
||||
klog.Errorf("%+v", err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
resp.WriteAsJson(sonarStatus)
|
||||
}
|
||||
|
||||
func (h PipelineSonarHandler) GetMultiBranchesPipelineSonarStatusHandler(request *restful.Request, resp *restful.Response) {
|
||||
projectId := request.PathParameter("devops")
|
||||
pipelineId := request.PathParameter("pipeline")
|
||||
branchId := request.PathParameter("branch")
|
||||
sonarStatus, err := h.pipelineSonarGetter.GetMultiBranchPipelineSonar(projectId, pipelineId, branchId)
|
||||
if err != nil {
|
||||
klog.Errorf("%+v", err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
resp.WriteAsJson(sonarStatus)
|
||||
}
|
||||
@@ -17,745 +17,23 @@ limitations under the License.
|
||||
package v1alpha2
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
restfulspec "github.com/emicklei/go-restful-openapi"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/proxy"
|
||||
"k8s.io/klog"
|
||||
|
||||
devopsv1alpha1 "kubesphere.io/api/devops/v1alpha1"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/authorization/authorizer"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
|
||||
"kubesphere.io/kubesphere/pkg/client/clientset/versioned"
|
||||
"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/s3"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
|
||||
|
||||
"net/http"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/devops"
|
||||
"kubesphere.io/kubesphere/pkg/kapis/generic"
|
||||
)
|
||||
|
||||
const (
|
||||
GroupName = "devops.kubesphere.io"
|
||||
RespOK = "ok"
|
||||
)
|
||||
|
||||
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha2"}
|
||||
|
||||
func AddToContainer(container *restful.Container, ksInformers externalversions.SharedInformerFactory, devopsClient devops.Interface,
|
||||
sonarqubeClient sonarqube.SonarInterface, ksClient versioned.Interface, s3Client s3.Interface, endpoint string,
|
||||
authorizer authorizer.Authorizer) error {
|
||||
ws := runtime.NewWebService(GroupVersion)
|
||||
|
||||
err := AddPipelineToWebService(ws, devopsClient, ksInformers, authorizer)
|
||||
func AddToContainer(container *restful.Container, endpoint string) error {
|
||||
proxy, err := generic.NewGenericProxy(endpoint, GroupVersion.Group, GroupVersion.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = AddSonarToWebService(ws, devopsClient, sonarqubeClient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = AddS2IToWebService(ws, ksClient, ksInformers, s3Client)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = AddJenkinsToContainer(ws, devopsClient, endpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
container.Add(ws)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddPipelineToWebService(webservice *restful.WebService, devopsClient devops.Interface, ksInformers externalversions.SharedInformerFactory,
|
||||
authorizer authorizer.Authorizer) error {
|
||||
|
||||
projectPipelineEnable := devopsClient != nil
|
||||
|
||||
if projectPipelineEnable {
|
||||
projectPipelineHandler := NewProjectPipelineHandler(devopsClient, ksInformers, authorizer)
|
||||
|
||||
webservice.Route(webservice.GET("/devops/{devops}/credentials/{credential}/usage").
|
||||
To(projectPipelineHandler.GetProjectCredentialUsage).
|
||||
Doc("Get the specified credential usage of the DevOps project").
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsCredentialTag}).
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("credential", "credential's ID, e.g. dockerhub-id")).
|
||||
Returns(http.StatusOK, RespOK, devops.Credential{}))
|
||||
|
||||
// match Jenkins api "/blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}"
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}").
|
||||
To(projectPipelineHandler.GetPipeline).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get the specified pipeline of the DevOps project").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Returns(http.StatusOK, RespOK, devops.Pipeline{}).
|
||||
Writes(devops.Pipeline{}))
|
||||
|
||||
// match Jenkins api: "jenkins_api/blue/rest/search"
|
||||
webservice.Route(webservice.GET("/search").
|
||||
To(projectPipelineHandler.ListPipelines).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Search DevOps resource. More info: https://github.com/jenkinsci/blueocean-plugin/tree/master/blueocean-rest#get-pipelines-across-organization").
|
||||
Param(webservice.QueryParameter("q", "query pipelines, condition for filtering.").
|
||||
Required(true).
|
||||
DataFormat("q=%s")).
|
||||
Param(webservice.QueryParameter("filter", "Filter some types of jobs. e.g. no-folder,will not get a job of type folder").
|
||||
Required(false).
|
||||
DataFormat("filter=%s")).
|
||||
Param(webservice.QueryParameter("start", "the item number that the search starts from.").
|
||||
Required(false).
|
||||
DataFormat("start=%d")).
|
||||
Param(webservice.QueryParameter("limit", "the limit item count of the search.").
|
||||
Required(false).
|
||||
DataFormat("limit=%d")).
|
||||
Returns(http.StatusOK, RespOK, devops.PipelineList{}).
|
||||
Writes(devops.PipelineList{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/runs/{run}/
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/runs/{run}").
|
||||
To(projectPipelineHandler.GetPipelineRun).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get details in the specified pipeline activity.").
|
||||
Param(webservice.PathParameter("devops", "the name of devops project")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Returns(http.StatusOK, RespOK, devops.PipelineRun{}).
|
||||
Writes(devops.PipelineRun{}))
|
||||
|
||||
// match Jenkins api "/blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/runs/"
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/runs").
|
||||
To(projectPipelineHandler.ListPipelineRuns).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get all runs of the specified pipeline").
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.QueryParameter("start", "the item number that the search starts from").
|
||||
Required(false).
|
||||
DataFormat("start=%d")).
|
||||
Param(webservice.QueryParameter("limit", "the limit item count of the search").
|
||||
Required(false).
|
||||
DataFormat("limit=%d")).
|
||||
Param(webservice.QueryParameter("branch", "the name of branch, same as repository branch, will be filtered by branch.").
|
||||
Required(false).
|
||||
DataFormat("branch=%s")).
|
||||
Returns(http.StatusOK, RespOK, devops.PipelineRunList{}).
|
||||
Writes(devops.PipelineRunList{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/pipelines/{pipeline}/runs/{run}/stop/
|
||||
webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/runs/{run}/stop").
|
||||
To(projectPipelineHandler.StopPipeline).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Stop pipeline").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Param(webservice.QueryParameter("blocking", "stop and between each retries will sleep.").
|
||||
Required(false).
|
||||
DataFormat("blocking=%t").
|
||||
DefaultValue("blocking=false")).
|
||||
Param(webservice.QueryParameter("timeOutInSecs", "the time of stop and between each retries sleep.").
|
||||
Required(false).
|
||||
DataFormat("timeOutInSecs=%d").
|
||||
DefaultValue("timeOutInSecs=10")).
|
||||
Returns(http.StatusOK, RespOK, devops.StopPipeline{}).
|
||||
Writes(devops.StopPipeline{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/pipelines/{pipeline}/runs/{run}/Replay/
|
||||
webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/runs/{run}/replay").
|
||||
To(projectPipelineHandler.ReplayPipeline).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Replay pipeline").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Returns(http.StatusOK, RespOK, devops.ReplayPipeline{}).
|
||||
Writes(devops.ReplayPipeline{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/runs/
|
||||
webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/runs").
|
||||
To(projectPipelineHandler.RunPipeline).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Run pipeline.").
|
||||
Reads(devops.RunPayload{}).
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Returns(http.StatusOK, RespOK, devops.RunPipeline{}).
|
||||
Writes(devops.RunPipeline{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/runs/{run}/artifacts
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/runs/{run}/artifacts").
|
||||
To(projectPipelineHandler.GetArtifacts).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get all artifacts in the specified pipeline.").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Param(webservice.QueryParameter("start", "the item number that the search starts from.").
|
||||
Required(false).
|
||||
DataFormat("start=%d")).
|
||||
Param(webservice.QueryParameter("limit", "the limit item count of the search.").
|
||||
Required(false).
|
||||
DataFormat("limit=%d")).
|
||||
Returns(http.StatusOK, "The filed of \"Url\" in response can download artifacts", []devops.Artifacts{}).
|
||||
Writes([]devops.Artifacts{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/runs/{run}/log/?start=0
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/runs/{run}/log").
|
||||
To(projectPipelineHandler.GetRunLog).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get run logs of the specified pipeline activity.").
|
||||
Produces("text/plain; charset=utf-8").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Param(webservice.QueryParameter("start", "the item number that the search starts from.").
|
||||
Required(false).
|
||||
DataFormat("start=%d").
|
||||
DefaultValue("start=0")))
|
||||
|
||||
// match "/blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/runs/{run}/nodes/{node}/steps/{step}/log/?start=0"
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/runs/{run}/nodes/{node}/steps/{step}/log").
|
||||
To(projectPipelineHandler.GetStepLog).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get pipelines step log.").
|
||||
Produces("text/plain; charset=utf-8").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Param(webservice.PathParameter("node", "pipeline node ID, the stage in pipeline.")).
|
||||
Param(webservice.PathParameter("step", "pipeline step ID, the step in pipeline.")).
|
||||
Param(webservice.QueryParameter("start", "the item number that the search starts from.").
|
||||
Required(false).
|
||||
DataFormat("start=%d").
|
||||
DefaultValue("start=0")))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/nodes/%s/steps/?limit=
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/runs/{run}/nodes/{node}/steps").
|
||||
To(projectPipelineHandler.GetNodeSteps).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get all steps in the specified node.").
|
||||
Param(webservice.PathParameter("devops", "the name of devops project")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build")).
|
||||
Param(webservice.PathParameter("node", "pipeline node ID, the stage in pipeline.")).
|
||||
Returns(http.StatusOK, RespOK, []devops.NodeSteps{}).
|
||||
Writes([]devops.NodeSteps{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/pipelines/{pipeline}/runs/{run}/nodes/?limit=10000
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/runs/{run}/nodes").
|
||||
To(projectPipelineHandler.GetPipelineRunNodes).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get all nodes in the specified activity. node is the stage in the pipeline task").
|
||||
Param(webservice.PathParameter("devops", "the name of devops project")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build")).
|
||||
Returns(http.StatusOK, RespOK, []devops.PipelineRunNodes{}).
|
||||
Writes([]devops.PipelineRunNodes{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/pipelines/{pipeline}/runs/{run}/nodes/{node}/steps/{step}
|
||||
webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/runs/{run}/nodes/{node}/steps/{step}").
|
||||
To(projectPipelineHandler.SubmitInputStep).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Proceed or Break the paused pipeline which is waiting for user input.").
|
||||
Reads(devops.CheckPlayload{}).
|
||||
Produces("text/plain; charset=utf-8").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Param(webservice.PathParameter("node", "pipeline node ID, the stage in pipeline.")).
|
||||
Param(webservice.PathParameter("step", "pipeline step ID")))
|
||||
|
||||
// out of scm get all steps in nodes.
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/runs/{run}/nodesdetail").
|
||||
To(projectPipelineHandler.GetNodesDetail).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get steps details inside a activity node. For a node, the steps which defined inside the node.").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Returns(http.StatusOK, RespOK, []devops.NodesDetail{}).
|
||||
Writes(devops.NodesDetail{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/pipelines/{pipeline}/branches/{branch}
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches/{branch}").
|
||||
To(projectPipelineHandler.GetBranchPipeline).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Get the specified branch pipeline of the DevOps project").
|
||||
Param(webservice.PathParameter("devops", "the name of devops project")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch")).
|
||||
Returns(http.StatusOK, RespOK, devops.BranchPipeline{}).
|
||||
Writes(devops.BranchPipeline{}))
|
||||
|
||||
// match Jenkins api "/blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/branches/{branch}/runs/{run}/"
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}").
|
||||
To(projectPipelineHandler.GetBranchPipelineRun).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Get details in the specified pipeline activity.").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Param(webservice.PathParameter("run", "pipeline run id, the unique id for a pipeline once build.")).
|
||||
Returns(http.StatusOK, RespOK, devops.PipelineRun{}).
|
||||
Writes(devops.PipelineRun{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/stop/
|
||||
webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/stop").
|
||||
To(projectPipelineHandler.StopBranchPipeline).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Stop the specified pipeline of the DevOps project.").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Param(webservice.QueryParameter("blocking", "stop and between each retries will sleep.").
|
||||
Required(false).
|
||||
DataFormat("blocking=%t").
|
||||
DefaultValue("blocking=false")).
|
||||
Param(webservice.QueryParameter("timeOutInSecs", "the time of stop and between each retries sleep.").
|
||||
Required(false).
|
||||
DataFormat("timeOutInSecs=%d").
|
||||
DefaultValue("timeOutInSecs=10")).
|
||||
Returns(http.StatusOK, RespOK, devops.StopPipeline{}).
|
||||
Writes(devops.StopPipeline{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/Replay/
|
||||
webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/replay").
|
||||
To(projectPipelineHandler.ReplayBranchPipeline).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Replay the specified pipeline of the DevOps project").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Returns(http.StatusOK, RespOK, devops.ReplayPipeline{}).
|
||||
Writes(devops.ReplayPipeline{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/branches/{}/runs/
|
||||
webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs").
|
||||
To(projectPipelineHandler.RunBranchPipeline).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Run the specified pipeline of the DevOps project.").
|
||||
Reads(devops.RunPayload{}).
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Returns(http.StatusOK, RespOK, devops.RunPipeline{}).
|
||||
Writes(devops.RunPipeline{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/branches/{branch}/runs/{run}/artifacts
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/artifacts").
|
||||
To(projectPipelineHandler.GetBranchArtifacts).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Get all artifacts generated from the specified run of the pipeline branch.").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Param(webservice.QueryParameter("start", "the item number that the search starts from.").
|
||||
Required(false).
|
||||
DataFormat("start=%d")).
|
||||
Param(webservice.QueryParameter("limit", "the limit item count of the search.").
|
||||
Required(false).
|
||||
DataFormat("limit=%d")).
|
||||
Returns(http.StatusOK, "The filed of \"Url\" in response can download artifacts", []devops.Artifacts{}).
|
||||
Writes([]devops.Artifacts{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/branches/{branch}/runs/{run}/log/?start=0
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/log").
|
||||
To(projectPipelineHandler.GetBranchRunLog).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Get run logs of the specified pipeline activity.").
|
||||
Produces("text/plain; charset=utf-8").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Param(webservice.QueryParameter("start", "the item number that the search starts from.").
|
||||
Required(false).
|
||||
DataFormat("start=%d").
|
||||
DefaultValue("start=0")))
|
||||
|
||||
// match "/blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/branches/{branch}/runs/{run}/nodes/{node}/steps/{step}/log/?start=0"
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/nodes/{node}/steps/{step}/log").
|
||||
To(projectPipelineHandler.GetBranchStepLog).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Get the step logs in the specified pipeline activity.").
|
||||
Produces("text/plain; charset=utf-8").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Param(webservice.PathParameter("run", "pipeline run id, the unique id for a pipeline once build.")).
|
||||
Param(webservice.PathParameter("node", "pipeline node id, the stage in pipeline.")).
|
||||
Param(webservice.PathParameter("step", "pipeline step id, the step in pipeline.")).
|
||||
Param(webservice.QueryParameter("start", "the item number that the search starts from.").
|
||||
Required(false).
|
||||
DataFormat("start=%d").
|
||||
DefaultValue("start=0")))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/?limit=
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/nodes/{node}/steps").
|
||||
To(projectPipelineHandler.GetBranchNodeSteps).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Get all steps in the specified node.").
|
||||
Param(webservice.PathParameter("devops", "the name of devops project")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Param(webservice.PathParameter("node", "pipeline node ID, the stage in pipeline.")).
|
||||
Returns(http.StatusOK, RespOK, []devops.NodeSteps{}).
|
||||
Writes([]devops.NodeSteps{}))
|
||||
|
||||
// match Jenkins api "/blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/branches/{branch}/runs/{run}/nodes"
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/nodes").
|
||||
To(projectPipelineHandler.GetBranchPipelineRunNodes).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Get run nodes.").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Param(webservice.PathParameter("run", "pipeline run id, the unique id for a pipeline once build.")).
|
||||
Param(webservice.QueryParameter("limit", "the limit item count of the search.").
|
||||
Required(false).
|
||||
DataFormat("limit=%d").
|
||||
DefaultValue("limit=10000")).
|
||||
Returns(http.StatusOK, RespOK, []devops.BranchPipelineRunNodes{}).
|
||||
Writes([]devops.BranchPipelineRunNodes{}))
|
||||
|
||||
// /blue/rest/organizations/jenkins/pipelines/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/nodes/{node}/steps/{step}
|
||||
webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/nodes/{node}/steps/{step}").
|
||||
To(projectPipelineHandler.SubmitBranchInputStep).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Proceed or Break the paused pipeline which waiting for user input.").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Param(webservice.PathParameter("node", "pipeline node ID, the stage in pipeline.")).
|
||||
Param(webservice.PathParameter("step", "pipeline step ID, the step in pipeline.")).
|
||||
Reads(devops.CheckPlayload{}).
|
||||
Produces("text/plain; charset=utf-8"))
|
||||
|
||||
// in scm get all steps in nodes.
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/nodesdetail").
|
||||
To(projectPipelineHandler.GetBranchNodesDetail).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Get steps details in an activity node. For a node, the steps which is defined inside the node.").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.PathParameter("branch", "the name of branch, same as repository branch.")).
|
||||
Param(webservice.PathParameter("run", "pipeline run ID, the unique ID for a pipeline once build.")).
|
||||
Returns(http.StatusOK, RespOK, []devops.NodesDetail{}).
|
||||
Writes(devops.NodesDetail{}))
|
||||
|
||||
// match /blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/branches/?filter=&start&limit=
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches").
|
||||
To(projectPipelineHandler.GetPipelineBranch).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("(MultiBranchesPipeline) Get all branches in the specified pipeline.").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.QueryParameter("filter", "filter remote scm. e.g. origin").
|
||||
Required(false).
|
||||
DataFormat("filter=%s")).
|
||||
Param(webservice.QueryParameter("start", "the count of branches start.").
|
||||
Required(false).
|
||||
DataFormat("start=%d").DefaultValue("start=0")).
|
||||
Param(webservice.QueryParameter("limit", "the count of branches limit.").
|
||||
Required(false).
|
||||
DataFormat("limit=%d").DefaultValue("limit=100")).
|
||||
Returns(http.StatusOK, RespOK, []devops.PipelineBranch{}).
|
||||
Writes([]devops.PipelineBranch{}))
|
||||
|
||||
// match /job/{devops}/job/{pipeline}/build?delay=0
|
||||
webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/scan").
|
||||
To(projectPipelineHandler.ScanBranch).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Scan remote Repository, Start a build if have new branch.").
|
||||
Produces("text/html; charset=utf-8").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")).
|
||||
Param(webservice.QueryParameter("delay", "the delay time to scan").Required(false).DataFormat("delay=%d")))
|
||||
|
||||
// match /job/project-8QnvykoJw4wZ/job/test-1/indexing/consoleText
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/consolelog").
|
||||
To(projectPipelineHandler.GetConsoleLog).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get scan reponsitory logs in the specified pipeline.").
|
||||
Produces("text/plain; charset=utf-8").
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline")))
|
||||
|
||||
// match /crumbIssuer/api/json/
|
||||
webservice.Route(webservice.GET("/crumbissuer").
|
||||
To(projectPipelineHandler.GetCrumb).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Doc("Get crumb issuer. A CrumbIssuer represents an algorithm to generate a nonce value, known as a crumb, to counter cross site request forgery exploits. Crumbs are typically hashes incorporating information that uniquely identifies an agent that sends a request, along with a guarded secret so that the crumb value cannot be forged by a third party.").
|
||||
Returns(http.StatusOK, RespOK, devops.Crumb{}).
|
||||
Writes(devops.Crumb{}))
|
||||
|
||||
// match "/blue/rest/organizations/jenkins/scm/%s/servers/"
|
||||
webservice.Route(webservice.GET("/scms/{scm}/servers").
|
||||
To(projectPipelineHandler.GetSCMServers).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsScmTag}).
|
||||
Doc("List all servers in the jenkins.").
|
||||
Param(webservice.PathParameter("scm", "The ID of the source configuration management (SCM).")).
|
||||
Returns(http.StatusOK, RespOK, []devops.SCMServer{}).
|
||||
Writes([]devops.SCMServer{}))
|
||||
|
||||
// match "/blue/rest/organizations/jenkins/scm/{scm}/organizations/?credentialId=github"
|
||||
webservice.Route(webservice.GET("/scms/{scm}/organizations").
|
||||
To(projectPipelineHandler.GetSCMOrg).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsScmTag}).
|
||||
Doc("List all organizations of the specified source configuration management (SCM) such as Github.").
|
||||
Param(webservice.PathParameter("scm", "the ID of the source configuration management (SCM).")).
|
||||
Param(webservice.QueryParameter("credentialId", "credential ID for source configuration management (SCM).").Required(true).DataFormat("credentialId=%s")).
|
||||
Returns(http.StatusOK, RespOK, []devops.SCMOrg{}).
|
||||
Writes([]devops.SCMOrg{}))
|
||||
|
||||
// match "/blue/rest/organizations/jenkins/scm/{scm}/organizations/{organization}/repositories/?credentialId=&pageNumber&pageSize="
|
||||
webservice.Route(webservice.GET("/scms/{scm}/organizations/{organization}/repositories").
|
||||
To(projectPipelineHandler.GetOrgRepo).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsScmTag}).
|
||||
Doc("List all repositories in the specified organization.").
|
||||
Param(webservice.PathParameter("scm", "The ID of the source configuration management (SCM).")).
|
||||
Param(webservice.PathParameter("organization", "organization ID, such as github username.")).
|
||||
Param(webservice.QueryParameter("credentialId", "credential ID for SCM.").Required(true).DataFormat("credentialId=%s")).
|
||||
Param(webservice.QueryParameter("pageNumber", "page number.").Required(true).DataFormat("pageNumber=%d")).
|
||||
Param(webservice.QueryParameter("pageSize", "the item count of one page.").Required(true).DataFormat("pageSize=%d")).
|
||||
Returns(http.StatusOK, RespOK, devops.OrgRepo{}).
|
||||
Writes(devops.OrgRepo{}))
|
||||
|
||||
// match "/blue/rest/organizations/jenkins/scm/%s/servers/" create bitbucket server
|
||||
webservice.Route(webservice.POST("/scms/{scm}/servers").
|
||||
To(projectPipelineHandler.CreateSCMServers).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsScmTag}).
|
||||
Doc("Create scm server if it does not exist in the Jenkins.").
|
||||
Param(webservice.PathParameter("scm", "The ID of the source configuration management (SCM).")).
|
||||
Reads(devops.CreateScmServerReq{}).
|
||||
Returns(http.StatusOK, RespOK, devops.SCMServer{}).
|
||||
Writes(devops.SCMServer{}))
|
||||
|
||||
// match "/blue/rest/organizations/jenkins/scm/github/validate/"
|
||||
webservice.Route(webservice.POST("/scms/{scm}/verify").
|
||||
To(projectPipelineHandler.Validate).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsScmTag}).
|
||||
Doc("Validate the access token of the specified source configuration management (SCM) such as Github").
|
||||
Param(webservice.PathParameter("scm", "the ID of the source configuration management (SCM).")).
|
||||
Returns(http.StatusOK, RespOK, devops.Validates{}).
|
||||
Writes(devops.Validates{}))
|
||||
|
||||
// match /git/notifyCommit/?url=
|
||||
webservice.Route(webservice.GET("/webhook/git").
|
||||
To(projectPipelineHandler.GetNotifyCommit).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsWebhookTag}).
|
||||
Doc("Get commit notification by HTTP GET method. Git webhook will request here.").
|
||||
Produces("text/plain; charset=utf-8").
|
||||
Param(webservice.QueryParameter("url", "Git url").Required(true).DataFormat("url=%s")))
|
||||
|
||||
// Gitlab or some other scm managers can only use HTTP method. match /git/notifyCommit/?url=
|
||||
webservice.Route(webservice.POST("/webhook/git").
|
||||
To(projectPipelineHandler.PostNotifyCommit).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsWebhookTag}).
|
||||
Doc("Get commit notification by HTTP POST method. Git webhook will request here.").
|
||||
Consumes("application/json").
|
||||
Produces("text/plain; charset=utf-8").
|
||||
Param(webservice.QueryParameter("url", "Git url").Required(true).DataFormat("url=%s")))
|
||||
|
||||
webservice.Route(webservice.POST("/webhook/github").
|
||||
To(projectPipelineHandler.GithubWebhook).
|
||||
Consumes("application/x-www-form-urlencoded", "application/json").
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsWebhookTag}).
|
||||
Doc("Get commit notification. Github webhook will request here."))
|
||||
|
||||
webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/checkScriptCompile").
|
||||
To(projectPipelineHandler.CheckScriptCompile).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of the CI/CD pipeline").DataFormat("pipeline=%s")).
|
||||
Consumes("application/x-www-form-urlencoded", "charset=utf-8").
|
||||
Produces("application/json", "charset=utf-8").
|
||||
Doc("Check pipeline script compile.").
|
||||
Reads(devops.ReqScript{}).
|
||||
Returns(http.StatusOK, RespOK, devops.CheckScript{}).
|
||||
Writes(devops.CheckScript{}))
|
||||
|
||||
webservice.Route(webservice.POST("/devops/{devops}/checkCron").
|
||||
To(projectPipelineHandler.CheckCron).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Produces("application/json", "charset=utf-8").
|
||||
Doc("Check cron script compile.").
|
||||
Reads(devops.CronData{}).
|
||||
Returns(http.StatusOK, RespOK, devops.CheckCronRes{}).
|
||||
Writes(devops.CheckCronRes{}))
|
||||
|
||||
// match /pipeline-model-converter/toJenkinsfile
|
||||
webservice.Route(webservice.POST("/tojenkinsfile").
|
||||
To(projectPipelineHandler.ToJenkinsfile).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsJenkinsfileTag}).
|
||||
Consumes("application/x-www-form-urlencoded").
|
||||
Produces("application/json", "charset=utf-8").
|
||||
Doc("Convert json to jenkinsfile format.").
|
||||
Reads(devops.ReqJson{}).
|
||||
Returns(http.StatusOK, RespOK, devops.ResJenkinsfile{}).
|
||||
Writes(devops.ResJenkinsfile{}))
|
||||
|
||||
// match /pipeline-model-converter/toJson
|
||||
/*
|
||||
* Considering the following reasons, we use a generic data struct here.
|
||||
* - A fixed go struct might need to change again once Jenkins has new features.
|
||||
* - No refer requirement for the specific data struct
|
||||
* Please read the official document if you want to know more details
|
||||
* https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/fc8d22192d7d3a17badc3b8af7191a84bb7fd4ca/EXTENDING.md#conversion-to-json-representation-from-jenkinsfile
|
||||
*/
|
||||
webservice.Route(webservice.POST("/tojson").
|
||||
To(projectPipelineHandler.ToJson).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsJenkinsfileTag}).
|
||||
Consumes("application/x-www-form-urlencoded").
|
||||
Produces("application/json", "charset=utf-8").
|
||||
Doc("Convert jenkinsfile to json format. Usually the frontend uses json to show or edit pipeline").
|
||||
Reads(devops.ReqJenkinsfile{}).
|
||||
Returns(http.StatusOK, RespOK, map[string]interface{}{}).
|
||||
Writes(map[string]interface{}{}))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddSonarToWebService(webservice *restful.WebService, devopsClient devops.Interface, sonarClient sonarqube.SonarInterface) error {
|
||||
sonarEnable := devopsClient != nil && sonarClient != nil
|
||||
if sonarEnable {
|
||||
sonarHandler := NewPipelineSonarHandler(devopsClient, sonarClient)
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/sonarstatus").
|
||||
To(sonarHandler.GetPipelineSonarStatusHandler).
|
||||
Doc("Get the sonar quality information for the specified pipeline of the DevOps project. More info: https://docs.sonarqube.org/7.4/user-guide/metric-definitions/").
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of pipeline, e.g. sample-pipeline")).
|
||||
Returns(http.StatusOK, RespOK, []sonarqube.SonarStatus{}).
|
||||
Writes([]sonarqube.SonarStatus{}))
|
||||
|
||||
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/sonarstatus").
|
||||
To(sonarHandler.GetMultiBranchesPipelineSonarStatusHandler).
|
||||
Doc("Get the sonar quality check information for the specified pipeline branch of the DevOps project. More info: https://docs.sonarqube.org/7.4/user-guide/metric-definitions/").
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}).
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
Param(webservice.PathParameter("pipeline", "the name of pipeline, e.g. sample-pipeline")).
|
||||
Param(webservice.PathParameter("branch", "branch name, e.g. master")).
|
||||
Returns(http.StatusOK, RespOK, []sonarqube.SonarStatus{}).
|
||||
Writes([]sonarqube.SonarStatus{}))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddS2IToWebService(webservice *restful.WebService, ksClient versioned.Interface,
|
||||
ksInformer externalversions.SharedInformerFactory, s3Client s3.Interface) error {
|
||||
s2iEnable := ksClient != nil && ksInformer != nil && s3Client != nil
|
||||
|
||||
if s2iEnable {
|
||||
s2iHandler := NewS2iBinaryHandler(ksClient, ksInformer, s3Client)
|
||||
webservice.Route(webservice.PUT("/namespaces/{namespace}/s2ibinaries/{s2ibinary}/file").
|
||||
To(s2iHandler.UploadS2iBinaryHandler).
|
||||
Consumes("multipart/form-data").
|
||||
Produces(restful.MIME_JSON).
|
||||
Doc("Upload S2iBinary file").
|
||||
Param(webservice.PathParameter("namespace", "the name of namespaces")).
|
||||
Param(webservice.PathParameter("s2ibinary", "the name of s2ibinary")).
|
||||
Param(webservice.FormParameter("s2ibinary", "file to upload")).
|
||||
Param(webservice.FormParameter("md5", "md5 of file")).
|
||||
Returns(http.StatusOK, RespOK, devopsv1alpha1.S2iBinary{}))
|
||||
|
||||
webservice.Route(webservice.GET("/namespaces/{namespace}/s2ibinaries/{s2ibinary}/file/{file}").
|
||||
To(s2iHandler.DownloadS2iBinaryHandler).
|
||||
Produces(restful.MIME_OCTET).
|
||||
Doc("Download S2iBinary file").
|
||||
Param(webservice.PathParameter("namespace", "the name of namespaces")).
|
||||
Param(webservice.PathParameter("s2ibinary", "the name of s2ibinary")).
|
||||
Param(webservice.PathParameter("file", "the name of binary file")).
|
||||
Returns(http.StatusOK, RespOK, nil))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddJenkinsToContainer(webservice *restful.WebService, devopsClient devops.Interface, endpoint string) error {
|
||||
if devopsClient == nil {
|
||||
return nil
|
||||
}
|
||||
parse, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parse.Path = strings.Trim(parse.Path, "/")
|
||||
// this API does not belong any kind of auth scope, it should be removed in the future version
|
||||
// see also pkg/apiserver/request/requestinfo.go
|
||||
// Deprecated: Please use /devops/{devops}/jenkins/{path:*} instead
|
||||
webservice.Route(webservice.GET("/jenkins/{path:*}").
|
||||
Param(webservice.PathParameter("path", "Path stands for any suffix path.")).
|
||||
To(func(request *restful.Request, response *restful.Response) {
|
||||
u := request.Request.URL
|
||||
u.Host = parse.Host
|
||||
u.Scheme = parse.Scheme
|
||||
jenkins.SetBasicBearTokenHeader(&request.Request.Header)
|
||||
u.Path = strings.Replace(request.Request.URL.Path, fmt.Sprintf("/kapis/%s/%s/jenkins", GroupVersion.Group, GroupVersion.Version), "", 1)
|
||||
httpProxy := proxy.NewUpgradeAwareHandler(u, http.DefaultTransport, false, false, &errorResponder{})
|
||||
httpProxy.ServeHTTP(response, request.Request)
|
||||
}).
|
||||
Returns(http.StatusOK, RespOK, nil).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsJenkinsTag}))
|
||||
|
||||
handlerWithDevOps := func(request *restful.Request, response *restful.Response) {
|
||||
u := request.Request.URL
|
||||
devops := request.PathParameter("devops")
|
||||
u.Host = parse.Host
|
||||
u.Scheme = parse.Scheme
|
||||
jenkins.SetBasicBearTokenHeader(&request.Request.Header)
|
||||
u.Path = strings.Replace(request.Request.URL.Path, fmt.Sprintf("/kapis/%s/%s/devops/%s/jenkins",
|
||||
GroupVersion.Group, GroupVersion.Version, devops), "", 1)
|
||||
httpProxy := proxy.NewUpgradeAwareHandler(u, http.DefaultTransport, false, false, &errorResponder{})
|
||||
httpProxy.ServeHTTP(response, request.Request)
|
||||
}
|
||||
// some Jenkins API against with POST method
|
||||
webservice.Route(webservice.GET("/devops/{devops}/jenkins/{path:*}").
|
||||
Param(webservice.PathParameter("path", "Path stands for any suffix path.")).
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
To(handlerWithDevOps).
|
||||
Returns(http.StatusOK, RespOK, nil).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsJenkinsTag}))
|
||||
webservice.Route(webservice.POST("/devops/{devops}/jenkins/{path:*}").
|
||||
Param(webservice.PathParameter("path", "Path stands for any suffix path.")).
|
||||
Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")).
|
||||
To(handlerWithDevOps).
|
||||
Returns(http.StatusOK, RespOK, nil).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsJenkinsTag}))
|
||||
return nil
|
||||
}
|
||||
|
||||
type pipelineParam struct {
|
||||
Workspace string
|
||||
ProjectName string
|
||||
Name string
|
||||
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
type errorResponder struct{}
|
||||
|
||||
func (e *errorResponder) Error(w http.ResponseWriter, req *http.Request, err error) {
|
||||
klog.Error(err)
|
||||
return proxy.AddToContainer(container)
|
||||
}
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
/*
|
||||
Copyright 2020 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 v1alpha2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"code.cloudfoundry.org/bytefmt"
|
||||
"github.com/emicklei/go-restful"
|
||||
"k8s.io/klog"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/models/devops"
|
||||
"kubesphere.io/kubesphere/pkg/utils/hashutil"
|
||||
)
|
||||
|
||||
type S2iBinaryHandler struct {
|
||||
s2iUploader devops.S2iBinaryUploader
|
||||
}
|
||||
|
||||
func (h S2iBinaryHandler) UploadS2iBinaryHandler(req *restful.Request, resp *restful.Response) {
|
||||
ns := req.PathParameter("namespace")
|
||||
name := req.PathParameter("s2ibinary")
|
||||
|
||||
err := req.Request.ParseMultipartForm(bytefmt.MEGABYTE * 20)
|
||||
if err != nil {
|
||||
klog.Errorf("%+v", err)
|
||||
api.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
if len(req.Request.MultipartForm.File) == 0 {
|
||||
err := restful.NewError(http.StatusBadRequest, "could not get file from form")
|
||||
klog.Errorf("%+v", err)
|
||||
api.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
if len(req.Request.MultipartForm.File["s2ibinary"]) == 0 {
|
||||
err := restful.NewError(http.StatusBadRequest, "could not get file from form")
|
||||
klog.Errorf("%+v", err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
if len(req.Request.MultipartForm.File["s2ibinary"]) > 1 {
|
||||
err := restful.NewError(http.StatusBadRequest, "s2ibinary should only have one file")
|
||||
klog.Errorf("%+v", err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
defer req.Request.MultipartForm.RemoveAll()
|
||||
file, err := req.Request.MultipartForm.File["s2ibinary"][0].Open()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
filemd5, err := hashutil.GetMD5(file)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
md5, ok := req.Request.MultipartForm.Value["md5"]
|
||||
if ok && len(req.Request.MultipartForm.Value["md5"]) > 0 {
|
||||
if md5[0] != filemd5 {
|
||||
err := restful.NewError(http.StatusBadRequest, fmt.Sprintf("md5 not match, origin: %+v, calculate: %+v", md5[0], filemd5))
|
||||
klog.Error(err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
s2ibin, err := h.s2iUploader.UploadS2iBinary(ns, name, filemd5, req.Request.MultipartForm.File["s2ibinary"][0])
|
||||
if err != nil {
|
||||
klog.Errorf("%+v", err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
resp.WriteAsJson(s2ibin)
|
||||
|
||||
}
|
||||
|
||||
func (h S2iBinaryHandler) DownloadS2iBinaryHandler(req *restful.Request, resp *restful.Response) {
|
||||
ns := req.PathParameter("namespace")
|
||||
name := req.PathParameter("s2ibinary")
|
||||
fileName := req.PathParameter("file")
|
||||
url, err := h.s2iUploader.DownloadS2iBinary(ns, name, fileName)
|
||||
if err != nil {
|
||||
klog.Errorf("%+v", err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
http.Redirect(resp.ResponseWriter, req.Request, url, http.StatusFound)
|
||||
return
|
||||
}
|
||||
@@ -1,383 +0,0 @@
|
||||
/*
|
||||
Copyright 2020 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 v1alpha3
|
||||
|
||||
import (
|
||||
"github.com/emicklei/go-restful"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/klog"
|
||||
|
||||
"kubesphere.io/api/devops/v1alpha3"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
|
||||
"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
|
||||
"kubesphere.io/kubesphere/pkg/models/devops"
|
||||
servererr "kubesphere.io/kubesphere/pkg/server/errors"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
devopsClient "kubesphere.io/kubesphere/pkg/simple/client/devops"
|
||||
)
|
||||
|
||||
type devopsHandler struct {
|
||||
devops devops.DevopsOperator
|
||||
}
|
||||
|
||||
func newDevOpsHandler(devopsClient devopsClient.Interface, k8sclient kubernetes.Interface, ksclient kubesphere.Interface,
|
||||
ksInformers externalversions.SharedInformerFactory,
|
||||
k8sInformers informers.SharedInformerFactory) *devopsHandler {
|
||||
|
||||
return &devopsHandler{
|
||||
devops: devops.NewDevopsOperator(devopsClient, k8sclient, ksclient, ksInformers, k8sInformers),
|
||||
}
|
||||
}
|
||||
|
||||
// devopsproject handler about get/list/post/put/delete
|
||||
func (h *devopsHandler) GetDevOpsProject(request *restful.Request, response *restful.Response) {
|
||||
workspace := request.PathParameter("workspace")
|
||||
devops := request.PathParameter("devops")
|
||||
|
||||
project, err := h.devops.GetDevOpsProject(workspace, devops)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(project)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) ListDevOpsProject(request *restful.Request, response *restful.Response) {
|
||||
workspace := request.PathParameter("workspace")
|
||||
limit, offset := params.ParsePaging(request)
|
||||
|
||||
projectList, err := h.devops.ListDevOpsProject(workspace, limit, offset)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(projectList)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) CreateDevOpsProject(request *restful.Request, response *restful.Response) {
|
||||
workspace := request.PathParameter("workspace")
|
||||
var devOpsProject v1alpha3.DevOpsProject
|
||||
err := request.ReadEntity(&devOpsProject)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
created, err := h.devops.CreateDevOpsProject(workspace, &devOpsProject)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
} else if errors.IsConflict(err) {
|
||||
api.HandleConflict(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(created)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) UpdateDevOpsProject(request *restful.Request, response *restful.Response) {
|
||||
workspace := request.PathParameter("workspace")
|
||||
var devOpsProject v1alpha3.DevOpsProject
|
||||
err := request.ReadEntity(&devOpsProject)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
project, err := h.devops.UpdateDevOpsProject(workspace, &devOpsProject)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(project)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) DeleteDevOpsProject(request *restful.Request, response *restful.Response) {
|
||||
workspace := request.PathParameter("workspace")
|
||||
devops := request.PathParameter("devops")
|
||||
|
||||
err := h.devops.DeleteDevOpsProject(workspace, devops)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(servererr.None)
|
||||
}
|
||||
|
||||
// pipeline handler about get/list/post/put/delete
|
||||
func (h *devopsHandler) GetPipeline(request *restful.Request, response *restful.Response) {
|
||||
devops := request.PathParameter("devops")
|
||||
pipeline := request.PathParameter("pipeline")
|
||||
|
||||
obj, err := h.devops.GetPipelineObj(devops, pipeline)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(obj)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) ListPipeline(request *restful.Request, response *restful.Response) {
|
||||
devopsProject := request.PathParameter("devops")
|
||||
limit, offset := params.ParsePaging(request)
|
||||
|
||||
objs, err := h.devops.ListPipelineObj(devopsProject, nil, nil, limit, offset)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(objs)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) CreatePipeline(request *restful.Request, response *restful.Response) {
|
||||
devops := request.PathParameter("devops")
|
||||
var pipeline v1alpha3.Pipeline
|
||||
err := request.ReadEntity(&pipeline)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
created, err := h.devops.CreatePipelineObj(devops, &pipeline)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(created)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) UpdatePipeline(request *restful.Request, response *restful.Response) {
|
||||
devops := request.PathParameter("devops")
|
||||
|
||||
var pipeline v1alpha3.Pipeline
|
||||
err := request.ReadEntity(&pipeline)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
obj, err := h.devops.UpdatePipelineObj(devops, &pipeline)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(obj)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) DeletePipeline(request *restful.Request, response *restful.Response) {
|
||||
devops := request.PathParameter("devops")
|
||||
pipeline := request.PathParameter("pipeline")
|
||||
|
||||
klog.V(8).Infof("ready to delete pipeline %s/%s", devops, pipeline)
|
||||
err := h.devops.DeletePipelineObj(devops, pipeline)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(servererr.None)
|
||||
}
|
||||
|
||||
//credential handler about get/list/post/put/delete
|
||||
func (h *devopsHandler) GetCredential(request *restful.Request, response *restful.Response) {
|
||||
devops := request.PathParameter("devops")
|
||||
credential := request.PathParameter("credential")
|
||||
|
||||
obj, err := h.devops.GetCredentialObj(devops, credential)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(obj)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) ListCredential(request *restful.Request, response *restful.Response) {
|
||||
devops := request.PathParameter("devops")
|
||||
query := query.ParseQueryParameter(request)
|
||||
objs, err := h.devops.ListCredentialObj(devops, query)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(objs)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) CreateCredential(request *restful.Request, response *restful.Response) {
|
||||
devops := request.PathParameter("devops")
|
||||
var obj v1.Secret
|
||||
err := request.ReadEntity(&obj)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
created, err := h.devops.CreateCredentialObj(devops, &obj)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(created)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) UpdateCredential(request *restful.Request, response *restful.Response) {
|
||||
devops := request.PathParameter("devops")
|
||||
var obj v1.Secret
|
||||
err := request.ReadEntity(&obj)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
updated, err := h.devops.UpdateCredentialObj(devops, &obj)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(updated)
|
||||
}
|
||||
|
||||
func (h *devopsHandler) DeleteCredential(request *restful.Request, response *restful.Response) {
|
||||
devops := request.PathParameter("devops")
|
||||
credential := request.PathParameter("credential")
|
||||
|
||||
err := h.devops.DeleteCredentialObj(devops, credential)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
if errors.IsNotFound(err) {
|
||||
api.HandleNotFound(response, request, err)
|
||||
return
|
||||
}
|
||||
api.HandleBadRequest(response, request, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteEntity(servererr.None)
|
||||
}
|
||||
@@ -19,173 +19,23 @@
|
||||
package v1alpha3
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
restfulspec "github.com/emicklei/go-restful-openapi"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
|
||||
"kubesphere.io/api/devops/v1alpha3"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
|
||||
kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
|
||||
"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
devopsClient "kubesphere.io/kubesphere/pkg/simple/client/devops"
|
||||
"kubesphere.io/kubesphere/pkg/kapis/generic"
|
||||
)
|
||||
|
||||
const (
|
||||
GroupName = "devops.kubesphere.io"
|
||||
RespOK = "ok"
|
||||
)
|
||||
|
||||
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha3"}
|
||||
|
||||
func AddToContainer(container *restful.Container, devopsClient devopsClient.Interface,
|
||||
k8sclient kubernetes.Interface, ksclient kubesphere.Interface,
|
||||
ksInformers externalversions.SharedInformerFactory,
|
||||
k8sInformers informers.SharedInformerFactory) error {
|
||||
devopsEnable := devopsClient != nil
|
||||
if devopsEnable {
|
||||
ws := runtime.NewWebService(GroupVersion)
|
||||
handler := newDevOpsHandler(devopsClient, k8sclient, ksclient, ksInformers, k8sInformers)
|
||||
// credential
|
||||
ws.Route(ws.GET("/devops/{devops}/credentials").
|
||||
To(handler.ListCredential).
|
||||
Param(ws.PathParameter("devops", "devops name")).
|
||||
Param(ws.QueryParameter(query.ParameterName, "name used to do filtering").Required(false)).
|
||||
Param(ws.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")).
|
||||
Param(ws.QueryParameter(query.ParameterLimit, "limit").Required(false)).
|
||||
Param(ws.QueryParameter(query.ParameterAscending, "sort parameters, e.g. ascending=false").Required(false).DefaultValue("ascending=false")).
|
||||
Param(ws.QueryParameter(query.ParameterOrderBy, "sort parameters, e.g. orderBy=createTime")).
|
||||
Doc("list the credentials of the specified devops for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, api.ListResult{Items: []interface{}{}}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.POST("/devops/{devops}/credentials").
|
||||
To(handler.CreateCredential).
|
||||
Param(ws.PathParameter("devops", "devops name")).
|
||||
Doc("create the credential of the specified devops for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1alpha3.Pipeline{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.GET("/devops/{devops}/credentials/{credential}").
|
||||
To(handler.GetCredential).
|
||||
Param(ws.PathParameter("devops", "project name")).
|
||||
Param(ws.PathParameter("credential", "pipeline name")).
|
||||
Doc("get the credential of the specified devops for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1.Secret{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.PUT("/devops/{devops}/credentials/{credential}").
|
||||
To(handler.UpdateCredential).
|
||||
Param(ws.PathParameter("devops", "project name")).
|
||||
Param(ws.PathParameter("credential", "credential name")).
|
||||
Doc("put the credential of the specified devops for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1.Secret{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.DELETE("/devops/{devops}/credentials/{credential}").
|
||||
To(handler.DeleteCredential).
|
||||
Param(ws.PathParameter("devops", "project name")).
|
||||
Param(ws.PathParameter("credential", "credential name")).
|
||||
Doc("delete the credential of the specified devops for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1.Secret{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}))
|
||||
|
||||
// pipeline
|
||||
ws.Route(ws.GET("/devops/{devops}/pipelines").
|
||||
To(handler.ListPipeline).
|
||||
Param(ws.PathParameter("devops", "devops name")).
|
||||
Param(ws.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
|
||||
Required(false).
|
||||
DataFormat("limit=%d,page=%d").
|
||||
DefaultValue("limit=10,page=1")).
|
||||
Doc("list the pipelines of the specified devops for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, api.ListResult{Items: []interface{}{}}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.POST("/devops/{devops}/pipelines").
|
||||
To(handler.CreatePipeline).
|
||||
Param(ws.PathParameter("devops", "devops name")).
|
||||
Doc("create the pipeline of the specified devops for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1alpha3.Pipeline{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.GET("/devops/{devops}/pipelines/{pipeline}").
|
||||
To(handler.GetPipeline).
|
||||
Operation("getPipelineByName").
|
||||
Param(ws.PathParameter("devops", "project name")).
|
||||
Param(ws.PathParameter("pipeline", "pipeline name")).
|
||||
Doc("get the pipeline of the specified devops for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1alpha3.Pipeline{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.PUT("/devops/{devops}/pipelines/{pipeline}").
|
||||
To(handler.UpdatePipeline).
|
||||
Param(ws.PathParameter("devops", "project name")).
|
||||
Param(ws.PathParameter("pipeline", "pipeline name")).
|
||||
Doc("put the pipeline of the specified devops for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1alpha3.Pipeline{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.DELETE("/devops/{devops}/pipelines/{pipeline}").
|
||||
To(handler.DeletePipeline).
|
||||
Param(ws.PathParameter("devops", "project name")).
|
||||
Param(ws.PathParameter("pipeline", "pipeline name")).
|
||||
Doc("delete the pipeline of the specified devops for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1alpha3.Pipeline{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}))
|
||||
|
||||
// devops
|
||||
ws.Route(ws.GET("/workspaces/{workspace}/devops").
|
||||
To(handler.ListDevOpsProject).
|
||||
Param(ws.PathParameter("workspace", "workspace name")).
|
||||
Param(ws.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
|
||||
Required(false).
|
||||
DataFormat("limit=%d,page=%d").
|
||||
DefaultValue("limit=10,page=1")).Doc("List the devopsproject of the specified workspace for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, api.ListResult{Items: []interface{}{}}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.POST("/workspaces/{workspace}/devops").
|
||||
To(handler.CreateDevOpsProject).
|
||||
Param(ws.PathParameter("workspace", "workspace name")).
|
||||
Doc("Create the devopsproject of the specified workspace for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1alpha3.DevOpsProject{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.GET("/workspaces/{workspace}/devops/{devops}").
|
||||
To(handler.GetDevOpsProject).
|
||||
Param(ws.PathParameter("workspace", "workspace name")).
|
||||
Param(ws.PathParameter("devops", "project name")).
|
||||
Doc("Get the devopsproject of the specified workspace for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1alpha3.DevOpsProject{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.PUT("/workspaces/{workspace}/devops/{devops}").
|
||||
To(handler.UpdateDevOpsProject).
|
||||
Param(ws.PathParameter("workspace", "workspace name")).
|
||||
Param(ws.PathParameter("devops", "project name")).
|
||||
Doc("Put the devopsproject of the specified workspace for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1alpha3.DevOpsProject{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
ws.Route(ws.DELETE("/workspaces/{workspace}/devops/{devops}").
|
||||
To(handler.DeleteDevOpsProject).
|
||||
Param(ws.PathParameter("workspace", "workspace name")).
|
||||
Param(ws.PathParameter("devops", "project name")).
|
||||
Doc("Get the devopsproject of the specified workspace for the current user").
|
||||
Returns(http.StatusOK, api.StatusOK, []v1alpha3.DevOpsProject{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
|
||||
|
||||
container.Add(ws)
|
||||
func AddToContainer(container *restful.Container, endpoint string) error {
|
||||
proxy, err := generic.NewGenericProxy(endpoint, GroupVersion.Group, GroupVersion.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
return proxy.AddToContainer(container)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user