2015-09-21 22:51:27 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-09-21 22:51:27 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-06-06 22:43:57 +00:00
|
|
|
package podgc
|
2015-09-21 22:51:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/client/cache"
|
2016-02-05 21:58:03 +00:00
|
|
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
2015-09-21 22:51:27 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2016-04-13 18:38:32 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/metrics"
|
2016-01-15 07:32:10 +00:00
|
|
|
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
|
2016-02-02 10:57:06 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/wait"
|
2015-09-21 22:51:27 +00:00
|
|
|
"k8s.io/kubernetes/pkg/watch"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-10-06 09:12:00 +00:00
|
|
|
gcCheckPeriod = 20 * time.Second
|
2015-09-21 22:51:27 +00:00
|
|
|
)
|
|
|
|
|
2016-06-06 22:43:57 +00:00
|
|
|
type PodGCController struct {
|
2016-01-29 06:34:08 +00:00
|
|
|
kubeClient clientset.Interface
|
2015-09-21 22:51:27 +00:00
|
|
|
podStore cache.StoreToPodLister
|
2016-09-14 18:35:38 +00:00
|
|
|
podStoreSyncer *cache.Controller
|
2015-10-10 00:15:00 +00:00
|
|
|
deletePod func(namespace, name string) error
|
2015-09-21 22:51:27 +00:00
|
|
|
threshold int
|
|
|
|
}
|
|
|
|
|
2016-06-06 22:43:57 +00:00
|
|
|
func New(kubeClient clientset.Interface, resyncPeriod controller.ResyncPeriodFunc, threshold int) *PodGCController {
|
2016-04-13 18:38:32 +00:00
|
|
|
if kubeClient != nil && kubeClient.Core().GetRESTClient().GetRateLimiter() != nil {
|
|
|
|
metrics.RegisterMetricAndTrackRateLimiterUsage("gc_controller", kubeClient.Core().GetRESTClient().GetRateLimiter())
|
|
|
|
}
|
2016-06-06 22:43:57 +00:00
|
|
|
gcc := &PodGCController{
|
2015-09-21 22:51:27 +00:00
|
|
|
kubeClient: kubeClient,
|
2015-10-10 00:15:00 +00:00
|
|
|
threshold: threshold,
|
|
|
|
deletePod: func(namespace, name string) error {
|
2016-02-03 21:21:05 +00:00
|
|
|
return kubeClient.Core().Pods(namespace).Delete(name, api.NewDeleteOptions(0))
|
2015-09-21 22:51:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-12-17 11:57:07 +00:00
|
|
|
terminatedSelector := fields.ParseSelectorOrDie("status.phase!=" + string(api.PodPending) + ",status.phase!=" + string(api.PodRunning) + ",status.phase!=" + string(api.PodUnknown))
|
2015-09-21 22:51:27 +00:00
|
|
|
|
2016-09-14 18:35:38 +00:00
|
|
|
gcc.podStore.Indexer, gcc.podStoreSyncer = cache.NewIndexerInformer(
|
2015-09-21 22:51:27 +00:00
|
|
|
&cache.ListWatch{
|
2015-12-10 09:39:03 +00:00
|
|
|
ListFunc: func(options api.ListOptions) (runtime.Object, error) {
|
|
|
|
options.FieldSelector = terminatedSelector
|
2016-02-03 21:21:05 +00:00
|
|
|
return gcc.kubeClient.Core().Pods(api.NamespaceAll).List(options)
|
2015-09-21 22:51:27 +00:00
|
|
|
},
|
2015-12-10 09:39:03 +00:00
|
|
|
WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
|
|
|
|
options.FieldSelector = terminatedSelector
|
2016-02-03 21:21:05 +00:00
|
|
|
return gcc.kubeClient.Core().Pods(api.NamespaceAll).Watch(options)
|
2015-09-21 22:51:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&api.Pod{},
|
2015-10-06 09:12:00 +00:00
|
|
|
resyncPeriod(),
|
2016-09-14 18:35:38 +00:00
|
|
|
cache.ResourceEventHandlerFuncs{},
|
2016-05-02 04:35:18 +00:00
|
|
|
// We don't need to build a index for podStore here actually, but build one for consistency.
|
|
|
|
// It will ensure that if people start making use of the podStore in more specific ways,
|
|
|
|
// they'll get the benefits they expect. It will also reserve the name for future refactorings.
|
|
|
|
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
2015-09-21 22:51:27 +00:00
|
|
|
)
|
|
|
|
return gcc
|
|
|
|
}
|
|
|
|
|
2016-06-06 22:43:57 +00:00
|
|
|
func (gcc *PodGCController) Run(stop <-chan struct{}) {
|
2015-09-21 22:51:27 +00:00
|
|
|
go gcc.podStoreSyncer.Run(stop)
|
2016-02-02 10:57:06 +00:00
|
|
|
go wait.Until(gcc.gc, gcCheckPeriod, stop)
|
2015-09-21 22:51:27 +00:00
|
|
|
<-stop
|
|
|
|
}
|
|
|
|
|
2016-06-06 22:43:57 +00:00
|
|
|
func (gcc *PodGCController) gc() {
|
2015-09-21 22:51:27 +00:00
|
|
|
terminatedPods, _ := gcc.podStore.List(labels.Everything())
|
|
|
|
terminatedPodCount := len(terminatedPods)
|
|
|
|
sort.Sort(byCreationTimestamp(terminatedPods))
|
|
|
|
|
|
|
|
deleteCount := terminatedPodCount - gcc.threshold
|
|
|
|
|
|
|
|
if deleteCount > terminatedPodCount {
|
|
|
|
deleteCount = terminatedPodCount
|
|
|
|
}
|
|
|
|
if deleteCount > 0 {
|
|
|
|
glog.Infof("garbage collecting %v pods", deleteCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wait sync.WaitGroup
|
|
|
|
for i := 0; i < deleteCount; i++ {
|
|
|
|
wait.Add(1)
|
|
|
|
go func(namespace string, name string) {
|
|
|
|
defer wait.Done()
|
2015-10-10 00:15:00 +00:00
|
|
|
if err := gcc.deletePod(namespace, name); err != nil {
|
2015-09-21 22:51:27 +00:00
|
|
|
// ignore not founds
|
2016-01-15 07:32:10 +00:00
|
|
|
defer utilruntime.HandleError(err)
|
2015-09-21 22:51:27 +00:00
|
|
|
}
|
|
|
|
}(terminatedPods[i].Namespace, terminatedPods[i].Name)
|
|
|
|
}
|
|
|
|
wait.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// byCreationTimestamp sorts a list by creation timestamp, using their names as a tie breaker.
|
|
|
|
type byCreationTimestamp []*api.Pod
|
|
|
|
|
|
|
|
func (o byCreationTimestamp) Len() int { return len(o) }
|
|
|
|
func (o byCreationTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
|
|
|
|
|
|
|
|
func (o byCreationTimestamp) Less(i, j int) bool {
|
|
|
|
if o[i].CreationTimestamp.Equal(o[j].CreationTimestamp) {
|
|
|
|
return o[i].Name < o[j].Name
|
|
|
|
}
|
|
|
|
return o[i].CreationTimestamp.Before(o[j].CreationTimestamp)
|
|
|
|
}
|