|
| 1 | +/* |
| 2 | +Copyright 2016 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/golang/glog" |
| 21 | + "github.com/prometheus/client_golang/prometheus" |
| 22 | + "golang.org/x/net/context" |
| 23 | + "k8s.io/client-go/kubernetes" |
| 24 | + "k8s.io/client-go/pkg/api" |
| 25 | + "k8s.io/client-go/pkg/apis/extensions/v1beta1" |
| 26 | + "k8s.io/client-go/tools/cache" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + descReplicaSetStatusReplicas = prometheus.NewDesc( |
| 31 | + "kube_replicaset_status_replicas", |
| 32 | + "The number of replicas per ReplicaSet.", |
| 33 | + []string{"namespace", "replicaset"}, nil, |
| 34 | + ) |
| 35 | + descReplicaSetStatusFullyLabeledReplicas = prometheus.NewDesc( |
| 36 | + "kube_replicaset_status_fully_labeled_replicas", |
| 37 | + "The number of fully labeled replicas per ReplicaSet.", |
| 38 | + []string{"namespace", "replicaset"}, nil, |
| 39 | + ) |
| 40 | + descReplicaSetStatusReadyReplicas = prometheus.NewDesc( |
| 41 | + "kube_replicaset_status_ready_replicas", |
| 42 | + "The number of ready replicas per ReplicaSet.", |
| 43 | + []string{"namespace", "replicaset"}, nil, |
| 44 | + ) |
| 45 | + descReplicaSetStatusObservedGeneration = prometheus.NewDesc( |
| 46 | + "kube_replicaset_status_observed_generation", |
| 47 | + "The generation observed by the ReplicaSet controller.", |
| 48 | + []string{"namespace", "replicaset"}, nil, |
| 49 | + ) |
| 50 | + descReplicaSetSpecReplicas = prometheus.NewDesc( |
| 51 | + "kube_replicaset_spec_replicas", |
| 52 | + "Number of desired pods for a ReplicaSet.", |
| 53 | + []string{"namespace", "replicaset"}, nil, |
| 54 | + ) |
| 55 | + descReplicaSetMetadataGeneration = prometheus.NewDesc( |
| 56 | + "kube_replicaset_metadata_generation", |
| 57 | + "Sequence number representing a specific generation of the desired state.", |
| 58 | + []string{"namespace", "replicaset"}, nil, |
| 59 | + ) |
| 60 | +) |
| 61 | + |
| 62 | +type ReplicaSetLister func() ([]v1beta1.ReplicaSet, error) |
| 63 | + |
| 64 | +func (l ReplicaSetLister) List() ([]v1beta1.ReplicaSet, error) { |
| 65 | + return l() |
| 66 | +} |
| 67 | + |
| 68 | +func RegisterReplicaSetCollector(registry prometheus.Registerer, kubeClient kubernetes.Interface) { |
| 69 | + client := kubeClient.Extensions().RESTClient() |
| 70 | + rslw := cache.NewListWatchFromClient(client, "replicasets", api.NamespaceAll, nil) |
| 71 | + rsinf := cache.NewSharedInformer(rslw, &v1beta1.ReplicaSet{}, resyncPeriod) |
| 72 | + |
| 73 | + replicaSetLister := ReplicaSetLister(func() (replicasets []v1beta1.ReplicaSet, err error) { |
| 74 | + for _, c := range rsinf.GetStore().List() { |
| 75 | + replicasets = append(replicasets, *(c.(*v1beta1.ReplicaSet))) |
| 76 | + } |
| 77 | + return replicasets, nil |
| 78 | + }) |
| 79 | + |
| 80 | + registry.MustRegister(&replicasetCollector{store: replicaSetLister}) |
| 81 | + go rsinf.Run(context.Background().Done()) |
| 82 | +} |
| 83 | + |
| 84 | +type replicasetStore interface { |
| 85 | + List() (replicasets []v1beta1.ReplicaSet, err error) |
| 86 | +} |
| 87 | + |
| 88 | +// replicasetCollector collects metrics about all replicasets in the cluster. |
| 89 | +type replicasetCollector struct { |
| 90 | + store replicasetStore |
| 91 | +} |
| 92 | + |
| 93 | +// Describe implements the prometheus.Collector interface. |
| 94 | +func (dc *replicasetCollector) Describe(ch chan<- *prometheus.Desc) { |
| 95 | + ch <- descReplicaSetStatusReplicas |
| 96 | + ch <- descReplicaSetStatusFullyLabeledReplicas |
| 97 | + ch <- descReplicaSetStatusReadyReplicas |
| 98 | + ch <- descReplicaSetStatusObservedGeneration |
| 99 | + ch <- descReplicaSetSpecReplicas |
| 100 | + ch <- descReplicaSetMetadataGeneration |
| 101 | +} |
| 102 | + |
| 103 | +// Collect implements the prometheus.Collector interface. |
| 104 | +func (dc *replicasetCollector) Collect(ch chan<- prometheus.Metric) { |
| 105 | + dpls, err := dc.store.List() |
| 106 | + if err != nil { |
| 107 | + glog.Errorf("listing replicasets failed: %s", err) |
| 108 | + return |
| 109 | + } |
| 110 | + for _, d := range dpls { |
| 111 | + dc.collectReplicaSet(ch, d) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func (dc *replicasetCollector) collectReplicaSet(ch chan<- prometheus.Metric, d v1beta1.ReplicaSet) { |
| 116 | + addGauge := func(desc *prometheus.Desc, v float64, lv ...string) { |
| 117 | + lv = append([]string{d.Namespace, d.Name}, lv...) |
| 118 | + ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, lv...) |
| 119 | + } |
| 120 | + addGauge(descReplicaSetStatusReplicas, float64(d.Status.Replicas)) |
| 121 | + addGauge(descReplicaSetStatusFullyLabeledReplicas, float64(d.Status.FullyLabeledReplicas)) |
| 122 | + addGauge(descReplicaSetStatusReadyReplicas, float64(d.Status.ReadyReplicas)) |
| 123 | + addGauge(descReplicaSetStatusObservedGeneration, float64(d.Status.ObservedGeneration)) |
| 124 | + addGauge(descReplicaSetSpecReplicas, float64(*d.Spec.Replicas)) |
| 125 | + addGauge(descReplicaSetMetadataGeneration, float64(d.ObjectMeta.Generation)) |
| 126 | +} |
0 commit comments