Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / kube-state-metrics / pkg / metric / generator.go
1 /*
2 Copyright 2019 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 metric
18
19 import (
20         "strings"
21
22         metricsstore "k8s.io/kube-state-metrics/pkg/metrics_store"
23 )
24
25 // FamilyGenerator provides everything needed to generate a metric family with a
26 // Kubernetes object.
27 type FamilyGenerator struct {
28         Name         string
29         Help         string
30         Type         Type
31         GenerateFunc func(obj interface{}) *Family
32 }
33
34 // Generate calls the FamilyGenerator.GenerateFunc and gives the family its
35 // name. The reasoning behind injecting the name at such a late point in time is
36 // deduplication in the code, preventing typos made by developers as
37 // well as saving memory.
38 func (g *FamilyGenerator) Generate(obj interface{}) *Family {
39         family := g.GenerateFunc(obj)
40         family.Name = g.Name
41         return family
42 }
43
44 func (g *FamilyGenerator) generateHeader() string {
45         header := strings.Builder{}
46         header.WriteString("# HELP ")
47         header.WriteString(g.Name)
48         header.WriteByte(' ')
49         header.WriteString(g.Help)
50         header.WriteByte('\n')
51         header.WriteString("# TYPE ")
52         header.WriteString(g.Name)
53         header.WriteByte(' ')
54         header.WriteString(string(g.Type))
55
56         return header.String()
57 }
58
59 // ExtractMetricFamilyHeaders takes in a slice of FamilyGenerator metrics and
60 // returns the extracted headers.
61 func ExtractMetricFamilyHeaders(families []FamilyGenerator) []string {
62         headers := make([]string, len(families))
63
64         for i, f := range families {
65                 headers[i] = f.generateHeader()
66         }
67
68         return headers
69 }
70
71 // ComposeMetricGenFuncs takes a slice of metric families and returns a function
72 // that composes their metric generation functions into a single one.
73 func ComposeMetricGenFuncs(familyGens []FamilyGenerator) func(obj interface{}) []metricsstore.FamilyByteSlicer {
74         return func(obj interface{}) []metricsstore.FamilyByteSlicer {
75                 families := make([]metricsstore.FamilyByteSlicer, len(familyGens))
76
77                 for i, gen := range familyGens {
78                         families[i] = gen.Generate(obj)
79                 }
80
81                 return families
82         }
83 }
84
85 type whiteBlackLister interface {
86         IsIncluded(string) bool
87         IsExcluded(string) bool
88 }
89
90 // FilterMetricFamilies takes a white- and a blacklist and a slice of metric
91 // families and returns a filtered slice.
92 func FilterMetricFamilies(l whiteBlackLister, families []FamilyGenerator) []FamilyGenerator {
93         filtered := []FamilyGenerator{}
94
95         for _, f := range families {
96                 if l.IsIncluded(f.Name) {
97                         filtered = append(filtered, f)
98                 }
99         }
100
101         return filtered
102 }