Merge pull request #54333 from liggitt/webhook-service-resolver

Automatic merge from submit-queue (batch tested with PRs 54363, 54333). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Ensure port on resolved service host

The resolved host should include a port so it can be used by dialers directly. It's also not necessary to reparse the URL when constructing directly.

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2017-10-23 11:30:25 -07:00 committed by GitHub
commit fd3878a59d
2 changed files with 8 additions and 7 deletions

View File

@ -32,11 +32,10 @@ var _ admissioninit.ServiceResolver = defaultServiceResolver{}
// ResolveEndpoint constructs a service URL from a given namespace and name
// note that the name and namespace are required and by default all created addresses use HTTPS scheme.
// for example:
// name=ross namespace=andromeda resolves to https://ross.andromeda.svc
// name=ross namespace=andromeda resolves to https://ross.andromeda.svc:443
func (sr defaultServiceResolver) ResolveEndpoint(namespace, name string) (*url.URL, error) {
if len(name) == 0 || len(namespace) == 0 {
return &url.URL{}, errors.New("cannot resolve an empty service name or namespace")
return nil, errors.New("cannot resolve an empty service name or namespace")
}
return url.Parse(fmt.Sprintf("https://%s.%s.svc", name, namespace))
return &url.URL{Scheme: "https", Host: fmt.Sprintf("%s.%s.svc:443", name, namespace)}, nil
}

View File

@ -30,7 +30,7 @@ func TestDefaultServiceResolver(t *testing.T) {
expectError bool
}{
// scenario 1: a service name along with a namespace resolves
{serviceName: "ross", serviceNamespace: "andromeda", expectedOutput: "https://ross.andromeda.svc"},
{serviceName: "ross", serviceNamespace: "andromeda", expectedOutput: "https://ross.andromeda.svc:443"},
// scenario 2: a service name without a namespace does not resolve
{serviceName: "ross", expectError: true},
// scenario 3: cannot resolve an empty service name
@ -49,8 +49,10 @@ func TestDefaultServiceResolver(t *testing.T) {
if err == nil && scenario.expectError {
t.Error("expected an error but got nothing")
}
if serviceURL.String() != scenario.expectedOutput {
t.Errorf("expected = %s, got = %s", scenario.expectedOutput, serviceURL.String())
if !scenario.expectError {
if serviceURL.String() != scenario.expectedOutput {
t.Errorf("expected = %s, got = %s", scenario.expectedOutput, serviceURL.String())
}
}
})
}