1local k = import 'ksonnet-util/kausal.libsonnet';
2
3{
4  local container = k.core.v1.container,
5  local pvc = k.core.v1.persistentVolumeClaim,
6  local statefulSet = k.apps.v1.statefulSet,
7  local deployment = k.apps.v1.deployment,
8  local volumeMount = k.core.v1.volumeMount,
9
10  ruler_args:: $._config.commonArgs {
11    target: 'ruler',
12  } + if $._config.using_boltdb_shipper then {
13    // Use PVC for caching
14    'boltdb.shipper.cache-location': '/data/boltdb-cache',
15  } else {},
16
17  _config+:: {
18    // run rulers as statefulsets when using boltdb-shipper to avoid using node disk for storing the index.
19    stateful_rulers: if self.using_boltdb_shipper && !self.use_index_gateway then true else super.stateful_rulers,
20  },
21
22  ruler_container::
23    if $._config.ruler_enabled then
24      container.new('ruler', $._images.ruler) +
25      container.withPorts($.util.defaultPorts) +
26      container.withArgsMixin(k.util.mapToFlags($.ruler_args)) +
27      k.util.resourcesRequests('1', '6Gi') +
28      k.util.resourcesLimits('16', '16Gi') +
29      $.util.readinessProbe +
30      $.jaeger_mixin +
31      if $._config.stateful_rulers then
32        container.withVolumeMountsMixin([
33          volumeMount.new('ruler-data', '/data'),
34        ]) else {}
35    else {},
36
37  ruler_deployment:
38    if $._config.ruler_enabled && !$._config.stateful_rulers then
39      deployment.new('ruler', 2, [$.ruler_container]) +
40      deployment.mixin.spec.template.spec.withTerminationGracePeriodSeconds(600) +
41      $.config_hash_mixin +
42      k.util.configVolumeMount('loki', '/etc/loki/config') +
43      k.util.configVolumeMount(
44        $._config.overrides_configmap_mount_name,
45        $._config.overrides_configmap_mount_path,
46      ) +
47      k.util.antiAffinity
48    else {},
49
50  ruler_service: if !$._config.ruler_enabled
51  then {}
52  else
53    if $._config.stateful_rulers
54    then k.util.serviceFor($.ruler_statefulset)
55    else k.util.serviceFor($.ruler_deployment),
56
57
58  // PVC for rulers when running as statefulsets
59  ruler_data_pvc:: if $._config.ruler_enabled && $._config.stateful_rulers then
60    pvc.new('ruler-data') +
61    pvc.mixin.spec.resources.withRequests({ storage: $._config.ruler_pvc_size }) +
62    pvc.mixin.spec.withAccessModes(['ReadWriteOnce']) +
63    pvc.mixin.spec.withStorageClassName($._config.ruler_pvc_class)
64  else {},
65
66  ruler_statefulset: if $._config.ruler_enabled && $._config.stateful_rulers then
67    statefulSet.new('ruler', 2, [$.ruler_container], $.ruler_data_pvc) +
68    statefulSet.mixin.spec.withServiceName('ruler') +
69    statefulSet.mixin.spec.withPodManagementPolicy('Parallel') +
70    $.config_hash_mixin +
71    k.util.configVolumeMount('loki', '/etc/loki/config') +
72    k.util.configVolumeMount(
73      $._config.overrides_configmap_mount_name,
74      $._config.overrides_configmap_mount_path,
75    ) +
76    k.util.antiAffinity +
77    statefulSet.mixin.spec.updateStrategy.withType('RollingUpdate') +
78    statefulSet.mixin.spec.template.spec.securityContext.withFsGroup(10001)  // 10001 is the group ID assigned to Loki in the Dockerfile
79  else {},
80}
81