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
25bc76da
Commit
25bc76da
authored
Jul 23, 2017
by
dhilipkumars
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Do not try run preStop hook when the gracePeriod is 0
Add UT for lifeCycle hooks
parent
60007128
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
136 additions
and
2 deletions
+136
-2
BUILD
pkg/kubelet/kuberuntime/BUILD
+1
-0
kuberuntime_container.go
pkg/kubelet/kuberuntime/kuberuntime_container.go
+2
-2
kuberuntime_container_test.go
pkg/kubelet/kuberuntime/kuberuntime_container_test.go
+133
-0
No files found.
pkg/kubelet/kuberuntime/BUILD
View file @
25bc76da
...
@@ -89,6 +89,7 @@ go_test(
...
@@ -89,6 +89,7 @@ go_test(
"//pkg/kubelet/apis/cri/v1alpha1/runtime:go_default_library",
"//pkg/kubelet/apis/cri/v1alpha1/runtime:go_default_library",
"//pkg/kubelet/container:go_default_library",
"//pkg/kubelet/container:go_default_library",
"//pkg/kubelet/container/testing:go_default_library",
"//pkg/kubelet/container/testing:go_default_library",
"//pkg/kubelet/lifecycle:go_default_library",
"//pkg/kubelet/metrics:go_default_library",
"//pkg/kubelet/metrics:go_default_library",
"//vendor/github.com/golang/mock/gomock:go_default_library",
"//vendor/github.com/golang/mock/gomock:go_default_library",
"//vendor/github.com/google/cadvisor/info/v1:go_default_library",
"//vendor/github.com/google/cadvisor/info/v1:go_default_library",
...
...
pkg/kubelet/kuberuntime/kuberuntime_container.go
View file @
25bc76da
...
@@ -574,8 +574,8 @@ func (m *kubeGenericRuntimeManager) killContainer(pod *v1.Pod, containerID kubec
...
@@ -574,8 +574,8 @@ func (m *kubeGenericRuntimeManager) killContainer(pod *v1.Pod, containerID kubec
glog
.
V
(
2
)
.
Infof
(
"Killing container %q with %d second grace period"
,
containerID
.
String
(),
gracePeriod
)
glog
.
V
(
2
)
.
Infof
(
"Killing container %q with %d second grace period"
,
containerID
.
String
(),
gracePeriod
)
// Run the pre-stop lifecycle hooks if applicable
.
// Run the pre-stop lifecycle hooks if applicable
and if there is enough time to run it
if
containerSpec
.
Lifecycle
!=
nil
&&
containerSpec
.
Lifecycle
.
PreStop
!=
nil
{
if
containerSpec
.
Lifecycle
!=
nil
&&
containerSpec
.
Lifecycle
.
PreStop
!=
nil
&&
gracePeriod
>
0
{
gracePeriod
=
gracePeriod
-
m
.
executePreStopHook
(
pod
,
containerID
,
containerSpec
,
gracePeriod
)
gracePeriod
=
gracePeriod
-
m
.
executePreStopHook
(
pod
,
containerID
,
containerSpec
,
gracePeriod
)
}
}
// always give containers a minimal shutdown window to avoid unnecessary SIGKILLs
// always give containers a minimal shutdown window to avoid unnecessary SIGKILLs
...
...
pkg/kubelet/kuberuntime/kuberuntime_container_test.go
View file @
25bc76da
...
@@ -18,6 +18,7 @@ package kuberuntime
...
@@ -18,6 +18,7 @@ package kuberuntime
import
(
import
(
"path/filepath"
"path/filepath"
"strings"
"testing"
"testing"
"time"
"time"
...
@@ -28,6 +29,7 @@ import (
...
@@ -28,6 +29,7 @@ import (
runtimeapi
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
runtimeapi
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
kubecontainer
"k8s.io/kubernetes/pkg/kubelet/container"
kubecontainer
"k8s.io/kubernetes/pkg/kubelet/container"
containertest
"k8s.io/kubernetes/pkg/kubelet/container/testing"
containertest
"k8s.io/kubernetes/pkg/kubelet/container/testing"
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
)
)
// TestRemoveContainer tests removing the container and its corresponding container logs.
// TestRemoveContainer tests removing the container and its corresponding container logs.
...
@@ -289,3 +291,134 @@ func TestGenerateContainerConfig(t *testing.T) {
...
@@ -289,3 +291,134 @@ func TestGenerateContainerConfig(t *testing.T) {
_
,
err
=
m
.
generateContainerConfig
(
&
podWithContainerSecurityContext
.
Spec
.
Containers
[
0
],
podWithContainerSecurityContext
,
0
,
""
,
podWithContainerSecurityContext
.
Spec
.
Containers
[
0
]
.
Image
)
_
,
err
=
m
.
generateContainerConfig
(
&
podWithContainerSecurityContext
.
Spec
.
Containers
[
0
],
podWithContainerSecurityContext
,
0
,
""
,
podWithContainerSecurityContext
.
Spec
.
Containers
[
0
]
.
Image
)
assert
.
Error
(
t
,
err
)
assert
.
Error
(
t
,
err
)
}
}
func
TestLifeCycleHook
(
t
*
testing
.
T
)
{
// Setup
fakeRuntime
,
_
,
m
,
_
:=
createTestRuntimeManager
()
gracePeriod
:=
int64
(
30
)
cID
:=
kubecontainer
.
ContainerID
{
Type
:
"docker"
,
ID
:
"foo"
,
}
testPod
:=
&
v1
.
Pod
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"bar"
,
Namespace
:
"default"
,
},
Spec
:
v1
.
PodSpec
{
Containers
:
[]
v1
.
Container
{
{
Name
:
"foo"
,
Image
:
"busybox"
,
ImagePullPolicy
:
v1
.
PullIfNotPresent
,
Command
:
[]
string
{
"testCommand"
},
WorkingDir
:
"testWorkingDir"
,
},
},
},
}
cmdPostStart
:=
&
v1
.
Lifecycle
{
PostStart
:
&
v1
.
Handler
{
Exec
:
&
v1
.
ExecAction
{
Command
:
[]
string
{
"PostStartCMD"
},
},
},
}
httpLifeCycle
:=
&
v1
.
Lifecycle
{
PreStop
:
&
v1
.
Handler
{
HTTPGet
:
&
v1
.
HTTPGetAction
{
Host
:
"testHost.com"
,
Path
:
"/GracefulExit"
,
},
},
}
cmdLifeCycle
:=
&
v1
.
Lifecycle
{
PreStop
:
&
v1
.
Handler
{
Exec
:
&
v1
.
ExecAction
{
Command
:
[]
string
{
"PreStopCMD"
},
},
},
}
fakeRunner
:=
&
containertest
.
FakeContainerCommandRunner
{}
fakeHttp
:=
&
fakeHTTP
{}
lcHanlder
:=
lifecycle
.
NewHandlerRunner
(
fakeHttp
,
fakeRunner
,
nil
)
m
.
runner
=
lcHanlder
// Configured and works as expected
t
.
Run
(
"PreStop-CMDExec"
,
func
(
t
*
testing
.
T
)
{
testPod
.
Spec
.
Containers
[
0
]
.
Lifecycle
=
cmdLifeCycle
m
.
killContainer
(
testPod
,
cID
,
"foo"
,
"testKill"
,
&
gracePeriod
)
if
fakeRunner
.
Cmd
[
0
]
!=
cmdLifeCycle
.
PreStop
.
Exec
.
Command
[
0
]
{
t
.
Errorf
(
"CMD Prestop hook was not invoked"
)
}
})
// Configured and working HTTP hook
t
.
Run
(
"PreStop-HTTPGet"
,
func
(
t
*
testing
.
T
)
{
defer
func
()
{
fakeHttp
.
url
=
""
}()
testPod
.
Spec
.
Containers
[
0
]
.
Lifecycle
=
httpLifeCycle
m
.
killContainer
(
testPod
,
cID
,
"foo"
,
"testKill"
,
&
gracePeriod
)
if
!
strings
.
Contains
(
fakeHttp
.
url
,
httpLifeCycle
.
PreStop
.
HTTPGet
.
Host
)
{
t
.
Errorf
(
"HTTP Prestop hook was not invoked"
)
}
})
// When there is no time to run PreStopHook
t
.
Run
(
"PreStop-NoTimeToRun"
,
func
(
t
*
testing
.
T
)
{
gracePeriodLocal
:=
int64
(
0
)
testPod
.
DeletionGracePeriodSeconds
=
&
gracePeriodLocal
testPod
.
Spec
.
TerminationGracePeriodSeconds
=
&
gracePeriodLocal
m
.
killContainer
(
testPod
,
cID
,
"foo"
,
"testKill"
,
&
gracePeriodLocal
)
if
strings
.
Contains
(
fakeHttp
.
url
,
httpLifeCycle
.
PreStop
.
HTTPGet
.
Host
)
{
t
.
Errorf
(
"HTTP Should not execute when gracePeriod is 0"
)
}
})
// Post Start script
t
.
Run
(
"PostStart-CmdExe"
,
func
(
t
*
testing
.
T
)
{
// Fake all the things you need before trying to create a container
fakeSandBox
,
_
:=
makeAndSetFakePod
(
t
,
m
,
fakeRuntime
,
testPod
)
fakeSandBoxConfig
,
_
:=
m
.
generatePodSandboxConfig
(
testPod
,
0
)
testPod
.
Spec
.
Containers
[
0
]
.
Lifecycle
=
cmdPostStart
testContainer
:=
&
testPod
.
Spec
.
Containers
[
0
]
fakePodStatus
:=
&
kubecontainer
.
PodStatus
{
ContainerStatuses
:
[]
*
kubecontainer
.
ContainerStatus
{
{
ID
:
kubecontainer
.
ContainerID
{
Type
:
"docker"
,
ID
:
testContainer
.
Name
,
},
Name
:
testContainer
.
Name
,
State
:
kubecontainer
.
ContainerStateCreated
,
CreatedAt
:
time
.
Unix
(
0
,
time
.
Now
()
.
Unix
()),
},
},
}
// Now try to create a container, which should in turn invoke PostStart Hook
_
,
err
:=
m
.
startContainer
(
fakeSandBox
.
Id
,
fakeSandBoxConfig
,
testContainer
,
testPod
,
fakePodStatus
,
nil
,
""
)
if
err
!=
nil
{
t
.
Errorf
(
"startContainer erro =%v"
,
err
)
}
if
fakeRunner
.
Cmd
[
0
]
!=
cmdPostStart
.
PostStart
.
Exec
.
Command
[
0
]
{
t
.
Errorf
(
"CMD PostStart hook was not invoked"
)
}
})
}
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