You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
New Feature - Downscaling Jobs Using Admission Controller (Kyverno And Gatekeeper) (#42)
* added downscaling jobs feature using admission controller (kyverno and gatekeeper). Refactored docs in order to explain how to use the new feature and how to use matching_labels args
* added downscaling jobs feature using admission controller (kyverno and gatekeeper). Refactored docs in order to explain how to use the new feature and how to use matching_labels. Rebased args
* small syntax fix after rebase
* added doc for downscaling daemonset feature
@@ -340,6 +340,134 @@ Available command line options:
340
340
For backwards compatibility, if this argument is not specified,
341
341
py-kube-downscaler will apply to all resources.
342
342
343
+
`--admission-controller`
344
+
345
+
: Optional: admission controller used by the kube-downscaler to downscale and upscale
346
+
jobs. Required only if "jobs" are specified inside "--include-resources" arg.
347
+
Supported Admission Controllers are
348
+
\[gatekeeper, kyverno*\]
349
+
350
+
*Make sure to read the dedicated section below to understand how to use the
351
+
--admission-controller feature correctly
352
+
353
+
### Scaling Jobs
354
+
355
+
Before scaling jobs make sure the Admission Controller of your choice is correctly installed inside the cluster.
356
+
Kube-Downscaler performs some health checks that are displayed inside logs when the `--debug` arg is present.
357
+
If you are using Gatekeeper, Kube-Downscaler will install a new Custom Resource Definition
358
+
called `kubedownscalerjobsconstraint`
359
+
360
+
**When using this feature you need to exclude Kyverno or Gatekeeper resources from downscaling otherwise
361
+
the admission controller pods won't be able to donwscale jobs.**
362
+
You can use `EXCLUDE_NAMESPACES` environment variable or `--exclude-namespaces` arg to exclude `"kyverno"` or `"gatekeeper-system"` namespaces.
363
+
To have a more fine-grained control you can use `EXCLUDE_DEPLOYMENTS` environment variable
364
+
or `--exclude-deployments` arg to exclude only certain resources inside `"kyverno"` or `"gatekeeper-system"` namespaces
365
+
366
+
**<u>Important</u>:** Jobs started from CronJobs are excluded by default unless you have included `cronjobs` inside `--include-resources` argument
367
+
368
+
**Annotations:** both the `downscaler/exclude` and `downscaler/exclude-until` annotations are fully supported
369
+
inside jobs to exclude them from downscaling. However, when using `downscaler/exclude-until`, the time<u>**must**</u> be specified in the RFC format `YYYY-MM-DDT00:00:00Z`
370
+
otherwise the exclusion won't work.
371
+
Please check the example below
372
+
373
+
```yaml {.sourceCode .yaml}
374
+
apiVersion: batch/v1
375
+
kind: Job
376
+
metadata:
377
+
namespace: default
378
+
name: testjob
379
+
annotations:
380
+
downscaler/exclude-until: "2024-01-31T00:00:00Z"
381
+
spec:
382
+
template:
383
+
spec:
384
+
containers:
385
+
- image: nginx
386
+
name: testjob
387
+
restartPolicy: Never
388
+
```
389
+
390
+
**Arguments and Env:** you can also use `EXCLUDE_DEPLOYMENTS` environment variable or the argument `--exclude-deployments`
391
+
to exclude jobs. As described above, despite their names, these variables work for any type of workload
annotations are not supported if specified directly inside the Job definition due to limitations
396
+
on computing days of the week inside the policies. However you can still use
397
+
these annotations at Namespace level to downscale/upscale Jobs
398
+
399
+
400
+
**Deleting Policies:** if for some reason you want to delete all resources blocking jobs, you can use these commands:
401
+
402
+
Gatekeeper
403
+
404
+
``` {.sourceCode .sh}
405
+
kubectl delete constraints -A -l origin=kube-downscaler
406
+
```
407
+
408
+
Kyverno
409
+
410
+
``` {.sourceCode .sh}
411
+
kubectl delete policies -A -l origin=kube-downscaler
412
+
```
413
+
414
+
### Scaling DaemonSet
415
+
416
+
The feature to scale DaemonSets can be very useful for reducing the base occupancy of a node. If enabled, the DaemonSets downscaling algorithm works like this:
417
+
418
+
1) Downtime Hours: Kube Downscaler will add to each targeted DaemonSet a Node Selector that cannot be satisfied `kube-downscaler-non-existent=true`
419
+
2) Uptime Hours: Kube Downscaler will remove the `kube-downscaler-non-existent=true` Node Selector from each targeted DaemonSet
420
+
421
+
### Matching Labels Argument
422
+
423
+
Labels, in Kubernetes, are key-value pairs that can be used to identify and group resources.
424
+
425
+
You can use the `--matching-labels` argument to include only certain resources in the namespaces
426
+
that are targeted by the Kube Downscaler. inside this argument you can specify:
427
+
- labels written in this format [key=value]
428
+
- regular expressions that target this format [key=value].
429
+
430
+
Each entry must be separated by a comma (`,`). If multiple entries are specified, the Kube Downscaler evaluates them as an OR condition
431
+
432
+
To make it more clear, given the following resource
433
+
434
+
```yaml {.sourceCode .yaml}
435
+
kind: Deployment
436
+
metadata:
437
+
labels:
438
+
app: nginx
439
+
type: example
440
+
name: nginx
441
+
spec:
442
+
replicas: 1
443
+
selector:
444
+
matchLabels:
445
+
app: nginx
446
+
template:
447
+
metadata:
448
+
labels:
449
+
app: nginx
450
+
spec:
451
+
containers:
452
+
- image: nginx
453
+
name: nginx
454
+
```
455
+
456
+
Kube-Downscaler will evaluate the input of the `--matching-labels` argument against _app=nginx_
457
+
and _type=example_. If at least one of the two key-value pairs matches the resource will be downscaled
458
+
459
+
Example of valid inputs are:
460
+
461
+
`--matching-labels=hello=world`: if the resource has a label "hello" equals to "world" it will be downscaled
462
+
463
+
`--matching-labels=hello=world,version=2.0`: if the resource has a label "hello" equals to "world"
464
+
or a label "version" equal to "2.0" it will be downscaled
465
+
466
+
`--matching-labels=^it-plt.*`: if the resource has a label that starts with "it-plt" it will be downscaled
467
+
468
+
`--matching-labels=^it-plt.*,not-critical=true`: if the resource has a label that starts with "it-plt" or a label
469
+
"not-critical" equals to "true" it will be downscaled
470
+
343
471
### Namespace Defaults
344
472
345
473
`DEFAULT_UPTIME`, `DEFAULT_DOWNTIME`, `FORCE_UPTIME` and exclusion can
Copy file name to clipboardExpand all lines: kube_downscaler/cmd.py
+6Lines changed: 6 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@
10
10
"horizontalpodautoscalers",
11
11
"rollouts",
12
12
"scaledobjects",
13
+
"jobs",
13
14
"daemonsets",
14
15
"poddisruptionbudgets",
15
16
]
@@ -106,4 +107,9 @@ def get_parser():
106
107
default=os.getenv("MATCHING_LABELS", ""),
107
108
help="Apply downscaling to resources with the supplied labels. This is a comma-separated list of regex patterns. This is optional, downscaling will be applied to all resources by default.",
108
109
)
110
+
parser.add_argument(
111
+
"--admission-controller",
112
+
default=os.getenv("ADMISSION_CONTROLLER", ""),
113
+
help="Apply downscaling to jobs using the supplied admission controller. Jobs should be included inside --include-resources if you want to use this parameter. kyverno and gatekeeper are supported.",
0 commit comments