Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dashboard): better error handling in dashboard code #336

Merged
merged 1 commit into from
Jan 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 54 additions & 32 deletions dashboard/backend/handler/api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"fmt"
"net/http"

restful "github.com/emicklei/go-restful"
log "github.com/golang/glog"

"github.com/emicklei/go-restful"
"github.com/tensorflow/k8s/dashboard/backend/client"
"github.com/tensorflow/k8s/pkg/apis/tensorflow/v1alpha1"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/api/errors"
)

// APIHandler handles the API calls
Expand Down Expand Up @@ -88,24 +91,30 @@ func CreateHTTPAPIHandler(client client.ClientManager) (http.Handler, error) {
}

func (apiHandler *APIHandler) handleGetTfJobs(request *restful.Request, response *restful.Response) {

//TODO: namespace handling
jobs, err := apiHandler.cManager.TfJobClient.TensorflowV1alpha1().TfJobs("default").List(metav1.ListOptions{})

namespace := "default"
jobs, err := apiHandler.cManager.TfJobClient.TensorflowV1alpha1().TfJobs(namespace).List(metav1.ListOptions{})
if err != nil {
panic(err)
log.Warningf("failed to list TfJobs under namespace %v: %v", namespace, err)
response.WriteError(http.StatusInternalServerError, err)
} else {
log.Infof("successfully listed TfJobs under namespace %v", namespace)
response.WriteHeaderAndEntity(http.StatusOK, jobs)
}

response.WriteHeaderAndEntity(http.StatusOK, jobs)
}

func (apiHandler *APIHandler) handleGetTfJobDetail(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
name := request.PathParameter("tfjob")

job, err := apiHandler.cManager.TfJobClient.TensorflowV1alpha1().TfJobs(namespace).Get(name, metav1.GetOptions{})
if err != nil {
panic(err)
log.Infof("cannot find TfJob %v under namespace %v, error: %v", name, namespace, err)
if errors.IsNotFound(err) {
response.WriteError(http.StatusNotFound, err)
} else {
response.WriteError(http.StatusInternalServerError, err)
}
return
}

tfJobDetail := TfJobDetail{
Expand All @@ -117,15 +126,18 @@ func (apiHandler *APIHandler) handleGetTfJobDetail(request *restful.Request, res
LabelSelector: fmt.Sprintf("tensorflow.org=,app=tensorboard,runtime_id=%s", job.Spec.RuntimeId),
})
if err != nil {
panic(err)
}

if len(tbSpec.Items) > 0 {
log.Warningf("failed to list TensorBoard for TfJob %v under namespace %v, error: %v", job.Name, job.Namespace, err)
// TODO maybe partial result?
response.WriteError(http.StatusNotFound, err)
return
} else if len(tbSpec.Items) > 0 {
// Should never be more than 1 service that matched, handle error
// Handle case where no tensorboard is found
// Handle case where no TensorBoard is found
tfJobDetail.TbService = &tbSpec.Items[0]
log.Warningf("more than one TensorBoards found for TfJob %v under namespace %v, this should be impossible",
job.Name, job.Namespace)
} else {
fmt.Println(fmt.Sprintf("Couldn't find a TensorBoard service for TfJob %s", job.Name))
log.Warningf("Couldn't find a TensorBoard service for TfJob %v under namespace %v", job.Name, job.Namespace)
}
}

Expand All @@ -134,45 +146,55 @@ func (apiHandler *APIHandler) handleGetTfJobDetail(request *restful.Request, res
LabelSelector: fmt.Sprintf("tensorflow.org=,runtime_id=%s", job.Spec.RuntimeId),
})
if err != nil {
panic(err)
log.Warningf("failed to list pods for TfJob %v under namespace %v: %v", name, namespace, err)
response.WriteError(http.StatusInternalServerError, err)
} else {
log.Infof("successfully listed pods for TfJob %v under namespace %v", name, namespace)
tfJobDetail.Pods = pods.Items
response.WriteHeaderAndEntity(http.StatusOK, tfJobDetail)
}
tfJobDetail.Pods = pods.Items

response.WriteHeaderAndEntity(http.StatusOK, tfJobDetail)
}

func (apiHandler *APIHandler) handleDeploy(request *restful.Request, response *restful.Response) {
client := apiHandler.cManager.TfJobClient
clt := apiHandler.cManager.TfJobClient
tfJob := new(v1alpha1.TfJob)
if err := request.ReadEntity(tfJob); err != nil {
panic(err)
response.WriteError(http.StatusBadRequest, err)
return
}
j, err := client.TensorflowV1alpha1().TfJobs(tfJob.Namespace).Create(tfJob)
j, err := clt.TensorflowV1alpha1().TfJobs(tfJob.Namespace).Create(tfJob)
if err != nil {
panic(err)
log.Warningf("failed to deploy TfJob %v under namespace %v: %v", tfJob.Name, tfJob.Namespace, err)
response.WriteError(http.StatusInternalServerError, err)
} else {
log.Infof("successfully deployed TfJob %v under namespace %v", tfJob.Name, tfJob.Namespace)
response.WriteHeaderAndEntity(http.StatusCreated, j)
}
response.WriteHeaderAndEntity(http.StatusCreated, j)
}

func (apiHandler *APIHandler) handleDeleteTfJob(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
name := request.PathParameter("tfjob")
client := apiHandler.cManager.TfJobClient
err := client.TensorflowV1alpha1().TfJobs(namespace).Delete(name, &metav1.DeleteOptions{})
clt := apiHandler.cManager.TfJobClient
err := clt.TensorflowV1alpha1().TfJobs(namespace).Delete(name, &metav1.DeleteOptions{})
if err != nil {
panic(err)
log.Warningf("failed to delete TfJob %v under namespace %v: %v", name, namespace, err)
response.WriteError(http.StatusInternalServerError, err)
} else {
log.Infof("successfully deleted TfJob %v under namespace %v", name, namespace)
response.WriteHeader(http.StatusNoContent)
}
response.WriteHeader(http.StatusOK)
}

func (apiHandler *APIHandler) handleGetPodLogs(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
name := request.PathParameter("podname")

logs, err := apiHandler.cManager.ClientSet.CoreV1().Pods(namespace).GetLogs(name, &v1.PodLogOptions{}).Do().Raw()
if err != nil {
panic(err)
log.Warningf("failed to get pod logs for TfJob %v under namespace %v: %v", name, namespace, err)
response.WriteError(http.StatusInternalServerError, err)
} else {
log.Infof("successfully get pod logs for TfJob %v under namespace %v", name, namespace)
response.WriteHeaderAndEntity(http.StatusOK, string(logs))
}

response.WriteHeaderAndEntity(http.StatusOK, string(logs))
}