Skip to content

Commit 10de0f4

Browse files
authored
Add library for termination handler (#243)
1 parent 4804992 commit 10de0f4

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Lib for [Kubernetes on GCP Node Termination Event Handler](https://github.com/GoogleCloudPlatform/k8s-node-termination-handler)
2+
3+
This library will provide a DaemonSet and a ClusterRole/ServiceAccount to run it.
4+
5+
## Usage
6+
7+
```
8+
{
9+
local handler = import 'k8s-node-termination-handler/handler.libsonnet',
10+
handler: handler.new({
11+
namespace: 'kube-system',
12+
slack_webhook: 'http://hook.slack.com/AAABBBCCC1112222333/',
13+
}),
14+
}
15+
```
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
new(config):: {
3+
local k = import 'ksonnet-util/kausal.libsonnet',
4+
5+
local _config = {
6+
namespace: 'kube-system',
7+
slack_webhook: '',
8+
} + config,
9+
10+
11+
_images+:: {
12+
node_termination_handler: 'k8s.gcr.io/gke-node-termination-handler@sha256:aca12d17b222dfed755e28a44d92721e477915fb73211d0a0f8925a1fa847cca',
13+
},
14+
15+
16+
local container = k.core.v1.container,
17+
container::
18+
container.new('node-termination-handler', self._images.node_termination_handler) +
19+
container.withCommand(['./node-termination-handler']) +
20+
container.withArgsMixin([
21+
'--logtostderr',
22+
'--exclude-pods=$(POD_NAME):$(POD_NAMESPACE)',
23+
'-v=10',
24+
'--taint=cloud.google.com/impending-node-termination::NoSchedule',
25+
]) +
26+
container.withEnv([
27+
container.envType.fromFieldPath('POD_NAME', 'metadata.name'),
28+
container.envType.fromFieldPath('POD_NAMESPACE', 'metadata.namespace'),
29+
container.envType.new('SLACK_WEBHOOK_URL', _config.slack_webhook),
30+
]) +
31+
container.mixin.securityContext.capabilities.withAdd(['SYS_BOOT']) +
32+
k.util.resourcesLimits('150m', '30Mi'),
33+
34+
local daemonSet = k.apps.v1.daemonSet,
35+
local tolerations = daemonSet.mixin.spec.template.spec.tolerationsType,
36+
local nodeAffinity = daemonSet.mixin.spec.template.spec.affinity.nodeAffinity,
37+
local nodeSelector = nodeAffinity.requiredDuringSchedulingIgnoredDuringExecutionType,
38+
daemonset:
39+
daemonSet.new('node-termination-handler', [self.container]) +
40+
daemonSet.mixin.spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.mixinInstance(
41+
nodeSelector.withNodeSelectorTerms([
42+
nodeSelector.nodeSelectorTermsType.new() +
43+
nodeSelector.nodeSelectorTermsType.withMatchExpressions([
44+
nodeSelector.nodeSelectorTermsType.matchFieldsType
45+
.withKey('cloud.google.com/gke-accelerator')
46+
.withOperator('Exists'),
47+
nodeSelector.nodeSelectorTermsType.matchFieldsType
48+
.withKey('cloud.google.com/gke-preemptible')
49+
.withOperator('Exists'),
50+
]),
51+
])
52+
) +
53+
daemonSet.mixin.metadata.withNamespace(_config.namespace) +
54+
daemonSet.mixin.spec.template.spec.withHostPid(true) +
55+
daemonSet.mixin.spec.template.spec.withTolerations([
56+
tolerations.new() +
57+
tolerations
58+
.withOperator('Exists')
59+
.withEffect('NoSchedule'),
60+
tolerations.new() +
61+
tolerations
62+
.withOperator('Exists')
63+
.withEffect('NoExecute'),
64+
]),
65+
66+
local serviceAccount = k.core.v1.serviceAccount,
67+
service_account:
68+
serviceAccount.new('node-termination-handler') +
69+
serviceAccount.mixin.metadata.withNamespace(_config.namespace),
70+
71+
local clusterRole = k.rbac.v1.clusterRole,
72+
local clusterRoleRule = k.rbac.v1.clusterRole.rulesType,
73+
cluster_role:
74+
clusterRole.new() +
75+
clusterRole.mixin.metadata.withName('node-termination-handler-role') +
76+
clusterRole.withRulesMixin([
77+
clusterRoleRule.new() +
78+
clusterRoleRule.withApiGroups('') +
79+
clusterRoleRule.withResources(['nodes']) +
80+
clusterRoleRule.withVerbs(['get', 'update']),
81+
clusterRoleRule.new() +
82+
clusterRoleRule.withApiGroups('') +
83+
clusterRoleRule.withResources(['events']) +
84+
clusterRoleRule.withVerbs(['create']),
85+
clusterRoleRule.new() +
86+
clusterRoleRule.withApiGroups('') +
87+
clusterRoleRule.withResources(['pods']) +
88+
clusterRoleRule.withVerbs(['get', 'list', 'delete']),
89+
]),
90+
91+
local clusterRoleBinding = k.rbac.v1.clusterRoleBinding,
92+
cluster_role_binding:
93+
clusterRoleBinding.new() +
94+
clusterRoleBinding.mixin.metadata.withName('node-termination-handler-role-binding') +
95+
clusterRoleBinding.mixin.roleRef.withApiGroup('rbac.authorization.k8s.io') +
96+
clusterRoleBinding.mixin.roleRef.withKind('ClusterRole') +
97+
clusterRoleBinding.mixin.roleRef.withName('node-termination-handler-role') +
98+
clusterRoleBinding.withSubjectsMixin({
99+
kind: 'ServiceAccount',
100+
name: 'node-termination-handler',
101+
namespace: _config.namespace,
102+
}),
103+
},
104+
}

0 commit comments

Comments
 (0)