blob: 47b96a709fe5eed776198cc223315ebd71a5b18f [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package config
18
19import (
20 "errors"
21 "fmt"
22 "net/url"
23)
24
25// ServiceResolver knows how to convert a service reference into an actual location.
26type ServiceResolver interface {
27 ResolveEndpoint(namespace, name string) (*url.URL, error)
28}
29
30type defaultServiceResolver struct{}
31
32func 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
40func (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}