local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
local endpoints = k.core.v1.endpoints;
local endpointSubset = endpoints.subsetsType;
local endpointPort = endpointSubset.portsType;
local service = k.core.v1.service;
local servicePort = k.core.v1.service.mixin.spec.portsType;
local masterIP = '185.95.218.11';

{
  prometheus+:: {
    kubeSchedulerPrometheusDiscoveryService:
      local p = servicePort.newNamed('http-metrics', 10251, 10251);
      service.new('kube-scheduler', { 'k8s-app': 'kube-scheduler' }, p) +
      service.mixin.metadata.withNamespace('kube-system') +
      service.mixin.metadata.withLabels({ 'k8s-app': 'kube-scheduler' }) +
      service.mixin.spec.withClusterIp('None') +
      service.mixin.spec.withSelector({}),
    kubeSchedulerPrometheusDiscoveryEndpoints:
      local port = endpointPort.new() +
                   endpointPort.withName('http-metrics') +
                   endpointPort.withPort(10251) +
                   endpointPort.withProtocol('TCP');
      local subset = endpointSubset.new() +
                     endpointSubset.withAddresses([
                       { ip: masterIP },
                     ]) +
                     endpointSubset.withPorts(port);

      endpoints.new() +
      endpoints.mixin.metadata.withName('kube-scheduler') +
      endpoints.mixin.metadata.withNamespace('kube-system') +
      endpoints.mixin.metadata.withLabels({ 'k8s-app': 'kube-scheduler' }) +
      endpoints.withSubsets(subset),
    kubeControllerManagerPrometheusDiscoveryService:
      local p = servicePort.newNamed('http-metrics', 10252, 10252);
      service.new('kube-controller-manager', { 'k8s-app': 'kube-controller-manager' }, p) +
      service.mixin.metadata.withNamespace('kube-system') +
      service.mixin.metadata.withLabels({ 'k8s-app': 'kube-controller-manager' }) +
      service.mixin.spec.withClusterIp('None') +
      service.mixin.spec.withSelector({}),
    kubeControllerManagerPrometheusDiscoveryEndpoints:
      local port = endpointPort.new() +
                   endpointPort.withName('http-metrics') +
                   endpointPort.withPort(10252) +
                   endpointPort.withProtocol('TCP');
      local subset = endpointSubset.new() +
                     endpointSubset.withAddresses([
                       { ip: masterIP },
                     ]) +
                     endpointSubset.withPorts(port);

      endpoints.new() +
      endpoints.mixin.metadata.withName('kube-controller-manager') +
      endpoints.mixin.metadata.withNamespace('kube-system') +
      endpoints.mixin.metadata.withLabels({ 'k8s-app': 'kube-controller-manager' }) +
      endpoints.withSubsets(subset),
    serviceMonitorKubeScheduler+:
      {
        spec+: {
          endpoints: [
            {
              port: 'http-metrics',
              interval: '30s',
            },
          ],
        },
      },
    serviceMonitorKubeControllerManager+:
      {
        spec+: {
          endpoints: [
            {
              port: 'http-metrics',
              interval: '30s',
              metricRelabelings: (import 'kube-prometheus/dropping-deprecated-metrics-relabelings.libsonnet') + [
                {
                  sourceLabels: ['__name__'],
                  regex: 'etcd_(debugging|disk|request|server).*',
                  action: 'drop',
                },
              ],
            },
          ],
        },
      },
  },
}
