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 1 commit
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
6 changes: 6 additions & 0 deletions pkg/api/operators/apis/experiment/v1alpha2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ const (

// Default value of Spec.TemplatePath
DefaultTrialTemplatePath = "defaultTrialTemplate.yaml"

// Name of the configMap for Trial templates
TrialTemplatesName = "trial-template"
Copy link
Member

Choose a reason for hiding this comment

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

line no 23 has it added already


// Name of the configMap for MetricsCollector templates
MetricsCollectorTemplatesName = "metrics-collector-template"
Copy link
Member

Choose a reason for hiding this comment

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

Is this the default metric config map similar to trial?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I will rename it, like for Trial template

)
25 changes: 14 additions & 11 deletions pkg/controller/v1alpha2/experiment/util/client_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ import (
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"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

type katibClient struct {
client client.Client
}

const (
trialTemplatesName = "trial-template"
metricsCollectorTemplatesName = "metricscollector-template"
)

func NewClient(options client.Options) (*katibClient, error) {
cfg, err := config.GetConfig()
if err != nil {
Expand All @@ -51,7 +45,7 @@ func NewClient(options client.Options) (*katibClient, error) {
func (k *katibClient) GetExperimentList(namespace ...string) (*experimentv1alpha2.ExperimentList, error) {
ns := getNamespace(namespace...)
expList := &experimentv1alpha2.ExperimentList{}
listOpt := cl.InNamespace(ns)
listOpt := client.InNamespace(ns)

if err := k.client.List(context.Background(), listOpt, expList); err != nil {
return expList, err
Expand All @@ -68,12 +62,21 @@ func (k *katibClient) CreateExperiment(experiment *experimentv1alpha2.Experiment
return nil
}

func (k *katibClient) GetConfigMap(name string, namespace ...string) (map[string]string, error) {
ns := getNamespace(namespace...)
configMap := &apiv1.ConfigMap{}
if err := k.client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: ns}, configMap); err != nil {
return map[string]string{}, err
}
return configMap.Data, 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 {
if err := k.client.Get(context.Background(), types.NamespacedName{Name: experimentv1alpha2.TrialTemplatesName, Namespace: ns}, trialTemplates); err != nil {
return nil, err
}
return trialTemplates.Data, nil
Expand All @@ -84,7 +87,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: trialTemplatesName, Namespace: ns}, trialTemplates); err != nil {
if err := k.client.Get(context.Background(), types.NamespacedName{Name: experimentv1alpha2.TrialTemplatesName, Namespace: ns}, trialTemplates); err != nil {
return err
}
trialTemplates.Data = newTrialTemplates
Expand All @@ -100,7 +103,7 @@ func (k *katibClient) GetMetricsCollectorTemplates(namespace ...string) (map[str
ns := getNamespace(namespace...)
metricsCollectorTemplates := &apiv1.ConfigMap{}

if err := k.client.Get(context.Background(), types.NamespacedName{Name: metricsCollectorTemplatesName, Namespace: ns}, metricsCollectorTemplates); err != nil {
if err := k.client.Get(context.Background(), types.NamespacedName{Name: experimentv1alpha2.MetricsCollectorTemplatesName, Namespace: ns}, metricsCollectorTemplates); err != nil {
return nil, err
}
return metricsCollectorTemplates.Data, nil
Expand All @@ -112,7 +115,7 @@ func (k *katibClient) UpdateMetricsCollectorTemplates(newMCTemplates map[string]
ns := getNamespace(namespace...)
metricsCollectorTemplates := &apiv1.ConfigMap{}

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

Expand Down
22 changes: 6 additions & 16 deletions pkg/controller/v1alpha2/experiment/util/template_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ package util

import (
"bytes"
"context"
"errors"
"text/template"

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"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -64,10 +61,15 @@ func getTrialTemplate(instance *experimentsv1alpha2.Experiment) (*template.Templ
configMapName := templateSpec.ConfigMapName
templatePath := templateSpec.TemplatePath

configMap, err := getConfigMap(configMapName, configMapNS)
katibClient, err := NewClient(client.Options{})
if err != nil {
return nil, err
}
configMap, err := katibClient.GetConfigMap(configMapName, configMapNS)
if err != nil {
return nil, err
}

if configMapTemplate, ok := configMap[templatePath]; !ok {
err = errors.New(string(metav1.StatusReasonNotFound))
return nil, err
Expand All @@ -81,15 +83,3 @@ func getTrialTemplate(instance *experimentsv1alpha2.Experiment) (*template.Templ

return tpl, nil
}

func getConfigMap(name, namespace string) (map[string]string, error) {
katibClient, err := NewClient(client.Options{})
if err != nil {
return map[string]string{}, err
}
configMap := &apiv1.ConfigMap{}
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
}