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 (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2014-09-26 23:28:30 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-11 23:01:29 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2014-09-11 17:02:53 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
2014-09-11 17:04:13 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2"
|
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-15 23:01:33 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/binding"
|
2014-08-11 07:34:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/controller"
|
2014-08-14 19:48:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/endpoint"
|
2014-08-11 07:34:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/etcd"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/pod"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/service"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-09-11 23:01:29 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
|
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
|
2014-09-11 23:01:29 +00:00
|
|
|
EtcdHelper tools.EtcdHelper
|
2014-07-27 02:16:39 +00:00
|
|
|
HealthCheckMinions bool
|
|
|
|
Minions []string
|
|
|
|
MinionCacheTTL time.Duration
|
|
|
|
MinionRegexp string
|
|
|
|
PodInfoGetter client.PodInfoGetter
|
2014-09-26 23:28:30 +00:00
|
|
|
NodeResources api.NodeResources
|
2014-07-27 02:16:39 +00:00
|
|
|
}
|
|
|
|
|
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
|
2014-08-14 19:48:34 +00:00
|
|
|
endpointRegistry endpoint.Registry
|
2014-08-11 07:34:59 +00:00
|
|
|
minionRegistry minion.Registry
|
2014-08-15 23:01:33 +00:00
|
|
|
bindingRegistry binding.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-09-11 23:01:29 +00:00
|
|
|
// NewEtcdHelper returns an EtcdHelper for the provided arguments or an error if the version
|
|
|
|
// is incorrect.
|
|
|
|
func NewEtcdHelper(etcdServers []string, version string) (helper tools.EtcdHelper, err error) {
|
|
|
|
client := goetcd.NewClient(etcdServers)
|
|
|
|
if version == "" {
|
|
|
|
version = latest.Version
|
|
|
|
}
|
2014-09-25 22:08:09 +00:00
|
|
|
versionInterfaces, err := latest.InterfacesFor(version)
|
2014-09-11 23:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return helper, err
|
|
|
|
}
|
2014-09-25 22:08:09 +00:00
|
|
|
return tools.EtcdHelper{client, versionInterfaces.Codec, versionInterfaces.ResourceVersioner}, nil
|
2014-09-11 23:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new instance of Master connected to the given etcd server.
|
2014-07-27 02:16:39 +00:00
|
|
|
func New(c *Config) *Master {
|
2014-08-20 00:41:06 +00:00
|
|
|
minionRegistry := makeMinionRegistry(c)
|
2014-09-23 20:43:48 +00:00
|
|
|
serviceRegistry := etcd.NewRegistry(c.EtcdHelper, nil)
|
|
|
|
manifestFactory := &pod.BasicManifestFactory{
|
|
|
|
ServiceRegistry: serviceRegistry,
|
|
|
|
}
|
2014-06-16 06:29:07 +00:00
|
|
|
m := &Master{
|
2014-09-23 20:43:48 +00:00
|
|
|
podRegistry: etcd.NewRegistry(c.EtcdHelper, manifestFactory),
|
|
|
|
controllerRegistry: etcd.NewRegistry(c.EtcdHelper, nil),
|
|
|
|
serviceRegistry: serviceRegistry,
|
|
|
|
endpointRegistry: etcd.NewRegistry(c.EtcdHelper, nil),
|
|
|
|
bindingRegistry: etcd.NewRegistry(c.EtcdHelper, manifestFactory),
|
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-20 00:41:06 +00:00
|
|
|
func makeMinionRegistry(c *Config) minion.Registry {
|
2014-08-11 07:34:59 +00:00
|
|
|
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-09-26 23:28:30 +00:00
|
|
|
minionRegistry, err = minion.NewCloudRegistry(c.Cloud, c.MinionRegexp, &c.NodeResources)
|
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-09-26 23:28:30 +00:00
|
|
|
minionRegistry = minion.NewRegistry(c.Minions, c.NodeResources)
|
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) {
|
2014-08-20 00:33:54 +00:00
|
|
|
podCache := NewPodCache(podInfoGetter, m.podRegistry)
|
|
|
|
go util.Forever(func() { podCache.UpdateAllContainers() }, time.Second*30)
|
2014-08-04 20:09:49 +00:00
|
|
|
|
2014-06-16 06:29:07 +00:00
|
|
|
m.storage = map[string]apiserver.RESTStorage{
|
2014-09-08 21:40:56 +00:00
|
|
|
"pods": pod.NewREST(&pod.RESTConfig{
|
2014-08-11 07:34:59 +00:00
|
|
|
CloudProvider: cloud,
|
|
|
|
PodCache: podCache,
|
|
|
|
PodInfoGetter: podInfoGetter,
|
|
|
|
Registry: m.podRegistry,
|
2014-09-17 22:26:01 +00:00
|
|
|
Minions: m.client,
|
2014-08-11 07:34:59 +00:00
|
|
|
}),
|
2014-09-08 21:40:56 +00:00
|
|
|
"replicationControllers": controller.NewREST(m.controllerRegistry, m.podRegistry),
|
|
|
|
"services": service.NewREST(m.serviceRegistry, cloud, m.minionRegistry),
|
|
|
|
"endpoints": endpoint.NewREST(m.endpointRegistry),
|
|
|
|
"minions": minion.NewREST(m.minionRegistry),
|
2014-08-15 23:01:33 +00:00
|
|
|
|
|
|
|
// TODO: should appear only in scheduler API group.
|
2014-09-08 21:40:56 +00:00
|
|
|
"bindings": binding.NewREST(m.bindingRegistry),
|
2014-06-16 06:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// API_v1beta1 returns the resources and codec for API version v1beta1.
|
2014-09-26 00:20:28 +00:00
|
|
|
func (m *Master) API_v1beta1() (map[string]apiserver.RESTStorage, runtime.Codec, string, runtime.SelfLinker) {
|
2014-08-09 21:12:55 +00:00
|
|
|
storage := make(map[string]apiserver.RESTStorage)
|
|
|
|
for k, v := range m.storage {
|
|
|
|
storage[k] = v
|
2014-06-16 06:29:07 +00:00
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
return storage, v1beta1.Codec, "/api/v1beta1", latest.SelfLinker
|
2014-06-24 01:28:06 +00:00
|
|
|
}
|
2014-09-11 17:04:13 +00:00
|
|
|
|
|
|
|
// API_v1beta2 returns the resources and codec for API version v1beta2.
|
2014-09-26 00:20:28 +00:00
|
|
|
func (m *Master) API_v1beta2() (map[string]apiserver.RESTStorage, runtime.Codec, string, runtime.SelfLinker) {
|
2014-09-11 17:04:13 +00:00
|
|
|
storage := make(map[string]apiserver.RESTStorage)
|
|
|
|
for k, v := range m.storage {
|
|
|
|
storage[k] = v
|
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
return storage, v1beta2.Codec, "/api/v1beta1", latest.SelfLinker
|
2014-09-11 17:04:13 +00:00
|
|
|
}
|