move RangeRegistry to a separate package for generic usage

This commit is contained in:
AdoHe 2016-08-09 13:10:48 +08:00
parent b5ce23c48d
commit a6539f846a
8 changed files with 67 additions and 24 deletions

View File

@ -28,6 +28,7 @@ import (
"k8s.io/kubernetes/pkg/api/rest" "k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/registry/endpoint" "k8s.io/kubernetes/pkg/registry/endpoint"
"k8s.io/kubernetes/pkg/registry/namespace" "k8s.io/kubernetes/pkg/registry/namespace"
"k8s.io/kubernetes/pkg/registry/rangeallocation"
"k8s.io/kubernetes/pkg/registry/service" "k8s.io/kubernetes/pkg/registry/service"
servicecontroller "k8s.io/kubernetes/pkg/registry/service/ipallocator/controller" servicecontroller "k8s.io/kubernetes/pkg/registry/service/ipallocator/controller"
portallocatorcontroller "k8s.io/kubernetes/pkg/registry/service/portallocator/controller" portallocatorcontroller "k8s.io/kubernetes/pkg/registry/service/portallocator/controller"
@ -45,11 +46,11 @@ type Controller struct {
NamespaceRegistry namespace.Registry NamespaceRegistry namespace.Registry
ServiceRegistry service.Registry ServiceRegistry service.Registry
ServiceClusterIPRegistry service.RangeRegistry ServiceClusterIPRegistry rangeallocation.RangeRegistry
ServiceClusterIPInterval time.Duration ServiceClusterIPInterval time.Duration
ServiceClusterIPRange *net.IPNet ServiceClusterIPRange *net.IPNet
ServiceNodePortRegistry service.RangeRegistry ServiceNodePortRegistry rangeallocation.RangeRegistry
ServiceNodePortInterval time.Duration ServiceNodePortInterval time.Duration
ServiceNodePortRange utilnet.PortRange ServiceNodePortRange utilnet.PortRange

View File

@ -71,6 +71,7 @@ import (
pvcetcd "k8s.io/kubernetes/pkg/registry/persistentvolumeclaim/etcd" pvcetcd "k8s.io/kubernetes/pkg/registry/persistentvolumeclaim/etcd"
podetcd "k8s.io/kubernetes/pkg/registry/pod/etcd" podetcd "k8s.io/kubernetes/pkg/registry/pod/etcd"
podtemplateetcd "k8s.io/kubernetes/pkg/registry/podtemplate/etcd" podtemplateetcd "k8s.io/kubernetes/pkg/registry/podtemplate/etcd"
"k8s.io/kubernetes/pkg/registry/rangeallocation"
resourcequotaetcd "k8s.io/kubernetes/pkg/registry/resourcequota/etcd" resourcequotaetcd "k8s.io/kubernetes/pkg/registry/resourcequota/etcd"
secretetcd "k8s.io/kubernetes/pkg/registry/secret/etcd" secretetcd "k8s.io/kubernetes/pkg/registry/secret/etcd"
"k8s.io/kubernetes/pkg/registry/service" "k8s.io/kubernetes/pkg/registry/service"
@ -137,8 +138,8 @@ type Master struct {
namespaceRegistry namespace.Registry namespaceRegistry namespace.Registry
serviceRegistry service.Registry serviceRegistry service.Registry
endpointRegistry endpoint.Registry endpointRegistry endpoint.Registry
serviceClusterIPAllocator service.RangeRegistry serviceClusterIPAllocator rangeallocation.RangeRegistry
serviceNodePortAllocator service.RangeRegistry serviceNodePortAllocator rangeallocation.RangeRegistry
// storage for third party objects // storage for third party objects
thirdPartyStorage storage.Interface thirdPartyStorage storage.Interface
@ -341,7 +342,7 @@ func (m *Master) initV1ResourcesStorage(c *Config) {
serviceRESTStorage, serviceStatusStorage := serviceetcd.NewREST(restOptions("services")) serviceRESTStorage, serviceStatusStorage := serviceetcd.NewREST(restOptions("services"))
m.serviceRegistry = service.NewRegistry(serviceRESTStorage) m.serviceRegistry = service.NewRegistry(serviceRESTStorage)
var serviceClusterIPRegistry service.RangeRegistry var serviceClusterIPRegistry rangeallocation.RangeRegistry
serviceClusterIPRange := m.ServiceClusterIPRange serviceClusterIPRange := m.ServiceClusterIPRange
if serviceClusterIPRange == nil { if serviceClusterIPRange == nil {
glog.Fatalf("service clusterIPRange is nil") glog.Fatalf("service clusterIPRange is nil")
@ -362,7 +363,7 @@ func (m *Master) initV1ResourcesStorage(c *Config) {
}) })
m.serviceClusterIPAllocator = serviceClusterIPRegistry m.serviceClusterIPAllocator = serviceClusterIPRegistry
var serviceNodePortRegistry service.RangeRegistry var serviceNodePortRegistry rangeallocation.RangeRegistry
serviceNodePortAllocator := portallocator.NewPortAllocatorCustom(m.ServiceNodePortRange, func(max int, rangeSpec string) allocator.Interface { serviceNodePortAllocator := portallocator.NewPortAllocatorCustom(m.ServiceNodePortRange, func(max int, rangeSpec string) allocator.Interface {
mem := allocator.NewAllocationMap(max, rangeSpec) mem := allocator.NewAllocationMap(max, rangeSpec)
// TODO etcdallocator package to return a storage interface via the storageFactory // TODO etcdallocator package to return a storage interface via the storageFactory

View File

@ -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

View File

@ -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
}

View File

@ -25,7 +25,7 @@ import (
k8serr "k8s.io/kubernetes/pkg/api/errors" k8serr "k8s.io/kubernetes/pkg/api/errors"
storeerr "k8s.io/kubernetes/pkg/api/errors/storage" storeerr "k8s.io/kubernetes/pkg/api/errors/storage"
"k8s.io/kubernetes/pkg/api/unversioned" "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/registry/service/allocator"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/storage" "k8s.io/kubernetes/pkg/storage"
@ -52,9 +52,9 @@ type Etcd struct {
resource unversioned.GroupResource resource unversioned.GroupResource
} }
// Etcd implements allocator.Interface and service.RangeRegistry // Etcd implements allocator.Interface and rangeallocation.RangeRegistry
var _ allocator.Interface = &Etcd{} var _ allocator.Interface = &Etcd{}
var _ service.RangeRegistry = &Etcd{} var _ rangeallocation.RangeRegistry = &Etcd{}
// NewEtcd returns an allocator that is backed by Etcd and can manage // NewEtcd returns an allocator that is backed by Etcd and can manage
// persisting the snapshot state of allocation after each allocation is made. // persisting the snapshot state of allocation after each allocation is made.

View File

@ -24,6 +24,7 @@ import (
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/errors"
client "k8s.io/kubernetes/pkg/client/unversioned" 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"
"k8s.io/kubernetes/pkg/registry/service/ipallocator" "k8s.io/kubernetes/pkg/registry/service/ipallocator"
"k8s.io/kubernetes/pkg/util/runtime" "k8s.io/kubernetes/pkg/util/runtime"
@ -49,12 +50,12 @@ type Repair struct {
interval time.Duration interval time.Duration
registry service.Registry registry service.Registry
network *net.IPNet 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 // 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. // 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{ return &Repair{
interval: interval, interval: interval,
registry: registry, registry: registry,

View File

@ -23,6 +23,7 @@ import (
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/errors"
client "k8s.io/kubernetes/pkg/client/unversioned" 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"
"k8s.io/kubernetes/pkg/registry/service/portallocator" "k8s.io/kubernetes/pkg/registry/service/portallocator"
"k8s.io/kubernetes/pkg/util/net" "k8s.io/kubernetes/pkg/util/net"
@ -35,12 +36,12 @@ type Repair struct {
interval time.Duration interval time.Duration
registry service.Registry registry service.Registry
portRange net.PortRange 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 // 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. // 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{ return &Repair{
interval: interval, interval: interval,
registry: registry, registry: registry,

View File

@ -101,14 +101,3 @@ func (s *storage) ExportService(ctx api.Context, name string, options unversione
} }
return obj.(*api.Service), nil 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
}