-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathK8sHorizontalPodAutoscaler.py
118 lines (85 loc) · 4 KB
/
K8sHorizontalPodAutoscaler.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.md', which is part of this source code package.
#
import json
from kubernetes_py.K8sObject import K8sObject
from kubernetes_py.models.v1.HorizontalPodAutoscaler import HorizontalPodAutoscaler
from kubernetes_py.models.v1beta1.SubresourceReference import SubresourceReference
from kubernetes_py.utils import convert
class K8sHorizontalPodAutoscaler(K8sObject):
VALID_HPA_TARGET_KINDS = ["ReplicationController", "Deployment"]
def __init__(self, config=None, name=None):
super(K8sHorizontalPodAutoscaler, self).__init__(config=config, obj_type="HorizontalPodAutoscaler", name=name)
# ------------------------------------------------------------------------------------- override
def create(self):
super(K8sHorizontalPodAutoscaler, self).create()
self.get()
return self
def update(self):
super(K8sHorizontalPodAutoscaler, self).update()
self.get()
return self
def list(self, pattern=None, labels=None):
ls = super(K8sHorizontalPodAutoscaler, self).list(labels=labels)
hpas = list(map(lambda x: HorizontalPodAutoscaler(x), ls))
if pattern is not None:
hpas = list(filter(lambda x: pattern in x.name, hpas))
k8s = []
for x in hpas:
z = K8sHorizontalPodAutoscaler(config=self.config, name=x.name).from_model(m=x)
k8s.append(z)
return k8s
# ------------------------------------------------------------------------------------- get
def get(self):
self.model = HorizontalPodAutoscaler(self.get_model())
return self
# ------------------------------------------------------------------------------------- cpu_percent
@property
def cpu_percent(self):
return self.model.spec.cpu_utilization
@cpu_percent.setter
def cpu_percent(self, pct=None):
self.model.spec.cpu_utilization = pct
# ------------------------------------------------------------------------------------- min replicas
@property
def min_replicas(self):
return self.model.spec.min_replicas
@min_replicas.setter
def min_replicas(self, min=None):
self.model.spec.min_replicas = min
# ------------------------------------------------------------------------------------- max replicas
@property
def max_replicas(self):
return self.model.spec.max_replicas
@max_replicas.setter
def max_replicas(self, max=None):
self.model.spec.max_replicas = max
# ------------------------------------------------------------------------------------- scaleRef
@property
def scale_ref(self):
return self.model.spec.scale_target_ref
@scale_ref.setter
def scale_ref(self, ref=None):
if not isinstance(ref, tuple):
raise SyntaxError("K8sHorizontalPodAutoscaler: scale_ref must be a tuple of the form (kind, name).")
kind, name = ref
if kind not in self.VALID_HPA_TARGET_KINDS:
raise SyntaxError("K8sHorizontalPodAutoscaler: scale_ref.kind: [ {} ] is invalid.".format(kind))
ref = {"apiVersion": "v1" if kind == "ReplicationController" else "extensions/v1beta1", "kind": kind, "name": name}
sub = SubresourceReference(ref)
self.model.spec.scale_target_ref = sub
# ------------------------------------------------------------------------------------- from_json
@staticmethod
def from_json(j=None, cfg=None):
try:
d = convert(json.loads(j))
model = HorizontalPodAutoscaler(model=d)
k8s = K8sHorizontalPodAutoscaler(config=cfg, name="yo").from_model(m=model)
return k8s
except TypeError as err:
raise SyntaxError("K8sHorizontalPodAutoscaler: json: [ {} ] is invalid: [ {} ]".format(j, err))
except ValueError as err:
raise SyntaxError("K8sHorizontalPodAutoscaler: json: [ {} ] is invalid: [ {} ]".format(j, err))