2 Copyright 2018 The Kubernetes Authors All rights reserved.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
23 "k8s.io/apimachinery/pkg/api/meta"
24 "k8s.io/apimachinery/pkg/types"
27 // FamilyByteSlicer represents a metric family that can be converted to its string
29 type FamilyByteSlicer interface {
33 // MetricsStore implements the k8s.io/client-go/tools/cache.Store
34 // interface. Instead of storing entire Kubernetes objects, it stores metrics
35 // generated based on those objects.
36 type MetricsStore struct {
39 // metrics is a map indexed by Kubernetes object id, containing a slice of
40 // metric families, containing a slice of metrics. We need to keep metrics
41 // grouped by metric families in order to zip families with their help text in
42 // MetricsStore.WriteAll().
43 metrics map[types.UID][][]byte
44 // headers contains the header (TYPE and HELP) of each metric family. It is
45 // later on zipped with with their corresponding metric families in
46 // MetricStore.WriteAll().
49 // generateMetricsFunc generates metrics based on a given Kubernetes object
50 // and returns them grouped by metric family.
51 generateMetricsFunc func(interface{}) []FamilyByteSlicer
54 // NewMetricsStore returns a new MetricsStore
55 func NewMetricsStore(headers []string, generateFunc func(interface{}) []FamilyByteSlicer) *MetricsStore {
57 generateMetricsFunc: generateFunc,
59 metrics: map[types.UID][][]byte{},
63 // Implementing k8s.io/client-go/tools/cache.Store interface
65 // Add inserts adds to the MetricsStore by calling the metrics generator functions and
66 // adding the generated metrics to the metrics map that underlies the MetricStore.
67 func (s *MetricsStore) Add(obj interface{}) error {
68 o, err := meta.Accessor(obj)
74 defer s.mutex.Unlock()
76 families := s.generateMetricsFunc(obj)
77 familyStrings := make([][]byte, len(families))
79 for i, f := range families {
80 familyStrings[i] = f.ByteSlice()
83 s.metrics[o.GetUID()] = familyStrings
88 // Update updates the existing entry in the MetricsStore.
89 func (s *MetricsStore) Update(obj interface{}) error {
90 // TODO: For now, just call Add, in the future one could check if the resource version changed?
94 // Delete deletes an existing entry in the MetricsStore.
95 func (s *MetricsStore) Delete(obj interface{}) error {
97 o, err := meta.Accessor(obj)
103 defer s.mutex.Unlock()
105 delete(s.metrics, o.GetUID())
110 // List implements the List method of the store interface.
111 func (s *MetricsStore) List() []interface{} {
115 // ListKeys implements the ListKeys method of the store interface.
116 func (s *MetricsStore) ListKeys() []string {
120 // Get implements the Get method of the store interface.
121 func (s *MetricsStore) Get(obj interface{}) (item interface{}, exists bool, err error) {
122 return nil, false, nil
125 // GetByKey implements the GetByKey method of the store interface.
126 func (s *MetricsStore) GetByKey(key string) (item interface{}, exists bool, err error) {
127 return nil, false, nil
130 // Replace will delete the contents of the store, using instead the
132 func (s *MetricsStore) Replace(list []interface{}, _ string) error {
134 s.metrics = map[types.UID][][]byte{}
137 for _, o := range list {
147 // Resync implements the Resync method of the store interface.
148 func (s *MetricsStore) Resync() error {
152 // WriteAll writes all metrics of the store into the given writer, zipped with the
153 // help text of each metric family.
154 func (s *MetricsStore) WriteAll(w io.Writer) {
156 defer s.mutex.RUnlock()
158 for i, help := range s.headers {
159 w.Write([]byte(help))
160 w.Write([]byte{'\n'})
161 for _, metricFamilies := range s.metrics {
162 w.Write([]byte(metricFamilies[i]))