forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.go
93 lines (82 loc) · 2.22 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package util
import (
"fmt"
"strconv"
kapi "k8s.io/kubernetes/pkg/api"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/util/intstr"
"github.com/openshift/origin/pkg/route/api"
)
// UnsecuredRoute will return a route with enough info so that it can direct traffic to
// the service provided by --service. Callers of this helper are responsible for providing
// tls configuration, path, and the hostname of the route.
func UnsecuredRoute(kc *kclient.Client, namespace, routeName, serviceName, portString string) (*api.Route, error) {
if len(routeName) == 0 {
routeName = serviceName
}
svc, err := kc.Services(namespace).Get(serviceName)
if err != nil {
if len(portString) == 0 {
return nil, fmt.Errorf("you need to provide a route port via --port when exposing a non-existent service")
}
return &api.Route{
ObjectMeta: kapi.ObjectMeta{
Name: routeName,
},
Spec: api.RouteSpec{
To: api.RouteTargetReference{
Name: serviceName,
},
Port: resolveRoutePort(portString),
},
}, nil
}
ok, port := supportsTCP(svc)
if !ok {
return nil, fmt.Errorf("service %q doesn't support TCP", svc.Name)
}
route := &api.Route{
ObjectMeta: kapi.ObjectMeta{
Name: routeName,
Labels: svc.Labels,
},
Spec: api.RouteSpec{
To: api.RouteTargetReference{
Name: serviceName,
},
},
}
// If the service has multiple ports and the user didn't specify --port,
// then default the route port to a service port name.
if len(port.Name) > 0 && len(portString) == 0 {
route.Spec.Port = resolveRoutePort(port.Name)
}
// --port uber alles
if len(portString) > 0 {
route.Spec.Port = resolveRoutePort(portString)
}
return route, nil
}
func resolveRoutePort(portString string) *api.RoutePort {
if len(portString) == 0 {
return nil
}
var routePort intstr.IntOrString
integer, err := strconv.Atoi(portString)
if err != nil {
routePort = intstr.FromString(portString)
} else {
routePort = intstr.FromInt(integer)
}
return &api.RoutePort{
TargetPort: routePort,
}
}
func supportsTCP(svc *kapi.Service) (bool, kapi.ServicePort) {
for _, port := range svc.Spec.Ports {
if port.Protocol == kapi.ProtocolTCP {
return true, port
}
}
return false, kapi.ServicePort{}
}