Skip to content
Snippets Groups Projects
Select Git revision
1 result Searching

ManagerTest.php

Blame
  • customizing.md 8.96 KiB

    Customizing Kube-Prometheus

    This section:

    • describes how to customize the kube-prometheus library via compiling the kube-prometheus manifests yourself (as an alternative to the README.md quickstart section).
    • still doesn't require you to make a copy of this entire repository, but rather only a copy of a few select files.

    Installing

    The content of this project consists of a set of jsonnet files making up a library to be consumed.

    Install this library in your own project with jsonnet-bundler (the jsonnet package manager):

    $ mkdir my-kube-prometheus; cd my-kube-prometheus
    $ jb init  # Creates the initial/empty `jsonnetfile.json`
    # Install the kube-prometheus dependency
    $ jb install github.com/prometheus-operator/kube-prometheus/jsonnet/kube-prometheus@main # Creates `vendor/` & `jsonnetfile.lock.json`, and fills in `jsonnetfile.json`
    
    $ wget https://raw.githubusercontent.com/prometheus-operator/kube-prometheus/main/example.jsonnet -O example.jsonnet
    $ wget https://raw.githubusercontent.com/prometheus-operator/kube-prometheus/main/build.sh -O build.sh
    $ chmod +x build.sh

    jb can be installed with go install -a github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb@latest

    An e.g. of how to install a given version of this library: jb install github.com/prometheus-operator/kube-prometheus/jsonnet/kube-prometheus@main

    In order to update the kube-prometheus dependency, simply use the jsonnet-bundler update functionality:

    $ jb update

    Generating

    e.g. of how to compile the manifests: ./build.sh example.jsonnet

    before compiling, install gojsontoyaml tool with go install github.com/brancz/gojsontoyaml@latest and jsonnet with go install github.com/google/go-jsonnet/cmd/jsonnet@latest

    Here's example.jsonnet:

    Note: some of the following components must be configured beforehand. See configuration and customization-examples.

    local kp =
      (import 'kube-prometheus/main.libsonnet') +
      // Uncomment the following imports to enable its patches
      // (import 'kube-prometheus/addons/anti-affinity.libsonnet') +
      // (import 'kube-prometheus/addons/managed-cluster.libsonnet') +
      // (import 'kube-prometheus/addons/node-ports.libsonnet') +
      // (import 'kube-prometheus/addons/static-etcd.libsonnet') +
      // (import 'kube-prometheus/addons/custom-metrics.libsonnet') +
      // (import 'kube-prometheus/addons/external-metrics.libsonnet') +
      // (import 'kube-prometheus/addons/pyrra.libsonnet') +
      {
        values+:: {
          common+: {
            namespace: 'monitoring',
          },
        },
      };
    
    { 'setup/0namespace-namespace': kp.kubePrometheus.namespace } +
    {
      ['setup/prometheus-operator-' + name]: kp.prometheusOperator[name]
      for name in std.filter((function(name) name != 'serviceMonitor' && name != 'prometheusRule'), std.objectFields(kp.prometheusOperator))
    } +
    // { 'setup/pyrra-slo-CustomResourceDefinition': kp.pyrra.crd } +
    // serviceMonitor and prometheusRule are separated so that they can be created after the CRDs are ready
    { 'prometheus-operator-serviceMonitor': kp.prometheusOperator.serviceMonitor } +
    { 'prometheus-operator-prometheusRule': kp.prometheusOperator.prometheusRule } +
    { 'kube-prometheus-prometheusRule': kp.kubePrometheus.prometheusRule } +
    { ['alertmanager-' + name]: kp.alertmanager[name] for name in std.objectFields(kp.alertmanager) } +
    { ['blackbox-exporter-' + name]: kp.blackboxExporter[name] for name in std.objectFields(kp.blackboxExporter) } +
    { ['grafana-' + name]: kp.grafana[name] for name in std.objectFields(kp.grafana) } +
    // { ['pyrra-' + name]: kp.pyrra[name] for name in std.objectFields(kp.pyrra) if name != 'crd' } +
    { ['kube-state-metrics-' + name]: kp.kubeStateMetrics[name] for name in std.objectFields(kp.kubeStateMetrics) } +
    { ['kubernetes-' + name]: kp.kubernetesControlPlane[name] for name in std.objectFields(kp.kubernetesControlPlane) }
    { ['node-exporter-' + name]: kp.nodeExporter[name] for name in std.objectFields(kp.nodeExporter) } +
    { ['prometheus-' + name]: kp.prometheus[name] for name in std.objectFields(kp.prometheus) } +
    { ['prometheus-adapter-' + name]: kp.prometheusAdapter[name] for name in std.objectFields(kp.prometheusAdapter) }

    And here's the build.sh script (which uses vendor/ to render all manifests in a json structure of {filename: manifest-content}):

    #!/usr/bin/env bash
    
    # This script uses arg $1 (name of *.jsonnet file to use) to generate the manifests/*.yaml files.
    
    set -e
    set -x
    # only exit with zero if all commands of the pipeline exit successfully
    set -o pipefail
    
    # Make sure to use project tooling
    PATH="$(pwd)/tmp/bin:${PATH}"
    
    # Make sure to start with a clean 'manifests' dir
    rm -rf manifests
    mkdir -p manifests/setup
    
    # Calling gojsontoyaml is optional, but we would like to generate yaml, not json
    jsonnet -J vendor -m manifests "${1-example.jsonnet}" | xargs -I{} sh -c 'cat {} | gojsontoyaml > {}.yaml' -- {}
    
    # Make sure to remove json files
    find manifests -type f ! -name '*.yaml' -delete
    rm -f kustomization
    

    Note you need jsonnet (go install github.com/google/go-jsonnet/cmd/jsonnet@latest) and gojsontoyaml (go install github.com/brancz/gojsontoyaml@latest) installed to run build.sh. If you just want json output, not yaml, then you can skip the pipe and everything afterwards.