From a6539f846a7ac24db97aedc37778a148bb583fc0 Mon Sep 17 00:00:00 2001 From: AdoHe Date: Tue, 9 Aug 2016 13:10:48 +0800 Subject: [PATCH] move RangeRegistry to a separate package for generic usage --- pkg/master/controller.go | 5 +-- pkg/master/master.go | 9 +++--- pkg/registry/rangeallocation/doc.go | 19 ++++++++++++ pkg/registry/rangeallocation/registry.go | 31 +++++++++++++++++++ pkg/registry/service/allocator/etcd/etcd.go | 6 ++-- .../service/ipallocator/controller/repair.go | 5 +-- .../portallocator/controller/repair.go | 5 +-- pkg/registry/service/registry.go | 11 ------- 8 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 pkg/registry/rangeallocation/doc.go create mode 100644 pkg/registry/rangeallocation/registry.go diff --git a/pkg/master/controller.go b/pkg/master/controller.go index fdf5e92b0c3..9d6323cb906 100644 --- a/pkg/master/controller.go +++ b/pkg/master/controller.go @@ -28,6 +28,7 @@ import ( "k8s.io/kubernetes/pkg/api/rest" "k8s.io/kubernetes/pkg/registry/endpoint" "k8s.io/kubernetes/pkg/registry/namespace" + "k8s.io/kubernetes/pkg/registry/rangeallocation" "k8s.io/kubernetes/pkg/registry/service" servicecontroller "k8s.io/kubernetes/pkg/registry/service/ipallocator/controller" portallocatorcontroller "k8s.io/kubernetes/pkg/registry/service/portallocator/controller" @@ -45,11 +46,11 @@ type Controller struct { NamespaceRegistry namespace.Registry ServiceRegistry service.Registry - ServiceClusterIPRegistry service.RangeRegistry + ServiceClusterIPRegistry rangeallocation.RangeRegistry ServiceClusterIPInterval time.Duration ServiceClusterIPRange *net.IPNet - ServiceNodePortRegistry service.RangeRegistry + ServiceNodePortRegistry rangeallocation.RangeRegistry ServiceNodePortInterval time.Duration ServiceNodePortRange utilnet.PortRange diff --git a/pkg/master/master.go b/pkg/master/master.go index fc870b9992e..691a507eae6 100644 --- a/pkg/master/master.go +++ b/pkg/master/master.go @@ -71,6 +71,7 @@ import ( pvcetcd "k8s.io/kubernetes/pkg/registry/persistentvolumeclaim/etcd" podetcd "k8s.io/kubernetes/pkg/registry/pod/etcd" podtemplateetcd "k8s.io/kubernetes/pkg/registry/podtemplate/etcd" + "k8s.io/kubernetes/pkg/registry/rangeallocation" resourcequotaetcd "k8s.io/kubernetes/pkg/registry/resourcequota/etcd" secretetcd "k8s.io/kubernetes/pkg/registry/secret/etcd" "k8s.io/kubernetes/pkg/registry/service" @@ -137,8 +138,8 @@ type Master struct { namespaceRegistry namespace.Registry serviceRegistry service.Registry endpointRegistry endpoint.Registry - serviceClusterIPAllocator service.RangeRegistry - serviceNodePortAllocator service.RangeRegistry + serviceClusterIPAllocator rangeallocation.RangeRegistry + serviceNodePortAllocator rangeallocation.RangeRegistry // storage for third party objects thirdPartyStorage storage.Interface @@ -341,7 +342,7 @@ func (m *Master) initV1ResourcesStorage(c *Config) { serviceRESTStorage, serviceStatusStorage := serviceetcd.NewREST(restOptions("services")) m.serviceRegistry = service.NewRegistry(serviceRESTStorage) - var serviceClusterIPRegistry service.RangeRegistry + var serviceClusterIPRegistry rangeallocation.RangeRegistry serviceClusterIPRange := m.ServiceClusterIPRange if serviceClusterIPRange == nil { glog.Fatalf("service clusterIPRange is nil") @@ -362,7 +363,7 @@ func (m *Master) initV1ResourcesStorage(c *Config) { }) m.serviceClusterIPAllocator = serviceClusterIPRegistry - var serviceNodePortRegistry service.RangeRegistry + var serviceNodePortRegistry rangeallocation.RangeRegistry serviceNodePortAllocator := portallocator.NewPortAllocatorCustom(m.ServiceNodePortRange, func(max int, rangeSpec string) allocator.Interface { mem := allocator.NewAllocationMap(max, rangeSpec) // TODO etcdallocator package to return a storage interface via the storageFactory diff --git a/pkg/registry/rangeallocation/doc.go b/pkg/registry/rangeallocation/doc.go new file mode 100644 index 00000000000..fad697557e2 --- /dev/null +++ b/pkg/registry/rangeallocation/doc.go @@ -0,0 +1,19 @@ +/* +Copyright 2016 The Kubernetes Authors. + +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 rangeallocation provides the Registry interface for storing RangeAllocation +// api objects. +package rangeallocation diff --git a/pkg/registry/rangeallocation/registry.go b/pkg/registry/rangeallocation/registry.go new file mode 100644 index 00000000000..5fd3aa3af77 --- /dev/null +++ b/pkg/registry/rangeallocation/registry.go @@ -0,0 +1,31 @@ +/* +Copyright 2016 The Kubernetes Authors. + +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 rangeallocation + +import ( + "k8s.io/kubernetes/pkg/api" +) + +// RangeRegistry is a registry that can retrieve or persist a RangeAllocation object. +type RangeRegistry interface { + // Get returns the latest allocation, an empty object if no allocation has been made, + // or an error if the allocation could not be retrieved. + Get() (*api.RangeAllocation, error) + // CreateOrUpdate should create or update the provide allocation, unless a conflict + // has occurred since the item was last created. + CreateOrUpdate(*api.RangeAllocation) error +} diff --git a/pkg/registry/service/allocator/etcd/etcd.go b/pkg/registry/service/allocator/etcd/etcd.go index 4e52ae362be..60dbbe50f98 100644 --- a/pkg/registry/service/allocator/etcd/etcd.go +++ b/pkg/registry/service/allocator/etcd/etcd.go @@ -25,7 +25,7 @@ import ( k8serr "k8s.io/kubernetes/pkg/api/errors" storeerr "k8s.io/kubernetes/pkg/api/errors/storage" "k8s.io/kubernetes/pkg/api/unversioned" - "k8s.io/kubernetes/pkg/registry/service" + "k8s.io/kubernetes/pkg/registry/rangeallocation" "k8s.io/kubernetes/pkg/registry/service/allocator" "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/storage" @@ -52,9 +52,9 @@ type Etcd struct { resource unversioned.GroupResource } -// Etcd implements allocator.Interface and service.RangeRegistry +// Etcd implements allocator.Interface and rangeallocation.RangeRegistry var _ allocator.Interface = &Etcd{} -var _ service.RangeRegistry = &Etcd{} +var _ rangeallocation.RangeRegistry = &Etcd{} // NewEtcd returns an allocator that is backed by Etcd and can manage // persisting the snapshot state of allocation after each allocation is made. diff --git a/pkg/registry/service/ipallocator/controller/repair.go b/pkg/registry/service/ipallocator/controller/repair.go index dc37a5a4a6e..4caa9812cf7 100644 --- a/pkg/registry/service/ipallocator/controller/repair.go +++ b/pkg/registry/service/ipallocator/controller/repair.go @@ -24,6 +24,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" client "k8s.io/kubernetes/pkg/client/unversioned" + "k8s.io/kubernetes/pkg/registry/rangeallocation" "k8s.io/kubernetes/pkg/registry/service" "k8s.io/kubernetes/pkg/registry/service/ipallocator" "k8s.io/kubernetes/pkg/util/runtime" @@ -49,12 +50,12 @@ type Repair struct { interval time.Duration registry service.Registry network *net.IPNet - alloc service.RangeRegistry + alloc rangeallocation.RangeRegistry } // NewRepair creates a controller that periodically ensures that all clusterIPs are uniquely allocated across the cluster // and generates informational warnings for a cluster that is not in sync. -func NewRepair(interval time.Duration, registry service.Registry, network *net.IPNet, alloc service.RangeRegistry) *Repair { +func NewRepair(interval time.Duration, registry service.Registry, network *net.IPNet, alloc rangeallocation.RangeRegistry) *Repair { return &Repair{ interval: interval, registry: registry, diff --git a/pkg/registry/service/portallocator/controller/repair.go b/pkg/registry/service/portallocator/controller/repair.go index d80c06d099e..90ebcb1132e 100644 --- a/pkg/registry/service/portallocator/controller/repair.go +++ b/pkg/registry/service/portallocator/controller/repair.go @@ -23,6 +23,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" client "k8s.io/kubernetes/pkg/client/unversioned" + "k8s.io/kubernetes/pkg/registry/rangeallocation" "k8s.io/kubernetes/pkg/registry/service" "k8s.io/kubernetes/pkg/registry/service/portallocator" "k8s.io/kubernetes/pkg/util/net" @@ -35,12 +36,12 @@ type Repair struct { interval time.Duration registry service.Registry portRange net.PortRange - alloc service.RangeRegistry + alloc rangeallocation.RangeRegistry } // NewRepair creates a controller that periodically ensures that all ports are uniquely allocated across the cluster // and generates informational warnings for a cluster that is not in sync. -func NewRepair(interval time.Duration, registry service.Registry, portRange net.PortRange, alloc service.RangeRegistry) *Repair { +func NewRepair(interval time.Duration, registry service.Registry, portRange net.PortRange, alloc rangeallocation.RangeRegistry) *Repair { return &Repair{ interval: interval, registry: registry, diff --git a/pkg/registry/service/registry.go b/pkg/registry/service/registry.go index 34b955bbb35..a90bc67aedb 100644 --- a/pkg/registry/service/registry.go +++ b/pkg/registry/service/registry.go @@ -101,14 +101,3 @@ func (s *storage) ExportService(ctx api.Context, name string, options unversione } 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 { - // Get returns the latest allocation, an empty object if no allocation has been made, - // or an error if the allocation could not be retrieved. - Get() (*api.RangeAllocation, error) - // CreateOrUpdate should create or update the provide allocation, unless a conflict - // has occurred since the item was last created. - CreateOrUpdate(*api.RangeAllocation) error -}