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"
|
|
|
|
|
2014-08-06 03:10:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-06-16 06:29:07 +00:00
|
|
|
"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-08-11 07:34:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/controller"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/endpoint"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/etcd"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/memory"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/pod"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/service"
|
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"
|
2014-08-11 07:34:59 +00:00
|
|
|
|
|
|
|
goetcd "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
|
|
|
)
|
|
|
|
|
2014-07-27 02:16:39 +00:00
|
|
|
// Config is a structure used to configure a Master.
|
|
|
|
type Config struct {
|
|
|
|
Client *client.Client
|
|
|
|
Cloud cloudprovider.Interface
|
|
|
|
EtcdServers []string
|
|
|
|
HealthCheckMinions bool
|
|
|
|
Minions []string
|
|
|
|
MinionCacheTTL time.Duration
|
|
|
|
MinionRegexp string
|
|
|
|
PodInfoGetter client.PodInfoGetter
|
|
|
|
}
|
|
|
|
|
2014-06-16 06:29:07 +00:00
|
|
|
// Master contains state for a Kubernetes cluster master/api server.
|
|
|
|
type Master struct {
|
2014-08-11 07:34:59 +00:00
|
|
|
podRegistry pod.Registry
|
|
|
|
controllerRegistry controller.Registry
|
|
|
|
serviceRegistry service.Registry
|
|
|
|
minionRegistry minion.Registry
|
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-27 02:16:39 +00:00
|
|
|
func NewMemoryServer(c *Config) *Master {
|
2014-06-16 06:29:07 +00:00
|
|
|
m := &Master{
|
2014-08-11 07:34:59 +00:00
|
|
|
podRegistry: memory.NewRegistry(),
|
|
|
|
controllerRegistry: memory.NewRegistry(),
|
|
|
|
serviceRegistry: memory.NewRegistry(),
|
|
|
|
minionRegistry: minion.NewRegistry(c.Minions),
|
2014-07-27 02:16:39 +00:00
|
|
|
client: c.Client,
|
2014-06-16 06:29:07 +00:00
|
|
|
}
|
2014-07-27 02:16:39 +00:00
|
|
|
m.init(c.Cloud, c.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-27 02:16:39 +00:00
|
|
|
func New(c *Config) *Master {
|
2014-08-11 07:34:59 +00:00
|
|
|
etcdClient := goetcd.NewClient(c.EtcdServers)
|
2014-07-27 02:16:39 +00:00
|
|
|
minionRegistry := minionRegistryMaker(c)
|
2014-06-16 06:29:07 +00:00
|
|
|
m := &Master{
|
2014-08-11 07:34:59 +00:00
|
|
|
podRegistry: etcd.NewRegistry(etcdClient, minionRegistry),
|
|
|
|
controllerRegistry: etcd.NewRegistry(etcdClient, minionRegistry),
|
|
|
|
serviceRegistry: etcd.NewRegistry(etcdClient, minionRegistry),
|
2014-06-20 01:31:38 +00:00
|
|
|
minionRegistry: minionRegistry,
|
2014-07-27 02:16:39 +00:00
|
|
|
client: c.Client,
|
2014-06-16 06:29:07 +00:00
|
|
|
}
|
2014-07-27 02:16:39 +00:00
|
|
|
m.init(c.Cloud, c.PodInfoGetter)
|
2014-06-16 06:29:07 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-08-11 07:34:59 +00:00
|
|
|
func minionRegistryMaker(c *Config) minion.Registry {
|
|
|
|
var minionRegistry minion.Registry
|
2014-07-27 02:16:39 +00:00
|
|
|
if c.Cloud != nil && len(c.MinionRegexp) > 0 {
|
2014-07-18 03:54:54 +00:00
|
|
|
var err error
|
2014-08-11 07:34:59 +00:00
|
|
|
minionRegistry, err = minion.NewCloudRegistry(c.Cloud, c.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 {
|
2014-08-11 07:34:59 +00:00
|
|
|
minionRegistry = minion.NewRegistry(c.Minions)
|
2014-07-18 03:54:54 +00:00
|
|
|
}
|
2014-07-27 02:16:39 +00:00
|
|
|
if c.HealthCheckMinions {
|
2014-08-11 07:34:59 +00:00
|
|
|
minionRegistry = minion.NewHealthyRegistry(minionRegistry, &http.Client{})
|
2014-07-18 03:54:54 +00:00
|
|
|
}
|
2014-07-27 02:16:39 +00:00
|
|
|
if c.MinionCacheTTL > 0 {
|
2014-08-11 07:34:59 +00:00
|
|
|
cachingMinionRegistry, err := minion.NewCachingRegistry(minionRegistry, c.MinionCacheTTL)
|
2014-07-18 03:54:54 +00:00
|
|
|
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-08-04 20:09:49 +00:00
|
|
|
|
2014-08-11 07:34:59 +00:00
|
|
|
endpoints := endpoint.NewEndpointController(m.serviceRegistry, m.client)
|
2014-08-04 20:09:49 +00:00
|
|
|
go util.Forever(func() { endpoints.SyncServiceEndpoints() }, time.Second*10)
|
|
|
|
|
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-08-11 07:34:59 +00:00
|
|
|
"pods": pod.NewRegistryStorage(&pod.RegistryStorageConfig{
|
|
|
|
CloudProvider: cloud,
|
|
|
|
MinionLister: m.minionRegistry,
|
|
|
|
PodCache: podCache,
|
|
|
|
PodInfoGetter: podInfoGetter,
|
|
|
|
Registry: m.podRegistry,
|
|
|
|
Scheduler: s,
|
|
|
|
}),
|
|
|
|
"replicationControllers": controller.NewRegistryStorage(m.controllerRegistry, m.podRegistry),
|
|
|
|
"services": service.NewRegistryStorage(m.serviceRegistry, cloud, m.minionRegistry),
|
|
|
|
"minions": minion.NewRegistryStorage(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 {
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: myAddress,
|
2014-08-04 20:11:25 +00:00
|
|
|
Handler: m.ConstructHandler(apiPrefix),
|
2014-06-16 06:29:07 +00:00
|
|
|
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-08-06 03:10:48 +00:00
|
|
|
return apiserver.New(m.storage, api.Codec, apiPrefix)
|
2014-06-24 01:28:06 +00:00
|
|
|
}
|