Skip to content

Commit 58afd90

Browse files
committed
Add taint manager
The taint manager is a go binary to add/remove taint for a node.
1 parent 9407293 commit 58afd90

File tree

13 files changed

+680
-0
lines changed

13 files changed

+680
-0
lines changed

chartpress.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ charts:
5151
# singleuser-sample, a primitive user container to start with.
5252
singleuser-sample:
5353
valuesPath: singleuser.image
54+
55+
# taint-manager, an initContainer to add/remove taint for a node
56+
taint-manager:
57+
valuesPath: prePuller.taintmanager.image

images/taint-manager/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
taintmanager

images/taint-manager/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# syntax=docker/dockerfile:1
2+
3+
## Build
4+
FROM golang:1.18-bullseye AS build
5+
6+
WORKDIR /app
7+
8+
COPY go.mod ./
9+
COPY go.sum ./
10+
RUN go mod download
11+
12+
COPY *.go ./
13+
14+
RUN go build -o /taintmanager
15+
16+
## Deploy
17+
FROM gcr.io/distroless/base-debian11
18+
19+
WORKDIR /
20+
21+
COPY --from=build /taintmanager /taintmanager
22+
23+
USER nonroot:nonroot
24+
25+
CMD ["/taintmanager"]

images/taint-manager/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
In Cluster Taint Manager
2+
==========================
3+
4+
To add or remove taint of a node from a in-cluster pod.
5+
6+
Compile
7+
---------
8+
9+
```
10+
GOOS=linux GOARCH=amd64 go build -o taintmanager taintmanager.go
11+
```
12+
13+
Development and Debug
14+
-----------------------
15+
16+
The dev/debug environment is setup by `tilt`. To start, run `tilt up`.
17+
18+
19+
Test
20+
-------
21+
22+
The `test` directory contains YAML files for deploy a pod with required permissions to run taintmanager.
23+
Please change `namespace` field in `clusterrolebinding.yaml` before deploying to a cluster.
24+
25+
After deploying yaml files, run `kubectl cp` to copy compiled binary to the target pod and run the binary inside pod.

images/taint-manager/Tiltfile

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Welcome to Tilt!
2+
# To get you started as quickly as possible, we have created a
3+
# starter Tiltfile for you.
4+
#
5+
# Uncomment, modify, and delete any commands as needed for your
6+
# project's configuration.
7+
8+
cluster_name = 'k8scluster'
9+
project_name = 'taintmanager'
10+
11+
# Allow the cluster to avoid problems while having kubectl configured to talk to a remote cluster.
12+
allow_k8s_contexts(cluster_name)
13+
14+
# Use custom Dockerfile for Tilt builds, which only takes locally built binary for live reloading.
15+
dockerfile = '''
16+
FROM golang:1.19-alpine
17+
RUN go install github.com/go-delve/delve/cmd/dlv@latest
18+
COPY %s /usr/local/bin/%s
19+
''' % (project_name, project_name)# Build Docker image
20+
21+
# Tilt will automatically associate image builds with the resource(s)
22+
# that reference them (e.g. via Kubernetes or Docker Compose YAML).
23+
#
24+
# More info: https://docs.tilt.dev/api.html#api.docker_build
25+
#
26+
# docker_build('registry.example.com/my-image',
27+
# context='.',
28+
# # (Optional) Use a custom Dockerfile path
29+
# dockerfile='./deploy/app.dockerfile',
30+
# # (Optional) Filter the paths used in the build
31+
# only=['./app'],
32+
# # (Recommended) Updating a running container in-place
33+
# # https://docs.tilt.dev/live_update_reference.html
34+
# live_update=[
35+
# # Sync files from host to container
36+
# sync('./app', '/src/'),
37+
# # Execute commands inside the container when certain
38+
# # paths change
39+
# run('/src/codegen.sh', trigger=['./app/api'])
40+
# ]
41+
# )
42+
43+
44+
# Run local commands
45+
# Local commands can be helpful for one-time tasks like installing
46+
# project prerequisites. They can also manage long-lived processes
47+
# for non-containerized services or dependencies.
48+
#
49+
# More info: https://docs.tilt.dev/local_resource.html
50+
#
51+
# local_resource('install-helm',
52+
# cmd='which helm > /dev/null || brew install helm',
53+
# # `cmd_bat`, when present, is used instead of `cmd` on Windows.
54+
# cmd_bat=[
55+
# 'powershell.exe',
56+
# '-Noninteractive',
57+
# '-Command',
58+
# '& {if (!(Get-Command helm -ErrorAction SilentlyContinue)) {scoop install helm}}'
59+
# ]
60+
# )
61+
62+
# Building binary locally.
63+
local_resource('%s-binary' % project_name,
64+
'GOOS=linux GOARCH=amd64 go build -gcflags "all=-N -l" -o taintmanager taintmanager.go',
65+
deps=[
66+
'./taintmanager.go',
67+
],
68+
)
69+
70+
# Extensions are open-source, pre-packaged functions that extend Tilt
71+
#
72+
# More info: https://github.com/tilt-dev/tilt-extensions
73+
#
74+
load('ext://git_resource', 'git_checkout')
75+
76+
# Load the restart_process extension with the docker_build_with_restart func for live reloading.
77+
load('ext://restart_process', 'docker_build_with_restart')
78+
79+
# Wrap a docker_build to restart the given entrypoint after a Live Update.
80+
docker_build(
81+
'lthub/' + project_name,
82+
'.',
83+
dockerfile_contents=dockerfile,
84+
entrypoint='/go/bin/dlv --listen=0.0.0.0:50100 --api-version=2 --headless=true --only-same-user=false --accept-multiclient --check-go-version=false exec -- /usr/local/bin/taintmanager -remove jupyterhub:NoSchedule',
85+
live_update=[
86+
# Copy the binary so it gets restarted.
87+
sync(project_name, '/usr/local/bin/%s' % project_name),
88+
],
89+
)
90+
91+
# Apply Kubernetes manifests
92+
# Tilt will build & push any necessary images, re-deploying your
93+
# resources as they change.
94+
#
95+
# More info: https://docs.tilt.dev/api.html#api.k8s_yaml
96+
#
97+
# k8s_yaml(['k8s/deployment.yaml', 'k8s/service.yaml'])
98+
# Create the deployment from YAML file path.
99+
k8s_yaml('deployment.yaml')
100+
101+
# Configure the resource.
102+
k8s_resource(
103+
project_name,
104+
port_forwards = ["50100:50100"] # Set up the K8s port-forward to be able to connect to it locally.
105+
)

