| ... | ... |
@@ -3,7 +3,6 @@ package cmd |
| 3 | 3 |
import ( |
| 4 | 4 |
"fmt" |
| 5 | 5 |
"io" |
| 6 |
- "strings" |
|
| 7 | 6 |
|
| 8 | 7 |
"github.com/spf13/cobra" |
| 9 | 8 |
kapi "k8s.io/kubernetes/pkg/api" |
| ... | ... |
@@ -54,6 +53,7 @@ func NewCmdExpose(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.C |
| 54 | 54 |
cmd.Flags().Set("protocol", "")
|
| 55 | 55 |
cmd.Flag("protocol").DefValue = ""
|
| 56 | 56 |
cmd.Flag("protocol").Changed = false
|
| 57 |
+ cmd.Flag("port").Usage = "The port that the resource should serve on."
|
|
| 57 | 58 |
defRun := cmd.Run |
| 58 | 59 |
cmd.Run = func(cmd *cobra.Command, args []string) {
|
| 59 | 60 |
err := validate(cmd, f, args) |
| ... | ... |
@@ -105,35 +105,18 @@ func validate(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
|
| 105 | 105 |
if len(kcmdutil.GetFlagString(cmd, "protocol")) == 0 {
|
| 106 | 106 |
cmd.Flags().Set("protocol", "TCP")
|
| 107 | 107 |
} |
| 108 |
- return validateFlags(cmd, generator) |
|
| 109 | 108 |
case "": |
| 110 | 109 |
// Default exposing services as a route |
| 111 | 110 |
generator = "route/v1" |
| 112 | 111 |
cmd.Flags().Set("generator", generator)
|
| 113 | 112 |
fallthrough |
| 114 | 113 |
case "route/v1": |
| 115 |
- // We need to validate services exposed as routes |
|
| 116 |
- if err := validateFlags(cmd, generator); err != nil {
|
|
| 117 |
- return err |
|
| 118 |
- } |
|
| 119 |
- svc, err := kc.Services(info.Namespace).Get(info.Name) |
|
| 114 |
+ route, err := unsecuredRoute(kc, namespace, info.Name, info.Name, kcmdutil.GetFlagString(cmd, "port")) |
|
| 120 | 115 |
if err != nil {
|
| 121 | 116 |
return err |
| 122 | 117 |
} |
| 123 |
- |
|
| 124 |
- supportsTCP := false |
|
| 125 |
- for _, port := range svc.Spec.Ports {
|
|
| 126 |
- if port.Protocol == kapi.ProtocolTCP {
|
|
| 127 |
- if len(port.Name) > 0 {
|
|
| 128 |
- // Pass service port name as the route target port, if it is named |
|
| 129 |
- cmd.Flags().Set("target-port", port.Name)
|
|
| 130 |
- } |
|
| 131 |
- supportsTCP = true |
|
| 132 |
- break |
|
| 133 |
- } |
|
| 134 |
- } |
|
| 135 |
- if !supportsTCP {
|
|
| 136 |
- return fmt.Errorf("service %q doesn't support TCP", info.Name)
|
|
| 118 |
+ if route.Spec.Port != nil {
|
|
| 119 |
+ cmd.Flags().Set("port", route.Spec.Port.TargetPort.String())
|
|
| 137 | 120 |
} |
| 138 | 121 |
} |
| 139 | 122 |
|
| ... | ... |
@@ -151,71 +134,8 @@ func validate(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
|
| 151 | 151 |
if len(kcmdutil.GetFlagString(cmd, "protocol")) == 0 {
|
| 152 | 152 |
cmd.Flags().Set("protocol", "TCP")
|
| 153 | 153 |
} |
| 154 |
- return validateFlags(cmd, generator) |
|
| 155 | 154 |
} |
| 156 | 155 |
} |
| 157 | 156 |
|
| 158 | 157 |
return nil |
| 159 | 158 |
} |
| 160 |
- |
|
| 161 |
-// validateFlags filters out flags that are not supposed to be used |
|
| 162 |
-// when exposing a resource; depends on the provided generator |
|
| 163 |
-func validateFlags(cmd *cobra.Command, generator string) error {
|
|
| 164 |
- invalidFlags := []string{}
|
|
| 165 |
- |
|
| 166 |
- switch generator {
|
|
| 167 |
- case "service/v1", "service/v2": |
|
| 168 |
- if len(kcmdutil.GetFlagString(cmd, "hostname")) != 0 {
|
|
| 169 |
- invalidFlags = append(invalidFlags, "--hostname") |
|
| 170 |
- } |
|
| 171 |
- if len(kcmdutil.GetFlagString(cmd, "path")) != 0 {
|
|
| 172 |
- invalidFlags = append(invalidFlags, "--path") |
|
| 173 |
- } |
|
| 174 |
- case "route/v1": |
|
| 175 |
- if len(kcmdutil.GetFlagString(cmd, "protocol")) != 0 {
|
|
| 176 |
- invalidFlags = append(invalidFlags, "--protocol") |
|
| 177 |
- } |
|
| 178 |
- if len(kcmdutil.GetFlagString(cmd, "type")) != 0 {
|
|
| 179 |
- invalidFlags = append(invalidFlags, "--type") |
|
| 180 |
- } |
|
| 181 |
- if len(kcmdutil.GetFlagString(cmd, "selector")) != 0 {
|
|
| 182 |
- invalidFlags = append(invalidFlags, "--selector") |
|
| 183 |
- } |
|
| 184 |
- if len(kcmdutil.GetFlagString(cmd, "container-port")) != 0 {
|
|
| 185 |
- invalidFlags = append(invalidFlags, "--container-port") |
|
| 186 |
- } |
|
| 187 |
- if len(kcmdutil.GetFlagString(cmd, "target-port")) != 0 {
|
|
| 188 |
- invalidFlags = append(invalidFlags, "--target-port") |
|
| 189 |
- } |
|
| 190 |
- if len(kcmdutil.GetFlagString(cmd, "external-ip")) != 0 {
|
|
| 191 |
- invalidFlags = append(invalidFlags, "--external-ip") |
|
| 192 |
- } |
|
| 193 |
- if len(kcmdutil.GetFlagString(cmd, "port")) != 0 {
|
|
| 194 |
- invalidFlags = append(invalidFlags, "--port") |
|
| 195 |
- } |
|
| 196 |
- if len(kcmdutil.GetFlagString(cmd, "load-balancer-ip")) != 0 {
|
|
| 197 |
- invalidFlags = append(invalidFlags, "--load-balancer-ip") |
|
| 198 |
- } |
|
| 199 |
- if len(kcmdutil.GetFlagString(cmd, "session-affinity")) != 0 {
|
|
| 200 |
- invalidFlags = append(invalidFlags, "--session-affinity") |
|
| 201 |
- } |
|
| 202 |
- if kcmdutil.GetFlagBool(cmd, "create-external-load-balancer") {
|
|
| 203 |
- invalidFlags = append(invalidFlags, "--create-external-load-balancer") |
|
| 204 |
- } |
|
| 205 |
- } |
|
| 206 |
- |
|
| 207 |
- msg := "" |
|
| 208 |
- switch len(invalidFlags) {
|
|
| 209 |
- case 0: |
|
| 210 |
- return nil |
|
| 211 |
- |
|
| 212 |
- case 1: |
|
| 213 |
- msg = invalidFlags[0] |
|
| 214 |
- |
|
| 215 |
- default: |
|
| 216 |
- commaSeparated, last := invalidFlags[:len(invalidFlags)-1], invalidFlags[len(invalidFlags)-1] |
|
| 217 |
- msg = fmt.Sprintf("%s or %s", strings.Join(commaSeparated, ", "), last)
|
|
| 218 |
- } |
|
| 219 |
- |
|
| 220 |
- return fmt.Errorf("cannot use %s when generating a %s", msg, strings.Split(generator, "/")[0])
|
|
| 221 |
-} |
| ... | ... |
@@ -23,7 +23,7 @@ func (RouteGenerator) ParamNames() []kubectl.GeneratorParam {
|
| 23 | 23 |
return []kubectl.GeneratorParam{
|
| 24 | 24 |
{"labels", false},
|
| 25 | 25 |
{"default-name", true},
|
| 26 |
- {"target-port", false},
|
|
| 26 |
+ {"port", false},
|
|
| 27 | 27 |
{"name", false},
|
| 28 | 28 |
{"hostname", false},
|
| 29 | 29 |
{"path", false},
|
| ... | ... |
@@ -76,7 +76,7 @@ func (RouteGenerator) Generate(genericParams map[string]interface{}) (runtime.Ob
|
| 76 | 76 |
}, |
| 77 | 77 |
} |
| 78 | 78 |
|
| 79 |
- portString := params["target-port"] |
|
| 79 |
+ portString := params["port"] |
|
| 80 | 80 |
if len(portString) > 0 {
|
| 81 | 81 |
var targetPort intstr.IntOrString |
| 82 | 82 |
if port, err := strconv.Atoi(portString); err == nil {
|
| ... | ... |
@@ -41,8 +41,8 @@ func TestGenerateRoute(t *testing.T) {
|
| 41 | 41 |
}, |
| 42 | 42 |
Port: &routeapi.RoutePort{
|
| 43 | 43 |
TargetPort: intstr.IntOrString{
|
| 44 |
- Type: intstr.String, |
|
| 45 |
- StrVal: "svcportname", |
|
| 44 |
+ Type: intstr.Int, |
|
| 45 |
+ IntVal: 80, |
|
| 46 | 46 |
}, |
| 47 | 47 |
}, |
| 48 | 48 |
}, |
| ... | ... |
@@ -53,7 +53,7 @@ func TestGenerateRoute(t *testing.T) {
|
| 53 | 53 |
"labels": "foo=bar", |
| 54 | 54 |
"name": "test", |
| 55 | 55 |
"default-name": "someservice", |
| 56 |
- "port": "80", |
|
| 56 |
+ "port": "web", |
|
| 57 | 57 |
"ports": "80,443", |
| 58 | 58 |
"hostname": "www.example.com", |
| 59 | 59 |
}, |
| ... | ... |
@@ -66,6 +66,12 @@ func TestGenerateRoute(t *testing.T) {
|
| 66 | 66 |
}, |
| 67 | 67 |
Spec: routeapi.RouteSpec{
|
| 68 | 68 |
Host: "www.example.com", |
| 69 |
+ Port: &routeapi.RoutePort{
|
|
| 70 |
+ TargetPort: intstr.IntOrString{
|
|
| 71 |
+ Type: intstr.String, |
|
| 72 |
+ StrVal: "web", |
|
| 73 |
+ }, |
|
| 74 |
+ }, |
|
| 69 | 75 |
To: api.ObjectReference{
|
| 70 | 76 |
Name: "someservice", |
| 71 | 77 |
}, |
| ... | ... |
@@ -235,7 +235,7 @@ os::cmd::expect_failure 'oc expose service frontend --port=40 --type=NodePort' |
| 235 | 235 |
os::cmd::expect_success 'oc expose service frontend --path=/test' |
| 236 | 236 |
os::cmd::expect_success_and_text "oc get route frontend --output-version=v1 --template='{{.spec.path}}'" "/test"
|
| 237 | 237 |
os::cmd::expect_success_and_text "oc get route frontend --output-version=v1 --template='{{.spec.to.name}}'" "frontend" # routes to correct service
|
| 238 |
-os::cmd::expect_success_and_text "oc get route frontend --output-version=v1 --template='{{.spec.port.targetPort}}'" "<no value>" # no target port for services with unnamed ports
|
|
| 238 |
+os::cmd::expect_success_and_text "oc get route frontend --output-version=v1 --template='{{.spec.port.targetPort}}'" ""
|
|
| 239 | 239 |
os::cmd::expect_success 'oc delete svc,route -l name=frontend' |
| 240 | 240 |
# Test that external services are exposable |
| 241 | 241 |
os::cmd::expect_success 'oc create -f test/testdata/external-service.yaml' |