Skip to content

Commit

Permalink
support worker and metricsCollector template spec
Browse files Browse the repository at this point in the history
  • Loading branch information
hougangliu committed Feb 14, 2019
1 parent a5c8e02 commit 0b76b20
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 50 deletions.
5 changes: 4 additions & 1 deletion examples/gpu-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ spec:
- ftrl
workerSpec:
goTemplate:
templatePath: "/worker-template/gpuWorkerTemplate.yaml"
templateSpec:
configMapName: "worker-template"
templatePath: "gpuWorkerTemplate.yaml"
configMapNamespace: "kubeflow"
suggestionSpec:
suggestionAlgorithm: "random"
requestNumber: 3
5 changes: 4 additions & 1 deletion examples/grid-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ spec:
- ftrl
workerSpec:
goTemplate:
templatePath: "/worker-template/cpuWorkerTemplate.yaml"
templateSpec:
configMapName: "worker-template"
templatePath: "cpuWorkerTemplate.yaml"
configMapNamespace: "kubeflow"
suggestionSpec:
suggestionAlgorithm: "grid"
suggestionParameters:
Expand Down
5 changes: 4 additions & 1 deletion examples/hypb-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ spec:
max: "20"
workerSpec:
goTemplate:
templatePath: "/worker-template/cpuWorkerTemplate.yaml"
templateSpec:
configMapName: "worker-template"
templatePath: "cpuWorkerTemplate.yaml"
configMapNamespace: "kubeflow"
suggestionSpec:
suggestionAlgorithm: "hyperband"
suggestionParameters:
Expand Down
12 changes: 9 additions & 3 deletions pkg/api/operators/apis/studyjob/v1alpha1/studyjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,25 @@ const (
OptimizationTypeMaximize OptimizationType = "maximize"
)

