2014-08-14 19:48:34 +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 endpoint
|
|
|
|
|
|
|
|
import (
|
2014-09-26 20:34:55 +00:00
|
|
|
"fmt"
|
2014-08-14 19:48:34 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-11-02 20:30:25 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2015-03-06 23:29:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
2014-08-14 19:48:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-06 02:22:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-08-14 19:48:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
|
|
|
)
|
|
|
|
|
2014-09-08 21:40:56 +00:00
|
|
|
// REST adapts endpoints into apiserver's RESTStorage model.
|
|
|
|
type REST struct {
|
2014-08-14 19:48:34 +00:00
|
|
|
registry Registry
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:32:31 +00:00
|
|
|
// NewStorage returns a new rest.Storage implementation for endpoints
|
|
|
|
func NewStorage(registry Registry) *REST {
|
2014-09-08 21:40:56 +00:00
|
|
|
return &REST{
|
2014-08-14 19:48:34 +00:00
|
|
|
registry: registry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 02:31:41 +00:00
|
|
|
// Get satisfies the RESTStorage interface.
|
2014-09-26 15:46:04 +00:00
|
|
|
func (rs *REST) Get(ctx api.Context, id string) (runtime.Object, error) {
|
2014-09-26 19:18:42 +00:00
|
|
|
return rs.registry.GetEndpoints(ctx, id)
|
2014-08-14 19:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 02:31:41 +00:00
|
|
|
// List satisfies the RESTStorage interface.
|
2015-03-06 23:29:03 +00:00
|
|
|
func (rs *REST) List(ctx api.Context, label labels.Selector, field fields.Selector) (runtime.Object, error) {
|
2014-09-16 23:15:40 +00:00
|
|
|
if !label.Empty() || !field.Empty() {
|
2014-11-02 20:30:25 +00:00
|
|
|
return nil, errors.NewBadRequest("label/field selectors are not supported on endpoints")
|
2014-08-29 02:31:41 +00:00
|
|
|
}
|
2014-09-26 19:18:42 +00:00
|
|
|
return rs.registry.ListEndpoints(ctx)
|
2014-08-14 19:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Watch returns Endpoint events via a watch.Interface.
|
2015-03-21 16:24:16 +00:00
|
|
|
// It implements rest.Watcher.
|
2015-03-06 23:29:03 +00:00
|
|
|
func (rs *REST) Watch(ctx api.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
2014-09-26 19:18:42 +00:00
|
|
|
return rs.registry.WatchEndpoints(ctx, label, field, resourceVersion)
|
2014-08-14 19:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 20:34:55 +00:00
|
|
|
// Create satisfies the RESTStorage interface.
|
2015-02-10 17:49:56 +00:00
|
|
|
func (rs *REST) Create(ctx api.Context, obj runtime.Object) (runtime.Object, error) {
|
2014-09-26 20:34:55 +00:00
|
|
|
endpoints, ok := obj.(*api.Endpoints)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("not an endpoints: %#v", obj)
|
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if len(endpoints.Name) == 0 {
|
2014-09-26 20:34:55 +00:00
|
|
|
return nil, fmt.Errorf("id is required: %#v", obj)
|
|
|
|
}
|
2014-11-24 19:15:22 +00:00
|
|
|
if !api.ValidNamespace(ctx, &endpoints.ObjectMeta) {
|
|
|
|
return nil, errors.NewConflict("endpoints", endpoints.Namespace, fmt.Errorf("Endpoints.Namespace does not match the provided context"))
|
|
|
|
}
|
2014-11-12 21:27:10 +00:00
|
|
|
api.FillObjectMetaSystemFields(ctx, &endpoints.ObjectMeta)
|
|
|
|
|
2015-02-10 17:49:56 +00:00
|
|
|
err := rs.registry.UpdateEndpoints(ctx, endpoints)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rs.registry.GetEndpoints(ctx, endpoints.Name)
|
2014-08-14 19:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 20:34:55 +00:00
|
|
|
// Update satisfies the RESTStorage interface.
|
2015-02-10 17:49:56 +00:00
|
|
|
func (rs *REST) Update(ctx api.Context, obj runtime.Object) (runtime.Object, bool, error) {
|
2014-09-26 20:34:55 +00:00
|
|
|
endpoints, ok := obj.(*api.Endpoints)
|
|
|
|
if !ok {
|
2015-02-10 17:49:56 +00:00
|
|
|
return nil, false, fmt.Errorf("not an endpoints: %#v", obj)
|
|
|
|
}
|
|
|
|
err := rs.registry.UpdateEndpoints(ctx, endpoints)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2014-09-26 20:34:55 +00:00
|
|
|
}
|
2015-02-10 17:49:56 +00:00
|
|
|
out, err := rs.registry.GetEndpoints(ctx, endpoints.Name)
|
|
|
|
return out, false, err
|
2014-08-14 19:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// New implements the RESTStorage interface.
|
2014-09-08 21:40:56 +00:00
|
|
|
func (rs REST) New() runtime.Object {
|
2014-08-14 19:48:34 +00:00
|
|
|
return &api.Endpoints{}
|
|
|
|
}
|
2015-01-12 05:33:25 +00:00
|
|
|
|
|
|
|
func (*REST) NewList() runtime.Object {
|
|
|
|
return &api.EndpointsList{}
|
|
|
|
}
|