-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Boost pods before they are submitted by using a mutating webhook (#21)
- Loading branch information
Showing
25 changed files
with
514 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
|
||
"github.com/norbjd/k8s-pod-cpu-booster/pkg/webhook" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
func main() { | ||
klog.InitFlags(nil) | ||
|
||
var port uint | ||
var pathToCertFile string | ||
var pathToKeyFile string | ||
|
||
flag.UintVar(&port, "port", 8443, "listening port") | ||
flag.StringVar(&pathToCertFile, "cert", "", "path to cert file") | ||
flag.StringVar(&pathToKeyFile, "key", "", "path to key file") | ||
|
||
flag.Parse() | ||
|
||
err := webhook.Run(port, pathToCertFile, pathToKeyFile) | ||
if err != nil { | ||
klog.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.crt | ||
*.key |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# namespace is used in the certificate's CN and SAN | ||
NAMESPACE := $(shell grep 'namespace: ' kustomization.yaml | cut -d ' ' -f 2) | ||
|
||
mutating-webhook-certs: | ||
openssl genrsa -out ca.key 4096 | ||
openssl req -new -x509 -key ca.key -out ca.crt -days 3650 -nodes -subj "/CN=my-self-signed-ca" | ||
openssl req -x509 -CA ca.crt -CAkey ca.key -keyout tls.key -out tls.crt -sha256 -days 3650 -nodes -subj "/CN=mutating-webhook.${NAMESPACE}.svc" -addext "subjectAltName = DNS:mutating-webhook.${NAMESPACE}.svc" | ||
|
||
remove-certs: | ||
rm -f *.key *.crt | ||
|
||
build: | ||
kustomize build . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
namespace: pod-cpu-booster | ||
resources: | ||
- mutating-webhook.yaml | ||
- namespace.yaml | ||
- pod-cpu-boost-reset.yaml | ||
secretGenerator: | ||
- name: mutating-webhook-certs | ||
options: | ||
disableNameSuffixHash: true | ||
type: kubernetes.io/tls | ||
files: | ||
- ca.crt | ||
- tls.crt | ||
- tls.key | ||
replacements: | ||
- source: | ||
kind: Secret | ||
name: mutating-webhook-certs | ||
fieldPath: "data.[ca.crt]" | ||
targets: | ||
- select: | ||
kind: MutatingWebhookConfiguration | ||
name: k8s-pod-cpu-booster | ||
fieldPaths: | ||
- webhooks.[name=k8s-pod-cpu-booster.norbjd.github.io].clientConfig.caBundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
apiVersion: admissionregistration.k8s.io/v1 | ||
kind: MutatingWebhookConfiguration | ||
metadata: | ||
name: k8s-pod-cpu-booster | ||
webhooks: | ||
- name: k8s-pod-cpu-booster.norbjd.github.io | ||
clientConfig: | ||
caBundle: "" # will be overridden by kustomization replacement | ||
service: # namespace field is overridden by the namespace defined in kustomization.yaml | ||
name: mutating-webhook | ||
path: /mutate | ||
objectSelector: | ||
matchExpressions: | ||
# we don't want that creation of mutating-webhook pods triggers the webhook (otherwise pods won't start) | ||
- key: app | ||
operator: NotIn | ||
values: | ||
- mutating-webhook | ||
- key: norbjd.github.io/k8s-pod-cpu-booster-enabled | ||
operator: In | ||
values: | ||
- "true" | ||
rules: | ||
- apiGroups: [""] | ||
apiVersions: ["v1"] | ||
resources: ["pods"] | ||
operations: ["CREATE"] | ||
scope: Namespaced | ||
sideEffects: None | ||
admissionReviewVersions: ["v1"] | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: mutating-webhook | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: mutating-webhook | ||
template: | ||
metadata: | ||
labels: | ||
app: mutating-webhook | ||
spec: | ||
containers: | ||
- name: mutating-webhook | ||
image: ko://github.com/norbjd/k8s-pod-cpu-booster/cmd/webhook | ||
args: | ||
- -v=9 | ||
- -port=8443 | ||
- -cert=/etc/certs/tls.crt | ||
- -key=/etc/certs/tls.key | ||
ports: | ||
- containerPort: 8443 | ||
volumeMounts: | ||
- name: certs | ||
mountPath: /etc/certs | ||
readOnly: true | ||
volumes: | ||
- name: certs | ||
secret: | ||
secretName: mutating-webhook-certs | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: mutating-webhook | ||
spec: | ||
selector: | ||
app: mutating-webhook | ||
ports: | ||
- port: 443 | ||
targetPort: 8443 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: doesnt-matter # will be overridden by kustomize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: pod-cpu-boost-reset | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: pod-cpu-boost-reset | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- pods | ||
verbs: | ||
- list | ||
- watch | ||
- get | ||
- update | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: pod-cpu-boost-reset | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: pod-cpu-boost-reset | ||
subjects: | ||
- kind: ServiceAccount | ||
name: pod-cpu-boost-reset | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: pod-cpu-boost-reset | ||
spec: | ||
replicas: 1 # for now, we don't support multiple replicas | ||
selector: | ||
matchLabels: | ||
app: pod-cpu-boost-reset | ||
template: | ||
metadata: | ||
labels: | ||
app: pod-cpu-boost-reset | ||
spec: | ||
containers: | ||
- name: pod-cpu-boost-reset | ||
image: ko://github.com/norbjd/k8s-pod-cpu-booster/cmd/informer | ||
resources: | ||
limits: | ||
cpu: 100m | ||
memory: 100Mi | ||
requests: | ||
cpu: 100m | ||
memory: 100Mi | ||
serviceAccountName: pod-cpu-boost-reset | ||
terminationGracePeriodSeconds: 0 # TODO: change for production environments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.