type GoTemplate struct {
type TemplateSpec struct {
ConfigMapName string `json:"configMapName,omitempty"`
ConfigMapNamespace string `json:"configMapNamespace,omitempty"`
TemplatePath string `json:"templatePath,omitempty"`
}

type GoTemplate struct {
TemplateSpec *TemplateSpec `json:"templateSpec,omitempty"`
RawTemplate string `json:"rawTemplate,omitempty"`
}

type WorkerSpec struct {
Retain bool `json:"retain,omitempty"`
GoTemplate GoTemplate `json:"goTemplate,omitempty"`
GoTemplate *GoTemplate `json:"goTemplate,omitempty"`
}

type MetricsCollectorSpec struct {
Retain bool `json:"retain,omitempty"`
GoTemplate GoTemplate `json:"goTemplate,omitempty"`
GoTemplate *GoTemplate `json:"goTemplate,omitempty"`
}

type ServiceParameter struct {
Expand Down
97 changes: 53 additions & 44 deletions pkg/controller/studyjob/manifest_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package studyjob
import (
"bytes"
"context"
"fmt"
"text/template"

katibapi "github.com/kubeflow/katib/pkg/api"
Expand All @@ -28,30 +27,50 @@ import (
"k8s.io/apimachinery/pkg/util/uuid"
)

func getWorkerManifest(c katibapi.ManagerClient, studyID string, trial *katibapi.Trial, workerSpec *katibv1alpha1.WorkerSpec, kind string, ns string, dryrun bool) (string, *bytes.Buffer, error) {
var wtp *template.Template = nil
var err error
if workerSpec != nil && workerSpec.GoTemplate.RawTemplate != "" {
wtp, err = template.New("Worker").Parse(workerSpec.GoTemplate.RawTemplate)
func getTemplateStrFromConfigMap(cNamespace, cName, tPath string) (string, error) {
sjc, err := studyjobclient.NewStudyjobClient(nil)
if err != nil {
return "", err
}
return sjc.GetTemplate(cNamespace, cName, tPath)
}

func getTemplateStr(goTemplate *katibv1alpha1.GoTemplate, getDefaultTemplateSpec func()(string,string,string)) (string, error) {
if goTemplate.RawTemplate != "" {
return goTemplate.RawTemplate, nil
} else {
wPath := "defaultWorkerTemplate.yaml"
if workerSpec != nil && workerSpec.GoTemplate.TemplatePath != "" {
wPath = workerSpec.GoTemplate.TemplatePath
}
sjc, err := studyjobclient.NewStudyjobClient(nil)
if err != nil {
return "", nil, err
}
wtl, err := sjc.GetWorkerTemplates()
if err != nil {
return "", nil, err
}
if wt, ok := wtl[wPath]; !ok {
return "", nil, fmt.Errorf("No worker template name %s", wPath)
} else {
wtp, err = template.New("Worker").Parse(wt)
tName, tNamespace, tPath := getDefaultTemplateSpec()
if goTemplate.TemplateSpec != nil {
tName = goTemplate.TemplateSpec.ConfigMapName
tNamespace = goTemplate.TemplateSpec.ConfigMapNamespace
tPath = goTemplate.TemplateSpec.TemplatePath
}
return getTemplateStrFromConfigMap(tNamespace, tName, tPath)
}
}

func getDefaultWorkerTemplateSpec() (string, string, string) {
return getKatibNamespace(), "worker-template", "defaultWorkerTemplate.yaml"
}

func getDefaultMetricsTemplateSpec() (string, string, string) {
return getKatibNamespace(), "metricscollector-template", "defaultMetricsCollectorTemplate.yaml"
}

func getWorkerTemplateStr(workerSpec *katibv1alpha1.WorkerSpec) (string, error) {
if workerSpec == nil || workerSpec.GoTemplate == nil {
return getTemplateStrFromConfigMap(getDefaultWorkerTemplateSpec())
} else {
return getTemplateStr(workerSpec.GoTemplate, getDefaultWorkerTemplateSpec)
}
}

func getWorkerManifest(c katibapi.ManagerClient, studyID string, trial *katibapi.Trial, workerSpec *katibv1alpha1.WorkerSpec, kind string, ns string, dryrun bool) (string, *bytes.Buffer, error) {
wStr, err := getWorkerTemplateStr(workerSpec)
if err != nil {
return "", nil, err
}
wtp, err := template.New("Worker").Parse(wStr)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -91,9 +110,15 @@ func getWorkerManifest(c katibapi.ManagerClient, studyID string, trial *katibapi
return wid, &b, nil
}

func getMetricsCollectorTemplateStr(mcs *katibv1alpha1.MetricsCollectorSpec) (string, error) {
if mcs == nil || mcs.GoTemplate == nil {
return getTemplateStrFromConfigMap(getDefaultMetricsTemplateSpec())
} else {
return getTemplateStr(mcs.GoTemplate, getDefaultMetricsTemplateSpec)
}
}

func getMetricsCollectorManifest(studyID string, trialID string, workerID string, workerKind string, namespace string, mcs *katibv1alpha1.MetricsCollectorSpec) (*bytes.Buffer, error) {
var mtp *template.Template = nil
var err error
tmpValues := map[string]string{
"StudyID": studyID,
"TrialID": trialID,
Expand All @@ -102,27 +127,11 @@ func getMetricsCollectorManifest(studyID string, trialID string, workerID string
"NameSpace": namespace,
"ManagerSerivce": pkg.GetManagerAddr(),
}
if mcs != nil && mcs.GoTemplate.RawTemplate != "" {
mtp, err = template.New("MetricsCollector").Parse(mcs.GoTemplate.RawTemplate)
} else {
mctp := "defaultMetricsCollectorTemplate.yaml"
if mcs != nil && mcs.GoTemplate.TemplatePath != "" {
mctp = mcs.GoTemplate.TemplatePath
}
sjc, err := studyjobclient.NewStudyjobClient(nil)
if err != nil {
return nil, err
}
mtl, err := sjc.GetMetricsCollectorTemplates()
if err != nil {
return nil, err
}
if mt, ok := mtl[mctp]; !ok {
return nil, fmt.Errorf("No MetricsCollector template name %s", mctp)
} else {
mtp, err = template.New("MetricsCollector").Parse(mt)
}
tStr, err := getMetricsCollectorTemplateStr(mcs)
if err != nil {
return nil, err
}
mtp, err := template.New("MetricsCollector").Parse(tStr)
if err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller/studyjob/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ package studyjob

import (
"fmt"
"io/ioutil"
"log"
"strings"

katibapi "github.com/kubeflow/katib/pkg/api"
katibv1alpha1 "github.com/kubeflow/katib/pkg/api/operators/apis/studyjob/v1alpha1"
Expand Down Expand Up @@ -209,3 +211,8 @@ func contains(l []string, s string) bool {
}
return false
}

func getKatibNamespace() string {
data, _ := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
return strings.TrimSpace(string(data))
}
13 changes: 13 additions & 0 deletions pkg/manager/studyjobclient/studyjobclient.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package studyjobclient

import (
"fmt"
"io/ioutil"
"strings"

Expand Down Expand Up @@ -102,6 +103,18 @@ func (s *StudyjobClient) GetMetricsCollectorTemplates(namespace ...string) (map[
return cm.Data, nil
}

func (s *StudyjobClient) GetTemplate(namespace, name, path string) (string, error) {
cm, err := s.clientset.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return "", err
}
if _, ok := cm.Data[path]; !ok {
return "", fmt.Errorf("No tamplate name %s in configMap %s/%s", path, namespace, name)
} else {
return cm.Data[path], nil
}
}

func (s *StudyjobClient) UpdateMetricsCollectorTemplates(newMCTemplates map[string]string, namespace ...string) error {
ns := getNamespace(namespace...)
cm, err := s.clientset.CoreV1().ConfigMaps(ns).Get("metricscollector-template", metav1.GetOptions{})
Expand Down

0 comments on commit 0b76b20

Please sign in to comment.