From c9ab0361a86dc5deec6678ef6446fe0641018aa4 Mon Sep 17 00:00:00 2001 From: Ce Gao Date: Sat, 28 Sep 2019 09:51:46 +0800 Subject: [PATCH] feat: support namespace for trial template Signed-off-by: Ce Gao --- pkg/ui/v1alpha3/backend.go | 21 ++++++++++++------- .../frontend/src/actions/templateActions.js | 3 ++- .../src/components/HP/Create/Params/Trial.jsx | 5 +++-- .../src/components/Templates/Trial.jsx | 4 ---- pkg/ui/v1alpha3/frontend/src/sagas/index.js | 7 ++++--- pkg/util/v1alpha3/katibclient/katib_client.go | 8 +++++-- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/pkg/ui/v1alpha3/backend.go b/pkg/ui/v1alpha3/backend.go index 1996fd2920d..63375e648fe 100644 --- a/pkg/ui/v1alpha3/backend.go +++ b/pkg/ui/v1alpha3/backend.go @@ -9,18 +9,18 @@ import ( "strings" "time" + "github.com/ghodss/yaml" + "google.golang.org/grpc" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" + "sigs.k8s.io/controller-runtime/pkg/client" + experimentv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/experiments/v1alpha3" trialsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/trials/v1alpha3" api_pb_v1alpha3 "github.com/kubeflow/katib/pkg/apis/manager/v1alpha3" common_v1alpha3 "github.com/kubeflow/katib/pkg/common/v1alpha3" - + "github.com/kubeflow/katib/pkg/controller.v1alpha3/consts" "github.com/kubeflow/katib/pkg/util/v1alpha3/katibclient" - "google.golang.org/grpc" - "sigs.k8s.io/controller-runtime/pkg/client" - - "github.com/ghodss/yaml" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" ) func NewKatibUIHandler() *KatibUIHandler { @@ -390,9 +390,14 @@ func (k *KatibUIHandler) FetchNASJobInfo(w http.ResponseWriter, r *http.Request) w.Write(response) } +// FetchTrialTemplates gets the trial templates for the given namespace. func (k *KatibUIHandler) FetchTrialTemplates(w http.ResponseWriter, r *http.Request) { //enableCors(&w) - trialTemplates, err := k.katibClient.GetTrialTemplates() + namespace := r.URL.Query()["namespace"][0] + if namespace == "" { + namespace = consts.DefaultKatibNamespace + } + trialTemplates, err := k.katibClient.GetTrialTemplates(namespace) if err != nil { log.Printf("GetTrialTemplate failed: %v", err) http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/pkg/ui/v1alpha3/frontend/src/actions/templateActions.js b/pkg/ui/v1alpha3/frontend/src/actions/templateActions.js index addc5142f3c..dda0f15b577 100644 --- a/pkg/ui/v1alpha3/frontend/src/actions/templateActions.js +++ b/pkg/ui/v1alpha3/frontend/src/actions/templateActions.js @@ -23,8 +23,9 @@ export const FETCH_TRIAL_TEMPLATES_REQUEST = "FETCH_TRIAL_TEMPLATES_REQUEST" export const FETCH_TRIAL_TEMPLATES_SUCCESS = "FETCH_TRIAL_TEMPLATES_SUCCESS" export const FETCH_TRIAL_TEMPLATES_FAILURE = "FETCH_TRIAL_TEMPLATES_FAILURE" -export const fetchTrialTemplates = () => ({ +export const fetchTrialTemplates = (namespace) => ({ type: FETCH_TRIAL_TEMPLATES_REQUEST, + namespace }) export const ADD_TEMPLATE_REQUEST = "ADD_TEMPLATE_REQUEST" diff --git a/pkg/ui/v1alpha3/frontend/src/components/HP/Create/Params/Trial.jsx b/pkg/ui/v1alpha3/frontend/src/components/HP/Create/Params/Trial.jsx index fac16afb1a1..ad031d15afa 100644 --- a/pkg/ui/v1alpha3/frontend/src/components/HP/Create/Params/Trial.jsx +++ b/pkg/ui/v1alpha3/frontend/src/components/HP/Create/Params/Trial.jsx @@ -42,8 +42,8 @@ const styles = theme => ({ class TrialSpecParam extends React.Component { - componentDidMount() { - this.props.fetchTrialTemplates(); + onTrialNamespaceChange = (event) => { + this.props.fetchTrialTemplates(event.target.value); } onTrialChange = (event) => { @@ -70,6 +70,7 @@ class TrialSpecParam extends React.Component { diff --git a/pkg/ui/v1alpha3/frontend/src/components/Templates/Trial.jsx b/pkg/ui/v1alpha3/frontend/src/components/Templates/Trial.jsx index b595799a833..f2e15d6e8bc 100644 --- a/pkg/ui/v1alpha3/frontend/src/components/Templates/Trial.jsx +++ b/pkg/ui/v1alpha3/frontend/src/components/Templates/Trial.jsx @@ -19,10 +19,6 @@ const styles = theme => ({ }); class Trial extends React.Component { - - componentDidMount() { - this.props.fetchTrialTemplates(); - } openAddDialog = () => { this.props.openDialog("add"); diff --git a/pkg/ui/v1alpha3/frontend/src/sagas/index.js b/pkg/ui/v1alpha3/frontend/src/sagas/index.js index d4ad37580d7..203208ccb38 100644 --- a/pkg/ui/v1alpha3/frontend/src/sagas/index.js +++ b/pkg/ui/v1alpha3/frontend/src/sagas/index.js @@ -413,7 +413,8 @@ export const fetchTrialTemplates = function *() { const action = yield take(templateActions.FETCH_TRIAL_TEMPLATES_REQUEST); try { const result = yield call( - goFetchTrialTemplates + goFetchTrialTemplates, + action.namespace ) if (result.status === 200) { let data = Object.assign(result.data, {}) @@ -441,11 +442,11 @@ export const fetchTrialTemplates = function *() { } } -const goFetchTrialTemplates = function *() { +const goFetchTrialTemplates = function *(namespace) { try { const result = yield call( axios.get, - '/katib/fetch_trial_templates/', + `/katib/fetch_trial_templates/?namespace=${namespace}`, ) return result } catch (err) { diff --git a/pkg/util/v1alpha3/katibclient/katib_client.go b/pkg/util/v1alpha3/katibclient/katib_client.go index df1253dcc7b..2dcd2f6a965 100644 --- a/pkg/util/v1alpha3/katibclient/katib_client.go +++ b/pkg/util/v1alpha3/katibclient/katib_client.go @@ -21,6 +21,7 @@ import ( "strings" apiv1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes/scheme" "sigs.k8s.io/controller-runtime/pkg/client" @@ -150,7 +151,10 @@ func (k *KatibClient) GetTrialTemplates(namespace ...string) (map[string]string, ns := getNamespace(namespace...) trialTemplates := &apiv1.ConfigMap{} - if err := k.client.Get(context.Background(), types.NamespacedName{Name: experimentsv1alpha3.DefaultTrialConfigMapName, Namespace: ns}, trialTemplates); err != nil { + err := k.client.Get(context.TODO(), types.NamespacedName{Name: experimentsv1alpha3.DefaultTrialConfigMapName, Namespace: ns}, trialTemplates) + if err != nil && errors.IsNotFound(err) { + return map[string]string{}, nil + } else if err != nil { return nil, err } return trialTemplates.Data, nil @@ -161,7 +165,7 @@ func (k *KatibClient) UpdateTrialTemplates(newTrialTemplates map[string]string, ns := getNamespace(namespace...) trialTemplates := &apiv1.ConfigMap{} - if err := k.client.Get(context.Background(), types.NamespacedName{Name: experimentsv1alpha3.DefaultTrialConfigMapName, Namespace: ns}, trialTemplates); err != nil { + if err := k.client.Get(context.TODO(), types.NamespacedName{Name: experimentsv1alpha3.DefaultTrialConfigMapName, Namespace: ns}, trialTemplates); err != nil { return err } trialTemplates.Data = newTrialTemplates