2014-06-06 23:40:48 +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.
|
|
|
|
*/
|
2014-06-18 21:18:53 +00:00
|
|
|
|
2014-08-11 07:34:59 +00:00
|
|
|
package service
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
import (
|
2014-06-17 17:50:42 +00:00
|
|
|
"fmt"
|
2014-08-25 21:36:15 +00:00
|
|
|
"math/rand"
|
2014-06-06 23:40:48 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2014-06-12 20:17:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-03 21:16:00 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-09-01 05:10:49 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
|
2014-06-06 23:40:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
|
2014-06-17 17:50:42 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
2014-06-17 02:10:43 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-08-11 07:34:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
|
2014-09-06 02:22:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-07-13 04:46:01 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-08-14 19:48:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2014-09-08 21:40:56 +00:00
|
|
|
// REST adapts a service registry into apiserver's RESTStorage model.
|
|
|
|
type REST struct {
|
2014-08-11 07:34:59 +00:00
|
|
|
registry Registry
|
2014-06-17 17:50:42 +00:00
|
|
|
cloud cloudprovider.Interface
|
2014-08-11 07:34:59 +00:00
|
|
|
machines minion.Registry
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 21:40:56 +00:00
|
|
|
// NewREST returns a new REST.
|
|
|
|
func NewREST(registry Registry, cloud cloudprovider.Interface, machines minion.Registry) *REST {
|
|
|
|
return &REST{
|
2014-06-17 17:50:42 +00:00
|
|
|
registry: registry,
|
|
|
|
cloud: cloud,
|
2014-06-20 01:31:38 +00:00
|
|
|
machines: machines,
|
2014-06-17 17:50:42 +00:00
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:46:04 +00:00
|
|
|
func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) {
|
2014-08-11 07:34:59 +00:00
|
|
|
srv := obj.(*api.Service)
|
2014-09-01 05:10:49 +00:00
|
|
|
if errs := validation.ValidateService(srv); len(errs) > 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
return nil, errors.NewInvalid("service", srv.ID, errs)
|
2014-07-13 04:46:01 +00:00
|
|
|
}
|
2014-08-23 18:33:24 +00:00
|
|
|
|
|
|
|
srv.CreationTimestamp = util.Now()
|
|
|
|
|
2014-09-06 02:22:03 +00:00
|
|
|
return apiserver.MakeAsync(func() (runtime.Object, error) {
|
2014-08-11 07:34:59 +00:00
|
|
|
// TODO: Consider moving this to a rectification loop, so that we make/remove external load balancers
|
|
|
|
// correctly no matter what http operations happen.
|
|
|
|
if srv.CreateExternalLoadBalancer {
|
|
|
|
if rs.cloud == nil {
|
|
|
|
return nil, fmt.Errorf("requested an external service, but no cloud provider supplied.")
|
|
|
|
}
|
|
|
|
balancer, ok := rs.cloud.TCPLoadBalancer()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("The cloud provider does not support external TCP load balancers.")
|
|
|
|
}
|
|
|
|
zones, ok := rs.cloud.Zones()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("The cloud provider does not support zone enumeration.")
|
|
|
|
}
|
|
|
|
hosts, err := rs.machines.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
zone, err := zones.GetZone()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = balancer.CreateTCPLoadBalancer(srv.ID, zone.Region, srv.Port, hosts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-09-06 02:22:03 +00:00
|
|
|
err := rs.registry.CreateService(srv)
|
2014-08-11 07:34:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rs.registry.GetService(srv.ID)
|
|
|
|
}), nil
|
2014-07-13 04:46:01 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:46:04 +00:00
|
|
|
func (rs *REST) Delete(ctx api.Context, id string) (<-chan runtime.Object, error) {
|
2014-08-11 07:34:59 +00:00
|
|
|
service, err := rs.registry.GetService(id)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
2014-08-11 07:34:59 +00:00
|
|
|
return nil, err
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-09-06 02:22:03 +00:00
|
|
|
return apiserver.MakeAsync(func() (runtime.Object, error) {
|
2014-08-11 07:34:59 +00:00
|
|
|
rs.deleteExternalLoadBalancer(service)
|
2014-08-20 17:21:49 +00:00
|
|
|
return &api.Status{Status: api.StatusSuccess}, rs.registry.DeleteService(id)
|
2014-08-11 07:34:59 +00:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:46:04 +00:00
|
|
|
func (rs *REST) Get(ctx api.Context, id string) (runtime.Object, error) {
|
2014-08-11 07:34:59 +00:00
|
|
|
s, err := rs.registry.GetService(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-11 07:34:59 +00:00
|
|
|
return s, err
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 23:15:40 +00:00
|
|
|
// TODO: implement field selector?
|
2014-09-26 15:46:04 +00:00
|
|
|
func (rs *REST) List(ctx api.Context, label, field labels.Selector) (runtime.Object, error) {
|
2014-08-11 07:34:59 +00:00
|
|
|
list, err := rs.registry.ListServices()
|
2014-06-13 21:58:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-17 02:10:43 +00:00
|
|
|
var filtered []api.Service
|
|
|
|
for _, service := range list.Items {
|
2014-09-16 23:15:40 +00:00
|
|
|
if label.Matches(labels.Set(service.Labels)) {
|
2014-06-17 02:10:43 +00:00
|
|
|
filtered = append(filtered, service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list.Items = filtered
|
2014-06-08 04:07:20 +00:00
|
|
|
return list, err
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 19:48:34 +00:00
|
|
|
// Watch returns Services events via a watch.Interface.
|
|
|
|
// It implements apiserver.ResourceWatcher.
|
2014-09-26 15:46:04 +00:00
|
|
|
func (rs *REST) Watch(ctx api.Context, label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) {
|
2014-08-14 19:48:34 +00:00
|
|
|
return rs.registry.WatchServices(label, field, resourceVersion)
|
|
|
|
}
|
|
|
|
|
2014-09-08 21:40:56 +00:00
|
|
|
func (*REST) New() runtime.Object {
|
2014-08-11 07:34:59 +00:00
|
|
|
return &api.Service{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetServiceEnvironmentVariables populates a list of environment variables that are use
|
|
|
|
// in the container environment to get access to services.
|
|
|
|
func GetServiceEnvironmentVariables(registry Registry, machine string) ([]api.EnvVar, error) {
|
|
|
|
var result []api.EnvVar
|
|
|
|
services, err := registry.ListServices()
|
2014-06-13 21:58:08 +00:00
|
|
|
if err != nil {
|
2014-08-11 07:34:59 +00:00
|
|
|
return result, err
|
2014-06-13 21:58:08 +00:00
|
|
|
}
|
2014-08-11 07:34:59 +00:00
|
|
|
for _, service := range services.Items {
|
2014-09-23 20:14:33 +00:00
|
|
|
// Host
|
|
|
|
name := makeEnvVariableName(service.ID) + "_SERVICE_HOST"
|
|
|
|
result = append(result, api.EnvVar{Name: name, Value: machine})
|
|
|
|
// Port
|
|
|
|
name = makeEnvVariableName(service.ID) + "_SERVICE_PORT"
|
|
|
|
result = append(result, api.EnvVar{Name: name, Value: strconv.Itoa(service.Port)})
|
|
|
|
// Docker-compatible vars.
|
2014-08-11 07:34:59 +00:00
|
|
|
result = append(result, makeLinkVariables(service, machine)...)
|
|
|
|
}
|
2014-09-23 20:14:33 +00:00
|
|
|
// The 'SERVICE_HOST' variable is deprecated.
|
|
|
|
// TODO(thockin): get rid of it once ip-per-service is in and "deployed".
|
2014-08-11 07:34:59 +00:00
|
|
|
result = append(result, api.EnvVar{Name: "SERVICE_HOST", Value: machine})
|
|
|
|
return result, nil
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:46:04 +00:00
|
|
|
func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) {
|
2014-08-11 07:34:59 +00:00
|
|
|
srv := obj.(*api.Service)
|
2014-09-01 05:10:49 +00:00
|
|
|
if errs := validation.ValidateService(srv); len(errs) > 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
return nil, errors.NewInvalid("service", srv.ID, errs)
|
2014-08-11 07:34:59 +00:00
|
|
|
}
|
2014-09-06 02:22:03 +00:00
|
|
|
return apiserver.MakeAsync(func() (runtime.Object, error) {
|
2014-08-11 07:34:59 +00:00
|
|
|
// TODO: check to see if external load balancer status changed
|
2014-09-06 02:22:03 +00:00
|
|
|
err := rs.registry.UpdateService(srv)
|
2014-08-11 07:34:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rs.registry.GetService(srv.ID)
|
|
|
|
}), nil
|
|
|
|
}
|
2014-08-04 20:42:01 +00:00
|
|
|
|
2014-08-25 21:36:15 +00:00
|
|
|
// ResourceLocation returns a URL to which one can send traffic for the specified service.
|
2014-09-26 15:46:04 +00:00
|
|
|
func (rs *REST) ResourceLocation(ctx api.Context, id string) (string, error) {
|
2014-08-25 21:36:15 +00:00
|
|
|
e, err := rs.registry.GetEndpoints(id)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(e.Endpoints) == 0 {
|
|
|
|
return "", fmt.Errorf("no endpoints available for %v", id)
|
|
|
|
}
|
2014-08-27 23:10:44 +00:00
|
|
|
return "http://" + e.Endpoints[rand.Intn(len(e.Endpoints))], nil
|
2014-08-25 21:36:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 21:40:56 +00:00
|
|
|
func (rs *REST) deleteExternalLoadBalancer(service *api.Service) error {
|
2014-08-11 07:34:59 +00:00
|
|
|
if !service.CreateExternalLoadBalancer || rs.cloud == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
zones, ok := rs.cloud.Zones()
|
2014-08-04 20:42:01 +00:00
|
|
|
if !ok {
|
|
|
|
// We failed to get zone enumerator.
|
|
|
|
// As this should have failed when we tried in "create" too,
|
|
|
|
// assume external load balancer was never created.
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-11 07:34:59 +00:00
|
|
|
balancer, ok := rs.cloud.TCPLoadBalancer()
|
2014-08-04 20:42:01 +00:00
|
|
|
if !ok {
|
|
|
|
// See comment above.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
zone, err := zones.GetZone()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-04 16:58:10 +00:00
|
|
|
if err := balancer.DeleteTCPLoadBalancer(service.JSONBase.ID, zone.Region); err != nil {
|
2014-08-04 20:42:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:32:04 +00:00
|
|
|
func makeEnvVariableName(str string) string {
|
|
|
|
return strings.ToUpper(strings.Replace(str, "-", "_", -1))
|
|
|
|
}
|
|
|
|
|
2014-08-11 07:34:59 +00:00
|
|
|
func makeLinkVariables(service api.Service, machine string) []api.EnvVar {
|
2014-08-18 22:32:04 +00:00
|
|
|
prefix := makeEnvVariableName(service.ID)
|
2014-09-24 19:29:06 +00:00
|
|
|
protocol := "TCP"
|
|
|
|
if service.Protocol != "" {
|
|
|
|
protocol = service.Protocol
|
2014-06-25 23:23:15 +00:00
|
|
|
}
|
2014-09-24 19:29:06 +00:00
|
|
|
portPrefix := fmt.Sprintf("%s_PORT_%d_%s", prefix, service.Port, strings.ToUpper(protocol))
|
2014-08-11 07:34:59 +00:00
|
|
|
return []api.EnvVar{
|
|
|
|
{
|
|
|
|
Name: prefix + "_PORT",
|
2014-09-24 19:29:06 +00:00
|
|
|
Value: fmt.Sprintf("%s://%s:%d", strings.ToLower(protocol), machine, service.Port),
|
2014-08-11 07:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: portPrefix,
|
2014-09-24 19:29:06 +00:00
|
|
|
Value: fmt.Sprintf("%s://%s:%d", strings.ToLower(protocol), machine, service.Port),
|
2014-08-11 07:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: portPrefix + "_PROTO",
|
2014-09-24 19:29:06 +00:00
|
|
|
Value: strings.ToLower(protocol),
|
2014-08-11 07:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: portPrefix + "_PORT",
|
|
|
|
Value: strconv.Itoa(service.Port),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: portPrefix + "_ADDR",
|
|
|
|
Value: machine,
|
|
|
|
},
|
2014-07-25 16:18:48 +00:00
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|