Skip to content

Commit 5c889e0

Browse files
authored
Merge pull request #1 from fabiante/feat/minimal-controller
Add minimal working controller to kill non-running pods
2 parents a94375d + 5ceeed9 commit 5c889e0

24 files changed

+392
-44
lines changed

.golangci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ issues:
88
exclude-use-default: false
99
# restore some of the defaults
1010
# (fill in the rest as needed)
11+
exclude-dirs:
12+
- "test/e2e/*"
1113
exclude-rules:
1214
- path: "api/*"
1315
linters:

Makefile

+5-5
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
120120
docker-buildx: ## Build and push docker image for the manager for cross-platform support
121121
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
122122
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
123-
- $(CONTAINER_TOOL) buildx create --name podreaper-builder
124-
$(CONTAINER_TOOL) buildx use podreaper-builder
123+
- $(CONTAINER_TOOL) buildx create --name podbouncer-builder
124+
$(CONTAINER_TOOL) buildx use podbouncer-builder
125125
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
126-
- $(CONTAINER_TOOL) buildx rm podreaper-builder
126+
- $(CONTAINER_TOOL) buildx rm podbouncer-builder
127127
rm Dockerfile.cross
128128

129129
.PHONY: build-installer
@@ -140,11 +140,11 @@ endif
140140

141141
.PHONY: install
142142
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
143-
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
143+
# $(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
144144

145145
.PHONY: uninstall
146146
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
147-
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
147+
# $(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
148148

149149
.PHONY: deploy
150150
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.

PROJECT

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
domain: fabitee.de
66
layout:
77
- go.kubebuilder.io/v4
8-
projectName: podreaper
8+
projectName: podbouncer
99
repo: github.com/fabiante/podbouncer
10+
resources:
11+
- controller: true
12+
domain: fabitee.de
13+
kind: Pod
14+
version: v1alpha1
1015
version: "3"

README.md

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# podreaper
1+
# podbouncer
22

3-
Podreaper is a [Kubernetes operator](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/)
3+
Podbouncer is a [Kubernetes operator](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/)
44
responsible for deleting non-running pods after a configurable grace period.
55

66
## Description
77

8-
Using podreaper in your cluster will result in the deletion of all pods in one of
8+
Using podbouncer in your cluster will result in the deletion of all pods in one of
99
these states: `Pending`, `Completed`, `Failed`
1010

1111
This operator acts on pods of all namespaces, except the `kube-system` namespace.
@@ -24,7 +24,7 @@ This operator acts on pods of all namespaces, except the `kube-system` namespace
2424
**Build and push your image to the location specified by `IMG`:**
2525

2626
```sh
27-
make docker-build docker-push IMG=<some-registry>/podreaper:tag
27+
make docker-build docker-push IMG=<some-registry>/podbouncer:tag
2828
```
2929

3030
**NOTE:** This image ought to be published in the personal registry you specified.
@@ -40,7 +40,7 @@ make install
4040
**Deploy the Manager to the cluster with the image specified by `IMG`:**
4141

4242
```sh
43-
make deploy IMG=<some-registry>/podreaper:tag
43+
make deploy IMG=<some-registry>/podbouncer:tag
4444
```
4545

4646
> **NOTE**: If you encounter RBAC errors, you may need to grant yourself cluster-admin
@@ -81,7 +81,7 @@ Following are the steps to build the installer and distribute this project to us
8181
1. Build the installer for the image built and published in the registry:
8282

8383
```sh
84-
make build-installer IMG=<some-registry>/podreaper:tag
84+
make build-installer IMG=<some-registry>/podbouncer:tag
8585
```
8686

8787
NOTE: The makefile target mentioned above generates an 'install.yaml'
@@ -94,15 +94,26 @@ its dependencies.
9494
Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:
9595

