Merge pull request #2072 from shaowenchen/fix_create_pipeline

fix pipeline bugs
This commit is contained in:
KubeSphere CI Bot
2020-05-23 21:15:34 +08:00
committed by GitHub
16 changed files with 746 additions and 154 deletions

View File

@@ -20,7 +20,7 @@ type PipelineSonarHandler struct {
func NewProjectPipelineHandler(devopsClient devopsClient.Interface) ProjectPipelineHandler {
return ProjectPipelineHandler{
devopsOperator: devops.NewDevopsOperator(devopsClient),
devopsOperator: devops.NewDevopsOperator(devopsClient, nil, nil, nil, nil),
projectCredentialGetter: devops.NewProjectCredentialOperator(devopsClient),
}
}

View File

@@ -0,0 +1,385 @@
/*
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/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apis/devops/v1alpha3"
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"
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")
projectName := request.PathParameter("projectName")
project, err := h.devops.GetDevOpsProject(workspace, projectName)
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")
projectList, err := h.devops.ListDevOpsProject(workspace)
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
}
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")
projectName := request.PathParameter("projectName")
err := h.devops.DeleteDevOpsProject(workspace, projectName)
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) {
workspace := request.PathParameter("workspace")
projectName := request.PathParameter("projectName")
pipelineName := request.PathParameter("pipelineName")
obj, err := h.devops.GetPipelineObj(workspace, projectName, pipelineName)
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) {
workspace := request.PathParameter("workspace")
projectName := request.PathParameter("projectName")
objs, err := h.devops.ListPipelineObj(workspace, projectName)
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) {
workspace := request.PathParameter("workspace")
projectName := request.PathParameter("projectName")
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(workspace, projectName, &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) {
workspace := request.PathParameter("workspace")
projectName := request.PathParameter("projectName")
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(workspace, projectName, &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) {
workspace := request.PathParameter("workspace")
projectName := request.PathParameter("projectName")
pipelineName := request.PathParameter("pipelineName")
err := h.devops.DeletePipelineObj(workspace, projectName, pipelineName)
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) {
workspace := request.PathParameter("workspace")
projectName := request.PathParameter("projectName")
credentialName := request.PathParameter("credentialName")
obj, err := h.devops.GetCredentialObj(workspace, projectName, credentialName)
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) {
workspace := request.PathParameter("workspace")
projectName := request.PathParameter("projectName")
objs, err := h.devops.ListCredentialObj(workspace, projectName)
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) {
workspace := request.PathParameter("workspace")
projectName := request.PathParameter("projectName")
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(workspace, projectName, &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) {
workspace := request.PathParameter("workspace")
projectName := request.PathParameter("projectName")
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(workspace, projectName, &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) {
workspace := request.PathParameter("workspace")
projectName := request.PathParameter("projectName")
credentialName := request.PathParameter("credentialName")
err := h.devops.DeleteCredentialObj(workspace, projectName, credentialName)
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)
}

View File

@@ -0,0 +1,183 @@
/*
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"
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/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apis/devops/v1alpha3"
"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"
devopsClient "kubesphere.io/kubesphere/pkg/simple/client/devops"
"net/http"
)
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("/workspaces/{workspace}/devopsprojects/{projectName}/credential/").
To(handler.ListCredential).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "devops name")).
Doc("list the credential of the specified devops for the current user").
Returns(http.StatusOK, api.StatusOK, []v1alpha3.PipelineList{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
ws.Route(ws.POST("/workspaces/{workspace}/devopsprojects/{projectName}/credential/").
To(handler.CreateCredential).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "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("/workspaces/{workspace}/devopsprojects/{projectName}/credential/{credentialName}/").
To(handler.GetCredential).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "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("/workspaces/{workspace}/devopsprojects/{projectName}/credential/{credentialName}/").
To(handler.UpdateCredential).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "project name")).
Param(ws.PathParameter("credentialName", "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("/workspaces/{workspace}/devopsprojects/{projectName}/credential/{credentialName}/").
To(handler.DeleteCredential).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "project name")).
Param(ws.PathParameter("credentialName", "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("/workspaces/{workspace}/devopsprojects/{projectName}/pipelines/").
To(handler.ListPipeline).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "devops name")).
Doc("list the pipeline of the specified devops for the current user").
Returns(http.StatusOK, api.StatusOK, []v1alpha3.PipelineList{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
ws.Route(ws.POST("/workspaces/{workspace}/devopsprojects/{projectName}/pipelines/").
To(handler.CreatePipeline).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "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("/workspaces/{workspace}/devopsprojects/{projectName}/pipelines/{pipelineName}/").
To(handler.GetPipeline).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "project name")).
Param(ws.PathParameter("pipelineName", "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("/workspaces/{workspace}/devopsprojects/{projectName}/pipelines/{pipelineName}/").
To(handler.UpdatePipeline).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "project name")).
Param(ws.PathParameter("pipelineName", "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("/workspaces/{workspace}/devopsprojects/{projectName}/pipelines/{pipelineName}/").
To(handler.DeletePipeline).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "project name")).
Param(ws.PathParameter("pipelineName", "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}/devopsprojects/").
To(handler.ListDevOpsProject).
Param(ws.PathParameter("workspace", "workspace name")).
Doc("List the devopsproject of the specified workspace for the current user").
Returns(http.StatusOK, api.StatusOK, []v1alpha3.DevOpsProjectList{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag}))
ws.Route(ws.POST("/workspaces/{workspace}/devopsprojects/").
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}/devopsprojects/{projectName}/").
To(handler.GetDevOpsProject).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "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}/devopsprojects/{projectName}/").
To(handler.UpdateDevOpsProject).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "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}/devopsprojects/{projectName}/").
To(handler.DeleteDevOpsProject).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("projectName", "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)
}
return nil
}