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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
28 deletions
+34
-28
kubernetes.go
pkg/util/kubernetes.go
+34
-28
No files found.
pkg/util/kubernetes.go
View file @
378a27e8
...
@@ -65,42 +65,48 @@ type KubernetesSecret struct {
...
@@ -65,42 +65,48 @@ type KubernetesSecret struct {
Data
map
[
string
]
string
`json:"data,omitempty"`
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
// 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
// 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
// one, it looks up the service name in DNS. If that doesn't work, it returns the
// service URL.
// default service URL. If that's empty, it returns an HTTP localhost URL for the
func
GetServiceURL
(
serviceURL
,
serviceName
,
servicePort
string
)
string
{
// service port. If service port is empty, it panics.
if
serviceURL
==
""
{
func
GetServiceURL
(
serviceName
,
servicePort
,
serviceURL
string
)
(
string
,
error
)
{
serviceURL
=
MakeEnvVariableURL
(
serviceName
)
if
serviceName
!=
""
{
if
serviceURL
==
""
{
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
)
addrs
,
err
:=
net
.
LookupHost
(
serviceName
)
if
err
!=
nil
||
len
(
addrs
)
<
1
{
if
err
==
nil
&&
len
(
addrs
)
>
0
{
log
.
Fatalf
(
"cannot resolve service:%v. environment:%v
\n
"
,
serviceName
,
os
.
Environ
())
return
fmt
.
Sprintf
(
"http://%s:%s"
,
addrs
[
0
],
servicePort
),
nil
}
}
serviceURL
=
fmt
.
Sprintf
(
"http://%s:%s"
,
addrs
[
0
],
servicePort
)
}
}
}
}
return
serviceURL
if
serviceURL
!=
""
{
}
return
serviceURL
,
nil
}
// MakeEnvVariableURL takes a service name and returns the value of the
if
servicePort
!=
""
{
// environment variable that identifies its URL, if it exists, or the empty
serviceURL
=
fmt
.
Sprintf
(
"http://localhost:%s"
,
servicePort
)
// string, if it doesn't.
return
serviceURL
,
nil
func
MakeEnvVariableURL
(
str
string
)
string
{
}
prefix
:=
MakeEnvVariableName
(
str
)
url
:=
os
.
Getenv
(
prefix
+
"_PORT"
)
err
:=
fmt
.
Errorf
(
"cannot resolve service:%v in environment:%v
\n
"
,
serviceName
,
os
.
Environ
()
)
return
strings
.
Replace
(
url
,
"tcp"
,
"http"
,
1
)
return
""
,
err
}
}
//
MakeEnvVariableName is copied from the Kubernetes source,
//
GetServiceURLOrDie calls GetServiceURL and exits if it returns an error.
// which is referenced by the documentation for service environment variables.
func
GetServiceURLOrDie
(
serviceName
,
servicePort
,
serviceURL
string
)
string
{
func
MakeEnvVariableName
(
str
string
)
string
{
URL
,
err
:=
GetServiceURL
(
serviceName
,
servicePort
,
serviceURL
)
// TODO: If we simplify to "all names are DNS1123Subdomains" this
if
err
!=
nil
{
// will need two tweaks:
log
.
Fatal
(
err
)
// 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