Skip to content
This repository was archived by the owner on Oct 3, 2020. It is now read-only.

Commit 87a2a53

Browse files
committed
Merge branch 'AOEpeople-master'
2 parents 505df72 + 48f3487 commit 87a2a53

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

README.rst

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ Deployments are interchangeable by statefulset for this whole guide.
1616

1717
It will scale the deployment's replicas to zero if all of the following conditions are met:
1818

19-
* current time is not part of the "uptime" schedule (annotation ``downscaler/uptime``) or current time is part of the "downtime" schedule (``downscaler/downtime``)
19+
* current time is not part of the "uptime" schedule or current time is part of the "downtime" schedule. The schedules are being evaluated in following order:
20+
* ``downscaler/downtime`` annotation on the deployment/stateful set
21+
* ``downscaler/uptime`` annotation on the deployment/stateful set
22+
* ``downscaler/downtime`` annotation on the deployment/stateful set's namespace
23+
* ``downscaler/uptime`` annotation on th deployment/stateful set's namespace
24+
* ``--default-uptime`` cli argument
25+
* ``--default-downtime`` cli argument
26+
* ``DEFAULT_UPTIME`` environment variable
27+
* ``DEFAULT_DOWNTIME`` environment variable
2028
* the deployment's namespace is not part of the exclusion list (``kube-system`` is excluded by default)
2129
* the deployment's name is not part of the exclusion list
2230
* the deployment is not marked for exclusion (annotation ``downscaler/exclude: "true"``)
@@ -93,6 +101,27 @@ Available command line options:
93101
``--exclude-statefulsets``
94102
Exclude specific statefulsets from statefulsets, can also be configured via environment variable ``EXCLUDE_STATEFULSETS``
95103
104+
Namespace Defaults
105+
==================
106+
107+
``DEFAULT_UPTIME``, ``DEFAULT_DOWNTIME``, ``FORCE_UPTIME`` and exclusion can also be configured using Namespace annotations. Where configured these values supersede the other global default values.
108+
109+
.. code-block:: yaml
110+
111+
apiVersion: v1
112+
kind: Namespace
113+
metadata:
114+
name: foo
115+
labels:
116+
name: foo
117+
annotations:
118+
downscaler/uptime: Mon-Sun 06:00-21:00 Europe/Berlin
119+
120+
Following annotations are supported on the Namespace level:
121+
* ``downscaler/uptime``
122+
* ``downscaler/downtime``
123+
* ``downscaler/force-uptime``
124+
* ``downscaler/exclude``
96125
97126
Contributing
98127
============

helm-chart/templates/clusterrole.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ rules:
2929
- list
3030
- update
3131
- patch
32+
- apiGroups:
33+
- ""
34+
resources:
35+
- namespaces
36+
verbs:
37+
- get
38+
- list
3239
{{- end -}}

helm-chart/templates/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ spec:
2929
- --interval={{ .Values.interval }}
3030
{{- if .Values.namespace}}
3131
{{- if .Values.namespace.active_in}}
32-
"- --namespace="{{ .Values.namespace.active_in }}
32+
- --namespace={{ .Values.namespace.active_in }}
3333
{{- end }}
3434
{{- end }}
3535
{{ if .Values.debug.enable }}- --debug{{end }}

kube_downscaler/helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ def get_kube_api():
4040
config = pykube.KubeConfig.from_service_account()
4141
except FileNotFoundError:
4242
# local testing
43-
config = pykube.KubeConfig.from_file(os.path.expanduser('~/.kube/config'))
43+
config = pykube.KubeConfig.from_file(os.getenv('KUBECONFIG', os.path.expanduser('~/.kube/config')))
4444
api = pykube.HTTPClient(config)
4545
return api

kube_downscaler/scaler.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
ORIGINAL_REPLICAS_ANNOTATION = 'downscaler/original-replicas'
1313
FORCE_UPTIME_ANNOTATION = 'downscaler/force-uptime'
1414
EXCLUDE_ANNOTATION = 'downscaler/exclude'
15+
UPTIME_ANNOTATION = 'downscaler/uptime'
16+
DOWNTIME_ANNOTATION = 'downscaler/downtime'
1517

1618

1719
def within_grace_period(deploy, grace_period: int):
@@ -48,8 +50,8 @@ def autoscale_resource(resource: pykube.objects.NamespacedAPIObject,
4850
downtime = "ignored"
4951
is_uptime = True
5052
else:
51-
uptime = resource.annotations.get('downscaler/uptime', default_uptime)
52-
downtime = resource.annotations.get('downscaler/downtime', default_downtime)
53+
uptime = resource.annotations.get(UPTIME_ANNOTATION, default_uptime)
54+
downtime = resource.annotations.get(DOWNTIME_ANNOTATION, default_downtime)
5355
is_uptime = helper.matches_time_spec(now, uptime) and not helper.matches_time_spec(now, downtime)
5456

5557
original_replicas = resource.annotations.get(ORIGINAL_REPLICAS_ANNOTATION)
@@ -91,7 +93,19 @@ def autoscale_resources(api, kind, namespace: str,
9193
for resource in kind.objects(api, namespace=(namespace or pykube.all)):
9294
if resource.namespace in exclude_namespaces or resource.name in exclude_names:
9395
continue
94-
autoscale_resource(resource, default_uptime, default_downtime, forced_uptime, dry_run, now, grace_period)
96+
97+
# Override defaults with (optional) annotations from Namespace
98+
namespace_obj = pykube.Namespace.objects(api).get_by_name(resource.namespace)
99+
100+
if namespace_obj.annotations.get(EXCLUDE_ANNOTATION, 'false').lower() != 'false':
101+
logger.debug('Namespace %s was excluded (because of namespace annotation)', namespace)
102+
continue
103+
104+
default_uptime_for_namespace = namespace_obj.annotations.get(UPTIME_ANNOTATION, default_uptime)
105+
default_downtime_for_namespace = namespace_obj.annotations.get(DOWNTIME_ANNOTATION, default_downtime)
106+
forced_uptime_for_namespace = namespace_obj.annotations.get(FORCE_UPTIME_ANNOTATION, forced_uptime)
107+
108+
autoscale_resource(resource, default_uptime_for_namespace, default_downtime_for_namespace, forced_uptime_for_namespace, dry_run, now, grace_period)
95109

96110

97111
def scale(namespace: str, default_uptime: str, default_downtime: str, kinds: FrozenSet[str],

0 commit comments

Comments
 (0)