Poolmgr API tweaks + convenient Start function
This commit is contained in:
+5
-6
@@ -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)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to read request", 500)
|
http.Error(w, "Failed to read request", 500)
|
||||||
@@ -73,15 +73,14 @@ func (api *API) lookupApi(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
serviceUrl, err := api.lookup(&m)
|
serviceName, err := api.getServiceForFunction(&m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
code, msg := fission.GetHTTPError(err)
|
code, msg := fission.GetHTTPError(err)
|
||||||
log.Printf("Error: %v: %v", code, msg)
|
log.Printf("Error: %v: %v", code, msg)
|
||||||
http.Error(w, msg, code)
|
http.Error(w, msg, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
// return serviceUrl
|
w.Write([]byte(serviceName))
|
||||||
w.Write([]byte(serviceUrl))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *API) getFunctionEnv(m *fission.Metadata) (*fission.Environment, error) {
|
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
|
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
|
// Check function -> svc map
|
||||||
result, err := api.functionService.Get(m)
|
result, err := api.functionService.Get(m)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -153,7 +152,7 @@ func (api *API) lookup(m *fission.Metadata) (string, error) {
|
|||||||
|
|
||||||
func (api *API) Serve(port int) {
|
func (api *API) Serve(port int) {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/v1/lookup", api.lookupApi).Methods("GET")
|
r.HandleFunc("/v1/getServiceForFunction", api.getServiceForFunctionApi).Methods("POST")
|
||||||
|
|
||||||
address := fmt.Sprintf(":%v", port)
|
address := fmt.Sprintf(":%v", port)
|
||||||
log.Printf("starting poolmgr at port %v", port)
|
log.Printf("starting poolmgr at port %v", port)
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user