-
Notifications
You must be signed in to change notification settings - Fork 443
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
Changes from 3 commits
b9ff3c8
86cac8f
72e0c61
e3b7ffd
2a0ca93
5d7cb04
12a455a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
"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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for now, trial template can be stored in any configmap, not just There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest move getConfigMap into this file, too and then please call getConfigMap There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate with line 26