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

Add Katib Client in v1alpha2 #480

Merged
merged 7 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
117 changes: 19 additions & 98 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 106 additions & 2 deletions pkg/controller/v1alpha2/experiment/util/client_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,118 @@ limitations under the License.
package util

import (
"context"
"io/ioutil"
"strings"

experimentv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
cl "sigs.k8s.io/controller-runtime/pkg/client"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate with line 26

"sigs.k8s.io/controller-runtime/pkg/client/config"
)

func NewClient(options client.Options) (client.Client, error) {
type katibClient struct {
client client.Client
}

const (
trialTemplatesName = "trial-template"
metricsCollectorTemplatesName = "metricscollector-template"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding it in pkg/api/operators/apis/experiment/v1alpha2/constants.go? trialTemplate is already defined

)

func NewClient(options client.Options) (*katibClient, error) {
cfg, err := config.GetConfig()
if err != nil {
return nil, err
}
return client.New(cfg, options)
cl, err := client.New(cfg, options)
return &katibClient{
client: cl,
}, nil
}

func (k *katibClient) GetExperimentList(namespace ...string) (*experimentv1alpha2.ExperimentList, error) {
ns := getNamespace(namespace...)
expList := &experimentv1alpha2.ExperimentList{}
listOpt := cl.InNamespace(ns)

if err := k.client.List(context.Background(), listOpt, expList); err != nil {
return expList, err
}
return expList, nil

}

func (k *katibClient) CreateExperiment(experiment *experimentv1alpha2.Experiment, namespace ...string) error {

if err := k.client.Create(context.Background(), experiment); err != nil {
return err
}
return nil
}

func (k *katibClient) GetTrialTemplates(namespace ...string) (map[string]string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now, trial template can be stored in any configmap, not just trial-template

Copy link
Member Author

@andreyvelich andreyvelich Apr 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to get Trial templates configmap?


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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest move getConfigMap into this file, too and then please call getConfigMap

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if err := k.client.Get(context.Background(), types.NamespacedName{Name: trialTemplatesName, Namespace: ns}, trialTemplates); err != nil {
return nil, err
}
return trialTemplates.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: trialTemplatesName, Namespace: ns}, trialTemplates); err != nil {
return err
}
trialTemplates.Data = newTrialTemplates

if err := k.client.Update(context.Background(), trialTemplates); err != nil {
return err
}
return nil
}

func (k *katibClient) GetMetricsCollectorTemplates(namespace ...string) (map[string]string, error) {

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

if err := k.client.Get(context.Background(), types.NamespacedName{Name: metricsCollectorTemplatesName, Namespace: ns}, metricsCollectorTemplates); err != nil {
return nil, err
}
return metricsCollectorTemplates.Data, nil

}

func (k *katibClient) UpdateMetricsCollectorTemplates(newMCTemplates map[string]string, namespace ...string) error {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

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

if err := k.client.Get(context.Background(), types.NamespacedName{Name: metricsCollectorTemplatesName, Namespace: ns}, metricsCollectorTemplates); err != nil {
return err
}

metricsCollectorTemplates.Data = newMCTemplates

if err := k.client.Update(context.Background(), metricsCollectorTemplates); err != nil {
return err
}
return nil
}

func getNamespace(namespace ...string) string {
if len(namespace) == 0 {
data, _ := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
return strings.TrimSpace(string(data))
}
return namespace[0]
}
6 changes: 3 additions & 3 deletions pkg/controller/v1alpha2/experiment/util/template_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"errors"
"text/template"

apiv1alpha2 "github.com/kubeflow/katib/pkg/api/v1alpha2"
experimentsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2"
apiv1alpha2 "github.com/kubeflow/katib/pkg/api/v1alpha2"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -83,12 +83,12 @@ func getTrialTemplate(instance *experimentsv1alpha2.Experiment) (*template.Templ
}

func getConfigMap(name, namespace string) (map[string]string, error) {
client, err := NewClient(client.Options{})
katibClient, err := NewClient(client.Options{})
if err != nil {
return map[string]string{}, err
}
configMap := &apiv1.ConfigMap{}
if err := client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: namespace}, configMap); err != nil {
if err := katibClient.client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: namespace}, configMap); err != nil {
return map[string]string{}, err
}
return configMap.Data, nil
Expand Down