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
d624c7de
Commit
d624c7de
authored
Oct 06, 2015
by
feisky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pass the ContainerGCPolicy in Runtime.GarbageCollect
parent
69867fb5
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
31 additions
and
32 deletions
+31
-32
server.go
cmd/kubelet/app/server.go
+1
-1
container_gc.go
pkg/kubelet/container/container_gc.go
+6
-8
fake_runtime.go
pkg/kubelet/container/fake_runtime.go
+1
-1
runtime.go
pkg/kubelet/container/runtime.go
+2
-3
container_gc.go
pkg/kubelet/dockertools/container_gc.go
+10
-9
container_gc_test.go
pkg/kubelet/dockertools/container_gc_test.go
+5
-4
manager.go
pkg/kubelet/dockertools/manager.go
+2
-2
kubelet.go
pkg/kubelet/kubelet.go
+3
-3
rkt.go
pkg/kubelet/rkt/rkt.go
+1
-1
No files found.
cmd/kubelet/app/server.go
View file @
d624c7de
...
@@ -860,7 +860,7 @@ func CreateAndInitKubelet(kc *KubeletConfig) (k KubeletBootstrap, pc *config.Pod
...
@@ -860,7 +860,7 @@ func CreateAndInitKubelet(kc *KubeletConfig) (k KubeletBootstrap, pc *config.Pod
kubeClient
=
kc
.
KubeClient
kubeClient
=
kc
.
KubeClient
}
}
gcPolicy
:=
kube
let
.
ContainerGCPolicy
{
gcPolicy
:=
kube
container
.
ContainerGCPolicy
{
MinAge
:
kc
.
MinimumGCAge
,
MinAge
:
kc
.
MinimumGCAge
,
MaxPerPodContainer
:
kc
.
MaxPerPodContainerCount
,
MaxPerPodContainer
:
kc
.
MaxPerPodContainerCount
,
MaxContainers
:
kc
.
MaxContainerCount
,
MaxContainers
:
kc
.
MaxContainerCount
,
...
...
pkg/kubelet/container_gc.go
→
pkg/kubelet/container
/container
_gc.go
View file @
d624c7de
...
@@ -14,13 +14,11 @@ See the License for the specific language governing permissions and
...
@@ -14,13 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
package
kubelet
package
container
import
(
import
(
"fmt"
"fmt"
"time"
"time"
"k8s.io/kubernetes/pkg/kubelet/container"
)
)
// Specified a policy for garbage collecting containers.
// Specified a policy for garbage collecting containers.
...
@@ -39,7 +37,7 @@ type ContainerGCPolicy struct {
...
@@ -39,7 +37,7 @@ type ContainerGCPolicy struct {
// Manages garbage collection of dead containers.
// Manages garbage collection of dead containers.
//
//
// Implementation is thread-compatible.
// Implementation is thread-compatible.
type
c
ontainerGC
interface
{
type
C
ontainerGC
interface
{
// Garbage collect containers.
// Garbage collect containers.
GarbageCollect
()
error
GarbageCollect
()
error
}
}
...
@@ -47,14 +45,14 @@ type containerGC interface {
...
@@ -47,14 +45,14 @@ type containerGC interface {
// TODO(vmarmol): Preferentially remove pod infra containers.
// TODO(vmarmol): Preferentially remove pod infra containers.
type
realContainerGC
struct
{
type
realContainerGC
struct
{
// Container runtime
// Container runtime
runtime
container
.
Runtime
runtime
Runtime
// Policy for garbage collection.
// Policy for garbage collection.
policy
ContainerGCPolicy
policy
ContainerGCPolicy
}
}
// New
c
ontainerGC instance with the specified policy.
// New
C
ontainerGC instance with the specified policy.
func
newContainerGC
(
runtime
container
.
Runtime
,
policy
ContainerGCPolicy
)
(
c
ontainerGC
,
error
)
{
func
NewContainerGC
(
runtime
Runtime
,
policy
ContainerGCPolicy
)
(
C
ontainerGC
,
error
)
{
if
policy
.
MinAge
<
0
{
if
policy
.
MinAge
<
0
{
return
nil
,
fmt
.
Errorf
(
"invalid minimum garbage collection age: %v"
,
policy
.
MinAge
)
return
nil
,
fmt
.
Errorf
(
"invalid minimum garbage collection age: %v"
,
policy
.
MinAge
)
}
}
...
@@ -66,5 +64,5 @@ func newContainerGC(runtime container.Runtime, policy ContainerGCPolicy) (contai
...
@@ -66,5 +64,5 @@ func newContainerGC(runtime container.Runtime, policy ContainerGCPolicy) (contai
}
}
func
(
cgc
*
realContainerGC
)
GarbageCollect
()
error
{
func
(
cgc
*
realContainerGC
)
GarbageCollect
()
error
{
return
cgc
.
runtime
.
GarbageCollect
(
cgc
.
policy
.
MaxPerPodContainer
,
cgc
.
policy
.
MaxContainers
,
cgc
.
policy
.
MinAge
)
return
cgc
.
runtime
.
GarbageCollect
(
cgc
.
policy
)
}
}
pkg/kubelet/container/fake_runtime.go
View file @
d624c7de
...
@@ -305,7 +305,7 @@ func (f *FakeRuntime) PortForward(pod *Pod, port uint16, stream io.ReadWriteClos
...
@@ -305,7 +305,7 @@ func (f *FakeRuntime) PortForward(pod *Pod, port uint16, stream io.ReadWriteClos
return
f
.
Err
return
f
.
Err
}
}
func
(
f
*
FakeRuntime
)
GarbageCollect
(
maxPerPodContainer
,
maxContainers
int
,
minAge
time
.
Duration
)
error
{
func
(
f
*
FakeRuntime
)
GarbageCollect
(
gcPolicy
ContainerGCPolicy
)
error
{
f
.
Lock
()
f
.
Lock
()
defer
f
.
Unlock
()
defer
f
.
Unlock
()
...
...
pkg/kubelet/container/runtime.go
View file @
d624c7de
...
@@ -22,7 +22,6 @@ import (
...
@@ -22,7 +22,6 @@ import (
"io"
"io"
"reflect"
"reflect"
"strings"
"strings"
"time"
"github.com/golang/glog"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
...
@@ -75,8 +74,8 @@ type Runtime interface {
...
@@ -75,8 +74,8 @@ type Runtime interface {
// specifies whether the runtime returns all containers including those already
// specifies whether the runtime returns all containers including those already
// exited and dead containers (used for garbage collection).
// exited and dead containers (used for garbage collection).
GetPods
(
all
bool
)
([]
*
Pod
,
error
)
GetPods
(
all
bool
)
([]
*
Pod
,
error
)
// Garbage
collection of dead containers
// Garbage
Collect removes dead containers using the specified container gc policy
GarbageCollect
(
maxPerPodContainer
,
maxContainers
int
,
minAge
time
.
Duration
)
error
GarbageCollect
(
gcPolicy
ContainerGCPolicy
)
error
// Syncs the running pod into the desired pod.
// Syncs the running pod into the desired pod.
SyncPod
(
pod
*
api
.
Pod
,
runningPod
Pod
,
podStatus
api
.
PodStatus
,
pullSecrets
[]
api
.
Secret
,
backOff
*
util
.
Backoff
)
error
SyncPod
(
pod
*
api
.
Pod
,
runningPod
Pod
,
podStatus
api
.
PodStatus
,
pullSecrets
[]
api
.
Secret
,
backOff
*
util
.
Backoff
)
error
// KillPod kills all the containers of a pod. Pod may be nil, running pod must not be.
// KillPod kills all the containers of a pod. Pod may be nil, running pod must not be.
...
...
pkg/kubelet/dockertools/container_gc.go
View file @
d624c7de
...
@@ -26,6 +26,7 @@ import (
...
@@ -26,6 +26,7 @@ import (
docker
"github.com/fsouza/go-dockerclient"
docker
"github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
"github.com/golang/glog"
kubecontainer
"k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/types"
"k8s.io/kubernetes/pkg/types"
)
)
...
@@ -172,10 +173,10 @@ func (cgc *containerGC) evictableContainers(minAge time.Duration) (containersByE
...
@@ -172,10 +173,10 @@ func (cgc *containerGC) evictableContainers(minAge time.Duration) (containersByE
return
evictUnits
,
unidentifiedContainers
,
nil
return
evictUnits
,
unidentifiedContainers
,
nil
}
}
// Garbage
collection of dead containers
// Garbage
Collect removes dead containers using the specified container gc policy
func
(
cgc
*
containerGC
)
GarbageCollect
(
maxPerPodContainer
,
maxContainers
int
,
minAge
time
.
Duration
)
error
{
func
(
cgc
*
containerGC
)
GarbageCollect
(
gcPolicy
kubecontainer
.
ContainerGCPolicy
)
error
{
// Separate containers by evict units.
// Separate containers by evict units.
evictUnits
,
unidentifiedContainers
,
err
:=
cgc
.
evictableContainers
(
m
inAge
)
evictUnits
,
unidentifiedContainers
,
err
:=
cgc
.
evictableContainers
(
gcPolicy
.
M
inAge
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -190,14 +191,14 @@ func (cgc *containerGC) GarbageCollect(maxPerPodContainer, maxContainers int, mi
...
@@ -190,14 +191,14 @@ func (cgc *containerGC) GarbageCollect(maxPerPodContainer, maxContainers int, mi
}
}
// Enforce max containers per evict unit.
// Enforce max containers per evict unit.
if
m
axPerPodContainer
>=
0
{
if
gcPolicy
.
M
axPerPodContainer
>=
0
{
cgc
.
enforceMaxContainersPerEvictUnit
(
evictUnits
,
m
axPerPodContainer
)
cgc
.
enforceMaxContainersPerEvictUnit
(
evictUnits
,
gcPolicy
.
M
axPerPodContainer
)
}
}
// Enforce max total number of containers.
// Enforce max total number of containers.
if
maxContainers
>=
0
&&
evictUnits
.
NumContainers
()
>
m
axContainers
{
if
gcPolicy
.
MaxContainers
>=
0
&&
evictUnits
.
NumContainers
()
>
gcPolicy
.
M
axContainers
{
// Leave an equal number of containers per evict unit (min: 1).
// Leave an equal number of containers per evict unit (min: 1).
numContainersPerEvictUnit
:=
m
axContainers
/
evictUnits
.
NumEvictUnits
()
numContainersPerEvictUnit
:=
gcPolicy
.
M
axContainers
/
evictUnits
.
NumEvictUnits
()
if
numContainersPerEvictUnit
<
1
{
if
numContainersPerEvictUnit
<
1
{
numContainersPerEvictUnit
=
1
numContainersPerEvictUnit
=
1
}
}
...
@@ -205,14 +206,14 @@ func (cgc *containerGC) GarbageCollect(maxPerPodContainer, maxContainers int, mi
...
@@ -205,14 +206,14 @@ func (cgc *containerGC) GarbageCollect(maxPerPodContainer, maxContainers int, mi
// If we still need to evict, evict oldest first.
// If we still need to evict, evict oldest first.
numContainers
:=
evictUnits
.
NumContainers
()
numContainers
:=
evictUnits
.
NumContainers
()
if
numContainers
>
m
axContainers
{
if
numContainers
>
gcPolicy
.
M
axContainers
{
flattened
:=
make
([]
containerGCInfo
,
0
,
numContainers
)
flattened
:=
make
([]
containerGCInfo
,
0
,
numContainers
)
for
uid
:=
range
evictUnits
{
for
uid
:=
range
evictUnits
{
flattened
=
append
(
flattened
,
evictUnits
[
uid
]
...
)
flattened
=
append
(
flattened
,
evictUnits
[
uid
]
...
)
}
}
sort
.
Sort
(
byCreated
(
flattened
))
sort
.
Sort
(
byCreated
(
flattened
))
cgc
.
removeOldestN
(
flattened
,
numContainers
-
m
axContainers
)
cgc
.
removeOldestN
(
flattened
,
numContainers
-
gcPolicy
.
M
axContainers
)
}
}
}
}
...
...
pkg/kubelet/dockertools/container_gc_test.go
View file @
d624c7de
...
@@ -25,6 +25,7 @@ import (
...
@@ -25,6 +25,7 @@ import (
docker
"github.com/fsouza/go-dockerclient"
docker
"github.com/fsouza/go-dockerclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
kubecontainer
"k8s.io/kubernetes/pkg/kubelet/container"
)
)
func
newTestContainerGC
(
t
*
testing
.
T
)
(
*
containerGC
,
*
FakeDockerClient
)
{
func
newTestContainerGC
(
t
*
testing
.
T
)
(
*
containerGC
,
*
FakeDockerClient
)
{
...
@@ -92,7 +93,7 @@ func TestGarbageCollectZeroMaxContainers(t *testing.T) {
...
@@ -92,7 +93,7 @@ func TestGarbageCollectZeroMaxContainers(t *testing.T) {
makeContainerDetail
(
"1876"
,
false
,
makeTime
(
0
)),
makeContainerDetail
(
"1876"
,
false
,
makeTime
(
0
)),
)
)
assert
.
Nil
(
t
,
gc
.
GarbageCollect
(
1
,
0
,
time
.
Minute
))
assert
.
Nil
(
t
,
gc
.
GarbageCollect
(
kubecontainer
.
ContainerGCPolicy
{
time
.
Minute
,
1
,
0
}
))
assert
.
Len
(
t
,
fakeDocker
.
Removed
,
1
)
assert
.
Len
(
t
,
fakeDocker
.
Removed
,
1
)
}
}
...
@@ -113,7 +114,7 @@ func TestGarbageCollectNoMaxPerPodContainerLimit(t *testing.T) {
...
@@ -113,7 +114,7 @@ func TestGarbageCollectNoMaxPerPodContainerLimit(t *testing.T) {
makeContainerDetail
(
"5876"
,
false
,
makeTime
(
4
)),
makeContainerDetail
(
"5876"
,
false
,
makeTime
(
4
)),
)
)
assert
.
Nil
(
t
,
gc
.
GarbageCollect
(
-
1
,
4
,
time
.
Minute
))
assert
.
Nil
(
t
,
gc
.
GarbageCollect
(
kubecontainer
.
ContainerGCPolicy
{
time
.
Minute
,
-
1
,
4
}
))
assert
.
Len
(
t
,
fakeDocker
.
Removed
,
1
)
assert
.
Len
(
t
,
fakeDocker
.
Removed
,
1
)
}
}
...
@@ -134,7 +135,7 @@ func TestGarbageCollectNoMaxLimit(t *testing.T) {
...
@@ -134,7 +135,7 @@ func TestGarbageCollectNoMaxLimit(t *testing.T) {
makeContainerDetail
(
"5876"
,
false
,
makeTime
(
0
)),
makeContainerDetail
(
"5876"
,
false
,
makeTime
(
0
)),
)
)
assert
.
Nil
(
t
,
gc
.
GarbageCollect
(
1
,
-
1
,
time
.
Minute
))
assert
.
Nil
(
t
,
gc
.
GarbageCollect
(
kubecontainer
.
ContainerGCPolicy
{
time
.
Minute
,
1
,
-
1
}
))
assert
.
Len
(
t
,
fakeDocker
.
Removed
,
0
)
assert
.
Len
(
t
,
fakeDocker
.
Removed
,
0
)
}
}
...
@@ -306,7 +307,7 @@ func TestGarbageCollect(t *testing.T) {
...
@@ -306,7 +307,7 @@ func TestGarbageCollect(t *testing.T) {
gc
,
fakeDocker
:=
newTestContainerGC
(
t
)
gc
,
fakeDocker
:=
newTestContainerGC
(
t
)
fakeDocker
.
ContainerList
=
test
.
containers
fakeDocker
.
ContainerList
=
test
.
containers
fakeDocker
.
ContainerMap
=
test
.
containerDetails
fakeDocker
.
ContainerMap
=
test
.
containerDetails
assert
.
Nil
(
t
,
gc
.
GarbageCollect
(
2
,
6
,
time
.
Hour
))
assert
.
Nil
(
t
,
gc
.
GarbageCollect
(
kubecontainer
.
ContainerGCPolicy
{
time
.
Hour
,
2
,
6
}
))
verifyStringArrayEqualsAnyOrder
(
t
,
fakeDocker
.
Removed
,
test
.
expectedRemoved
)
verifyStringArrayEqualsAnyOrder
(
t
,
fakeDocker
.
Removed
,
test
.
expectedRemoved
)
}
}
}
}
pkg/kubelet/dockertools/manager.go
View file @
d624c7de
...
@@ -2025,6 +2025,6 @@ func (dm *DockerManager) GetNetNs(containerID kubecontainer.ContainerID) (string
...
@@ -2025,6 +2025,6 @@ func (dm *DockerManager) GetNetNs(containerID kubecontainer.ContainerID) (string
}
}
// Garbage collection of dead containers
// Garbage collection of dead containers
func
(
dm
*
DockerManager
)
GarbageCollect
(
maxPerPodContainer
,
maxContainers
int
,
minAge
time
.
Duration
)
error
{
func
(
dm
*
DockerManager
)
GarbageCollect
(
gcPolicy
kubecontainer
.
ContainerGCPolicy
)
error
{
return
dm
.
containerGC
.
GarbageCollect
(
maxPerPodContainer
,
maxContainers
,
minAge
)
return
dm
.
containerGC
.
GarbageCollect
(
gcPolicy
)
}
}
pkg/kubelet/kubelet.go
View file @
d624c7de
...
@@ -147,7 +147,7 @@ func NewMainKubelet(
...
@@ -147,7 +147,7 @@ func NewMainKubelet(
pullBurst
int
,
pullBurst
int
,
eventQPS
float32
,
eventQPS
float32
,
eventBurst
int
,
eventBurst
int
,
containerGCPolicy
ContainerGCPolicy
,
containerGCPolicy
kubecontainer
.
ContainerGCPolicy
,
sourcesReady
SourcesReadyFn
,
sourcesReady
SourcesReadyFn
,
registerNode
bool
,
registerNode
bool
,
standaloneMode
bool
,
standaloneMode
bool
,
...
@@ -356,7 +356,7 @@ func NewMainKubelet(
...
@@ -356,7 +356,7 @@ func NewMainKubelet(
}
}
// setup containerGC
// setup containerGC
containerGC
,
err
:=
n
ewContainerGC
(
klet
.
containerRuntime
,
containerGCPolicy
)
containerGC
,
err
:=
kubecontainer
.
N
ewContainerGC
(
klet
.
containerRuntime
,
containerGCPolicy
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -511,7 +511,7 @@ type Kubelet struct {
...
@@ -511,7 +511,7 @@ type Kubelet struct {
recorder
record
.
EventRecorder
recorder
record
.
EventRecorder
// Policy for handling garbage collection of dead containers.
// Policy for handling garbage collection of dead containers.
containerGC
c
ontainerGC
containerGC
kubecontainer
.
C
ontainerGC
// Manager for images.
// Manager for images.
imageManager
imageManager
imageManager
imageManager
...
...
pkg/kubelet/rkt/rkt.go
View file @
d624c7de
...
@@ -1083,7 +1083,7 @@ func (r *Runtime) GetContainerLogs(pod *api.Pod, containerID kubecontainer.Conta
...
@@ -1083,7 +1083,7 @@ func (r *Runtime) GetContainerLogs(pod *api.Pod, containerID kubecontainer.Conta
// GarbageCollect collects the pods/containers.
// GarbageCollect collects the pods/containers.
// TODO(yifan): Enforce the gc policy, also, it would be better if we can
// TODO(yifan): Enforce the gc policy, also, it would be better if we can
// just GC kubernetes pods.
// just GC kubernetes pods.
func
(
r
*
runtime
)
GarbageCollect
(
maxPerPodContainer
,
maxContainers
int
,
minAge
time
.
Duration
)
error
{
func
(
r
*
runtime
)
GarbageCollect
(
gcPolicy
kubecontainer
.
ContainerGCPolicy
)
error
{
if
err
:=
exec
.
Command
(
"systemctl"
,
"reset-failed"
)
.
Run
();
err
!=
nil
{
if
err
:=
exec
.
Command
(
"systemctl"
,
"reset-failed"
)
.
Run
();
err
!=
nil
{
glog
.
Errorf
(
"rkt: Failed to reset failed systemd services: %v"
,
err
)
glog
.
Errorf
(
"rkt: Failed to reset failed systemd services: %v"
,
err
)
}
}
...
...
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