Skip to content

Commit

Permalink
Merge pull request #316 from linkernetworks/phstsai/deletePod
Browse files Browse the repository at this point in the history
[Task] New deleting pod api
  • Loading branch information
John-Lin authored Sep 18, 2018
2 parents 537f01e + 433871d commit b97b1bd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/server/handler_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ func deletePodHandler(ctx *web.Context) {
})
}

func deletePodFromClusterHandler(ctx *web.Context) {
sp, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response

namespace := req.PathParameter("namespace")
pod := req.PathParameter("pod")

if err := sp.KubeCtl.DeletePod(pod, namespace); err != nil {
if errors.IsNotFound(err) {
response.NotFound(req.Request, resp.ResponseWriter, err)
} else {
response.InternalServerError(req.Request, resp.ResponseWriter, err)
}
return
}

resp.WriteEntity(response.ActionResponse{
Error: false,
Message: "Delete success",
})
}

func listPodHandler(ctx *web.Context) {
sp, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response

Expand Down
37 changes: 37 additions & 0 deletions src/server/handler_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,43 @@ func (suite *PodTestSuite) TestDeletePod() {
suite.Equal(0, n)
}

func (suite *PodTestSuite) TestDeletePodFromCluster() {
namespace := "default"
containers := []entity.Container{
{
Name: namesgenerator.GetRandomName(0),
Image: "busybox",
Command: []string{"sleep", "3600"},
},
}
tName := namesgenerator.GetRandomName(0)
pod := entity.Pod{
Name: tName,
Namespace: namespace,
Containers: containers,
Capability: true,
RestartPolicy: "Never",
NetworkType: entity.PodHostNetwork,
NodeAffinity: []string{},
}

err := p.CreatePod(suite.sp, &pod)
suite.NoError(err)

bodyBytes, err := json.MarshalIndent(pod, "", " ")
suite.NoError(err)

bodyReader := strings.NewReader(string(bodyBytes))
httpRequest, err := http.NewRequest("DELETE", "http://localhost:7890/v1/pods/"+pod.Namespace+"/"+pod.Name, bodyReader)
suite.NoError(err)

httpRequest.Header.Add("Content-Type", "application/json")
httpRequest.Header.Add("Authorization", suite.JWTBearer)
httpWriter := httptest.NewRecorder()
suite.wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(suite.T(), http.StatusOK, httpWriter)
}

func (suite *PodTestSuite) TestDeletePodWithInvalidID() {
httpRequest, err := http.NewRequest("DELETE", "http://localhost:7890/v1/pods/"+bson.NewObjectId().Hex(), nil)
suite.NoError(err)
Expand Down
1 change: 1 addition & 0 deletions src/server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func newPodService(sp *serviceprovider.Container) *restful.WebService {
webService.Filter(validateTokenMiddleware)
webService.Route(webService.POST("/").To(handler.RESTfulServiceHandler(sp, createPodHandler)))
webService.Route(webService.DELETE("/{id}").To(handler.RESTfulServiceHandler(sp, deletePodHandler)))
webService.Route(webService.DELETE("/{namespace}/{pod}").To(handler.RESTfulServiceHandler(sp, deletePodFromClusterHandler)))
webService.Route(webService.GET("/").To(handler.RESTfulServiceHandler(sp, listPodHandler)))
webService.Route(webService.GET("/{id}").To(handler.RESTfulServiceHandler(sp, getPodHandler)))
return webService
Expand Down

0 comments on commit b97b1bd

Please sign in to comment.