Skip to content

Commit

Permalink
feat: support namespace for trial template
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <gaoce@caicloud.io>
  • Loading branch information
gaocegege committed Sep 28, 2019
1 parent 45b3da8 commit c9ab036
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
21 changes: 13 additions & 8 deletions pkg/ui/v1alpha3/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/ui/v1alpha3/frontend/src/actions/templateActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -70,6 +70,7 @@ class TrialSpecParam extends React.Component {
<TextField
className={"Trial Namespace"}
value={this.props.trialNamespace}
onChange={this.onTrialNamespaceChange}
/>
</Grid>
</Grid>
Expand Down
4 changes: 0 additions & 4 deletions pkg/ui/v1alpha3/frontend/src/components/Templates/Trial.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ const styles = theme => ({
});

class Trial extends React.Component {

componentDidMount() {
this.props.fetchTrialTemplates();
}

openAddDialog = () => {
this.props.openDialog("add");
Expand Down
7 changes: 4 additions & 3 deletions pkg/ui/v1alpha3/frontend/src/sagas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions pkg/util/v1alpha3/katibclient/katib_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c9ab036

Please sign in to comment.