Skip to content

Commit

Permalink
move reusagebale code to base
Browse files Browse the repository at this point in the history
  • Loading branch information
yizhang-zen committed Nov 30, 2020
1 parent cbbbbeb commit 7306c9f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 48 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ make dev # run on cluster from $KUBECONFIG (defaults to ~/.kube/config)

### Test

Run unit tests: `make test`
Run a single suite: `go test -run TestSuiteFailedPodRescheduler github.com/aksgithub/kube_remediator/pkg/remediator`
Run a single test: comment out all other test in the suite and run the suite. TODO: improve.
- Run unit tests: `make test`
- Run a single suite: `go test -run TestSuiteFailedPodRescheduler github.com/aksgithub/kube_remediator/pkg/remediator`
- Run a single test: comment out all other test in the suite and run the suite. TODO: improve.

```bash
# CrashLoopBackOffRemediator: pod is rescheduled after restarting 5 times ?
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/golang/mock v1.3.1
github.com/google/cadvisor v0.34.0
github.com/googleapis/gnostic v0.3.1 // indirect
github.com/grosser/go-testcov v1.2.0 // indirect
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/json-iterator/go v1.1.7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsC
github.com/googleapis/gnostic v0.3.1 h1:WeAefnSUHlBb0iJKwxFDZdbfGwkd7xRNuV+IpXMJhYk=
github.com/googleapis/gnostic v0.3.1/go.mod h1:on+2t9HRStVgn95RSsFWFz+6Q0Snyqv1awfrALZdbtU=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grosser/go-testcov v1.2.0 h1:HKcgwi1GNB1LzH1mXI38GXyEPLYxSYD4FaIzHm858WY=
github.com/grosser/go-testcov v1.2.0/go.mod h1:DTjIHD69BN1AYLdU+iotHeGXFx/bL5RXZzfRsp4s5uA=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
Expand Down
22 changes: 11 additions & 11 deletions pkg/remediator/crashloopbackoffrescheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ func (p *CrashLoopBackOffRescheduler) Setup(logger *zap.Logger, client k8s.Clien
func (p *CrashLoopBackOffRescheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()

p.logger.Info("Starting")
// Check for any CrashLoopBackOff Pods first
p.reschedulePods()
p.logStartAndStop(func(){
// Check for any CrashLoopBackOff Pods first
p.reschedulePods()

informer := p.informerFactory.Core().V1().Pods().Informer()
informer := p.informerFactory.Core().V1().Pods().Informer()

informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: p.rescheduleIfNecessary,
})
informer.Run(ctx.Done())
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: p.rescheduleIfNecessary,
})
informer.Run(ctx.Done())

<-ctx.Done()
p.metrics.UnRegister()
p.logger.Info("Stopping", zap.String("reason", "Signal"))
<-ctx.Done()
p.metrics.UnRegister()
})
}

func (p *CrashLoopBackOffRescheduler) reschedulePods() {
Expand Down
22 changes: 11 additions & 11 deletions pkg/remediator/failedpodrescheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ func (p *FailedPodRescheduler) Setup(logger *zap.Logger, client k8s.ClientInterf
func (p *FailedPodRescheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()

p.logger.Info("Starting")
// Check for any Failed Pods first
p.reschedulePods()
// TODO: filter failed pods here to avoid overhead
informer := p.informerFactory.Core().V1().Pods().Informer()
p.logStartAndStop(func(){
// Check for any Failed Pods first
p.reschedulePods()
// TODO: filter failed pods here to avoid overhead
informer := p.informerFactory.Core().V1().Pods().Informer()

informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: p.rescheduleIfNecessary,
})
informer.Run(ctx.Done())
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: p.rescheduleIfNecessary,
})
informer.Run(ctx.Done())

<-ctx.Done()
p.logger.Info("Stopping", zap.String("reason", "Signal"))
<-ctx.Done()
})
}

func (p *FailedPodRescheduler) reschedulePods() {
Expand Down
24 changes: 1 addition & 23 deletions pkg/remediator/oldpoddeleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package remediator

import (
"context"
"github.com/aksgithub/kube_remediator/pkg/k8s"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sync"
Expand All @@ -16,23 +15,7 @@ type OldPodDeleter struct {
func (p *OldPodDeleter) Run(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()

ticker := time.NewTicker(1 * time.Hour)
defer ticker.Stop()

p.logger.Info("Starting")

// Run on start
p.deleteOldPods()

for {
select {
case <-ticker.C:
p.deleteOldPods() // untested section
case <-ctx.Done():
p.logger.Info("Stopping", zap.String("reason", "Signal"))
return
}
}
p.reconcileEvery(ctx, p.deleteOldPods, 1 * time.Hour)
}

func (p *OldPodDeleter) deleteOldPods() {
Expand All @@ -57,8 +40,3 @@ func (p *OldPodDeleter) deleteOldPods() {
}
}

func (p *OldPodDeleter) Setup(logger *zap.Logger, client k8s.ClientInterface) error {
p.client = client
p.logger = logger
return nil
}
34 changes: 34 additions & 0 deletions pkg/remediator/remediator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package remediator

import (
Expand All @@ -6,6 +7,7 @@ import (
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
"sync"
"time"
)

// will later be used to make arrays or remediators / testing
Expand All @@ -20,6 +22,38 @@ type Base struct {
logger *zap.Logger
}

func (p *Base) Setup(logger *zap.Logger, client k8s.ClientInterface) error {
p.client = client
p.logger = logger
return nil
}

func (p *Base) logStartAndStop(fn func()) {
defer p.logger.Info("Stopping", zap.String("reason", "Signal"))
p.logger.Info("Starting")
fn()
}

func (p *Base) reconcileEvery(ctx context.Context, fn func(), interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()

p.logStartAndStop(func(){
// Run on start
fn()

for {
select {
case <-ticker.C:
fn() // untested section
case <-ctx.Done():
return
}
}
})

}

func (p *Base) deletePod(pod v1.Pod) {
podInfo := []zap.Field{
zap.String("name", pod.ObjectMeta.Name),
Expand Down

0 comments on commit 7306c9f

Please sign in to comment.