-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathk8s.nix
317 lines (303 loc) · 9.81 KB
/
k8s.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# This module creates some `pog` tools that help make you more productive in [Kubernetes](https://kubernetes.io/)!
final: prev:
let
inherit (final) _ pog;
in
rec {
ka = pog {
name = "ka";
version = "0.0.1";
description = "a shorthand to see all pods";
flags = [
_.flags.k8s.namespace
{ name = "allnamespaces"; short = "A"; description = "load from all namespaces"; bool = true; }
{ name = "json"; description = "shorthand for json output"; bool = true; }
];
script = h: with h; ''
ns="--namespace $namespace"
${flag "allnamespaces"} && ns="--all-namespaces"
if ${flag "json"}; then
# shellcheck disable=SC2086
${_.k} get $ns pods --output json
else
# shellcheck disable=SC2086
${_.k} get $ns pods
fi
'';
};
kex = pog {
name = "kex";
version = "0.0.1";
description = "a quick and easy way to exec into a k8s pod!";
flags = [
_.flags.k8s.namespace
{
name = "pod";
description = "the id of the pod to exec into";
prompt = ''
${_.k} --namespace "$namespace" get pods |
${_.fzfq} --header-lines=1 |
${_.k8s.get_id}
'';
promptError = "you must specify a pod id!";
}
];
script = ''
${_.k} --namespace "$namespace" exec -it "$pod" -- sh -c "${_.globals.hacks.bash_or_sh}"
'';
};
krm = pog {
name = "krm";
version = "0.0.1";
description = "a quick and easy way to delete one or more pods on k8s!";
flags = [
_.flags.k8s.namespace
_.flags.common.force
];
script = ''
${_.k} --namespace "$namespace" get pods |
${_.fzfqm} --header-lines=1 |
${_.k8s.get_id} |
${_.xargs} --no-run-if-empty ${_.k} --namespace "$namespace" delete pods ''${force:+--grace-period=0 --force}
'';
};
klist = pog {
name = "klist";
description = "list out all the images in use on this k8s cluster!";
script = ''
${_.k} get pods --all-namespaces -o jsonpath='{..image}' |
${_.tr} -s '[[:space:]]' '\\n' |
${_.sort} |
${_.uniq} -c
'';
};
kshell = pog {
name = "kshell";
version = "0.0.1";
description = "a quick and easy way to pop into an ephemeral shell on k8s!";
flags = [
_.flags.k8s.namespace
_.flags.k8s.serviceaccount
_.flags.docker.image
{
name = "podtimeout";
default = "1m";
description = "The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one pod is running";
}
];
script = ''
debug "''${GREEN}running image '$image' on the '$namespace' namespace!''${RESET}"
pod_name="$(echo "''${USER:-user}-kshell-''$(${final.util-linux}/bin/uuidgen | ${_.head} -c 8)" | tr -cd '[:alnum:]-')"
overrides="$(echo "json.spec.serviceAccount = \"$serviceaccount\";" | gron -u | tr -d '\n')"
${_.k} run \
--stdin \
--tty \
--rm \
--restart Never \
--overrides="$overrides" \
--namespace "$namespace" \
--image-pull-policy=Always \
--image="$image" \
--pod-running-timeout="$podtimeout" \
"$pod_name" \
-- \
sh -c "${_.globals.hacks.bash_or_sh}"
'';
};
kroll = pog {
name = "kroll";
version = "0.0.1";
description = "a quick and easy way to roll a deployment's pods!";
flags = [
_.flags.k8s.namespace
{
name = "deployment";
description = "the deployment to roll. if not passed in, a dialog will pop up to select from";
prompt = ''${_.k} --namespace "$namespace" get deployment -o wide | ${_.fzfq} --header-lines=1 | ${_.k8s.get_id}'';
promptError = "you must specify a deployment to roll!";
completion = ''${_.k} get deployment | ${_.sed} '1d' | ${_.awk} '{print $1}' '';
}
];
script = ''
${_.k} --namespace "$namespace" \
patch deployment "$deployment" \
--patch "''$(${_.refresh_patch})"
${_.k} --namespace "$namespace" rollout status deployment/"$deployment"
'';
};
kdesc = pog {
name = "kdesc";
version = "0.0.1";
description = "a quick and easy way to describe k8s objects!";
flags = [
_.flags.k8s.namespace
{
name = "object";
description = "the object to describe";
prompt = ''${_.k} --namespace "$namespace" get all | ${_.fzfq} | ${_.k8s.get_id}'';
promptError = "you must specify an object to describe!";
}
];
script = ''
debug "''${GREEN}describing object '$object' in the '$namespace' namespace!''${RESET}"
${_.k} --namespace "$namespace" describe "$object"
'';
};
kedit = pog {
name = "kedit";
version = "0.0.1";
description = "a quick and easy way to edit k8s objects!";
flags = [
_.flags.k8s.namespace
{
name = "object";
description = "the object to edit";
prompt = ''${_.k} --namespace "$namespace" get all | ${_.fzfq} | ${_.k8s.get_id}'';
promptError = "you must specify an object to edit!";
}
];
script = ''
debug "''${GREEN}describing object '$object' in the '$namespace' namespace!''${RESET}"
${_.k} --namespace "$namespace" edit "$object"
'';
};
kdrain = pog {
name = "kdrain";
version = "0.0.1";
description = "a quick and easy way to drain one or more nodes on k8s!";
flags = [
_.flags.common.force
_.flags.k8s.nodes
];
script = ''
for node in $nodes; do
green "draining node '$node'"
${_.k} drain ''${force:+--delete-emptydir-data --ignore-daemonsets} "$node"
done
'';
};
klog = pog {
name = "klog";
version = "0.0.1";
description = "a quick and easy way to log one or more pods!";
arguments = [{ name = "FILTER"; }];
flags = [
_.flags.k8s.all_namespaces
_.flags.k8s.namespace
{
name = "containers";
description = "one or more containers to tail";
prompt = ''${_.k} get pods --namespace "$namespace" ''${all_namespaces:+-A} ${_.k8s.fmt.pod} | ${_.fzfqm} --header-lines=1 | ${_.k8s.get_id}'';
promptError = "you must specify one or more pods to get logs from!";
completion = ''${_.k} get pods | ${_.sed} '1d' | ${_.awk} '{print $1}' '';
}
{
name = "since";
description = "Return logs newer than a relative duration like 52, 2m, or 3h";
default = "10m";
}
];
script = helpers: ''
container_regex="($(echo "$containers" | tr '\n' '|' | ${_.sed} 's#.$##'))"
debug "stern ''${all_namespaces:+-A} --namespace $namespace --timestamps $container_regex"
# shellcheck disable=SC2046
${final.stern}/bin/stern ''${all_namespaces:+-A} --namespace "$namespace" --since "$since" --timestamps "$container_regex"
'';
};
kdiff = pog {
name = "kdiff";
version = "0.0.1";
description = "view a pretty diff of the file against the live cluster";
arguments = [{ name = "KUBESPEC"; }];
flags = [
_.flags.k8s.namespace
{
name = "clientside";
description = "run the diff on the clientside instead of serverside";
short = "";
bool = true;
}
];
script = helpers: with helpers; ''
spec="$1"
${file.notExists "spec"} && die "the file to render ('$spec') does not exist!"
side="true"
${flag "clientside"} && side="false"
${_.k} diff --namespace "$namespace" --server-side="$side" -f "$spec" | ${final.delta}/bin/delta
'';
};
ksecedit = pog {
name = "ksecedit";
version = "0.0.1";
description = "edit a k8s secret quickly and easily, inline!";
flags = [
_.flags.k8s.namespace
{
name = "secret";
description = "the secret to edit. if not passed in, a dialog will pop up to select from";
prompt = ''${_.k} --namespace "$namespace" get secret | ${_.fzfq} --header-lines=1 | ${_.k8s.get_id}'';
promptError = "you must specify a secret to edit!";
completion = ''${_.k} get secret | ${_.sed} '1d' | ${_.awk} '{print $1}' '';
}
];
script = helpers: with helpers; ''
${_.k} --namespace "$namespace" get secret "$secret" -o yaml | \
${_.yq} '.dataStrings = (.data | map_values(@base64d)) | del(.data)' | \
${final.moreutils}/bin/vipe | \
${_.yq} '.data = (.dataStrings | map_values(@base64)) | del(.dataStrings)' | \
${_.k} apply -f -
'';
};
refresh_secret = pog {
name = "refresh_secret";
description = "a quick and easy way to refresh external secrets!";
flags = [
_.flags.k8s.namespace
{
name = "secret";
description = "the secret to refresh";
prompt = ''
${_.k} --namespace "$namespace" get externalsecret.external-secrets.io |
${_.fzfqm} --header-lines=1 |
${_.awk} '{ print $1 }'
'';
promptError = "you must specify an external secret to refresh!";
}
];
script = ''
echo "''${secret}" | ${_.xargs} -I {} --no-run-if-empty \
${_.k} annotate --namespace "$namespace" --overwrite externalsecret.external-secrets.io {} refresh="$(${_.date} +%s)"
'';
};
kimg = pog {
name = "kimg";
version = "0.0.1";
description = "a shorthand to see pod's images";
flags = [
_.flags.k8s.namespace
{ name = "allnamespaces"; short = "A"; description = "load from all namespaces"; bool = true; }
];
script = h: with h; ''
ns="--namespace $namespace"
${flag "allnamespaces"} && ns="--all-namespaces"
# shellcheck disable=SC2086
${_.k} get pods $ns -o custom-columns=POD:metadata.name,IMAGE_HASH:status.containerStatuses[*].imageID
'';
};
ktools = k8s_pog_scripts;
k8s_pog_scripts = [
ka
kdesc
kdiff
kdrain
kedit
ksecedit
kex
kimg
klog
krm
kroll
kshell
refresh_secret
];
}