Poolmgr needs to know usage statistics for a function's pod. This change asynchronously taps poolmgr API when router uses a service. Poolmgr can use this information to control pod expiry. It may also be useful later as one of the metrics for autoscaling.
78 lines
1.7 KiB
Go
78 lines
1.7 KiB
Go
/*
|
|
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 client
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"bytes"
|
|
"encoding/json"
|
|
"github.com/platform9/fission"
|
|
"io/ioutil"
|
|
"net/url"
|
|
)
|
|
|
|
type Client struct {
|
|
poolmgrUrl string
|
|
}
|
|
|
|
func MakeClient(poolmgrUrl string) *Client {
|
|
return &Client{poolmgrUrl: strings.TrimSuffix(poolmgrUrl, "/")}
|
|
}
|
|
|
|
func (c *Client) GetServiceForFunction(metadata *fission.Metadata) (string, error) {
|
|
url := c.poolmgrUrl + "/v1/getServiceForFunction"
|
|
body, err := json.Marshal(metadata)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
resp, err := http.Post(url, "application/json", bytes.NewReader(body))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
return "", fission.MakeErrorFromHTTP(resp)
|
|
}
|
|
|
|
svcName, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(svcName), nil
|
|
}
|
|
|
|
func (c *Client) TapService(serviceUrl *url.URL) error {
|
|
url := c.poolmgrUrl + "/v1/tapService"
|
|
|
|
serviceUrlStr := serviceUrl.String()
|
|
|
|
resp, err := http.Post(url, "application/octet-stream", bytes.NewReader([]byte(serviceUrlStr)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != 200 {
|
|
return fission.MakeErrorFromHTTP(resp)
|
|
}
|
|
return nil
|
|
}
|