Skip to content

Commit

Permalink
feat: support namespace for trial template (#827)
Browse files Browse the repository at this point in the history
* WIP

Signed-off-by: Ce Gao <gaoce@caicloud.io>

* feat: support namespace for trial template

Signed-off-by: Ce Gao <gaoce@caicloud.io>

* fix: Use configmap function

Signed-off-by: Ce Gao <gaoce@caicloud.io>
  • Loading branch information
gaocegege authored and k8s-ci-robot committed Sep 29, 2019
1 parent 69904e9 commit afaf252
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 50 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 @@ -128,7 +128,7 @@ const HPParameters = (props) => {
goTemplate: {
templateSpec: {
configMapName: "trial-template",
configMapNamespace: data.metadata.namespace,
configMapNamespace: props.trialNamespace,
templatePath: props.trial,
}
}
Expand Down
76 changes: 49 additions & 27 deletions pkg/ui/v1alpha3/frontend/src/components/HP/Create/Params/Trial.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import FormControl from '@material-ui/core/FormControl';
import Select from '@material-ui/core/Select';
import TextField from '@material-ui/core/TextField';

import { connect } from 'react-redux';
import { changeTrial } from '../../../../actions/hpCreateActions';
Expand Down Expand Up @@ -41,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 @@ -54,38 +55,59 @@ class TrialSpecParam extends React.Component {

const { classes } = this.props
return (
<div className={classes.parameter}>
<Grid container alignItems={"center"}>
<Grid item xs={12} sm={3}>
<Typography variant={"subheading"}>
<Tooltip title={"Trial spec template"}>
<HelpOutlineIcon className={classes.help} color={"primary"}/>
</Tooltip>
{"TrialSpec"}
</Typography>
<div>
<div className={classes.parameter}>
<Grid container alignItems={"center"}>
<Grid item xs={12} sm={3}>
<Typography variant={"subheading"}>
<Tooltip title={"Trial namespace"}>
<HelpOutlineIcon className={classes.help} color={"primary"} />
</Tooltip>
{"Namespace"}
</Typography>
</Grid>
<Grid item xs={12} sm={8}>
<TextField
className={"Trial Namespace"}
value={this.props.trialNamespace}
onChange={this.onTrialNamespaceChange}
/>
</Grid>
</Grid>
<Grid item xs={12} sm={8}>
<FormControl variant="outlined" className={classes.formControl}>
<InputLabel>
Trial Spec
</div>
<div className={classes.parameter}>
<Grid container alignItems={"center"}>
<Grid item xs={12} sm={3}>
<Typography variant={"subheading"}>
<Tooltip title={"Trial spec template"}>
<HelpOutlineIcon className={classes.help} color={"primary"} />
</Tooltip>
{"TrialSpec"}
</Typography>
</Grid>
<Grid item xs={12} sm={8}>
<FormControl variant="outlined" className={classes.formControl}>
<InputLabel>
Trial Spec
</InputLabel>
<Select
value={this.props.trial}
onChange={this.onTrialChange}
input={
<OutlinedInput name={"TrialSpec"} labelWidth={100}/>
}
className={classes.select}
<Select
value={this.props.trial}
onChange={this.onTrialChange}
input={
<OutlinedInput name={"TrialSpec"} labelWidth={100} />
}
className={classes.select}
>
{names.map((spec, i) => {
return (
<MenuItem value={spec} key={i}>{spec}</MenuItem>
)
<MenuItem value={spec} key={i}>{spec}</MenuItem>
)
})}
</Select>
</FormControl>
</Select>
</FormControl>
</Grid>
</Grid>
</Grid>
</div>
</div>
)
}
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
15 changes: 9 additions & 6 deletions pkg/util/v1alpha3/katibclient/katib_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"

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 @@ -134,6 +135,7 @@ func (k *KatibClient) GetExperiment(name string, namespace ...string) (*experime
return exp, nil
}

// GetConfigMap returns the configmap for the given name and namespace.
func (k *KatibClient) GetConfigMap(name string, namespace ...string) (map[string]string, error) {
ns := getNamespace(namespace...)
configMap := &apiv1.ConfigMap{}
Expand All @@ -143,23 +145,24 @@ func (k *KatibClient) GetConfigMap(name string, namespace ...string) (map[string
return configMap.Data, nil
}

// GetTrialTemplates returns the trial template if it exists.
func (k *KatibClient) GetTrialTemplates(namespace ...string) (map[string]string, error) {

ns := getNamespace(namespace...)
trialTemplates := &apiv1.ConfigMap{}

if err := k.client.Get(context.Background(), types.NamespacedName{Name: experimentsv1alpha3.DefaultTrialConfigMapName, Namespace: ns}, trialTemplates); err != nil {
data, err := k.GetConfigMap(experimentsv1alpha3.DefaultTrialConfigMapName, ns)
if err != nil && errors.IsNotFound(err) {
return map[string]string{}, nil
} else if err != nil {
return nil, err
}
return trialTemplates.Data, nil

return data, nil
}

func (k *KatibClient) UpdateTrialTemplates(newTrialTemplates map[string]string, namespace ...string) error {
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 afaf252

Please sign in to comment.