images/taint-manager/deployment.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: taintmanager
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: taintmanager
10+
template:
11+
metadata:
12+
labels:
13+
app: taintmanager
14+
spec:
15+
containers:
16+
- image: lthub/taintmanager
17+
imagePullPolicy: IfNotPresent
18+
name: taintmanager
19+
env:
20+
- name: MY_POD_NAME
21+
valueFrom:
22+
fieldRef:
23+
fieldPath: metadata.name
24+
- name: MY_NODE_NAME
25+
valueFrom:
26+
fieldRef:
27+
fieldPath: spec.nodeName
28+
ports:
29+
- containerPort: 8080
30+
restartPolicy: Always

images/taint-manager/go.mod

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module taintmanager
2+
3+
go 1.18
4+
5+
require (
6+
k8s.io/api v0.25.4
7+
k8s.io/apimachinery v0.25.4
8+
k8s.io/client-go v0.25.4
9+
)
10+
11+
require (
12+
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
14+
github.com/go-logr/logr v1.2.3 // indirect
15+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
16+
github.com/go-openapi/jsonreference v0.20.0 // indirect
17+
github.com/go-openapi/swag v0.22.3 // indirect
18+
github.com/gogo/protobuf v1.3.2 // indirect
19+
github.com/golang/protobuf v1.5.2 // indirect
20+
github.com/google/gnostic v0.6.9 // indirect
21+
github.com/google/go-cmp v0.5.9 // indirect
22+
github.com/google/gofuzz v1.2.0 // indirect
23+
github.com/josharian/intern v1.0.0 // indirect
24+
github.com/json-iterator/go v1.1.12 // indirect
25+
github.com/mailru/easyjson v0.7.7 // indirect
26+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
27+
github.com/modern-go/reflect2 v1.0.2 // indirect
28+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
29+
golang.org/x/net v0.2.0 // indirect
30+
golang.org/x/oauth2 v0.2.0 // indirect
31+
golang.org/x/sys v0.2.0 // indirect
32+
golang.org/x/term v0.2.0 // indirect
33+
golang.org/x/text v0.4.0 // indirect
34+
golang.org/x/time v0.2.0 // indirect
35+
google.golang.org/appengine v1.6.7 // indirect
36+
google.golang.org/protobuf v1.28.1 // indirect
37+
gopkg.in/inf.v0 v0.9.1 // indirect
38+
gopkg.in/yaml.v2 v2.4.0 // indirect
39+
gopkg.in/yaml.v3 v3.0.1 // indirect
40+
k8s.io/klog/v2 v2.80.1 // indirect
41+
k8s.io/kube-openapi v0.0.0-20221123214604-86e75ddd809a // indirect
42+
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
43+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
44+
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
45+
sigs.k8s.io/yaml v1.3.0 // indirect
46+
)

0 commit comments

Comments
 (0)