-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Greg Lyons
committed
Oct 2, 2018
1 parent
fff7de4
commit 73aef58
Showing
7 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# PodDisruptionBudget Metrics | ||
|
||
| Metric name| Metric type | Labels/tags | Status | | ||
| ---------- | ----------- | ----------- | ----------- | | ||
| kube_pdb_created | Gauge | `pdb_name`=<pdb-name> <br> `namespace`=<pdb-namespace> | STABLE | ||
| kube_pdb_status_current_healthy | Gauge | `pdb_name`=<pdb-name> <br> `namespace`=<pdb-namespace> | STABLE | ||
| kube_pdb_status_desired_healthy | Gauge | `pdb_name`=<pdb-name> <br> `namespace`=<pdb-namespace> | STABLE | ||
| kube_pdb_status_pod_disruptions_allowed | Gauge | `pdb_name`=<pdb-name> <br> `namespace`=<pdb-namespace> | STABLE | ||
| kube_pdb_status_expected_pods | Gauge | `pdb_name`=<pdb-name> <br> `namespace`=<pdb-namespace> | STABLE | ||
| kube_pdb_status_observed_generation | Gauge | `pdb_name`=<pdb-name> <br> `namespace`=<pdb-namespace> | STABLE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package collectors | ||
|
||
import ( | ||
"github.com/golang/glog" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"golang.org/x/net/context" | ||
"k8s.io/api/policy/v1beta1" | ||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/kube-state-metrics/pkg/options" | ||
) | ||
|
||
var ( | ||
descPodDisruptionBudgetLabelsDefaultLabels = []string{"pdb_name", "namespace"} | ||
|
||
descPodDisruptionBudgetCreated = prometheus.NewDesc( | ||
"kube_pdb_created", | ||
"Unix creation timestamp", | ||
descPodDisruptionBudgetLabelsDefaultLabels, | ||
nil, | ||
) | ||
|
||
descPodDisruptionBudgetStatusCurrentHealthy = prometheus.NewDesc( | ||
"kube_pdb_status_current_healthy", | ||
"Current number of healthy pods", | ||
descPodDisruptionBudgetLabelsDefaultLabels, | ||
nil, | ||
) | ||
descPodDisruptionBudgetStatusDesiredHealthy = prometheus.NewDesc( | ||
"kube_pdb_status_desired_healthy", | ||
"Minimum desired number of healthy pods", | ||
descPodDisruptionBudgetLabelsDefaultLabels, | ||
nil, | ||
) | ||
descPodDisruptionBudgetStatusPodDisruptionsAllowed = prometheus.NewDesc( | ||
"kube_pdb_status_pod_disruptions_allowed", | ||
"Number of pod disruptions that are currently allowed", | ||
descPodDisruptionBudgetLabelsDefaultLabels, | ||
nil, | ||
) | ||
descPodDisruptionBudgetStatusExpectedPods = prometheus.NewDesc( | ||
"kube_pdb_status_expected_pods", | ||
"Total number of pods counted by this disruption budget", | ||
descPodDisruptionBudgetLabelsDefaultLabels, | ||
nil, | ||
) | ||
descPodDisruptionBudgetStatusObservedGeneration = prometheus.NewDesc( | ||
"kube_pdb_status_observed_generation", | ||
"Most recent generation observed when updating this PDB status", | ||
descPodDisruptionBudgetLabelsDefaultLabels, | ||
nil, | ||
) | ||
) | ||
|
||
type PodDisruptionBudgetLister func() (v1beta1.PodDisruptionBudgetList, error) | ||
|
||
func (l PodDisruptionBudgetLister) List() (v1beta1.PodDisruptionBudgetList, error) { | ||
return l() | ||
} | ||
|
||
func RegisterPodDisruptionBudgetCollector(registry prometheus.Registerer, informerFactories []informers.SharedInformerFactory, opts *options.Options) { | ||
|
||
infs := SharedInformerList{} | ||
for _, f := range informerFactories { | ||
infs = append(infs, f.Policy().V1beta1().PodDisruptionBudgets().Informer().(cache.SharedInformer)) | ||
} | ||
|
||
podDisruptionBudgetLister := PodDisruptionBudgetLister(func() (podDisruptionBudgets v1beta1.PodDisruptionBudgetList, err error) { | ||
for _, pdbinf := range infs { | ||
for _, pdb := range pdbinf.GetStore().List() { | ||
podDisruptionBudgets.Items = append(podDisruptionBudgets.Items, *(pdb.(*v1beta1.PodDisruptionBudget))) | ||
} | ||
} | ||
return podDisruptionBudgets, nil | ||
}) | ||
|
||
registry.MustRegister(&podDisruptionBudgetCollector{store: podDisruptionBudgetLister, opts: opts}) | ||
infs.Run(context.Background().Done()) | ||
} | ||
|
||
type podDisruptionBudgetStore interface { | ||
List() (v1beta1.PodDisruptionBudgetList, error) | ||
} | ||
|
||
// podDisruptionBudgetCollector collects metrics about all pod disruption budgets in the cluster. | ||
type podDisruptionBudgetCollector struct { | ||
store podDisruptionBudgetStore | ||
opts *options.Options | ||
} | ||
|
||
// Describe implements the prometheus.Collector interface. | ||
func (pdbc *podDisruptionBudgetCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- descPodDisruptionBudgetCreated | ||
ch <- descPodDisruptionBudgetStatusCurrentHealthy | ||
ch <- descPodDisruptionBudgetStatusDesiredHealthy | ||
ch <- descPodDisruptionBudgetStatusPodDisruptionsAllowed | ||
ch <- descPodDisruptionBudgetStatusExpectedPods | ||
ch <- descPodDisruptionBudgetStatusObservedGeneration | ||
} | ||
|
||
// Collect implements the prometheus.Collector interface. | ||
func (pdbc *podDisruptionBudgetCollector) Collect(ch chan<- prometheus.Metric) { | ||
podDisruptionBudget, err := pdbc.store.List() | ||
if err != nil { | ||
ScrapeErrorTotalMetric.With(prometheus.Labels{"resource": "poddisruptionbudget"}).Inc() | ||
glog.Errorf("listing pod disruption budgets failed: %s", err) | ||
return | ||
} | ||
ScrapeErrorTotalMetric.With(prometheus.Labels{"resource": "poddisruptionbudget"}).Add(0) | ||
|
||
ResourcesPerScrapeMetric.With(prometheus.Labels{"resource": "poddisruptionbudget"}).Observe(float64(len(podDisruptionBudget.Items))) | ||
for _, pdb := range podDisruptionBudget.Items { | ||
pdbc.collectPodDisruptionBudget(ch, pdb) | ||
} | ||
|
||
glog.V(4).Infof("collected %d poddisruptionsbudgets", len(podDisruptionBudget.Items)) | ||
} | ||
|
||
func (pdbc *podDisruptionBudgetCollector) collectPodDisruptionBudget(ch chan<- prometheus.Metric, pdb v1beta1.PodDisruptionBudget) { | ||
addGauge := func(desc *prometheus.Desc, v float64, lv ...string) { | ||
lv = append([]string{pdb.Name, pdb.Namespace}, lv...) | ||
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, lv...) | ||
} | ||
|
||
if !pdb.CreationTimestamp.IsZero() { | ||
addGauge(descPodDisruptionBudgetCreated, float64(pdb.CreationTimestamp.Unix())) | ||
} | ||
addGauge(descPodDisruptionBudgetStatusCurrentHealthy, float64(pdb.Status.CurrentHealthy)) | ||
addGauge(descPodDisruptionBudgetStatusDesiredHealthy, float64(pdb.Status.DesiredHealthy)) | ||
addGauge(descPodDisruptionBudgetStatusPodDisruptionsAllowed, float64(pdb.Status.PodDisruptionsAllowed)) | ||
addGauge(descPodDisruptionBudgetStatusExpectedPods, float64(pdb.Status.ExpectedPods)) | ||
addGauge(descPodDisruptionBudgetStatusObservedGeneration, float64(pdb.Status.ObservedGeneration)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package collectors | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"k8s.io/api/policy/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/kube-state-metrics/pkg/collectors/testutils" | ||
"k8s.io/kube-state-metrics/pkg/options" | ||
) | ||
|
||
type mockPodDisruptionBudgetStore struct { | ||
list func() (v1beta1.PodDisruptionBudgetList, error) | ||
} | ||
|
||
func (ns mockPodDisruptionBudgetStore) List() (v1beta1.PodDisruptionBudgetList, error) { | ||
return ns.list() | ||
} | ||
|
||
func TestPodDisruptionBudgetCollector(t *testing.T) { | ||
// Fixed metadata on type and help text. We prepend this to every expected | ||
// output so we only have to modify a single place when doing adjustments. | ||
const metadata = ` | ||
# HELP kube_pdb_created Unix creation timestamp | ||
# TYPE kube_pdb_created gauge | ||
# HELP kube_pdb_status_current_healthy Current number of healthy pods | ||
# TYPE kube_pdb_status_current_healthy gauge | ||
# HELP kube_pdb_status_desired_healthy Minimum desired number of healthy pods | ||
# TYPE kube_pdb_status_desired_healthy gauge | ||
# HELP kube_pdb_status_pod_disruptions_allowed Number of pod disruptions that are currently allowed | ||
# TYPE kube_pdb_status_pod_disruptions_allowed gauge | ||
# HELP kube_pdb_status_expected_pods Total number of pods counted by this disruption budget | ||
# TYPE kube_pdb_status_expected_pods gauge | ||
# HELP kube_pdb_status_observed_generation Most recent generation observed when updating this PDB status | ||
# TYPE kube_pdb_status_observed_generation gauge | ||
` | ||
cases := []struct { | ||
pdbs []v1beta1.PodDisruptionBudget | ||
want string | ||
}{ | ||
{ | ||
pdbs: []v1beta1.PodDisruptionBudget{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pdb1", | ||
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)}, | ||
Namespace: "ns1", | ||
Generation: 21, | ||
}, | ||
Status: v1beta1.PodDisruptionBudgetStatus{ | ||
CurrentHealthy: 12, | ||
DesiredHealthy: 10, | ||
PodDisruptionsAllowed: 2, | ||
ExpectedPods: 15, | ||
ObservedGeneration: 111, | ||
}, | ||
}, { | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pdb2", | ||
Namespace: "ns2", | ||
Generation: 14, | ||
}, | ||
Status: v1beta1.PodDisruptionBudgetStatus{ | ||
CurrentHealthy: 8, | ||
DesiredHealthy: 9, | ||
PodDisruptionsAllowed: 0, | ||
ExpectedPods: 10, | ||
ObservedGeneration: 1111, | ||
}, | ||
}, | ||
}, | ||
want: metadata + ` | ||
kube_pdb_created{namespace="ns1",pdb_name="pdb1"} 1.5e+09 | ||
kube_pdb_status_current_healthy{namespace="ns1",pdb_name="pdb1"} 12 | ||
kube_pdb_status_current_healthy{namespace="ns2",pdb_name="pdb2"} 8 | ||
kube_pdb_status_desired_healthy{namespace="ns1",pdb_name="pdb1"} 10 | ||
kube_pdb_status_desired_healthy{namespace="ns2",pdb_name="pdb2"} 9 | ||
kube_pdb_status_pod_disruptions_allowed{namespace="ns1",pdb_name="pdb1"} 2 | ||
kube_pdb_status_pod_disruptions_allowed{namespace="ns2",pdb_name="pdb2"} 0 | ||
kube_pdb_status_expected_pods{namespace="ns1",pdb_name="pdb1"} 15 | ||
kube_pdb_status_expected_pods{namespace="ns2",pdb_name="pdb2"} 10 | ||
kube_pdb_status_observed_generation{namespace="ns1",pdb_name="pdb1"} 111 | ||
kube_pdb_status_observed_generation{namespace="ns2",pdb_name="pdb2"} 1111 | ||
`, | ||
}, | ||
} | ||
for _, c := range cases { | ||
pdbc := &podDisruptionBudgetCollector{ | ||
store: &mockPodDisruptionBudgetStore{ | ||
list: func() (v1beta1.PodDisruptionBudgetList, error) { | ||
return v1beta1.PodDisruptionBudgetList{Items: c.pdbs}, nil | ||
}, | ||
}, | ||
opts: &options.Options{}, | ||
} | ||
if err := testutils.GatherAndCompare(pdbc, c.want, nil); err != nil { | ||
t.Errorf("unexpected collecting result:\n%s", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters