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
e21da839
Commit
e21da839
authored
Apr 14, 2017
by
Ricky Pai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
extract content-generation concern from `ensureHostsFile`
add tests to assert the output of `ensureHostsFile`
parent
f0ce5bd8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
1 deletion
+50
-1
kubelet_pods.go
pkg/kubelet/kubelet_pods.go
+7
-1
kubelet_pods_test.go
pkg/kubelet/kubelet_pods_test.go
+43
-0
No files found.
pkg/kubelet/kubelet_pods.go
View file @
e21da839
...
@@ -213,6 +213,12 @@ func ensureHostsFile(fileName, hostIP, hostName, hostDomainName string) error {
...
@@ -213,6 +213,12 @@ func ensureHostsFile(fileName, hostIP, hostName, hostDomainName string) error {
glog
.
V
(
4
)
.
Infof
(
"kubernetes-managed etc-hosts file exits. Will not be recreated: %q"
,
fileName
)
glog
.
V
(
4
)
.
Infof
(
"kubernetes-managed etc-hosts file exits. Will not be recreated: %q"
,
fileName
)
return
nil
return
nil
}
}
content
:=
hostsFileContent
(
hostIP
,
hostName
,
hostDomainName
)
return
ioutil
.
WriteFile
(
fileName
,
content
,
0644
)
}
// hostsFileContent is the content of the managed etc hosts
func
hostsFileContent
(
hostIP
,
hostName
,
hostDomainName
string
)
[]
byte
{
var
buffer
bytes
.
Buffer
var
buffer
bytes
.
Buffer
buffer
.
WriteString
(
"# Kubernetes-managed hosts file.
\n
"
)
buffer
.
WriteString
(
"# Kubernetes-managed hosts file.
\n
"
)
buffer
.
WriteString
(
"127.0.0.1
\t
localhost
\n
"
)
// ipv4 localhost
buffer
.
WriteString
(
"127.0.0.1
\t
localhost
\n
"
)
// ipv4 localhost
...
@@ -226,7 +232,7 @@ func ensureHostsFile(fileName, hostIP, hostName, hostDomainName string) error {
...
@@ -226,7 +232,7 @@ func ensureHostsFile(fileName, hostIP, hostName, hostDomainName string) error {
}
else
{
}
else
{
buffer
.
WriteString
(
fmt
.
Sprintf
(
"%s
\t
%s
\n
"
,
hostIP
,
hostName
))
buffer
.
WriteString
(
fmt
.
Sprintf
(
"%s
\t
%s
\n
"
,
hostIP
,
hostName
))
}
}
return
ioutil
.
WriteFile
(
fileName
,
buffer
.
Bytes
(),
0644
)
return
buffer
.
Bytes
(
)
}
}
// truncatePodHostnameIfNeeded truncates the pod hostname if it's longer than 63 chars.
// truncatePodHostnameIfNeeded truncates the pod hostname if it's longer than 63 chars.
...
...
pkg/kubelet/kubelet_pods_test.go
View file @
e21da839
...
@@ -114,6 +114,49 @@ func TestMakeMounts(t *testing.T) {
...
@@ -114,6 +114,49 @@ func TestMakeMounts(t *testing.T) {
assert
.
Equal
(
t
,
expectedMounts
,
mounts
,
"mounts of container %+v"
,
container
)
assert
.
Equal
(
t
,
expectedMounts
,
mounts
,
"mounts of container %+v"
,
container
)
}
}
func
TestHostsFileContent
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
hostIP
string
hostName
string
hostDomainName
string
expectedContent
string
}{
{
"123.45.67.89"
,
"podFoo"
,
""
,
`# Kubernetes-managed hosts file.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
123.45.67.89 podFoo
`
,
},
{
"203.0.113.1"
,
"podFoo"
,
"domainFoo"
,
`# Kubernetes-managed hosts file.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
203.0.113.1 podFoo.domainFoo podFoo
`
,
},
}
for
_
,
testCase
:=
range
testCases
{
actualContent
:=
string
(
hostsFileContent
(
testCase
.
hostIP
,
testCase
.
hostName
,
testCase
.
hostDomainName
))
assert
.
Equal
(
t
,
testCase
.
expectedContent
,
actualContent
,
"hosts file content not expected"
)
}
}
func
TestRunInContainerNoSuchPod
(
t
*
testing
.
T
)
{
func
TestRunInContainerNoSuchPod
(
t
*
testing
.
T
)
{
testKubelet
:=
newTestKubelet
(
t
,
false
/* controllerAttachDetachEnabled */
)
testKubelet
:=
newTestKubelet
(
t
,
false
/* controllerAttachDetachEnabled */
)
defer
testKubelet
.
Cleanup
()
defer
testKubelet
.
Cleanup
()
...
...
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