GenericPool is a pool of generic containers for an environment. GenericPoolManager keeps track of all GenericPools, creating them on-demand. The pool manager API is simply a "lookup" for the service URL of a function. If one exists it is returned immediately; otherwise, a generic pool is created, and then a pod is specialized from that pool. poolmgr is designed to run from within the cluster, since it connects to pod IP addresses directly. This is a first cut with many pieces missing. TODO: * Use versioned kubernetes clients instead of the unversioned one * Unit tests for GenericPoolMgr; improve unit test for GenericPool; test for API. * Handle cases where a service exists but pod backing it has failed. * On start up, use existing deployments/pods/services if they exist; in other words don't orphan resources on restart. * Kill idle resources (services, pods, even generic pools) * Autoscale generic pool (for example, by watching num ready pods)
78 lines
2.0 KiB
Go
78 lines
2.0 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 poolmgr
|
|
|
|
import (
|
|
"github.com/platform9/fission"
|
|
clientUnversioned "k8s.io/kubernetes/pkg/client/unversioned"
|
|
)
|
|
|
|
type (
|
|
GenericPoolManager struct {
|
|
pools map[fission.Environment]*GenericPool
|
|
kubernetesClient *clientUnversioned.Client
|
|
namespace string
|
|
|
|
requestChannel chan *request
|
|
}
|
|
request struct {
|
|
env *fission.Environment
|
|
responseChannel chan *response
|
|
}
|
|
response struct {
|
|
error
|
|
pool *GenericPool
|
|
}
|
|
)
|
|
|
|
func MakeGenericPoolManager(client *clientUnversioned.Client, namespace string) *GenericPoolManager {
|
|
gpm := &GenericPoolManager{
|
|
pools: make(map[fission.Environment]*GenericPool),
|
|
kubernetesClient: client,
|
|
namespace: namespace,
|
|
requestChannel: make(chan *request),
|
|
}
|
|
go gpm.service()
|
|
return gpm
|
|
}
|
|
|
|
func (gpm *GenericPoolManager) service() {
|
|
for {
|
|
select {
|
|
case req := <-gpm.requestChannel:
|
|
pool, ok := gpm.pools[*req.env]
|
|
if !ok {
|
|
pool, err := MakeGenericPool(gpm.kubernetesClient, req.env, 3, gpm.namespace)
|
|
if err != nil {
|
|
req.responseChannel <- &response{error: err}
|
|
continue
|
|
}
|
|
gpm.pools[*req.env] = pool
|
|
req.responseChannel <- &response{pool: pool}
|
|
}
|
|
req.responseChannel <- &response{pool: pool}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (gpm *GenericPoolManager) GetPool(env *fission.Environment) (*GenericPool, error) {
|
|
c := make(chan *response)
|
|
gpm.requestChannel <- &request{env: env, responseChannel: c}
|
|
resp := <-c
|
|
return resp.pool, resp.error
|
|
}
|