-
Notifications
You must be signed in to change notification settings - Fork 160
/
memcached.libsonnet
91 lines (82 loc) · 3.37 KB
/
memcached.libsonnet
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
local k = import 'ksonnet-util/kausal.libsonnet';
k {
util+:: {
// Convert number to k8s "quantity" (ie 1.5Gi -> "1536Mi")
// as per https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go
bytesToK8sQuantity(i)::
local remove_factors_exponent(x, y) =
if x % y > 0
then 0
else remove_factors_exponent(x / y, y) + 1;
local remove_factors_remainder(x, y) =
if x % y > 0
then x
else remove_factors_remainder(x / y, y);
local suffixes = ['', 'Ki', 'Mi', 'Gi'];
local suffix = suffixes[remove_factors_exponent(i, 1024)];
'%d%s' % [remove_factors_remainder(i, 1024), suffix],
},
memcached:: {
name:: error 'must specify name',
max_item_size:: '1m',
memory_limit_mb:: 1024,
overprovision_factor:: 1.2,
cpu_requests:: '500m',
cpu_limits:: '3',
connection_limit:: 1024,
memory_request_overhead_mb:: 100,
memory_request_bytes::
std.ceil((self.memory_limit_mb * self.overprovision_factor) + self.memory_request_overhead_mb) * 1024 * 1024,
memory_limits_bytes::
std.max(self.memory_limit_mb * 1.5 * 1024 * 1024, self.memory_request_bytes),
use_topology_spread:: false,
topology_spread_max_skew:: 1,
extended_options:: [],
local container = $.core.v1.container,
local containerPort = $.core.v1.containerPort,
memcached_container::
container.new('memcached', $._images.memcached) +
container.withPorts([containerPort.new('client', 11211)]) +
container.withArgs(
[
'-m %(memory_limit_mb)s' % self,
'-I %(max_item_size)s' % self,
'-c %(connection_limit)s' % self,
'-v',
] +
if std.length(self.extended_options) != 0 then ['--extended=' + std.join(',', self.extended_options)] else []
) +
$.util.resourcesRequests(self.cpu_requests, $.util.bytesToK8sQuantity(self.memory_request_bytes)) +
$.util.resourcesLimits(self.cpu_limits, $.util.bytesToK8sQuantity(self.memory_limits_bytes)),
memcached_exporter::
container.new('exporter', $._images.memcachedExporter) +
container.withPorts([containerPort.new('http-metrics', 9150)]) +
container.withArgs([
'--memcached.address=localhost:11211',
'--web.listen-address=0.0.0.0:9150',
]),
local statefulSet = $.apps.v1.statefulSet,
local topologySpreadConstraints = k.core.v1.topologySpreadConstraint,
statefulSet:
statefulSet.new(self.name, $._config.memcached_replicas, [
self.memcached_container,
self.memcached_exporter,
], []) +
statefulSet.spec.withServiceName(self.name) +
if self.use_topology_spread then
local pod_name = self.name;
statefulSet.spec.template.spec.withTopologySpreadConstraints(
// Evenly spread pods among available nodes.
topologySpreadConstraints.labelSelector.withMatchLabels({ name: pod_name }) +
topologySpreadConstraints.withTopologyKey('kubernetes.io/hostname') +
topologySpreadConstraints.withWhenUnsatisfiable('ScheduleAnyway') +
topologySpreadConstraints.withMaxSkew(self.topology_spread_max_skew),
)
else
$.util.antiAffinity,
local service = $.core.v1.service,
service:
$.util.serviceFor(self.statefulSet) +
service.spec.withClusterIp('None'),
},
}