Skip to content

Commit

Permalink
Add node affinity key: beta.kubernetes.io/arch for older versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
norseto committed Dec 15, 2019
2 parents ac709d3 + 4beb5c2 commit 049103b
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 15 deletions.
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
},
{
"name": "Microk8s",
"type": "go",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This CronJos cleans "Evicted" pods.

### Installation
```
kubectl apply -f https://github.com/norseto/k8s-watchdogs/releases/download/evicted-cleaner-v0.1.1/evicted-cleaner.yaml
kubectl apply -f https://github.com/norseto/k8s-watchdogs/releases/download/evicted-cleaner-v0.1.2/evicted-cleaner.yaml
```

## Pod Rebalancer
Expand Down
4 changes: 2 additions & 2 deletions cmd/pod-rebalancer/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

// Evicted Pod Cleaner
// Deletes all evicted pod.
// Pod Rebalancer
// Deletes pod scheduled biased node.

import (
"fmt"
Expand Down
38 changes: 28 additions & 10 deletions cmd/pod-rebalancer/rebalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ type replicaState struct {
type podState struct {
pod v1.Pod
node v1.Node
deleted bool
}

type rebalancer struct {
current *replicaState
maxRate float32
}

func (r *rebalancer) specReplicas() int32 {
Expand All @@ -33,7 +35,7 @@ func (r *rebalancer) currentReplicas() int32 {
}

func newRebalancer(current *replicaState) *rebalancer {
return &rebalancer{current: current}
return &rebalancer{current: current, maxRate: .25}
}

func (r *rebalancer) Rebalance(c *kubernetes.Clientset) (bool, error) {
Expand All @@ -46,21 +48,35 @@ func (r *rebalancer) Rebalance(c *kubernetes.Clientset) (bool, error) {
return false, nil
}

node, num := r.maxPodNode()
ave := float32(sr) / float32(nodeCount)
if len(node) > 0 && float32(num) >= ave+1.0 {
err := r.deleteNodePod(c, node)
return true, err
deleted := 0
maxDel := int(float32(sr) * r.maxRate)
if maxDel < 1 {
maxDel = 1
}

return false, nil
for i := 0; i < maxDel; i++ {
node, num := r.maxPodNode()
ave := float32(sr) / float32(nodeCount)
if len(node) <= 0 || float32(num) < ave+1.0 {
return deleted > 0, nil
}
if err := r.deleteNodePod(c, node); err != nil {
return deleted > 0, err
}
deleted++
}

return deleted > 0, nil
}

// deleteNodePod deletes only one pod per replicaset.
func (r *rebalancer) deleteNodePod(c *kubernetes.Clientset, node string) error {
for _, s := range r.current.podState {
if s.node.Name == node {
l := len(r.current.podState)
for i := 0; i < l; i++ {
s := &r.current.podState[i]
if s.node.Name == node && !s.deleted {
log.Debug("Deleting pod " + s.pod.Name + " in " + node)
s.deleted = true
return k8sutils.DeletePod(c, s.pod)
}
}
Expand All @@ -73,7 +89,9 @@ func (r *rebalancer) maxPodNode() (string, int) {
m[n.Name] = 0
}
for _, s := range r.current.podState {
m[s.node.Name]++
if !s.deleted {
m[s.node.Name]++
}
}

maxVal := 0
Expand Down
7 changes: 6 additions & 1 deletion evicted-cleaner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ spec:
operator: In
values:
- amd64
- matchExpressions:
- key: beta.kubernetes.io/arch
operator: In
values:
- amd64
containers:
- name: evicted-cleaner
image: docker.io/norseto/evicted-cleaner:0.1.1
image: docker.io/norseto/evicted-cleaner:0.1.2
imagePullPolicy: IfNotPresent
restartPolicy: OnFailure
activeDeadlineSeconds: 60
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cloud.google.com/go v0.34.0 h1:eOI3/cP2VTU6uZLDYAoic+eyzzB9YyGmJ7eIjl8rOPg=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -23,6 +24,7 @@ github.com/json-iterator/go v1.1.8 h1:QiWkFLKq0T7mpzwOTu6BzNDbfTE8OLrYhVKYMLF46O
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -64,7 +66,9 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
Expand Down
2 changes: 1 addition & 1 deletion pkg/k8sutils/rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (u *rsowners) IsRollingUpdating(rs *appsv1.ReplicaSet) bool {
// bool : True if pod of replicaset scheduling is limited.
func IsPodScheduleLimeted(rs appsv1.ReplicaSet) bool {
podSpec := rs.Spec.Template.Spec
return podSpec.Affinity != nil || len(podSpec.Tolerations) > 0 || len(podSpec.NodeSelector) > 0
return podSpec.Affinity != nil || len(podSpec.NodeSelector) > 0
}

// IsPodOwnedBy determins the owner of the pod is the specified replicaset
Expand Down
5 changes: 5 additions & 0 deletions pod-rebalancer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ spec:
operator: In
values:
- amd64
- matchExpressions:
- key: beta.kubernetes.io/arch
operator: In
values:
- amd64
containers:
- name: pod-rebalancer
image: docker.io/norseto/pod-rebalancer:0.1.1
Expand Down

0 comments on commit 049103b

Please sign in to comment.