From 6e86182e19b168fe7375ab0f07db09c6a780d36a Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Tue, 1 Nov 2016 18:14:23 -0700 Subject: [PATCH] Poolmgr API tweaks + convenient Start function --- poolmgr/api.go | 11 ++++---- poolmgr/poolmgr.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 poolmgr/poolmgr.go diff --git a/poolmgr/api.go b/poolmgr/api.go index d457650d..7928168f 100644 --- a/poolmgr/api.go +++ b/poolmgr/api.go @@ -58,7 +58,7 @@ func MakeAPI(gpm *GenericPoolManager, controller *controllerclient.Client) *API } } -func (api *API) lookupApi(w http.ResponseWriter, r *http.Request) { +func (api *API) getServiceForFunctionApi(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { http.Error(w, "Failed to read request", 500) @@ -73,15 +73,14 @@ func (api *API) lookupApi(w http.ResponseWriter, r *http.Request) { return } - serviceUrl, err := api.lookup(&m) + serviceName, err := api.getServiceForFunction(&m) if err != nil { code, msg := fission.GetHTTPError(err) log.Printf("Error: %v: %v", code, msg) http.Error(w, msg, code) } - // return serviceUrl - w.Write([]byte(serviceUrl)) + w.Write([]byte(serviceName)) } func (api *API) getFunctionEnv(m *fission.Metadata) (*fission.Environment, error) { @@ -112,7 +111,7 @@ func (api *API) getFunctionEnv(m *fission.Metadata) (*fission.Environment, error return env, nil } -func (api *API) lookup(m *fission.Metadata) (string, error) { +func (api *API) getServiceForFunction(m *fission.Metadata) (string, error) { // Check function -> svc map result, err := api.functionService.Get(m) if err == nil { @@ -153,7 +152,7 @@ func (api *API) lookup(m *fission.Metadata) (string, error) { func (api *API) Serve(port int) { r := mux.NewRouter() - r.HandleFunc("/v1/lookup", api.lookupApi).Methods("GET") + r.HandleFunc("/v1/getServiceForFunction", api.getServiceForFunctionApi).Methods("POST") address := fmt.Sprintf(":%v", port) log.Printf("starting poolmgr at port %v", port) diff --git a/poolmgr/poolmgr.go b/poolmgr/poolmgr.go new file mode 100644 index 00000000..6ac3ba4b --- /dev/null +++ b/poolmgr/poolmgr.go @@ -0,0 +1,65 @@ +/* +Copyright 2016 The Fission Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package poolmgr + +import ( + "log" + "strings" + + "k8s.io/client-go/1.4/kubernetes" + "k8s.io/client-go/1.4/rest" + + controllerclient "github.com/platform9/fission/controller/client" +) + +// Get a kubernetes client using the pod's service account. This only +// works when we're running inside a kubernetes cluster. +func getKubernetesClient() (*kubernetes.Clientset, error) { + // creates the in-cluster config + config, err := rest.InClusterConfig() + if err != nil { + log.Printf("Error getting kubernetes client config: %v", err) + return nil, err + } + + // creates the clientset + clientset, err := kubernetes.NewForConfig(config) + if err != nil { + log.Printf("Error getting kubernetes client: %v", err) + return nil, err + } + + return clientset, nil +} + +func StartPoolmgr(controllerUrl string, namespace string, port int) error { + controllerUrl = strings.TrimSuffix(controllerUrl, "/") + controllerClient := controllerclient.MakeClient(controllerUrl) + + kubernetesClient, err := getKubernetesClient() + if err != nil { + log.Printf("Failed to get kubernetes client: %v", err) + return err + } + + gpm := MakeGenericPoolManager(controllerUrl, kubernetesClient, namespace) + + api := MakeAPI(gpm, controllerClient) + go api.Serve(port) + + return nil +}