Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
H
helm3
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
helm3
Commits
378a27e8
Commit
378a27e8
authored
Apr 06, 2016
by
jackgr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Streamline service lookup
parent
30f8db7f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
27 deletions
+33
-27
kubernetes.go
pkg/util/kubernetes.go
+33
-27
No files found.
pkg/util/kubernetes.go
View file @
378a27e8
...
...
@@ -65,42 +65,48 @@ type KubernetesSecret struct {
Data
map
[
string
]
string
`json:"data,omitempty"`
}
// GetServiceURL takes a
default service URL, a service name and a service port
,
// GetServiceURL takes a
service name, a service port, and a default service URL
,
// and returns a URL for accessing the service. It first looks for an environment
// variable set by Kubernetes by transposing the service name. If it can't find
// one, it looks up the service name in DNS. If that fails, it returns the default
// service URL.
func
GetServiceURL
(
serviceURL
,
serviceName
,
servicePort
string
)
string
{
if
serviceURL
==
""
{
serviceURL
=
MakeEnvVariableURL
(
serviceName
)
if
serviceURL
==
""
{
// one, it looks up the service name in DNS. If that doesn't work, it returns the
// default service URL. If that's empty, it returns an HTTP localhost URL for the
// service port. If service port is empty, it panics.
func
GetServiceURL
(
serviceName
,
servicePort
,
serviceURL
string
)
(
string
,
error
)
{
if
serviceName
!=
""
{
varBase
:=
strings
.
Replace
(
serviceName
,
"-"
,
"_"
,
-
1
)
varName
:=
strings
.
ToUpper
(
varBase
)
+
"_PORT"
serviceURL
:=
os
.
Getenv
(
varName
)
if
serviceURL
!=
""
{
return
strings
.
Replace
(
serviceURL
,
"tcp"
,
"http"
,
1
),
nil
}
if
servicePort
!=
""
{
addrs
,
err
:=
net
.
LookupHost
(
serviceName
)
if
err
!=
nil
||
len
(
addrs
)
<
1
{
log
.
Fatalf
(
"cannot resolve service:%v. environment:%v
\n
"
,
serviceName
,
os
.
Environ
())
if
err
==
nil
&&
len
(
addrs
)
>
0
{
return
fmt
.
Sprintf
(
"http://%s:%s"
,
addrs
[
0
],
servicePort
),
nil
}
}
}
serviceURL
=
fmt
.
Sprintf
(
"http://%s:%s"
,
addrs
[
0
],
servicePort
)
if
serviceURL
!=
""
{
return
serviceURL
,
nil
}
if
servicePort
!=
""
{
serviceURL
=
fmt
.
Sprintf
(
"http://localhost:%s"
,
servicePort
)
return
serviceURL
,
nil
}
return
serviceURL
err
:=
fmt
.
Errorf
(
"cannot resolve service:%v in environment:%v
\n
"
,
serviceName
,
os
.
Environ
())
return
""
,
err
}
// MakeEnvVariableURL takes a service name and returns the value of the
// environment variable that identifies its URL, if it exists, or the empty
// string, if it doesn't.
func
MakeEnvVariableURL
(
str
string
)
string
{
prefix
:=
MakeEnvVariableName
(
str
)
url
:=
os
.
Getenv
(
prefix
+
"_PORT"
)
return
strings
.
Replace
(
url
,
"tcp"
,
"http"
,
1
)
}
// GetServiceURLOrDie calls GetServiceURL and exits if it returns an error.
func
GetServiceURLOrDie
(
serviceName
,
servicePort
,
serviceURL
string
)
string
{
URL
,
err
:=
GetServiceURL
(
serviceName
,
servicePort
,
serviceURL
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
// MakeEnvVariableName is copied from the Kubernetes source,
// which is referenced by the documentation for service environment variables.
func
MakeEnvVariableName
(
str
string
)
string
{
// TODO: If we simplify to "all names are DNS1123Subdomains" this
// will need two tweaks:
// 1) Handle leading digits
// 2) Handle dots
return
strings
.
ToUpper
(
strings
.
Replace
(
str
,
"-"
,
"_"
,
-
1
))
return
URL
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment