2019-01-12 04:58:27 +00:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// findServicePort finds the service port by name or numerically.
|
2019-08-30 18:33:25 +00:00
|
|
|
func findServicePort(svc *v1.Service, port int32) (*v1.ServicePort, error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
for _, svcPort := range svc.Spec.Ports {
|
2019-08-30 18:33:25 +00:00
|
|
|
if svcPort.Port == port {
|
2019-01-12 04:58:27 +00:00
|
|
|
return &svcPort, nil
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
return nil, errors.NewServiceUnavailable(fmt.Sprintf("no service port %d found for service %q", port, svc.Name))
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceLocation returns a URL to which one can send traffic for the specified service.
|
2019-08-30 18:33:25 +00:00
|
|
|
func ResolveEndpoint(services listersv1.ServiceLister, endpoints listersv1.EndpointsLister, namespace, id string, port int32) (*url.URL, error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
svc, err := services.Services(namespace).Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
func ResolveCluster(services listersv1.ServiceLister, namespace, id string, port int32) (*url.URL, error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
svc, err := services.Services(namespace).Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
return &url.URL{
|
|
|
|
Scheme: "https",
|
2019-08-30 18:33:25 +00:00
|
|
|
Host: net.JoinHostPort(svc.Spec.ExternalName, fmt.Sprintf("%d", port)),
|
2019-01-12 04:58:27 +00:00
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported service type %q", svc.Spec.Type)
|
|
|
|
}
|
|
|
|
}
|