2014-10-28 00:56:33 +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 (
|
2015-01-20 03:25:06 +00:00
|
|
|
"net"
|
2014-10-28 00:56:33 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-01-12 05:33:25 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
|
2014-10-28 00:56:33 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m *Master) serviceWriterLoop(stop chan struct{}) {
|
|
|
|
for {
|
|
|
|
// Update service & endpoint records.
|
|
|
|
// TODO: when it becomes possible to change this stuff,
|
|
|
|
// stop polling and start watching.
|
|
|
|
// TODO: add endpoints of all replicas, not just the elected master.
|
2015-01-19 21:50:00 +00:00
|
|
|
if err := m.createMasterNamespaceIfNeeded(api.NamespaceDefault); err != nil {
|
|
|
|
glog.Errorf("Can't create master namespace: %v", err)
|
|
|
|
}
|
2015-01-20 03:25:06 +00:00
|
|
|
if m.serviceReadWriteIP != nil {
|
|
|
|
if err := m.createMasterServiceIfNeeded("kubernetes", m.serviceReadWriteIP, m.serviceReadWritePort); err != nil {
|
2014-10-28 00:56:33 +00:00
|
|
|
glog.Errorf("Can't create rw service: %v", err)
|
|
|
|
}
|
2015-02-19 03:54:15 +00:00
|
|
|
if err := m.ensureEndpointsContain("kubernetes", m.publicIP, m.publicReadWritePort); err != nil {
|
2014-10-28 00:56:33 +00:00
|
|
|
glog.Errorf("Can't create rw endpoints: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2014-10-28 23:49:52 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Master) roServiceWriterLoop(stop chan struct{}) {
|
|
|
|
for {
|
|
|
|
// Update service & endpoint records.
|
|
|
|
// TODO: when it becomes possible to change this stuff,
|
|
|
|
// stop polling and start watching.
|
2015-01-19 21:50:00 +00:00
|
|
|
if err := m.createMasterNamespaceIfNeeded(api.NamespaceDefault); err != nil {
|
|
|
|
glog.Errorf("Can't create master namespace: %v", err)
|
|
|
|
}
|
2015-01-20 03:25:06 +00:00
|
|
|
if m.serviceReadOnlyIP != nil {
|
|
|
|
if err := m.createMasterServiceIfNeeded("kubernetes-ro", m.serviceReadOnlyIP, m.serviceReadOnlyPort); err != nil {
|
2014-10-28 00:56:33 +00:00
|
|
|
glog.Errorf("Can't create ro service: %v", err)
|
|
|
|
}
|
2015-02-19 03:54:15 +00:00
|
|
|
if err := m.ensureEndpointsContain("kubernetes-ro", m.publicIP, m.publicReadOnlyPort); err != nil {
|
2014-10-28 23:49:52 +00:00
|
|
|
glog.Errorf("Can't create ro endpoints: %v", err)
|
2014-10-28 00:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 21:50:00 +00:00
|
|
|
// createMasterNamespaceIfNeeded will create the namespace that contains the master services if it doesn't already exist
|
|
|
|
func (m *Master) createMasterNamespaceIfNeeded(ns string) error {
|
|
|
|
ctx := api.NewContext()
|
|
|
|
if _, err := m.namespaceRegistry.Get(ctx, api.NamespaceDefault); err == nil {
|
|
|
|
// the namespace already exists
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
namespace := &api.Namespace{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: ns,
|
|
|
|
Namespace: "",
|
|
|
|
},
|
|
|
|
}
|
2015-02-10 14:26:26 +00:00
|
|
|
_, err := m.storage["namespaces"].(apiserver.RESTCreater).Create(ctx, namespace)
|
|
|
|
return err
|
2015-01-19 21:50:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 00:56:33 +00:00
|
|
|
// createMasterServiceIfNeeded will create the specified service if it
|
|
|
|
// doesn't already exist.
|
2015-01-20 03:25:06 +00:00
|
|
|
func (m *Master) createMasterServiceIfNeeded(serviceName string, serviceIP net.IP, servicePort int) error {
|
2014-10-28 00:56:33 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
if _, err := m.serviceRegistry.GetService(ctx, serviceName); err == nil {
|
|
|
|
// The service already exists.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
svc := &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: serviceName,
|
2014-11-18 17:49:00 +00:00
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
Labels: map[string]string{"provider": "kubernetes", "component": "apiserver"},
|
2014-10-28 00:56:33 +00:00
|
|
|
},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
2015-01-20 03:25:06 +00:00
|
|
|
Port: servicePort,
|
2014-11-18 17:49:00 +00:00
|
|
|
// maintained by this code, not by the pod selector
|
2015-01-26 17:52:50 +00:00
|
|
|
Selector: nil,
|
|
|
|
PortalIP: serviceIP.String(),
|
|
|
|
Protocol: api.ProtocolTCP,
|
|
|
|
SessionAffinity: api.AffinityTypeNone,
|
2014-10-30 13:29:11 +00:00
|
|
|
},
|
2014-10-28 00:56:33 +00:00
|
|
|
}
|
2015-02-10 14:26:26 +00:00
|
|
|
_, err := m.storage["services"].(apiserver.RESTCreater).Create(ctx, svc)
|
|
|
|
return err
|
2014-10-28 00:56:33 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 23:49:52 +00:00
|
|
|
// ensureEndpointsContain sets the endpoints for the given service. Also removes
|
|
|
|
// excess endpoints (as determined by m.masterCount). Extra endpoints could appear
|
|
|
|
// in the list if, for example, the master starts running on a different machine,
|
|
|
|
// changing IP addresses.
|
2015-02-19 03:54:15 +00:00
|
|
|
func (m *Master) ensureEndpointsContain(serviceName string, ip net.IP, port int) error {
|
2014-10-28 00:56:33 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
e, err := m.endpointRegistry.GetEndpoints(ctx, serviceName)
|
2015-02-23 21:53:21 +00:00
|
|
|
if err != nil || e.Protocol != api.ProtocolTCP {
|
2014-11-18 17:49:00 +00:00
|
|
|
e = &api.Endpoints{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: serviceName,
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
2015-02-23 21:53:21 +00:00
|
|
|
Protocol: api.ProtocolTCP,
|
2014-11-18 17:49:00 +00:00
|
|
|
}
|
2014-10-28 00:56:33 +00:00
|
|
|
}
|
2014-10-28 23:49:52 +00:00
|
|
|
found := false
|
|
|
|
for i := range e.Endpoints {
|
2015-02-19 03:54:15 +00:00
|
|
|
ep := &e.Endpoints[i]
|
2015-02-23 21:53:21 +00:00
|
|
|
if ep.IP == ip.String() && ep.Port == port {
|
|
|
|
found = true
|
|
|
|
break
|
2014-10-28 23:49:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2015-02-23 21:53:21 +00:00
|
|
|
e.Endpoints = append(e.Endpoints, api.Endpoint{IP: ip.String(), Port: port})
|
2015-03-07 10:00:45 +00:00
|
|
|
if len(e.Endpoints) > m.masterCount {
|
|
|
|
// We append to the end and remove from the beginning, so this should
|
|
|
|
// converge rapidly with all masters performing this operation.
|
|
|
|
e.Endpoints = e.Endpoints[len(e.Endpoints)-m.masterCount:]
|
|
|
|
}
|
|
|
|
return m.endpointRegistry.UpdateEndpoints(ctx, e)
|
2014-10-28 23:49:52 +00:00
|
|
|
}
|
2015-03-07 10:00:45 +00:00
|
|
|
// We didn't make any changes, no need to actually call update.
|
|
|
|
return nil
|
2014-10-28 00:56:33 +00:00
|
|
|
}
|