Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 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 config |
| 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) (*url.URL, error) |
| 28 | } |
| 29 | |
| 30 | type defaultServiceResolver struct{} |
| 31 | |
| 32 | func NewDefaultServiceResolver() ServiceResolver { |
| 33 | return &defaultServiceResolver{} |
| 34 | } |
| 35 | |
| 36 | // ResolveEndpoint constructs a service URL from a given namespace and name |
| 37 | // note that the name and namespace are required and by default all created addresses use HTTPS scheme. |
| 38 | // for example: |
| 39 | // name=ross namespace=andromeda resolves to https://ross.andromeda.svc:443 |
| 40 | func (sr defaultServiceResolver) ResolveEndpoint(namespace, name string) (*url.URL, error) { |
| 41 | if len(name) == 0 || len(namespace) == 0 { |
| 42 | return nil, errors.New("cannot resolve an empty service name or namespace") |
| 43 | } |
| 44 | return &url.URL{Scheme: "https", Host: fmt.Sprintf("%s.%s.svc:443", name, namespace)}, nil |
| 45 | } |