From 5fdfc4bde38a2eb2721b61e5c59938ca1bc2e061 Mon Sep 17 00:00:00 2001 From: AdoHe Date: Sat, 5 Mar 2016 11:23:50 -0500 Subject: [PATCH] fix can not export service bug --- api/swagger-spec/v1.json | 16 ++++++++++++++++ docs/api-reference/v1/operations.html | 18 +++++++++++++++++- pkg/registry/registrytest/service.go | 8 ++++++++ pkg/registry/service/etcd/etcd.go | 1 + pkg/registry/service/registry.go | 18 ++++++++++++++++++ pkg/registry/service/rest.go | 6 ++++++ pkg/registry/service/strategy.go | 2 +- pkg/registry/service/strategy_test.go | 3 +++ 8 files changed, 70 insertions(+), 2 deletions(-) diff --git a/api/swagger-spec/v1.json b/api/swagger-spec/v1.json index 63df851ec53..d33a9c625a8 100644 --- a/api/swagger-spec/v1.json +++ b/api/swagger-spec/v1.json @@ -13288,6 +13288,22 @@ "required": false, "allowMultiple": false }, + { + "type": "boolean", + "paramType": "query", + "name": "export", + "description": "Should this value be exported. Export strips fields that a user can not specify.", + "required": false, + "allowMultiple": false + }, + { + "type": "boolean", + "paramType": "query", + "name": "exact", + "description": "Should the export be exact. Exact export maintains cluster-specific fields like 'Namespace'", + "required": false, + "allowMultiple": false + }, { "type": "string", "paramType": "path", diff --git a/docs/api-reference/v1/operations.html b/docs/api-reference/v1/operations.html index 3fe2aeafe73..32bbca4c81c 100755 --- a/docs/api-reference/v1/operations.html +++ b/docs/api-reference/v1/operations.html @@ -15104,6 +15104,22 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; } +

QueryParameter

+

export

+

Should this value be exported. Export strips fields that a user can not specify.

+

false

+

boolean

+ + + +

QueryParameter

+

exact

+

Should the export be exact. Exact export maintains cluster-specific fields like Namespace

+

false

+

boolean

+ + +

PathParameter

namespace

object name and auth scope, such as for teams and projects

@@ -29901,7 +29917,7 @@ span.icon > [class^="icon-"], span.icon > [class*=" icon-"] { cursor: default; } diff --git a/pkg/registry/registrytest/service.go b/pkg/registry/registrytest/service.go index 643c306cb72..f634d7ffcfd 100644 --- a/pkg/registry/registrytest/service.go +++ b/pkg/registry/registrytest/service.go @@ -20,6 +20,7 @@ import ( "sync" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/watch" ) @@ -112,3 +113,10 @@ func (r *ServiceRegistry) WatchServices(ctx api.Context, options *api.ListOption return nil, r.Err } + +func (r *ServiceRegistry) ExportService(ctx api.Context, name string, options unversioned.ExportOptions) (*api.Service, error) { + r.mu.Lock() + defer r.mu.Lock() + + return r.Service, r.Err +} diff --git a/pkg/registry/service/etcd/etcd.go b/pkg/registry/service/etcd/etcd.go index 28505d2c87e..f244e6d742f 100644 --- a/pkg/registry/service/etcd/etcd.go +++ b/pkg/registry/service/etcd/etcd.go @@ -59,6 +59,7 @@ func NewREST(opts generic.RESTOptions) (*REST, *StatusREST) { CreateStrategy: service.Strategy, UpdateStrategy: service.Strategy, + ExportStrategy: service.Strategy, Storage: storageInterface, } diff --git a/pkg/registry/service/registry.go b/pkg/registry/service/registry.go index 154dcb9d192..8fdbabab64b 100644 --- a/pkg/registry/service/registry.go +++ b/pkg/registry/service/registry.go @@ -17,8 +17,11 @@ limitations under the License. package service import ( + "fmt" + "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/rest" + "k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/watch" ) @@ -30,6 +33,7 @@ type Registry interface { DeleteService(ctx api.Context, name string) error UpdateService(ctx api.Context, svc *api.Service) (*api.Service, error) WatchServices(ctx api.Context, options *api.ListOptions) (watch.Interface, error) + ExportService(ctx api.Context, name string, options unversioned.ExportOptions) (*api.Service, error) } // storage puts strong typing around storage calls @@ -84,6 +88,20 @@ func (s *storage) WatchServices(ctx api.Context, options *api.ListOptions) (watc return s.Watch(ctx, options) } +// If StandardStorage implements rest.Exporter, returns exported service. +// Otherwise export is not supported. +func (s *storage) ExportService(ctx api.Context, name string, options unversioned.ExportOptions) (*api.Service, error) { + exporter, isExporter := s.StandardStorage.(rest.Exporter) + if !isExporter { + return nil, fmt.Errorf("export is not supported") + } + obj, err := exporter.Export(ctx, name, options) + if err != nil { + return nil, err + } + return obj.(*api.Service), nil +} + // TODO: Move to a general location (as other components may need allocation in future; it's not service specific) // RangeRegistry is a registry that can retrieve or persist a RangeAllocation object. type RangeRegistry interface { diff --git a/pkg/registry/service/rest.go b/pkg/registry/service/rest.go index 44f602d0c92..af6a591a16f 100644 --- a/pkg/registry/service/rest.go +++ b/pkg/registry/service/rest.go @@ -198,6 +198,12 @@ func (rs *REST) Watch(ctx api.Context, options *api.ListOptions) (watch.Interfac return rs.registry.WatchServices(ctx, options) } +// Export returns Service stripped of cluster-specific information. +// It implements rest.Exporter. +func (rs *REST) Export(ctx api.Context, name string, opts unversioned.ExportOptions) (runtime.Object, error) { + return rs.registry.ExportService(ctx, name, opts) +} + func (*REST) New() runtime.Object { return &api.Service{} } diff --git a/pkg/registry/service/strategy.go b/pkg/registry/service/strategy.go index 59290abb169..bc87a810711 100644 --- a/pkg/registry/service/strategy.go +++ b/pkg/registry/service/strategy.go @@ -90,7 +90,7 @@ func (svcStrategy) Export(obj runtime.Object, exact bool) error { return nil } if t.Spec.ClusterIP != api.ClusterIPNone { - t.Spec.ClusterIP = "" + t.Spec.ClusterIP = "" } if t.Spec.Type == api.ServiceTypeNodePort { for i := range t.Spec.Ports { diff --git a/pkg/registry/service/strategy_test.go b/pkg/registry/service/strategy_test.go index 925753e25e8..13b6eeb4791 100644 --- a/pkg/registry/service/strategy_test.go +++ b/pkg/registry/service/strategy_test.go @@ -81,6 +81,9 @@ func TestExportService(t *testing.T) { Name: "foo", Namespace: "bar", }, + Spec: api.ServiceSpec{ + ClusterIP: "", + }, }, }, {