9696
```sh
97-
kubectl apply -f https://raw.githubusercontent.com/<org>/podreaper/<tag or branch>/dist/install.yaml
97+
kubectl apply -f https://raw.githubusercontent.com/<org>/podbouncer/<tag or branch>/dist/install.yaml
98+
```
99+
100+
## Testing
101+
102+
### Create Test Pods
103+
104+
To test podbouncer, you must have some non-running pods in your cluster:
105+
106+
```shell
107+
kubectl run --restart=Never --image busybox some-pod
108+
kubectl run --restart=Never --image busybox some-other-pod
98109
```
99110

100111
## Contributing
101-
// TODO(user): Add detailed information on how you would like others to contribute to this project
102112

103-
**NOTE:** Run `make help` for more information on all potential `make` targets
113+
This project is a personal learning project.
104114

105-
More information can be found via the [Kubebuilder Documentation](https://book.kubebuilder.io/introduction.html)
115+
You are welcome to use it, learn from it. You are welcome to submit pull requests,
116+
but I do not guarantee any level of activity on this project.
106117

107118
## License
108119

api/.gitkeep

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This directory is required by the Dockerfile.
2+
3+
Let's rather keep this empty dir instead of chaning the Dockerfile.

cmd/main.go

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3535
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3636
"sigs.k8s.io/controller-runtime/pkg/webhook"
37+
38+
"github.com/fabiante/podbouncer/internal/controller"
3739
// +kubebuilder:scaffold:imports
3840
)
3941

@@ -138,6 +140,13 @@ func main() {
138140
os.Exit(1)
139141
}
140142

143+
if err = (&controller.PodReconciler{
144+
Client: mgr.GetClient(),
145+
Scheme: mgr.GetScheme(),
146+
}).SetupWithManager(mgr); err != nil {
147+
setupLog.Error(err, "unable to create controller", "controller", "Pod")
148+
os.Exit(1)
149+
}
141150
// +kubebuilder:scaffold:builder
142151

