Replace godep with dep
This commit is contained in:
parent
1e7489927c
commit
bf5616c65b
14883 changed files with 3937406 additions and 361781 deletions
44
vendor/k8s.io/apiserver/pkg/util/proxy/BUILD
generated
vendored
Normal file
44
vendor/k8s.io/apiserver/pkg/util/proxy/BUILD
generated
vendored
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["proxy_test.go"],
|
||||
library = ":go_default_library",
|
||||
deps = [
|
||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
||||
"//vendor/k8s.io/client-go/listers/core/v1:go_default_library",
|
||||
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["proxy.go"],
|
||||
deps = [
|
||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
||||
"//vendor/k8s.io/client-go/listers/core/v1:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
127
vendor/k8s.io/apiserver/pkg/util/proxy/proxy.go
generated
vendored
Normal file
127
vendor/k8s.io/apiserver/pkg/util/proxy/proxy.go
generated
vendored
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
Copyright 2017 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 proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
listersv1 "k8s.io/client-go/listers/core/v1"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
)
|
||||
|
||||
// findServicePort finds the service port by name or numerically.
|
||||
func findServicePort(svc *v1.Service, port intstr.IntOrString) (*v1.ServicePort, error) {
|
||||
for _, svcPort := range svc.Spec.Ports {
|
||||
if (port.Type == intstr.Int && int32(svcPort.Port) == port.IntVal) || (port.Type == intstr.String && svcPort.Name == port.StrVal) {
|
||||
return &svcPort, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.NewServiceUnavailable(fmt.Sprintf("no service port %q found for service %q", port.String(), svc.Name))
|
||||
}
|
||||
|
||||
// ResourceLocation returns a URL to which one can send traffic for the specified service.
|
||||
func ResolveEndpoint(services listersv1.ServiceLister, endpoints listersv1.EndpointsLister, namespace, id string) (*url.URL, error) {
|
||||
svc, err := services.Services(namespace).Get(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
port := intstr.FromInt(443)
|
||||
svcPort, err := findServicePort(svc, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case svc.Spec.Type == v1.ServiceTypeClusterIP, svc.Spec.Type == v1.ServiceTypeLoadBalancer, svc.Spec.Type == v1.ServiceTypeNodePort:
|
||||
// these are fine
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported service type %q", svc.Spec.Type)
|
||||
}
|
||||
|
||||
eps, err := endpoints.Endpoints(namespace).Get(svc.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(eps.Subsets) == 0 {
|
||||
return nil, errors.NewServiceUnavailable(fmt.Sprintf("no endpoints available for service %q", svc.Name))
|
||||
}
|
||||
|
||||
// Pick a random Subset to start searching from.
|
||||
ssSeed := rand.Intn(len(eps.Subsets))
|
||||
|
||||
// Find a Subset that has the port.
|
||||
for ssi := 0; ssi < len(eps.Subsets); ssi++ {
|
||||
ss := &eps.Subsets[(ssSeed+ssi)%len(eps.Subsets)]
|
||||
if len(ss.Addresses) == 0 {
|
||||
continue
|
||||
}
|
||||
for i := range ss.Ports {
|
||||
if ss.Ports[i].Name == svcPort.Name {
|
||||
// Pick a random address.
|
||||
ip := ss.Addresses[rand.Intn(len(ss.Addresses))].IP
|
||||
port := int(ss.Ports[i].Port)
|
||||
return &url.URL{
|
||||
Scheme: "https",
|
||||
Host: net.JoinHostPort(ip, strconv.Itoa(port)),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, errors.NewServiceUnavailable(fmt.Sprintf("no endpoints available for service %q", id))
|
||||
}
|
||||
|
||||
func ResolveCluster(services listersv1.ServiceLister, namespace, id string) (*url.URL, error) {
|
||||
svc, err := services.Services(namespace).Get(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
port := intstr.FromInt(443)
|
||||
|
||||
switch {
|
||||
case svc.Spec.Type == v1.ServiceTypeClusterIP && svc.Spec.ClusterIP == v1.ClusterIPNone:
|
||||
return nil, fmt.Errorf(`cannot route to service with ClusterIP "None"`)
|
||||
// use IP from a clusterIP for these service types
|
||||
case svc.Spec.Type == v1.ServiceTypeClusterIP, svc.Spec.Type == v1.ServiceTypeLoadBalancer, svc.Spec.Type == v1.ServiceTypeNodePort:
|
||||
svcPort, err := findServicePort(svc, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &url.URL{
|
||||
Scheme: "https",
|
||||
Host: net.JoinHostPort(svc.Spec.ClusterIP, fmt.Sprintf("%d", svcPort.Port)),
|
||||
}, nil
|
||||
case svc.Spec.Type == v1.ServiceTypeExternalName:
|
||||
if port.Type != intstr.Int {
|
||||
return nil, fmt.Errorf("named ports not supported")
|
||||
}
|
||||
return &url.URL{
|
||||
Scheme: "https",
|
||||
Host: net.JoinHostPort(svc.Spec.ExternalName, port.String()),
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported service type %q", svc.Spec.Type)
|
||||
}
|
||||
}
|
||||
243
vendor/k8s.io/apiserver/pkg/util/proxy/proxy_test.go
generated
vendored
Normal file
243
vendor/k8s.io/apiserver/pkg/util/proxy/proxy_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,243 @@
|
|||
/*
|
||||
Copyright 2017 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 proxy
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
v1listers "k8s.io/client-go/listers/core/v1"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
)
|
||||
|
||||
func TestResolve(t *testing.T) {
|
||||
matchingEndpoints := func(svc *v1.Service) []*v1.Endpoints {
|
||||
ports := []v1.EndpointPort{}
|
||||
for _, p := range svc.Spec.Ports {
|
||||
if p.TargetPort.Type != intstr.Int {
|
||||
continue
|
||||
}
|
||||
ports = append(ports, v1.EndpointPort{Name: p.Name, Port: p.TargetPort.IntVal})
|
||||
}
|
||||
|
||||
return []*v1.Endpoints{{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: svc.Namespace, Name: svc.Name},
|
||||
Subsets: []v1.EndpointSubset{{
|
||||
Addresses: []v1.EndpointAddress{{Hostname: "dummy-host", IP: "127.0.0.1"}},
|
||||
Ports: ports,
|
||||
}},
|
||||
}}
|
||||
}
|
||||
|
||||
type expectation struct {
|
||||
url string
|
||||
error bool
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
services []*v1.Service
|
||||
endpoints func(svc *v1.Service) []*v1.Endpoints
|
||||
|
||||
clusterMode expectation
|
||||
endpointMode expectation
|
||||
}{
|
||||
{
|
||||
name: "cluster ip without 443 port",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
ClusterIP: "hit",
|
||||
Ports: []v1.ServicePort{
|
||||
{Port: 1234, TargetPort: intstr.FromInt(1234)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: matchingEndpoints,
|
||||
|
||||
clusterMode: expectation{error: true},
|
||||
endpointMode: expectation{error: true},
|
||||
},
|
||||
{
|
||||
name: "cluster ip",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
ClusterIP: "hit",
|
||||
Ports: []v1.ServicePort{
|
||||
{Name: "https", Port: 443, TargetPort: intstr.FromInt(1443)},
|
||||
{Port: 1234, TargetPort: intstr.FromInt(1234)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: matchingEndpoints,
|
||||
|
||||
clusterMode: expectation{url: "https://hit:443"},
|
||||
endpointMode: expectation{url: "https://127.0.0.1:1443"},
|
||||
},
|
||||
{
|
||||
name: "cluster ip without endpoints",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
ClusterIP: "hit",
|
||||
Ports: []v1.ServicePort{
|
||||
{Name: "https", Port: 443, TargetPort: intstr.FromInt(1443)},
|
||||
{Port: 1234, TargetPort: intstr.FromInt(1234)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: nil,
|
||||
|
||||
clusterMode: expectation{url: "https://hit:443"},
|
||||
endpointMode: expectation{error: true},
|
||||
},
|
||||
{
|
||||
name: "none cluster ip",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
ClusterIP: v1.ClusterIPNone,
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: nil,
|
||||
|
||||
clusterMode: expectation{error: true},
|
||||
endpointMode: expectation{error: true},
|
||||
},
|
||||
{
|
||||
name: "loadbalancer",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeLoadBalancer,
|
||||
ClusterIP: "lb",
|
||||
Ports: []v1.ServicePort{
|
||||
{Name: "https", Port: 443, TargetPort: intstr.FromInt(1443)},
|
||||
{Port: 1234, TargetPort: intstr.FromInt(1234)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: matchingEndpoints,
|
||||
|
||||
clusterMode: expectation{url: "https://lb:443"},
|
||||
endpointMode: expectation{url: "https://127.0.0.1:1443"},
|
||||
},
|
||||
{
|
||||
name: "node port",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeNodePort,
|
||||
ClusterIP: "np",
|
||||
Ports: []v1.ServicePort{
|
||||
{Name: "https", Port: 443, TargetPort: intstr.FromInt(1443)},
|
||||
{Port: 1234, TargetPort: intstr.FromInt(1234)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: matchingEndpoints,
|
||||
|
||||
clusterMode: expectation{url: "https://np:443"},
|
||||
endpointMode: expectation{url: "https://127.0.0.1:1443"},
|
||||
},
|
||||
{
|
||||
name: "external name",
|
||||
services: []*v1.Service{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "one", Name: "alfa"},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeExternalName,
|
||||
ExternalName: "foo.bar.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoints: nil,
|
||||
|
||||
clusterMode: expectation{url: "https://foo.bar.com:443"},
|
||||
endpointMode: expectation{error: true},
|
||||
},
|
||||
{
|
||||
name: "missing service",
|
||||
services: nil,
|
||||
endpoints: nil,
|
||||
|
||||
clusterMode: expectation{error: true},
|
||||
endpointMode: expectation{error: true},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
serviceCache := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
serviceLister := v1listers.NewServiceLister(serviceCache)
|
||||
for i := range test.services {
|
||||
if err := serviceCache.Add(test.services[i]); err != nil {
|
||||
t.Fatalf("%s unexpected service add error: %v", test.name, err)
|
||||
}
|
||||
}
|
||||
|
||||
endpointCache := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
endpointLister := v1listers.NewEndpointsLister(endpointCache)
|
||||
if test.endpoints != nil {
|
||||
for _, svc := range test.services {
|
||||
for _, ep := range test.endpoints(svc) {
|
||||
if err := endpointCache.Add(ep); err != nil {
|
||||
t.Fatalf("%s unexpected endpoint add error: %v", test.name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
check := func(mode string, expected expectation, url *url.URL, err error) {
|
||||
switch {
|
||||
case err == nil && expected.error:
|
||||
t.Errorf("%s in %s mode expected error, got none", test.name, mode)
|
||||
case err != nil && expected.error:
|
||||
// ignore
|
||||
case err != nil:
|
||||
t.Errorf("%s in %s mode unexpected error: %v", test.name, mode, err)
|
||||
case url.String() != expected.url:
|
||||
t.Errorf("%s in %s mode expected url %q, got %q", test.name, mode, expected.url, url.String())
|
||||
}
|
||||
}
|
||||
|
||||
clusterURL, err := ResolveCluster(serviceLister, "one", "alfa")
|
||||
check("cluster", test.clusterMode, clusterURL, err)
|
||||
|
||||
endpointURL, err := ResolveEndpoint(serviceLister, endpointLister, "one", "alfa")
|
||||
check("endpoint", test.endpointMode, endpointURL, err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue