github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/production/ksonnet/promtail/promtail.libsonnet (about) 1 local scrape_config = import './scrape_config.libsonnet'; 2 local config = import 'config.libsonnet'; 3 local k = import 'ksonnet-util/kausal.libsonnet'; 4 5 // backwards compatibility with ksonnet 6 local envVar = if std.objectHasAll(k.core.v1, 'envVar') then k.core.v1.envVar else k.core.v1.container.envType; 7 8 config + scrape_config { 9 namespace: 10 k.core.v1.namespace.new($._config.namespace), 11 12 // The RBAC functions in kausal.libsonnet require namespace to be set 13 local namespaced_k = k { 14 _config+:: { namespace: $._config.namespace }, 15 }, 16 17 local policyRule = k.rbac.v1.policyRule, 18 19 promtail_rbac: 20 namespaced_k.util.rbac($._config.promtail_cluster_role_name, [ 21 policyRule.new() + 22 policyRule.withApiGroups(['']) + 23 policyRule.withResources(['nodes', 'nodes/proxy', 'services', 'endpoints', 'pods']) + 24 policyRule.withVerbs(['get', 'list', 'watch']), 25 ]), 26 27 promtail_config+:: { 28 local service_url(client) = 29 if std.objectHasAll(client, 'username') then 30 '%(scheme)s://%(username)s:%(password)s@%(hostname)s/loki/api/v1/push' % client 31 else 32 '%(scheme)s://%(hostname)s/loki/api/v1/push' % client, 33 34 local client_config(client) = client { 35 url: service_url(client), 36 }, 37 38 clients: std.map(client_config, $._config.promtail_config.clients), 39 }, 40 41 local configMap = k.core.v1.configMap, 42 43 promtail_config_map: 44 configMap.new($._config.promtail_configmap_name) + 45 configMap.withData({ 46 'promtail.yml': k.util.manifestYaml($.promtail_config), 47 }), 48 49 promtail_args:: { 50 'config.file': '/etc/promtail/promtail.yml', 51 }, 52 53 local container = k.core.v1.container, 54 55 promtail_container:: 56 container.new('promtail', $._images.promtail) + 57 container.withPorts(k.core.v1.containerPort.new(name='http-metrics', port=80)) + 58 container.withArgsMixin(k.util.mapToFlags($.promtail_args)) + 59 container.withEnv([ 60 envVar.fromFieldPath('HOSTNAME', 'spec.nodeName'), 61 ]) + 62 container.mixin.readinessProbe.httpGet.withPath('/ready') + 63 container.mixin.readinessProbe.httpGet.withPort(80) + 64 container.mixin.readinessProbe.withInitialDelaySeconds(10) + 65 container.mixin.readinessProbe.withTimeoutSeconds(1) + 66 container.mixin.securityContext.withPrivileged(true) + 67 container.mixin.securityContext.withRunAsUser(0), 68 69 local daemonSet = k.apps.v1.daemonSet, 70 71 promtail_daemonset: 72 daemonSet.new($._config.promtail_pod_name, [$.promtail_container]) + 73 daemonSet.mixin.spec.template.spec.withServiceAccount($._config.promtail_cluster_role_name) + 74 k.util.configMapVolumeMount($.promtail_config_map, '/etc/promtail') + 75 k.util.hostVolumeMount('varlog', '/var/log', '/var/log') + 76 k.util.hostVolumeMount('varlibdockercontainers', $._config.promtail_config.container_root_path + '/containers', $._config.promtail_config.container_root_path + '/containers', readOnly=true), 77 }