allow creation of role and rolebindings for other namespaces in jsonnet
replaced default namespaces rbac rules by a loop set right variable name
This commit is contained in:
28
docs/monitoring-other-namespaces.md
Normal file
28
docs/monitoring-other-namespaces.md
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# Monitoring other Kubernetes Namespaces
|
||||||
|
This guide will help you monitor applications in other Namespaces. By default the RBAC rules are only enabled for the `Default` and `kube-system` Namespace during Install.
|
||||||
|
|
||||||
|
# Setup
|
||||||
|
You have to give the list of the Namespaces that you want to be able to monitor.
|
||||||
|
This is done in the variable `prometheus.roleSpecificNamespaces`. You usually set this in your `.jsonnet` file when building the manifests.
|
||||||
|
|
||||||
|
Ex to create the needed `Role` and `Rolebindig` for the Namespace `foo` :
|
||||||
|
```
|
||||||
|
local kp = (import 'kube-prometheus/kube-prometheus.libsonnet') + {
|
||||||
|
_config+:: {
|
||||||
|
namespace: 'monitoring',
|
||||||
|
|
||||||
|
prometheus+:: {
|
||||||
|
namespaces: ["default", "kube-system","foo"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
{ ['00namespace-' + name]: kp.kubePrometheus[name] for name in std.objectFields(kp.kubePrometheus) } +
|
||||||
|
{ ['0prometheus-operator-' + name]: kp.prometheusOperator[name] for name in std.objectFields(kp.prometheusOperator) } +
|
||||||
|
{ ['node-exporter-' + name]: kp.nodeExporter[name] for name in std.objectFields(kp.nodeExporter) } +
|
||||||
|
{ ['kube-state-metrics-' + name]: kp.kubeStateMetrics[name] for name in std.objectFields(kp.kubeStateMetrics) } +
|
||||||
|
{ ['alertmanager-' + name]: kp.alertmanager[name] for name in std.objectFields(kp.alertmanager) } +
|
||||||
|
{ ['prometheus-' + name]: kp.prometheus[name] for name in std.objectFields(kp.prometheus) } +
|
||||||
|
{ ['grafana-' + name]: kp.grafana[name] for name in std.objectFields(kp.grafana) }
|
||||||
|
|
||||||
|
```
|
@@ -21,6 +21,7 @@ local k = import 'ksonnet/ksonnet.beta.3/k.libsonnet';
|
|||||||
replicas: 2,
|
replicas: 2,
|
||||||
rules: {},
|
rules: {},
|
||||||
renderedRules: {},
|
renderedRules: {},
|
||||||
|
namespaces: ["default", "kube-system",$._config.namespace],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -55,16 +56,20 @@ local k = import 'ksonnet/ksonnet.beta.3/k.libsonnet';
|
|||||||
groups: $._config.prometheus.rules.groups,
|
groups: $._config.prometheus.rules.groups,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
roleBindingDefault:
|
roleBindingSpecificNamespace:
|
||||||
local roleBinding = k.rbac.v1.roleBinding;
|
local roleBinding = k.rbac.v1.roleBinding;
|
||||||
|
|
||||||
roleBinding.new() +
|
local newSpecificRoleBinding(namespace) =
|
||||||
roleBinding.mixin.metadata.withName('prometheus-' + $._config.prometheus.name) +
|
roleBinding.new() +
|
||||||
roleBinding.mixin.metadata.withNamespace('default') +
|
roleBinding.mixin.metadata.withName('prometheus-' + $._config.prometheus.name) +
|
||||||
roleBinding.mixin.roleRef.withApiGroup('rbac.authorization.k8s.io') +
|
roleBinding.mixin.metadata.withNamespace(namespace) +
|
||||||
roleBinding.mixin.roleRef.withName('prometheus-' + $._config.prometheus.name) +
|
roleBinding.mixin.roleRef.withApiGroup('rbac.authorization.k8s.io') +
|
||||||
roleBinding.mixin.roleRef.mixinInstance({ kind: 'Role' }) +
|
roleBinding.mixin.roleRef.withName('prometheus-' + $._config.prometheus.name) +
|
||||||
roleBinding.withSubjects([{ kind: 'ServiceAccount', name: 'prometheus-' + $._config.prometheus.name, namespace: $._config.namespace }]),
|
roleBinding.mixin.roleRef.mixinInstance({ kind: 'Role' }) +
|
||||||
|
roleBinding.withSubjects([{ kind: 'ServiceAccount', name: 'prometheus-' + $._config.prometheus.name, namespace: namespace }]);
|
||||||
|
|
||||||
|
local roleBindigList = k.rbac.v1.roleBindingList;
|
||||||
|
roleBindigList.new([newSpecificRoleBinding(x) for x in $._config.prometheus.namespaces]),
|
||||||
clusterRole:
|
clusterRole:
|
||||||
local clusterRole = k.rbac.v1.clusterRole;
|
local clusterRole = k.rbac.v1.clusterRole;
|
||||||
local policyRule = clusterRole.rulesType;
|
local policyRule = clusterRole.rulesType;
|
||||||
@@ -108,16 +113,6 @@ local k = import 'ksonnet/ksonnet.beta.3/k.libsonnet';
|
|||||||
roleBinding.mixin.roleRef.withName('prometheus-' + $._config.prometheus.name + '-config') +
|
roleBinding.mixin.roleRef.withName('prometheus-' + $._config.prometheus.name + '-config') +
|
||||||
roleBinding.mixin.roleRef.mixinInstance({ kind: 'Role' }) +
|
roleBinding.mixin.roleRef.mixinInstance({ kind: 'Role' }) +
|
||||||
roleBinding.withSubjects([{ kind: 'ServiceAccount', name: 'prometheus-' + $._config.prometheus.name, namespace: $._config.namespace }]),
|
roleBinding.withSubjects([{ kind: 'ServiceAccount', name: 'prometheus-' + $._config.prometheus.name, namespace: $._config.namespace }]),
|
||||||
roleBindingNamespace:
|
|
||||||
local roleBinding = k.rbac.v1.roleBinding;
|
|
||||||
|
|
||||||
roleBinding.new() +
|
|
||||||
roleBinding.mixin.metadata.withName('prometheus-' + $._config.prometheus.name) +
|
|
||||||
roleBinding.mixin.metadata.withNamespace($._config.namespace) +
|
|
||||||
roleBinding.mixin.roleRef.withApiGroup('rbac.authorization.k8s.io') +
|
|
||||||
roleBinding.mixin.roleRef.withName('prometheus-' + $._config.prometheus.name) +
|
|
||||||
roleBinding.mixin.roleRef.mixinInstance({ kind: 'Role' }) +
|
|
||||||
roleBinding.withSubjects([{ kind: 'ServiceAccount', name: 'prometheus-' + $._config.prometheus.name, namespace: $._config.namespace }]),
|
|
||||||
clusterRoleBinding:
|
clusterRoleBinding:
|
||||||
local clusterRoleBinding = k.rbac.v1.clusterRoleBinding;
|
local clusterRoleBinding = k.rbac.v1.clusterRoleBinding;
|
||||||
|
|
||||||
@@ -127,10 +122,9 @@ local k = import 'ksonnet/ksonnet.beta.3/k.libsonnet';
|
|||||||
clusterRoleBinding.mixin.roleRef.withName('prometheus-' + $._config.prometheus.name) +
|
clusterRoleBinding.mixin.roleRef.withName('prometheus-' + $._config.prometheus.name) +
|
||||||
clusterRoleBinding.mixin.roleRef.mixinInstance({ kind: 'ClusterRole' }) +
|
clusterRoleBinding.mixin.roleRef.mixinInstance({ kind: 'ClusterRole' }) +
|
||||||
clusterRoleBinding.withSubjects([{ kind: 'ServiceAccount', name: 'prometheus-' + $._config.prometheus.name, namespace: $._config.namespace }]),
|
clusterRoleBinding.withSubjects([{ kind: 'ServiceAccount', name: 'prometheus-' + $._config.prometheus.name, namespace: $._config.namespace }]),
|
||||||
roleKubeSystem:
|
roleSpecificNamespace:
|
||||||
local role = k.rbac.v1.role;
|
local role = k.rbac.v1.role;
|
||||||
local policyRule = role.rulesType;
|
local policyRule = role.rulesType;
|
||||||
|
|
||||||
local coreRule = policyRule.new() +
|
local coreRule = policyRule.new() +
|
||||||
policyRule.withApiGroups(['']) +
|
policyRule.withApiGroups(['']) +
|
||||||
policyRule.withResources([
|
policyRule.withResources([
|
||||||
@@ -141,56 +135,14 @@ local k = import 'ksonnet/ksonnet.beta.3/k.libsonnet';
|
|||||||
]) +
|
]) +
|
||||||
policyRule.withVerbs(['get', 'list', 'watch']);
|
policyRule.withVerbs(['get', 'list', 'watch']);
|
||||||
|
|
||||||
role.new() +
|
local newSpecificRole(namespace) =
|
||||||
role.mixin.metadata.withName('prometheus-' + $._config.prometheus.name) +
|
role.new() +
|
||||||
role.mixin.metadata.withNamespace('kube-system') +
|
role.mixin.metadata.withName('prometheus-' + $._config.prometheus.name) +
|
||||||
role.withRules(coreRule),
|
role.mixin.metadata.withNamespace(namespace) +
|
||||||
roleDefault:
|
role.withRules(coreRule);
|
||||||
local role = k.rbac.v1.role;
|
|
||||||
local policyRule = role.rulesType;
|
|
||||||
|
|
||||||
local coreRule = policyRule.new() +
|
local roleList = k.rbac.v1.roleList;
|
||||||
policyRule.withApiGroups(['']) +
|
roleList.new([newSpecificRole(x) for x in $._config.prometheus.namespaces]),
|
||||||
policyRule.withResources([
|
|
||||||
'nodes',
|
|
||||||
'services',
|
|
||||||
'endpoints',
|
|
||||||
'pods',
|
|
||||||
]) +
|
|
||||||
policyRule.withVerbs(['get', 'list', 'watch']);
|
|
||||||
|
|
||||||
role.new() +
|
|
||||||
role.mixin.metadata.withName('prometheus-' + $._config.prometheus.name) +
|
|
||||||
role.mixin.metadata.withNamespace('default') +
|
|
||||||
role.withRules(coreRule),
|
|
||||||
roleBindingKubeSystem:
|
|
||||||
local roleBinding = k.rbac.v1.roleBinding;
|
|
||||||
|
|
||||||
roleBinding.new() +
|
|
||||||
roleBinding.mixin.metadata.withName('prometheus-' + $._config.prometheus.name) +
|
|
||||||
roleBinding.mixin.metadata.withNamespace('kube-system') +
|
|
||||||
roleBinding.mixin.roleRef.withApiGroup('rbac.authorization.k8s.io') +
|
|
||||||
roleBinding.mixin.roleRef.withName('prometheus-' + $._config.prometheus.name) +
|
|
||||||
roleBinding.mixin.roleRef.mixinInstance({ kind: 'Role' }) +
|
|
||||||
roleBinding.withSubjects([{ kind: 'ServiceAccount', name: 'prometheus-' + $._config.prometheus.name, namespace: $._config.namespace }]),
|
|
||||||
roleNamespace:
|
|
||||||
local role = k.rbac.v1.role;
|
|
||||||
local policyRule = role.rulesType;
|
|
||||||
|
|
||||||
local coreRule = policyRule.new() +
|
|
||||||
policyRule.withApiGroups(['']) +
|
|
||||||
policyRule.withResources([
|
|
||||||
'nodes',
|
|
||||||
'services',
|
|
||||||
'endpoints',
|
|
||||||
'pods',
|
|
||||||
]) +
|
|
||||||
policyRule.withVerbs(['get', 'list', 'watch']);
|
|
||||||
|
|
||||||
role.new() +
|
|
||||||
role.mixin.metadata.withName('prometheus-' + $._config.prometheus.name) +
|
|
||||||
role.mixin.metadata.withNamespace($._config.namespace) +
|
|
||||||
role.withRules(coreRule),
|
|
||||||
prometheus:
|
prometheus:
|
||||||
local container = k.core.v1.pod.mixin.spec.containersType;
|
local container = k.core.v1.pod.mixin.spec.containersType;
|
||||||
local resourceRequirements = container.mixin.resourcesType;
|
local resourceRequirements = container.mixin.resourcesType;
|
||||||
|
Reference in New Issue
Block a user