2014-06-16 06:29:07 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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 master
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
2014-06-17 17:50:42 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
2014-06-16 06:29:07 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry"
|
2014-06-28 22:35:51 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/scheduler"
|
2014-06-16 06:29:07 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
"github.com/coreos/go-etcd/etcd"
|
2014-06-27 18:04:25 +00:00
|
|
|
"github.com/golang/glog"
|
2014-06-16 06:29:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Master contains state for a Kubernetes cluster master/api server.
|
|
|
|
type Master struct {
|
|
|
|
podRegistry registry.PodRegistry
|
|
|
|
controllerRegistry registry.ControllerRegistry
|
|
|
|
serviceRegistry registry.ServiceRegistry
|
2014-06-20 01:31:38 +00:00
|
|
|
minionRegistry registry.MinionRegistry
|
2014-07-15 10:47:40 +00:00
|
|
|
storage map[string]apiserver.RESTStorage
|
2014-07-18 20:22:26 +00:00
|
|
|
client *client.Client
|
2014-06-16 06:29:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 11:54:05 +00:00
|
|
|
// NewMemoryServer returns a new instance of Master backed with memory (not etcd).
|
2014-07-18 20:22:26 +00:00
|
|
|
func NewMemoryServer(minions []string, podInfoGetter client.PodInfoGetter, cloud cloudprovider.Interface, client *client.Client) *Master {
|
2014-06-16 06:29:07 +00:00
|
|
|
m := &Master{
|
|
|
|
podRegistry: registry.MakeMemoryRegistry(),
|
|
|
|
controllerRegistry: registry.MakeMemoryRegistry(),
|
|
|
|
serviceRegistry: registry.MakeMemoryRegistry(),
|
2014-06-20 01:31:38 +00:00
|
|
|
minionRegistry: registry.MakeMinionRegistry(minions),
|
2014-07-18 20:22:26 +00:00
|
|
|
client: client,
|
2014-06-16 06:29:07 +00:00
|
|
|
}
|
2014-07-02 00:08:32 +00:00
|
|
|
m.init(cloud, podInfoGetter)
|
2014-06-16 06:29:07 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-07-15 11:54:05 +00:00
|
|
|
// New returns a new instance of Master connected to the given etcdServer.
|
2014-07-18 03:54:54 +00:00
|
|
|
func New(etcdServers, minions []string, podInfoGetter client.PodInfoGetter, cloud cloudprovider.Interface, minionRegexp string, client *client.Client, healthCheckMinions bool, cacheMinionsTTL time.Duration) *Master {
|
2014-06-16 06:29:07 +00:00
|
|
|
etcdClient := etcd.NewClient(etcdServers)
|
2014-07-18 03:54:54 +00:00
|
|
|
minionRegistry := minionRegistryMaker(minions, cloud, minionRegexp, healthCheckMinions, cacheMinionsTTL)
|
2014-06-16 06:29:07 +00:00
|
|
|
m := &Master{
|
2014-06-20 01:31:38 +00:00
|
|
|
podRegistry: registry.MakeEtcdRegistry(etcdClient, minionRegistry),
|
|
|
|
controllerRegistry: registry.MakeEtcdRegistry(etcdClient, minionRegistry),
|
|
|
|
serviceRegistry: registry.MakeEtcdRegistry(etcdClient, minionRegistry),
|
|
|
|
minionRegistry: minionRegistry,
|
2014-07-18 20:22:26 +00:00
|
|
|
client: client,
|
2014-06-16 06:29:07 +00:00
|
|
|
}
|
2014-07-02 00:08:32 +00:00
|
|
|
m.init(cloud, podInfoGetter)
|
2014-06-16 06:29:07 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:54:54 +00:00
|
|
|
func minionRegistryMaker(minions []string, cloud cloudprovider.Interface, minionRegexp string, healthCheck bool, cacheTTL time.Duration) registry.MinionRegistry {
|
|
|
|
var minionRegistry registry.MinionRegistry
|
2014-07-02 00:08:32 +00:00
|
|
|
if cloud != nil && len(minionRegexp) > 0 {
|
2014-07-18 03:54:54 +00:00
|
|
|
var err error
|
|
|
|
minionRegistry, err = registry.MakeCloudMinionRegistry(cloud, minionRegexp)
|
2014-07-02 00:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to initalize cloud minion registry reverting to static registry (%#v)", err)
|
|
|
|
}
|
2014-06-16 06:29:07 +00:00
|
|
|
}
|
2014-07-18 03:54:54 +00:00
|
|
|
if minionRegistry == nil {
|
|
|
|
minionRegistry = registry.MakeMinionRegistry(minions)
|
|
|
|
}
|
|
|
|
if healthCheck {
|
|
|
|
minionRegistry = registry.NewHealthyMinionRegistry(minionRegistry, &http.Client{})
|
|
|
|
}
|
|
|
|
if cacheTTL > 0 {
|
|
|
|
cachingMinionRegistry, err := registry.NewCachingMinionRegistry(minionRegistry, cacheTTL)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to initialize caching layer, ignoring cache.")
|
|
|
|
} else {
|
|
|
|
minionRegistry = cachingMinionRegistry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return minionRegistry
|
2014-07-02 00:08:32 +00:00
|
|
|
}
|
2014-06-16 06:29:07 +00:00
|
|
|
|
2014-07-02 00:08:32 +00:00
|
|
|
func (m *Master) init(cloud cloudprovider.Interface, podInfoGetter client.PodInfoGetter) {
|
|
|
|
podCache := NewPodCache(podInfoGetter, m.podRegistry, time.Second*30)
|
2014-06-19 05:24:06 +00:00
|
|
|
go podCache.Loop()
|
2014-07-15 10:47:40 +00:00
|
|
|
random := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
|
|
|
|
s := scheduler.NewRandomFitScheduler(m.podRegistry, random)
|
2014-06-16 06:29:07 +00:00
|
|
|
m.storage = map[string]apiserver.RESTStorage{
|
2014-07-02 00:08:32 +00:00
|
|
|
"pods": registry.MakePodRegistryStorage(m.podRegistry, podInfoGetter, s, m.minionRegistry, cloud, podCache),
|
2014-07-11 13:46:22 +00:00
|
|
|
"replicationControllers": registry.NewControllerRegistryStorage(m.controllerRegistry, m.podRegistry),
|
2014-06-20 01:31:38 +00:00
|
|
|
"services": registry.MakeServiceRegistryStorage(m.serviceRegistry, cloud, m.minionRegistry),
|
|
|
|
"minions": registry.MakeMinionRegistryStorage(m.minionRegistry),
|
2014-06-16 06:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-15 11:54:05 +00:00
|
|
|
// Run begins serving the Kubernetes API. It never returns.
|
2014-06-16 06:29:07 +00:00
|
|
|
func (m *Master) Run(myAddress, apiPrefix string) error {
|
2014-07-18 20:22:26 +00:00
|
|
|
endpoints := registry.MakeEndpointController(m.serviceRegistry, m.client)
|
2014-06-16 06:29:07 +00:00
|
|
|
go util.Forever(func() { endpoints.SyncServiceEndpoints() }, time.Second*10)
|
|
|
|
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: myAddress,
|
|
|
|
Handler: apiserver.New(m.storage, apiPrefix),
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
MaxHeaderBytes: 1 << 20,
|
|
|
|
}
|
|
|
|
return s.ListenAndServe()
|
|
|
|
}
|
2014-06-24 01:28:06 +00:00
|
|
|
|
2014-07-15 13:03:08 +00:00
|
|
|
// ConstructHandler returns an http.Handler which serves the Kubernetes API.
|
2014-07-11 10:10:24 +00:00
|
|
|
// Instead of calling Run, you can call this function to get a handler for your own server.
|
2014-07-15 11:54:05 +00:00
|
|
|
// It is intended for testing. Only call once.
|
2014-06-24 01:28:06 +00:00
|
|
|
func (m *Master) ConstructHandler(apiPrefix string) http.Handler {
|
2014-07-18 20:22:26 +00:00
|
|
|
endpoints := registry.MakeEndpointController(m.serviceRegistry, m.client)
|
2014-06-24 01:28:06 +00:00
|
|
|
go util.Forever(func() { endpoints.SyncServiceEndpoints() }, time.Second*10)
|
|
|
|
|
|
|
|
return apiserver.New(m.storage, apiPrefix)
|
|
|
|
}
|