Skip to content

Commit 7d78675

Browse files
author
OpenShift Bot
committed
Merge pull request #5457 from kargakis/expose-right-port
Merged by openshift-bot
2 parents 400d31d + af96952 commit 7d78675

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

pkg/cmd/cli/cmd/expose.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,15 @@ func validate(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
100100
if len(cmdutil.GetFlagString(cmd, "protocol")) == 0 {
101101
cmd.Flags().Set("protocol", "TCP")
102102
}
103-
return validateFlags(cmd, "service/v1")
103+
return validateFlags(cmd, generator)
104104
case "":
105105
// Default exposing services as a route
106-
cmd.Flags().Set("generator", "route/v1")
106+
generator = "route/v1"
107+
cmd.Flags().Set("generator", generator)
107108
fallthrough
108109
case "route/v1":
109110
// We need to validate services exposed as routes
110-
if err := validateFlags(cmd, "route/v1"); err != nil {
111+
if err := validateFlags(cmd, generator); err != nil {
111112
return err
112113
}
113114
svc, err := kc.Services(info.Namespace).Get(info.Name)
@@ -118,12 +119,14 @@ func validate(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
118119
supportsTCP := false
119120
for _, port := range svc.Spec.Ports {
120121
if port.Protocol == kapi.ProtocolTCP {
122+
// Pass service target port as the route port
123+
cmd.Flags().Set("port", port.TargetPort.String())
121124
supportsTCP = true
122125
break
123126
}
124127
}
125128
if !supportsTCP {
126-
return fmt.Errorf("service %s doesn't support TCP", info.Name)
129+
return fmt.Errorf("service %q doesn't support TCP", info.Name)
127130
}
128131
}
129132

@@ -133,14 +136,15 @@ func validate(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
133136
return fmt.Errorf("cannot expose a %s as a route", mapping.Kind)
134137
case "":
135138
// Default exposing everything except services as a service
136-
cmd.Flags().Set("generator", "service/v1")
139+
generator = "service/v2"
140+
cmd.Flags().Set("generator", generator)
137141
fallthrough
138142
case "service/v1", "service/v2":
139143
// Set default protocol back for generating services
140144
if len(cmdutil.GetFlagString(cmd, "protocol")) == 0 {
141145
cmd.Flags().Set("protocol", "TCP")
142146
}
143-
return validateFlags(cmd, "service/v1")
147+
return validateFlags(cmd, generator)
144148
}
145149
}
146150

@@ -179,6 +183,12 @@ func validateFlags(cmd *cobra.Command, generator string) error {
179183
if len(cmdutil.GetFlagString(cmd, "port")) != 0 {
180184
invalidFlags = append(invalidFlags, "--port")
181185
}
186+
if len(cmdutil.GetFlagString(cmd, "load-balancer-ip")) != 0 {
187+
invalidFlags = append(invalidFlags, "--load-balancer-ip")
188+
}
189+
if len(cmdutil.GetFlagString(cmd, "session-affinity")) != 0 {
190+
invalidFlags = append(invalidFlags, "--session-affinity")
191+
}
182192
if cmdutil.GetFlagBool(cmd, "create-external-load-balancer") {
183193
invalidFlags = append(invalidFlags, "--create-external-load-balancer")
184194
}

pkg/route/generator/generate.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package generator
22

33
import (
44
"fmt"
5-
"strconv"
65
"strings"
76

87
kapi "k8s.io/kubernetes/pkg/api"
@@ -68,10 +67,8 @@ func (RouteGenerator) Generate(genericParams map[string]interface{}) (runtime.Ob
6867
if !found || len(portString) == 0 {
6968
portString = strings.Split(params["ports"], ",")[0]
7069
}
71-
72-
port, err := strconv.ParseInt(portString, 10, 0)
73-
if err != nil {
74-
return nil, err
70+
if len(portString) == 0 {
71+
return nil, fmt.Errorf("exposed service does not have any target ports specified")
7572
}
7673

7774
return &api.Route{
@@ -85,10 +82,7 @@ func (RouteGenerator) Generate(genericParams map[string]interface{}) (runtime.Ob
8582
Name: params["default-name"],
8683
},
8784
Port: &api.RoutePort{
88-
TargetPort: util.IntOrString{
89-
Kind: util.IntstrInt,
90-
IntVal: int(port),
91-
},
85+
TargetPort: util.NewIntOrStringFromString(portString),
9286
},
9387
},
9488
}, nil

pkg/route/generator/generate_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func TestGenerateRoute(t *testing.T) {
3939
},
4040
Port: &routeapi.RoutePort{
4141
TargetPort: util.IntOrString{
42-
Kind: util.IntstrInt,
43-
IntVal: 80,
42+
Kind: util.IntstrString,
43+
StrVal: "80",
4444
},
4545
},
4646
},
@@ -68,8 +68,8 @@ func TestGenerateRoute(t *testing.T) {
6868
},
6969
Port: &routeapi.RoutePort{
7070
TargetPort: util.IntOrString{
71-
Kind: util.IntstrInt,
72-
IntVal: 80,
71+
Kind: util.IntstrString,
72+
StrVal: "80",
7373
},
7474
},
7575
},

test/cmd/basicresources.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ oc delete svc external
7878
# Expose multiport service and verify we set a port in the route
7979
oc create -f test/fixtures/multiport-service.yaml
8080
oc expose svc/frontend --name route-with-set-port
81-
os::util::get_object_assert 'route route-with-set-port' "{{.spec.port.targetPort}}" '5432'
81+
os::util::get_object_assert 'route route-with-set-port' "{{.spec.port.targetPort}}" '8080'
8282
echo "expose: ok"
8383

8484
oc delete all --all

0 commit comments

Comments
 (0)