Skip to content

Commit

Permalink
katib ui: adapt environment in which cluster role is unavailable (#1141)
Browse files Browse the repository at this point in the history
* katib ui: adapt environments which cluster role is unavailable

* use init_fn

* fix gofmt

* update

* fix error message

* fallback plan
  • Loading branch information
sperlingxx authored Apr 16, 2020
1 parent 2238eee commit 109d8f8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
10 changes: 3 additions & 7 deletions pkg/ui/v1alpha3/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,13 @@ func (k *KatibUIHandler) DeleteTemplate(w http.ResponseWriter, r *http.Request)

func (k *KatibUIHandler) FetchNamespaces(w http.ResponseWriter, r *http.Request) {

namespaceList, err := k.katibClient.GetNamespaceList()
// Get all available namespaces
namespaces, err := k.getAvailableNamespaces()
if err != nil {
log.Printf("GetNamespaceList failed: %v", err)
log.Printf("GetAvailableNamespaces failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var namespaces []string

for _, namespace := range namespaceList.Items {
namespaces = append(namespaces, namespace.ObjectMeta.Name)
}

response, err := json.Marshal(namespaces)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions pkg/ui/v1alpha3/hp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ import (

// FetchAllHPJobs gets experiments in all namespaces.
func (k *KatibUIHandler) FetchAllHPJobs(w http.ResponseWriter, r *http.Request) {
// Use "" to get experiments in all namespaces.
jobs, err := k.getExperimentList("", JobTypeHP)
// At first, try to list experiments in cluster scope
jobs, err := k.getExperimentList([]string{""}, JobTypeHP)
if err != nil {
// If failed, just try to list experiments from own namespace
jobs, err = k.getExperimentList([]string{}, JobTypeHP)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
9 changes: 6 additions & 3 deletions pkg/ui/v1alpha3/nas.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import (
)

func (k *KatibUIHandler) FetchAllNASJobs(w http.ResponseWriter, r *http.Request) {
//enableCors(&w)
// Use "" to get experiments in all namespaces.
jobs, err := k.getExperimentList("", JobTypeNAS)
// At first, try to list experiments in cluster scope
jobs, err := k.getExperimentList([]string{""}, JobTypeHP)
if err != nil {
// If failed, just try to list experiments from own namespace
jobs, err = k.getExperimentList([]string{}, JobTypeHP)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
30 changes: 23 additions & 7 deletions pkg/ui/v1alpha3/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1alpha3

import (
"encoding/json"
"github.com/kubeflow/katib/pkg/controller.v1alpha3/consts"
"log"
"net/http"
"strconv"
Expand All @@ -12,10 +13,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (k *KatibUIHandler) getExperimentList(namespace string, typ JobType) ([]JobView, error) {
func (k *KatibUIHandler) getExperimentList(namespace []string, typ JobType) ([]JobView, error) {
jobs := make([]JobView, 0)

el, err := k.katibClient.GetExperimentList(namespace)
el, err := k.katibClient.GetExperimentList(namespace...)
if err != nil {
log.Printf("GetExperimentList failed: %v", err)
return nil, err
Expand Down Expand Up @@ -50,16 +51,15 @@ func enableCors(w *http.ResponseWriter) {
func (k *KatibUIHandler) getTrialTemplatesViewList() ([]TrialTemplatesView, error) {
trialTemplatesViewList := make([]TrialTemplatesView, 0)

// Get all namespaces
namespaceList, err := k.katibClient.GetNamespaceList()
// Get all available namespaces
namespaces, err := k.getAvailableNamespaces()
if err != nil {
log.Printf("GetNamespaceList failed: %v", err)
log.Printf("GetAvailableNamespaces failed: %v", err)
return nil, err
}

// Get Trial Template ConfigMap for each namespace
for _, namespace := range namespaceList.Items {
ns := namespace.ObjectMeta.Name
for _, ns := range namespaces {
trialTemplatesConfigMapList, err := k.katibClient.GetTrialTemplates(ns)
if err != nil {
log.Printf("GetTrialTemplates failed: %v", err)
Expand All @@ -72,6 +72,22 @@ func (k *KatibUIHandler) getTrialTemplatesViewList() ([]TrialTemplatesView, erro
}
return trialTemplatesViewList, nil
}

func (k *KatibUIHandler) getAvailableNamespaces() ([]string, error) {
var namespaces []string

namespaceList, err := k.katibClient.GetNamespaceList()
if err != nil {
namespaces = append(namespaces, consts.DefaultKatibNamespace)
return namespaces, nil
}
for _, ns := range namespaceList.Items {
namespaces = append(namespaces, ns.ObjectMeta.Name)
}

return namespaces, nil
}

func getTrialTemplatesView(templatesConfigMapList *apiv1.ConfigMapList) TrialTemplatesView {

trialTemplateView := TrialTemplatesView{
Expand Down

0 comments on commit 109d8f8

Please sign in to comment.