Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
e9f1b0f9
Commit
e9f1b0f9
authored
Nov 16, 2016
by
Random-Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix hostname truncate.
parent
d51e27fe
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
5 deletions
+50
-5
kubelet_pods.go
pkg/kubelet/kubelet_pods.go
+21
-5
kubelet_pods_test.go
pkg/kubelet/kubelet_pods_test.go
+29
-0
No files found.
pkg/kubelet/kubelet_pods.go
View file @
e9f1b0f9
...
...
@@ -228,13 +228,29 @@ func makePortMappings(container *api.Container) (ports []kubecontainer.PortMappi
return
}
// truncatePodHostnameIfNeeded truncates the pod hostname if it's longer than 63 chars.
func
truncatePodHostnameIfNeeded
(
podName
,
hostname
string
)
(
string
,
error
)
{
// Cap hostname at 63 chars (specification is 64bytes which is 63 chars and the null terminating char).
const
hostnameMaxLen
=
63
if
len
(
hostname
)
<=
hostnameMaxLen
{
return
hostname
,
nil
}
truncated
:=
hostname
[
:
hostnameMaxLen
]
glog
.
Errorf
(
"hostname for pod:%q was longer than %d. Truncated hostname to :%q"
,
podName
,
hostnameMaxLen
,
truncated
)
// hostname should not end with '-' or '.'
truncated
=
strings
.
TrimRight
(
truncated
,
"-."
)
if
len
(
truncated
)
==
0
{
// This should never happen.
return
""
,
fmt
.
Errorf
(
"hostname for pod %q was invalid: %q"
,
podName
,
hostname
)
}
return
truncated
,
nil
}
// GeneratePodHostNameAndDomain creates a hostname and domain name for a pod,
// given that pod's spec and annotations or returns an error.
func
(
kl
*
Kubelet
)
GeneratePodHostNameAndDomain
(
pod
*
api
.
Pod
)
(
string
,
string
,
error
)
{
// TODO(vmarmol): Handle better.
// Cap hostname at 63 chars (specification is 64bytes which is 63 chars and the null terminating char).
clusterDomain
:=
kl
.
clusterDomain
const
hostnameMaxLen
=
63
podAnnotations
:=
pod
.
Annotations
if
podAnnotations
==
nil
{
podAnnotations
=
make
(
map
[
string
]
string
)
...
...
@@ -252,9 +268,9 @@ func (kl *Kubelet) GeneratePodHostNameAndDomain(pod *api.Pod) (string, string, e
hostname
=
hostnameCandidate
}
}
if
len
(
hostname
)
>
hostnameMaxLen
{
hostname
=
hostname
[
:
hostnameMaxLen
]
glog
.
Errorf
(
"hostname for pod:%q was longer than %d. Truncated hostname to :%q"
,
pod
.
Name
,
hostnameMaxLen
,
hostname
)
hostname
,
err
:=
truncatePodHostnameIfNeeded
(
pod
.
Name
,
hostname
)
if
err
!=
nil
{
return
""
,
""
,
err
}
hostDomain
:=
""
...
...
pkg/kubelet/kubelet_pods_test.go
View file @
e9f1b0f9
...
...
@@ -1494,3 +1494,32 @@ func TestHasHostNamespace(t *testing.T) {
}
}
}
func
TestTruncatePodHostname
(
t
*
testing
.
T
)
{
for
c
,
test
:=
range
map
[
string
]
struct
{
input
string
output
string
}{
"valid hostname"
:
{
input
:
"test.pod.hostname"
,
output
:
"test.pod.hostname"
,
},
"too long hostname"
:
{
input
:
"1234567.1234567.1234567.1234567.1234567.1234567.1234567.1234567.1234567."
,
// 8*9=72 chars
output
:
"1234567.1234567.1234567.1234567.1234567.1234567.1234567.1234567"
,
//8*8-1=63 chars
},
"hostname end with ."
:
{
input
:
"1234567.1234567.1234567.1234567.1234567.1234567.1234567.123456.1234567."
,
// 8*9-1=71 chars
output
:
"1234567.1234567.1234567.1234567.1234567.1234567.1234567.123456"
,
//8*8-2=62 chars
},
"hostname end with -"
:
{
input
:
"1234567.1234567.1234567.1234567.1234567.1234567.1234567.123456-1234567."
,
// 8*9-1=71 chars
output
:
"1234567.1234567.1234567.1234567.1234567.1234567.1234567.123456"
,
//8*8-2=62 chars
},
}
{
t
.
Logf
(
"TestCase: %q"
,
c
)
output
,
err
:=
truncatePodHostnameIfNeeded
(
"test-pod"
,
test
.
input
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
test
.
output
,
output
)
}
}
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