Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move port splitting to common place; add to node resource location #7073

Merged
merged 1 commit into from Apr 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions pkg/registry/minion/rest.go
Expand Up @@ -24,12 +24,14 @@ import (
"strconv"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
)

Expand Down Expand Up @@ -129,12 +131,21 @@ func MatchNode(label labels.Selector, field fields.Selector) generic.Matcher {

// ResourceLocation returns a URL to which one can send traffic for the specified node.
func ResourceLocation(getter ResourceGetter, connection client.ConnectionInfoGetter, ctx api.Context, id string) (*url.URL, http.RoundTripper, error) {
nodeObj, err := getter.Get(ctx, id)
name, portReq, valid := util.SplitPort(id)
if !valid {
return nil, nil, errors.NewBadRequest(fmt.Sprintf("invalid node request %q", id))
}

nodeObj, err := getter.Get(ctx, name)
if err != nil {
return nil, nil, err
}
node := nodeObj.(*api.Node)
host := node.Name
host := node.Name // TODO: use node's IP, don't expect the name to resolve.

if portReq != "" {
return &url.URL{Host: net.JoinHostPort(host, portReq)}, nil, nil
}

scheme, port, transport, err := connection.GetConnectionInfo(host)
if err != nil {
Expand Down
13 changes: 4 additions & 9 deletions pkg/registry/pod/rest.go
Expand Up @@ -21,7 +21,6 @@ import (
"net"
"net/http"
"net/url"
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
Expand All @@ -31,6 +30,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
)

Expand Down Expand Up @@ -150,16 +150,11 @@ func getPod(getter ResourceGetter, ctx api.Context, name string) (*api.Pod, erro
func ResourceLocation(getter ResourceGetter, ctx api.Context, id string) (*url.URL, http.RoundTripper, error) {
// Allow ID as "podname" or "podname:port". If port is not specified,
// try to use the first defined port on the pod.
parts := strings.Split(id, ":")
if len(parts) > 2 {
name, port, valid := util.SplitPort(id)
if !valid {
return nil, nil, errors.NewBadRequest(fmt.Sprintf("invalid pod request %q", id))
}
name := parts[0]
port := ""
if len(parts) == 2 {
// TODO: if port is not a number but a "(container)/(portname)", do a name lookup.
port = parts[1]
}
// TODO: if port is not a number but a "(container)/(portname)", do a name lookup.

pod, err := getPod(getter, ctx, name)
if err != nil {
Expand Down
11 changes: 3 additions & 8 deletions pkg/registry/service/rest.go
Expand Up @@ -23,7 +23,6 @@ import (
"net/http"
"net/url"
"strconv"
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
Expand All @@ -34,6 +33,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/endpoint"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/golang/glog"
Expand Down Expand Up @@ -206,15 +206,10 @@ var _ = rest.Redirector(&REST{})
// ResourceLocation returns a URL to which one can send traffic for the specified service.
func (rs *REST) ResourceLocation(ctx api.Context, id string) (*url.URL, http.RoundTripper, error) {
// Allow ID as "svcname" or "svcname:port".
parts := strings.Split(id, ":")
if len(parts) > 2 {
svcName, portStr, valid := util.SplitPort(id)
if !valid {
return nil, nil, errors.NewBadRequest(fmt.Sprintf("invalid service request %q", id))
}
svcName := parts[0]
portStr := ""
if len(parts) == 2 {
portStr = parts[1]
}

eps, err := rs.endpoints.GetEndpoints(ctx, svcName)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions pkg/util/port_split.go
@@ -0,0 +1,40 @@
/*
Copyright 2015 Google Inc. All rights reserved.
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 util

import (
"strings"
)

// Takes a string of the form "name:port" or "name".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very similar to but different from http://golang.org/pkg/net/#SplitHostPort - is it worthwhile, really? or should we thunk down to that when we can?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we can investigate whether we can just use that. I wanted to at least get these three cases consistent, though.

// * If id is of the form "name" or "name:", then return (name, "", true)
// * If id is of the form "name:port", then return (name, port, true)
// * Otherwise, return ("", "", false)
// Additionally, name must be non-empty or valid will be returned false.
//
// Port is returned as a string, and it is not required to be numeric (could be
// used for a named port, for example).
func SplitPort(id string) (name, port string, valid bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that in all cases you test valid and then return an error, perhaps just return an error instead of the 'valid' bool?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is different in all three places, though... I also don't think util can return an api error (dependency goes the other direction), and if I return a regular error, then it has to get wrapped in all three places anyway.

parts := strings.Split(id, ":")
if len(parts) > 2 {
return "", "", false
}
if len(parts) == 2 {
return parts[0], parts[1], len(parts[0]) > 0
}
return id, "", len(id) > 0
}
65 changes: 65 additions & 0 deletions pkg/util/port_split_test.go
@@ -0,0 +1,65 @@
/*
Copyright 2015 Google Inc. All rights reserved.

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 util

import (
"testing"
)

func TestSplitPort(t *testing.T) {
table := []struct {
in string
name, port string
valid bool
}{
{
in: "aoeu:asdf",
name: "aoeu",
port: "asdf",
valid: true,
}, {
in: "aoeu:",
name: "aoeu",
valid: true,
}, {
in: ":asdf",
name: "",
port: "asdf",
}, {
in: "aoeu:asdf:htns",
}, {
in: "aoeu",
name: "aoeu",
valid: true,
}, {
in: "",
},
}

for _, item := range table {
name, port, valid := SplitPort(item.in)
if e, a := item.name, name; e != a {
t.Errorf("%q: Wanted %q, got %q", item.in, e, a)
}
if e, a := item.port, port; e != a {
t.Errorf("%q: Wanted %q, got %q", item.in, e, a)
}
if e, a := item.valid, valid; e != a {
t.Errorf("%q: Wanted %q, got %q", item.in, e, a)
}
}
}