Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / kube-state-metrics / pkg / metrics_store / metrics_store.go
1 /*
2 Copyright 2018 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 metricsstore
18
19 import (
20         "io"
21         "sync"
22
23         "k8s.io/apimachinery/pkg/api/meta"
24         "k8s.io/apimachinery/pkg/types"
25 )
26
27 // FamilyByteSlicer represents a metric family that can be converted to its string
28 // representation.
29 type FamilyByteSlicer interface {
30         ByteSlice() []byte
31 }
32
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 {
37         // Protects metrics
38         mutex sync.RWMutex
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().
47         headers []string
48
49         // generateMetricsFunc generates metrics based on a given Kubernetes object
50         // and returns them grouped by metric family.
51         generateMetricsFunc func(interface{}) []FamilyByteSlicer
52 }
53
54 // NewMetricsStore returns a new MetricsStore
55 func NewMetricsStore(headers []string, generateFunc func(interface{}) []FamilyByteSlicer) *MetricsStore {
56         return &MetricsStore{
57                 generateMetricsFunc: generateFunc,
58                 headers:             headers,
59                 metrics:             map[types.UID][][]byte{},
60         }
61 }
62
63 // Implementing k8s.io/client-go/tools/cache.Store interface
64
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)
69         if err != nil {
70                 return err
71         }
72
73         s.mutex.Lock()
74         defer s.mutex.Unlock()
75
76         families := s.generateMetricsFunc(obj)
77         familyStrings := make([][]byte, len(families))
78
79         for i, f := range families {
80                 familyStrings[i] = f.ByteSlice()
81         }
82
83         s.metrics[o.GetUID()] = familyStrings
84
85         return nil
86 }
87
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?
91         return s.Add(obj)
92 }
93
94 // Delete deletes an existing entry in the MetricsStore.
95 func (s *MetricsStore) Delete(obj interface{}) error {
96
97         o, err := meta.Accessor(obj)
98         if err != nil {
99                 return err
100         }
101
102         s.mutex.Lock()
103         defer s.mutex.Unlock()
104
105         delete(s.metrics, o.GetUID())
106
107         return nil
108 }
109
110 // List implements the List method of the store interface.
111 func (s *MetricsStore) List() []interface{} {
112         return nil
113 }
114
115 // ListKeys implements the ListKeys method of the store interface.
116 func (s *MetricsStore) ListKeys() []string {
117         return nil
118 }
119
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
123 }
124
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
128 }
129
130 // Replace will delete the contents of the store, using instead the
131 // given list.
132 func (s *MetricsStore) Replace(list []interface{}, _ string) error {
133         s.mutex.Lock()
134         s.metrics = map[types.UID][][]byte{}
135         s.mutex.Unlock()
136
137         for _, o := range list {
138                 err := s.Add(o)
139                 if err != nil {
140                         return err
141                 }
142         }
143
144         return nil
145 }
146
147 // Resync implements the Resync method of the store interface.
148 func (s *MetricsStore) Resync() error {
149         return nil
150 }
151
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) {
155         s.mutex.RLock()
156         defer s.mutex.RUnlock()
157
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]))
163                 }
164         }
165 }