Skip to content

Commit

Permalink
F - have reaper use sorting strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
brianberzins committed Apr 27, 2022
1 parent 9e152d7 commit d73e974
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 22 deletions.
85 changes: 85 additions & 0 deletions examples/pod-sorting-strategy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# example configuration with permission for running pod-reaper against
# an entire cluster

---
# namespace for the reaper
apiVersion: v1
kind: Namespace
metadata:
name: reaper

---
# service account for running pod-reaper
apiVersion: v1
kind: ServiceAccount
metadata:
name: pod-reaper-service-account
namespace: reaper

---
# minimal permissions required for running pod-reaper at cluster level
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: pod-reaper-cluster-role
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["list", "delete"]

---
# binding the above cluster role (permissions) to the above service account
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: pod-reaper-role-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: pod-reaper-cluster-role
subjects:
- kind: ServiceAccount
name: pod-reaper-service-account
namespace: reaper

---
# a basic pod-reaper deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-reaper
namespace: reaper # namespace matches above
spec:
replicas: 1
selector:
matchLabels:
app: pod-reaper
template:
metadata:
labels:
app: pod-reaper
spec:
serviceAccount: pod-reaper-service-account # service account from above
containers:
- name: chaos
image: brianberzins/pod-reaper:alpha
resources:
limits:
cpu: 30m
memory: 30Mi
requests:
cpu: 20m
memory: 20Mi
env:
- name: EXCLUDE_LABEL_KEY
value: "tier"
- name: EXCLUDE_LABEL_VALUES
value: "control-plane"
- name: SCHEDULE
value: "@every 20s"
- name: CHAOS_CHANCE
value: "1"
- name: MAX_PODS
value: "1"
- name: POD_SORTING_STRATEGY
value: "oldest-first"
32 changes: 13 additions & 19 deletions reaper/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type options struct {
annotationRequirement *labels.Requirement
dryRun bool
maxPods int
podSortingStrategy func([]v1.Pod)
rules rules.Rules
evict bool
}
Expand Down Expand Up @@ -235,44 +236,37 @@ func evict() (bool, error) {

func loadOptions() (options options, err error) {
options.namespace = namespace()
options.gracePeriod, err = gracePeriod()
if err != nil {
if options.gracePeriod, err = gracePeriod(); err != nil {
return options, err
}
options.schedule = schedule()
options.runDuration, err = runDuration()
if err != nil {
if options.runDuration, err = runDuration(); err != nil {
return options, err
}
options.labelExclusion, err = labelExclusion()
if err != nil {
if options.labelExclusion, err = labelExclusion(); err != nil {
return options, err
}
options.labelRequirement, err = labelRequirement()
if err != nil {
if options.labelRequirement, err = labelRequirement(); err != nil {
return options, err
}
options.annotationRequirement, err = annotationRequirement()
if err != nil {
if options.annotationRequirement, err = annotationRequirement(); err != nil {
return options, err
}
options.dryRun, err = dryRun()
if err != nil {
if options.dryRun, err = dryRun(); err != nil {
return options, err
}
options.maxPods, err = maxPods()
if err != nil {
if options.maxPods, err = maxPods(); err != nil {
return options, err
}

options.evict, err = evict()
if err != nil {
if options.podSortingStrategy, err = podSortingStrategy(); err != nil {
return options, err
}
if options.evict, err = evict(); err != nil {
return options, err
}

// rules
options.rules, err = rules.LoadRules()
if err != nil {
if options.rules, err = rules.LoadRules(); err != nil {
return options, err
}
return options, nil
Expand Down
4 changes: 1 addition & 3 deletions reaper/reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ func (reaper reaper) getPods() *v1.PodList {
listOptions.LabelSelector = selector.String()
}
podList, err := pods.List(listOptions)

// get a sorting strategy
// sort by that strategy
reaper.options.podSortingStrategy(podList.Items)

if err != nil {
logrus.WithError(err).Panic("unable to get pods from the cluster")
Expand Down

0 comments on commit d73e974

Please sign in to comment.