mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 05:57:25 +00:00
Svc REST: Move isValidAddress to storage.go
This commit is contained in:
parent
d7c8557281
commit
fe6f278ea1
@ -17,15 +17,11 @@ limitations under the License.
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
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"
|
||||||
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
|
||||||
"k8s.io/apiserver/pkg/registry/rest"
|
|
||||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
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"
|
||||||
@ -920,32 +916,6 @@ func (al *RESTAllocStuff) releaseClusterIP(service *api.Service) (released map[a
|
|||||||
return al.releaseIPs(toRelease)
|
return al.releaseIPs(toRelease)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isValidAddress(ctx context.Context, addr *api.EndpointAddress, pods rest.Getter) error {
|
|
||||||
if addr.TargetRef == nil {
|
|
||||||
return fmt.Errorf("Address has no target ref, skipping: %v", addr)
|
|
||||||
}
|
|
||||||
if genericapirequest.NamespaceValue(ctx) != addr.TargetRef.Namespace {
|
|
||||||
return fmt.Errorf("Address namespace doesn't match context namespace")
|
|
||||||
}
|
|
||||||
obj, err := pods.Get(ctx, addr.TargetRef.Name, &metav1.GetOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
pod, ok := obj.(*api.Pod)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("failed to cast to pod: %v", obj)
|
|
||||||
}
|
|
||||||
if pod == nil {
|
|
||||||
return fmt.Errorf("pod is missing, skipping (%s/%s)", addr.TargetRef.Namespace, addr.TargetRef.Name)
|
|
||||||
}
|
|
||||||
for _, podIP := range pod.Status.PodIPs {
|
|
||||||
if podIP.IP == addr.IP {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt.Errorf("pod ip(s) doesn't match endpoint ip, skipping: %v vs %s (%s/%s)", pod.Status.PodIPs, addr.IP, addr.TargetRef.Namespace, addr.TargetRef.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is O(N), but we expect haystack to be small;
|
// This is O(N), but we expect haystack to be small;
|
||||||
// so small that we expect a linear search to be faster
|
// so small that we expect a linear search to be faster
|
||||||
func containsNumber(haystack []int, needle int) bool {
|
func containsNumber(haystack []int, needle int) bool {
|
||||||
|
@ -453,6 +453,32 @@ func (r *GenericREST) ResourceLocation(ctx context.Context, id string) (*url.URL
|
|||||||
return nil, nil, errors.NewServiceUnavailable(fmt.Sprintf("no endpoints available for service %q", id))
|
return nil, nil, errors.NewServiceUnavailable(fmt.Sprintf("no endpoints available for service %q", id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isValidAddress(ctx context.Context, addr *api.EndpointAddress, pods rest.Getter) error {
|
||||||
|
if addr.TargetRef == nil {
|
||||||
|
return fmt.Errorf("Address has no target ref, skipping: %v", addr)
|
||||||
|
}
|
||||||
|
if genericapirequest.NamespaceValue(ctx) != addr.TargetRef.Namespace {
|
||||||
|
return fmt.Errorf("Address namespace doesn't match context namespace")
|
||||||
|
}
|
||||||
|
obj, err := pods.Get(ctx, addr.TargetRef.Name, &metav1.GetOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
pod, ok := obj.(*api.Pod)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("failed to cast to pod: %v", obj)
|
||||||
|
}
|
||||||
|
if pod == nil {
|
||||||
|
return fmt.Errorf("pod is missing, skipping (%s/%s)", addr.TargetRef.Namespace, addr.TargetRef.Name)
|
||||||
|
}
|
||||||
|
for _, podIP := range pod.Status.PodIPs {
|
||||||
|
if podIP.IP == addr.IP {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("pod ip(s) doesn't match endpoint ip, skipping: %v vs %s (%s/%s)", pod.Status.PodIPs, addr.IP, addr.TargetRef.Namespace, addr.TargetRef.Name)
|
||||||
|
}
|
||||||
|
|
||||||
// normalizeClusterIPs adjust clusterIPs based on ClusterIP. This must not
|
// normalizeClusterIPs adjust clusterIPs based on ClusterIP. This must not
|
||||||
// consider any other fields.
|
// consider any other fields.
|
||||||
func normalizeClusterIPs(oldSvc, newSvc *api.Service) {
|
func normalizeClusterIPs(oldSvc, newSvc *api.Service) {
|
||||||
|
Loading…
Reference in New Issue
Block a user