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
3751ecc7
Commit
3751ecc7
authored
Apr 30, 2015
by
Yifan Gu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kubelet/container: Move ShouldContainerBeRestarted() to runtime.
parent
72708d74
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
35 deletions
+38
-35
helpers.go
pkg/kubelet/container/helpers.go
+37
-0
manager.go
pkg/kubelet/dockertools/manager.go
+1
-35
No files found.
pkg/kubelet/container/helpers.go
View file @
3751ecc7
...
...
@@ -20,6 +20,7 @@ import (
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog"
)
// HandlerRunner runs a lifecycle handler for a container.
...
...
@@ -44,3 +45,39 @@ func TrimRuntimePrefix(fullString string) string {
}
return
fullString
[
idx
+
len
(
prefixSeparator
)
:
]
}
// ShouldContainerBeRestarted checks whether a container needs to be restarted.
// TODO(yifan): Think about how to refactor this.
func
ShouldContainerBeRestarted
(
container
*
api
.
Container
,
pod
*
api
.
Pod
,
podStatus
*
api
.
PodStatus
,
readinessManager
*
ReadinessManager
)
bool
{
podFullName
:=
GetPodFullName
(
pod
)
// Get all dead container status.
var
resultStatus
[]
*
api
.
ContainerStatus
for
i
,
containerStatus
:=
range
podStatus
.
ContainerStatuses
{
if
containerStatus
.
Name
==
container
.
Name
&&
containerStatus
.
State
.
Termination
!=
nil
{
resultStatus
=
append
(
resultStatus
,
&
podStatus
.
ContainerStatuses
[
i
])
}
}
// Set dead containers to unready state.
for
_
,
c
:=
range
resultStatus
{
readinessManager
.
RemoveReadiness
(
TrimRuntimePrefix
(
c
.
ContainerID
))
}
// Check RestartPolicy for dead container.
if
len
(
resultStatus
)
>
0
{
if
pod
.
Spec
.
RestartPolicy
==
api
.
RestartPolicyNever
{
glog
.
V
(
4
)
.
Infof
(
"Already ran container %q of pod %q, do nothing"
,
container
.
Name
,
podFullName
)
return
false
}
if
pod
.
Spec
.
RestartPolicy
==
api
.
RestartPolicyOnFailure
{
// Check the exit code of last run. Note: This assumes the result is sorted
// by the created time in reverse order.
if
resultStatus
[
0
]
.
State
.
Termination
.
ExitCode
==
0
{
glog
.
V
(
4
)
.
Infof
(
"Already successfully ran container %q of pod %q, do nothing"
,
container
.
Name
,
podFullName
)
return
false
}
}
}
return
true
}
pkg/kubelet/dockertools/manager.go
View file @
3751ecc7
...
...
@@ -1247,7 +1247,7 @@ func (dm *DockerManager) ComputePodContainerChanges(pod *api.Pod, runningPod kub
c
:=
runningPod
.
FindContainerByName
(
container
.
Name
)
if
c
==
nil
{
if
s
houldContainerBeRestarted
(
&
container
,
pod
,
&
podStatus
,
dm
.
readinessManager
)
{
if
kubecontainer
.
S
houldContainerBeRestarted
(
&
container
,
pod
,
&
podStatus
,
dm
.
readinessManager
)
{
// If we are here it means that the container is dead and should be restarted, or never existed and should
// be created. We may be inserting this ID again if the container has changed and it has
// RestartPolicy::Always, but it's not a big deal.
...
...
@@ -1315,37 +1315,3 @@ func (dm *DockerManager) ComputePodContainerChanges(pod *api.Pod, runningPod kub
ContainersToKeep
:
containersToKeep
,
},
nil
}
func
shouldContainerBeRestarted
(
container
*
api
.
Container
,
pod
*
api
.
Pod
,
podStatus
*
api
.
PodStatus
,
readinessManager
*
kubecontainer
.
ReadinessManager
)
bool
{
podFullName
:=
kubecontainer
.
GetPodFullName
(
pod
)
// Get all dead container status.
var
resultStatus
[]
*
api
.
ContainerStatus
for
i
,
containerStatus
:=
range
podStatus
.
ContainerStatuses
{
if
containerStatus
.
Name
==
container
.
Name
&&
containerStatus
.
State
.
Termination
!=
nil
{
resultStatus
=
append
(
resultStatus
,
&
podStatus
.
ContainerStatuses
[
i
])
}
}
// Set dead containers to unready state.
for
_
,
c
:=
range
resultStatus
{
readinessManager
.
RemoveReadiness
(
kubecontainer
.
TrimRuntimePrefix
(
c
.
ContainerID
))
}
// Check RestartPolicy for dead container.
if
len
(
resultStatus
)
>
0
{
if
pod
.
Spec
.
RestartPolicy
==
api
.
RestartPolicyNever
{
glog
.
V
(
4
)
.
Infof
(
"Already ran container %q of pod %q, do nothing"
,
container
.
Name
,
podFullName
)
return
false
}
if
pod
.
Spec
.
RestartPolicy
==
api
.
RestartPolicyOnFailure
{
// Check the exit code of last run. Note: This assumes the result is sorted
// by the created time in reverse order.
if
resultStatus
[
0
]
.
State
.
Termination
.
ExitCode
==
0
{
glog
.
V
(
4
)
.
Infof
(
"Already successfully ran container %q of pod %q, do nothing"
,
container
.
Name
,
podFullName
)
return
false
}
}
}
return
true
}
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