143152
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {

config/default/kustomization.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Adds namespace to all resources.
2-
namespace: podreaper-system
2+
namespace: podbouncer-system
33

44
# Value of this field is prepended to the
55
# names of all resources, e.g. a deployment named
66
# "wordpress" becomes "alices-wordpress".
77
# Note that it should also match with the prefix (text before '-') of the namespace
88
# field above.
9-
namePrefix: podreaper-
9+
namePrefix: podbouncer-
1010

1111
# Labels to add to all resources and selectors.
1212
#labels:

config/default/metrics_service.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ kind: Service
33
metadata:
44
labels:
55
control-plane: controller-manager
6-
app.kubernetes.io/name: podreaper
6+
app.kubernetes.io/name: podbouncer
77
app.kubernetes.io/managed-by: kustomize
88
name: controller-manager-metrics-service
99
namespace: system

config/manager/kustomization.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
resources:
22
- manager.yaml
3+
apiVersion: kustomize.config.k8s.io/v1beta1
4+
kind: Kustomization
5+
images:
6+
- name: controller
7+
newName: fabiante/podbouncer
8+
newTag: latest

config/manager/manager.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ kind: Namespace
33
metadata:
44
labels:
55
control-plane: controller-manager
6-
app.kubernetes.io/name: podreaper
6+
app.kubernetes.io/name: podbouncer
77
app.kubernetes.io/managed-by: kustomize
88
name: system
99
---
@@ -14,7 +14,7 @@ metadata:
1414
namespace: system
1515
labels:
1616
control-plane: controller-manager
17-
app.kubernetes.io/name: podreaper
17+
app.kubernetes.io/name: podbouncer
1818
app.kubernetes.io/managed-by: kustomize
1919
spec:
2020
selector:

config/network-policy/allow-metrics-traffic.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apiVersion: networking.k8s.io/v1
55
kind: NetworkPolicy
66
metadata:
77
labels:
8-
app.kubernetes.io/name: podreaper
8+
app.kubernetes.io/name: podbouncer
99
app.kubernetes.io/managed-by: kustomize
1010
name: allow-metrics-traffic
1111
namespace: system

config/prometheus/monitor.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ kind: ServiceMonitor
44
metadata:
55
labels:
66
control-plane: controller-manager
7-
app.kubernetes.io/name: podreaper
7+
app.kubernetes.io/name: podbouncer
88
app.kubernetes.io/managed-by: kustomize
99
name: controller-manager-metrics-monitor
1010
namespace: system

config/rbac/leader_election_role.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: rbac.authorization.k8s.io/v1
33
kind: Role
44
metadata:
55
labels:
6-
app.kubernetes.io/name: podreaper
6+
app.kubernetes.io/name: podbouncer
77
app.kubernetes.io/managed-by: kustomize
88
name: leader-election-role
99
rules:

config/rbac/leader_election_role_binding.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: rbac.authorization.k8s.io/v1
22
kind: RoleBinding
33
metadata:
44
labels:
5-
app.kubernetes.io/name: podreaper
5+
app.kubernetes.io/name: podbouncer
66
app.kubernetes.io/managed-by: kustomize
77
name: leader-election-rolebinding
88
roleRef:

config/rbac/role.yaml

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1+
---
12
apiVersion: rbac.authorization.k8s.io/v1
23
kind: ClusterRole
34
metadata:
4-
labels:
5-
app.kubernetes.io/name: podreaper
6-
app.kubernetes.io/managed-by: kustomize
75
name: manager-role
86
rules:
9-
- apiGroups: [""]
10-
resources: ["pods"]
11-
verbs: ["get", "list", "watch"]
7+
- apiGroups:
8+
- ""
9+
resources:
10+
- pods
11+
verbs:
12+
- create
13+
- delete
14+
- get
15+
- list
16+
- patch
17+
- update
18+
- watch
19+
- apiGroups:
20+
- ""
21+
resources:
22+
- pods/finalizers
23+
verbs:
24+
- update
25+
- apiGroups:
26+
- ""
27+
resources:
28+
- pods/status
29+
verbs:
30+
- get
31+
- patch
32+
- update

config/rbac/role_binding.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: rbac.authorization.k8s.io/v1
22
kind: ClusterRoleBinding
33
metadata:
44
labels:
5-
app.kubernetes.io/name: podreaper
5+
app.kubernetes.io/name: podbouncer
66
app.kubernetes.io/managed-by: kustomize
77
name: manager-rolebinding
88
roleRef:

config/rbac/service_account.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v1
22
kind: ServiceAccount
33
metadata:
44
labels:
5-
app.kubernetes.io/name: podreaper
5+
app.kubernetes.io/name: podbouncer
66
app.kubernetes.io/managed-by: kustomize
77
name: controller-manager
88
namespace: system

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ go 1.22.0
55
require (
66
github.com/onsi/ginkgo/v2 v2.19.0
77
github.com/onsi/gomega v1.33.1
8+
github.com/stretchr/testify v1.9.0
9+
k8s.io/api v0.31.0
810
k8s.io/apimachinery v0.31.0
911
k8s.io/client-go v0.31.0
1012
sigs.k8s.io/controller-runtime v0.19.1
@@ -49,6 +51,7 @@ require (
4951
github.com/modern-go/reflect2 v1.0.2 // indirect
5052
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5153
github.com/pkg/errors v0.9.1 // indirect
54+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
5255
github.com/prometheus/client_golang v1.19.1 // indirect
5356
github.com/prometheus/client_model v0.6.1 // indirect
5457
github.com/prometheus/common v0.55.0 // indirect
@@ -84,7 +87,6 @@ require (
8487
gopkg.in/inf.v0 v0.9.1 // indirect
8588
gopkg.in/yaml.v2 v2.4.0 // indirect
8689
gopkg.in/yaml.v3 v3.0.1 // indirect
87-
k8s.io/api v0.31.0 // indirect
8890
k8s.io/apiextensions-apiserver v0.31.0 // indirect
8991
k8s.io/apiserver v0.31.0 // indirect
9092
k8s.io/component-base v0.31.0 // indirect

0 commit comments

Comments
 (0)