From 829553816b90c8470b07bcb2a14d9e9f7fc69439 Mon Sep 17 00:00:00 2001 From: hougang liu Date: Thu, 9 May 2019 10:19:28 +0800 Subject: [PATCH] add common package --- pkg/{types.go => common/v1alpha1/common.go} | 2 +- pkg/common/v1alpha2/common.go | 31 +++++++++++++++++++ .../v1alpha1/studyjob/katib_api_util.go | 6 ++-- .../v1alpha1/studyjob/manifest_parser.go | 4 +-- .../v1alpha1/studyjob/studyjob_controller.go | 4 +-- .../v1alpha2/experiment/util/manifest_util.go | 4 +-- pkg/suggestion/v1alpha1/grid_service.go | 4 +-- pkg/suggestion/v1alpha1/hyperband_service.go | 4 +-- pkg/suggestion/v1alpha1/random_service.go | 4 +-- pkg/ui/ui.go | 4 +-- 10 files changed, 49 insertions(+), 18 deletions(-) rename pkg/{types.go => common/v1alpha1/common.go} (97%) create mode 100644 pkg/common/v1alpha2/common.go diff --git a/pkg/types.go b/pkg/common/v1alpha1/common.go similarity index 97% rename from pkg/types.go rename to pkg/common/v1alpha1/common.go index 7f0135d2001..24d19653b04 100644 --- a/pkg/types.go +++ b/pkg/common/v1alpha1/common.go @@ -1,4 +1,4 @@ -package pkg +package v1alpha1 import ( "os" diff --git a/pkg/common/v1alpha2/common.go b/pkg/common/v1alpha2/common.go new file mode 100644 index 00000000000..d0ff11559d6 --- /dev/null +++ b/pkg/common/v1alpha2/common.go @@ -0,0 +1,31 @@ +package v1alpha2 + +import ( + "os" + + experimentsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2" +) + +const ( + KatibManagerServiceIPEnvName = "KATIB_MANAGER_PORT_6789_TCP_ADDR" + KatibManagerServicePortEnvName = "KATIB_MANAGER_PORT_6789_TCP_PORT" + KatibManagerServiceNamespaceEnvName = "KATIB_MANAGER_NAMESPACE" + KatibManagerService = "katib-manager" + KatibManagerPort = "6789" + ManagerAddr = KatibManagerService + ":" + KatibManagerPort +) + +func GetManagerAddr() string { + ns := os.Getenv(experimentsv1alpha2.DefaultKatibNamespaceEnvName) + if len(ns) == 0 { + addr := os.Getenv(KatibManagerServiceIPEnvName) + port := os.Getenv(KatibManagerServicePortEnvName) + if len(addr) > 0 && len(port) > 0 { + return addr + ":" + port + } else { + return ManagerAddr + } + } else { + return KatibManagerService + "." + ns + ":" + KatibManagerPort + } +} diff --git a/pkg/controller/v1alpha1/studyjob/katib_api_util.go b/pkg/controller/v1alpha1/studyjob/katib_api_util.go index 1949b6109e5..a578518dfa1 100644 --- a/pkg/controller/v1alpha1/studyjob/katib_api_util.go +++ b/pkg/controller/v1alpha1/studyjob/katib_api_util.go @@ -21,7 +21,7 @@ import ( "log" "reflect" - "github.com/kubeflow/katib/pkg" + common "github.com/kubeflow/katib/pkg/common/v1alpha1" katibv1alpha1 "github.com/kubeflow/katib/pkg/api/operators/apis/studyjob/v1alpha1" katibapi "github.com/kubeflow/katib/pkg/api/v1alpha1" "google.golang.org/grpc" @@ -37,7 +37,7 @@ func initializeStudy(instance *katibv1alpha1.StudyJob) error { instance.Status.Condition = katibv1alpha1.ConditionRunning - conn, err := grpc.Dial(pkg.ManagerAddr, grpc.WithInsecure()) + conn, err := grpc.Dial(common.ManagerAddr, grpc.WithInsecure()) if err != nil { log.Printf("Connect katib manager error %v", err) instance.Status.Condition = katibv1alpha1.ConditionFailed @@ -248,7 +248,7 @@ func populateConfigForNAS(instance *katibv1alpha1.StudyJob) (*katibapi.StudyConf } func deleteStudy(instance *katibv1alpha1.StudyJob) error { - conn, err := grpc.Dial(pkg.ManagerAddr, grpc.WithInsecure()) + conn, err := grpc.Dial(common.ManagerAddr, grpc.WithInsecure()) if err != nil { log.Printf("Connect katib manager error %v", err) return err diff --git a/pkg/controller/v1alpha1/studyjob/manifest_parser.go b/pkg/controller/v1alpha1/studyjob/manifest_parser.go index e2dc760708d..936e2bb501d 100644 --- a/pkg/controller/v1alpha1/studyjob/manifest_parser.go +++ b/pkg/controller/v1alpha1/studyjob/manifest_parser.go @@ -20,7 +20,7 @@ import ( "fmt" "text/template" - "github.com/kubeflow/katib/pkg" + common "github.com/kubeflow/katib/pkg/common/v1alpha1" katibv1alpha1 "github.com/kubeflow/katib/pkg/api/operators/apis/studyjob/v1alpha1" katibapi "github.com/kubeflow/katib/pkg/api/v1alpha1" "github.com/kubeflow/katib/pkg/manager/v1alpha1/studyjobclient" @@ -100,7 +100,7 @@ func getMetricsCollectorManifest(studyID string, trialID string, workerID string "WorkerID": workerID, "WorkerKind": workerKind, "NameSpace": namespace, - "ManagerSerivce": pkg.GetManagerAddr(), + "ManagerSerivce": common.GetManagerAddr(), } if mcs != nil && mcs.GoTemplate.RawTemplate != "" { mtp, err = template.New("MetricsCollector").Parse(mcs.GoTemplate.RawTemplate) diff --git a/pkg/controller/v1alpha1/studyjob/studyjob_controller.go b/pkg/controller/v1alpha1/studyjob/studyjob_controller.go index fe4f0a6f6bb..94654917d3a 100644 --- a/pkg/controller/v1alpha1/studyjob/studyjob_controller.go +++ b/pkg/controller/v1alpha1/studyjob/studyjob_controller.go @@ -20,7 +20,7 @@ import ( "log" "strconv" - "github.com/kubeflow/katib/pkg" + common "github.com/kubeflow/katib/pkg/common/v1alpha1" katibv1alpha1 "github.com/kubeflow/katib/pkg/api/operators/apis/studyjob/v1alpha1" katibapi "github.com/kubeflow/katib/pkg/api/v1alpha1" pytorchjobv1beta1 "github.com/kubeflow/pytorch-operator/pkg/apis/pytorch/v1beta1" @@ -484,7 +484,7 @@ func (r *ReconcileStudyJobController) checkStatus(instance *katibv1alpha1.StudyJ grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)), } - conn, err := grpc.Dial(pkg.ManagerAddr, opts...) + conn, err := grpc.Dial(common.ManagerAddr, opts...) if err != nil { log.Printf("Connect katib manager error %v", err) instance.Status.Condition = katibv1alpha1.ConditionFailed diff --git a/pkg/controller/v1alpha2/experiment/util/manifest_util.go b/pkg/controller/v1alpha2/experiment/util/manifest_util.go index 888a14389b4..8395a471053 100644 --- a/pkg/controller/v1alpha2/experiment/util/manifest_util.go +++ b/pkg/controller/v1alpha2/experiment/util/manifest_util.go @@ -20,7 +20,7 @@ import ( "strings" "text/template" - "github.com/kubeflow/katib/pkg" + common "github.com/kubeflow/katib/pkg/common/v1alpha2" katibv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2" "github.com/kubeflow/katib/pkg/util/v1alpha2/katibclient" @@ -35,7 +35,7 @@ func getMetricsCollectorManifest(experimentName string, trialName string, jobKin "Trial": trialName, "JobKind": jobKind, "NameSpace": namespace, - "ManagerService": pkg.GetManagerAddr(), + "ManagerService": common.GetManagerAddr(), "MetricNames": strings.Join(metricNames, ";"), } if mcs != nil && mcs.GoTemplate.RawTemplate != "" { diff --git a/pkg/suggestion/v1alpha1/grid_service.go b/pkg/suggestion/v1alpha1/grid_service.go index 1d14e5c3c43..f820d1c4abe 100644 --- a/pkg/suggestion/v1alpha1/grid_service.go +++ b/pkg/suggestion/v1alpha1/grid_service.go @@ -6,7 +6,7 @@ import ( "log" "strconv" - "github.com/kubeflow/katib/pkg" + common "github.com/kubeflow/katib/pkg/common/v1alpha1" "github.com/kubeflow/katib/pkg/api/v1alpha1" "google.golang.org/grpc" @@ -138,7 +138,7 @@ func (s *GridSuggestService) genGrids(studyID string, pcs []*api.ParameterConfig } func (s *GridSuggestService) GetSuggestions(ctx context.Context, in *api.GetSuggestionsRequest) (*api.GetSuggestionsReply, error) { - conn, err := grpc.Dial(pkg.ManagerAddr, grpc.WithInsecure()) + conn, err := grpc.Dial(common.ManagerAddr, grpc.WithInsecure()) if err != nil { log.Printf("could not connect: %v", err) return &api.GetSuggestionsReply{}, err diff --git a/pkg/suggestion/v1alpha1/hyperband_service.go b/pkg/suggestion/v1alpha1/hyperband_service.go index 5ddae45213b..f92aeb3e11f 100644 --- a/pkg/suggestion/v1alpha1/hyperband_service.go +++ b/pkg/suggestion/v1alpha1/hyperband_service.go @@ -9,7 +9,7 @@ import ( "strconv" "strings" - "github.com/kubeflow/katib/pkg" + common "github.com/kubeflow/katib/pkg/common/v1alpha1" "github.com/kubeflow/katib/pkg/api/v1alpha1" "google.golang.org/grpc" @@ -403,7 +403,7 @@ func (h *HyperBandSuggestService) shLoopParamUpdate(studyID string, hbparam *Hyp } func (h *HyperBandSuggestService) GetSuggestions(ctx context.Context, in *api.GetSuggestionsRequest) (*api.GetSuggestionsReply, error) { - conn, err := grpc.Dial(pkg.ManagerAddr, grpc.WithInsecure()) + conn, err := grpc.Dial(common.ManagerAddr, grpc.WithInsecure()) if err != nil { log.Fatalf("could not connect: %v", err) return &api.GetSuggestionsReply{}, err diff --git a/pkg/suggestion/v1alpha1/random_service.go b/pkg/suggestion/v1alpha1/random_service.go index 6b9c31bb88f..89f27f898d1 100644 --- a/pkg/suggestion/v1alpha1/random_service.go +++ b/pkg/suggestion/v1alpha1/random_service.go @@ -7,7 +7,7 @@ import ( "strconv" "time" - "github.com/kubeflow/katib/pkg" + common "github.com/kubeflow/katib/pkg/common/v1alpha1" "github.com/kubeflow/katib/pkg/api/v1alpha1" "google.golang.org/grpc" ) @@ -33,7 +33,7 @@ func (s *RandomSuggestService) IntRandom(min, max int) int { } func (s *RandomSuggestService) GetSuggestions(ctx context.Context, in *api.GetSuggestionsRequest) (*api.GetSuggestionsReply, error) { - conn, err := grpc.Dial(pkg.ManagerAddr, grpc.WithInsecure()) + conn, err := grpc.Dial(common.ManagerAddr, grpc.WithInsecure()) if err != nil { log.Fatalf("could not connect: %v", err) return &api.GetSuggestionsReply{}, err diff --git a/pkg/ui/ui.go b/pkg/ui/ui.go index 239ec288990..d1fec49119a 100644 --- a/pkg/ui/ui.go +++ b/pkg/ui/ui.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/kubeflow/katib/pkg" + common "github.com/kubeflow/katib/pkg/common/v1alpha1" studyjobv1alpha1 "github.com/kubeflow/katib/pkg/api/operators/apis/studyjob/v1alpha1" "github.com/kubeflow/katib/pkg/api/v1alpha1" "github.com/kubeflow/katib/pkg/manager/v1alpha1/studyjobclient" @@ -54,7 +54,7 @@ func (k *KatibUIHandler) connectManager() (*grpc.ClientConn, api.ManagerClient, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)), } - conn, err := grpc.Dial(pkg.ManagerAddr, opts...) + conn, err := grpc.Dial(common.ManagerAddr, opts...) if err != nil { log.Printf("Connect katib manager error %v", err) return nil, nil, err