| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package webhook |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"net/url" |
| 23 |
) |
| 24 |
|
| 25 |
// ServiceResolver knows how to convert a service reference into an actual location. |
| 26 |
type ServiceResolver interface { |
| 27 |
ResolveEndpoint(namespace, name string, port int32) (*url.URL, error) |
| 28 |
} |
| 29 |
|
| 30 |
type defaultServiceResolver struct{} |
| 31 |
|
| 32 |
// NewDefaultServiceResolver creates a new default server resolver. |
| 33 |
func NewDefaultServiceResolver() ServiceResolver { |
| 34 |
return &defaultServiceResolver{} |
| 35 |
} |
| 36 |
|
| 37 |
// ResolveEndpoint constructs a service URL from a given namespace and name |
| 38 |
// note that the name, namespace, and port are required and by default all |
| 39 |
// created addresses use HTTPS scheme. |
| 40 |
// for example: |
| 41 |
// |
| 42 |
// name=ross namespace=andromeda resolves to https://ross.andromeda.svc:443 |
| 43 |
func (sr defaultServiceResolver) ResolveEndpoint(namespace, name string, port int32) (*url.URL, error) { |
| 44 |
if len(name) == 0 || len(namespace) == 0 || port == 0 { |
| 45 |
return nil, errors.New("cannot resolve an empty service name or namespace or port") |
| 46 |
} |
| 47 |
return &url.URL{Scheme: "https", Host: fmt.Sprintf("%s.%s.svc:%d", name, namespace, port)}, nil |
| 48 |
} |