mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 02:41:25 +00:00
allow to set the service reference on the allocator
This commit is contained in:
parent
e6f197a991
commit
b2c8190ee7
@ -18,14 +18,17 @@ package storage
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||||
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
apiservice "k8s.io/kubernetes/pkg/api/service"
|
apiservice "k8s.io/kubernetes/pkg/api/service"
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
"k8s.io/kubernetes/pkg/apis/core/validation"
|
"k8s.io/kubernetes/pkg/apis/core/validation"
|
||||||
|
"k8s.io/kubernetes/pkg/features"
|
||||||
"k8s.io/kubernetes/pkg/registry/core/service/ipallocator"
|
"k8s.io/kubernetes/pkg/registry/core/service/ipallocator"
|
||||||
"k8s.io/kubernetes/pkg/registry/core/service/portallocator"
|
"k8s.io/kubernetes/pkg/registry/core/service/portallocator"
|
||||||
netutils "k8s.io/utils/net"
|
netutils "k8s.io/utils/net"
|
||||||
@ -399,7 +402,18 @@ func (al *Allocators) allocIPs(service *api.Service, toAlloc map[api.IPFamily]st
|
|||||||
allocator = allocator.DryRun()
|
allocator = allocator.DryRun()
|
||||||
}
|
}
|
||||||
if ip == "" {
|
if ip == "" {
|
||||||
allocatedIP, err := allocator.AllocateNext()
|
var allocatedIP net.IP
|
||||||
|
var err error
|
||||||
|
if utilfeature.DefaultFeatureGate.Enabled(features.MultiCIDRServiceAllocator) {
|
||||||
|
svcAllocator, ok := allocator.(*ipallocator.Allocator)
|
||||||
|
if ok {
|
||||||
|
allocatedIP, err = svcAllocator.AllocateNextService(service)
|
||||||
|
} else {
|
||||||
|
allocatedIP, err = allocator.AllocateNext()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
allocatedIP, err = allocator.AllocateNext()
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return allocated, errors.NewInternalError(fmt.Errorf("failed to allocate a serviceIP: %v", err))
|
return allocated, errors.NewInternalError(fmt.Errorf("failed to allocate a serviceIP: %v", err))
|
||||||
}
|
}
|
||||||
@ -409,7 +423,18 @@ func (al *Allocators) allocIPs(service *api.Service, toAlloc map[api.IPFamily]st
|
|||||||
if parsedIP == nil {
|
if parsedIP == nil {
|
||||||
return allocated, errors.NewInternalError(fmt.Errorf("failed to parse service IP %q", ip))
|
return allocated, errors.NewInternalError(fmt.Errorf("failed to parse service IP %q", ip))
|
||||||
}
|
}
|
||||||
if err := allocator.Allocate(parsedIP); err != nil {
|
var err error
|
||||||
|
if utilfeature.DefaultFeatureGate.Enabled(features.MultiCIDRServiceAllocator) {
|
||||||
|
svcAllocator, ok := allocator.(*ipallocator.Allocator)
|
||||||
|
if ok {
|
||||||
|
err = svcAllocator.AllocateService(service, parsedIP)
|
||||||
|
} else {
|
||||||
|
err = allocator.Allocate(parsedIP)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = allocator.Allocate(parsedIP)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
el := field.ErrorList{field.Invalid(field.NewPath("spec", "clusterIPs"), service.Spec.ClusterIPs, fmt.Sprintf("failed to allocate IP %v: %v", ip, err))}
|
el := field.ErrorList{field.Invalid(field.NewPath("spec", "clusterIPs"), service.Spec.ClusterIPs, fmt.Sprintf("failed to allocate IP %v: %v", ip, err))}
|
||||||
return allocated, errors.NewInvalid(api.Kind("Service"), service.Name, el)
|
return allocated, errors.NewInvalid(api.Kind("Service"), service.Name, el)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user