generated from kubernetes/kubernetes-template-project
/
types.go
229 lines (214 loc) · 7.6 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2023 The Kubernetes Authors.
//
// 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 collector
import (
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/pointer"
"sigs.k8s.io/usage-metrics-collector/pkg/api/collectorcontrollerv1alpha1"
)
func (c *Collector) init() error {
if c.MetricsPrometheusCollector == nil {
return errors.Errorf("must specify MetricsPrometheusCollector")
}
c.defaultMetricsPrometheusCollector()
sccfg, err := c.getSideCarConfigs()
if err != nil {
return err
}
c.sideCarConfigs = sccfg
c.labler.Extension.SideCar = sccfg
// initialize internal IDs
var labelId int
for i := range c.MetricsPrometheusCollector.Extensions.Pods {
c.MetricsPrometheusCollector.Extensions.Pods[i].ID = collectorcontrollerv1alpha1.LabelId(labelId)
labelId++
}
for i := range c.MetricsPrometheusCollector.Extensions.Quota {
c.MetricsPrometheusCollector.Extensions.Quota[i].ID = collectorcontrollerv1alpha1.LabelId(labelId)
labelId++
}
for i := range c.MetricsPrometheusCollector.Extensions.Nodes {
c.MetricsPrometheusCollector.Extensions.Nodes[i].ID = collectorcontrollerv1alpha1.LabelId(labelId)
labelId++
}
for i := range c.MetricsPrometheusCollector.Extensions.Namespaces {
c.MetricsPrometheusCollector.Extensions.Namespaces[i].ID = collectorcontrollerv1alpha1.LabelId(labelId)
labelId++
}
for i := range c.MetricsPrometheusCollector.Extensions.NodeTaints {
c.MetricsPrometheusCollector.Extensions.NodeTaints[i].ID = collectorcontrollerv1alpha1.LabelId(labelId)
labelId++
}
for i := range c.MetricsPrometheusCollector.Extensions.PVCs {
c.MetricsPrometheusCollector.Extensions.PVCs[i].ID = collectorcontrollerv1alpha1.LabelId(labelId)
labelId++
}
for i := range c.MetricsPrometheusCollector.Extensions.PVs {
c.MetricsPrometheusCollector.Extensions.PVs[i].ID = collectorcontrollerv1alpha1.LabelId(labelId)
labelId++
}
for i := range c.sideCarConfigs {
for j := range c.sideCarConfigs[i].Labels {
c.sideCarConfigs[i].Labels[j].ID = collectorcontrollerv1alpha1.LabelId(labelId)
labelId++
}
}
var maskId int
for i := range c.MetricsPrometheusCollector.Aggregations {
for j := range c.MetricsPrometheusCollector.Aggregations[i].Levels {
c.MetricsPrometheusCollector.Aggregations[i].Levels[j].Mask.ID = collectorcontrollerv1alpha1.LabelsMaskId(maskId)
maskId++
}
}
// setup indexes
c.initExtensionLabelIndexes()
for i := range c.Aggregations {
for j := range c.Aggregations[i].Levels {
c.initInternalLabelsMask(&c.Aggregations[i].Levels[j].Mask, c.Extensions)
}
}
if c.SaveSamplesLocally != nil {
for i := range c.SaveSamplesLocally.SampleSources {
c.initInternalLabelsMask(&c.SaveSamplesLocally.SampleSources[i].Mask, c.Extensions)
}
}
if err := c.validateMetricsPrometheusCollector(); err != nil {
return err
}
c.labler.BuiltIn.UseQuotaNameForPriorityClass = c.BuiltIn.UseQuotaNameForPriorityClass
c.labler.Extension.Extensions = c.Extensions
return nil
}
func (c *Collector) defaultMetricsPrometheusCollector() {
c.Kind = "MetricsPrometheusCollector"
c.APIVersion = "v1alpha1"
for i := range c.Aggregations {
for j := range c.Aggregations[i].Levels {
if c.Aggregations[i].Levels[j].Operation == "" {
c.Aggregations[i].Levels[j].Operation = collectorcontrollerv1alpha1.SumOperation
}
}
}
if c.SaveSamplesLocally != nil {
if c.SaveSamplesLocally.DirectoryPath == "" {
c.SaveSamplesLocally.DirectoryPath = collectorcontrollerv1alpha1.DefaultSamplesLocalDirectoryPath
}
if c.SaveSamplesLocally.TimeFormat == "" {
c.SaveSamplesLocally.TimeFormat = collectorcontrollerv1alpha1.DefaultSamplesTimeFormat
}
if c.SaveSamplesLocally.SaveJSON == nil && c.SaveSamplesLocally.SaveProto == nil {
c.SaveSamplesLocally.SaveProto = pointer.BoolPtr(true)
}
}
}
func (c *Collector) validateMetricsPrometheusCollector() error {
resourceAlias := sets.NewString()
for _, v := range c.Resources {
resourceAlias.Insert(v)
}
if !c.PreComputeMetrics.Enabled && c.SaveSamplesLocally != nil {
return errors.Errorf("must use preComputeMetrics with saveSamplesLocally")
}
for i, a := range c.Aggregations {
for _, s := range a.Sources.Container {
if !collectorcontrollerv1alpha1.ContainerSources.Has(s) {
return errors.Errorf("invalid container source '%s'", s)
}
}
for _, s := range a.Sources.Pod {
if !collectorcontrollerv1alpha1.PodSources.Has(s) {
return errors.Errorf("invalid pod source '%s'", s)
}
}
for _, s := range a.Sources.Node {
if !collectorcontrollerv1alpha1.NodeSources.Has(s) {
return errors.Errorf("invalid node source '%s'", s)
}
}
for _, s := range a.Sources.CGroup {
found := func() bool {
if c.CGroupMetrics.RootSource.Name == s {
return true
}
for _, c := range c.CGroupMetrics.Sources {
if c.Name == s {
return true
}
}
return false
}()
if !found {
return errors.Errorf("invalid cgroup source '%s'", s)
}
}
for _, s := range a.Sources.Quota {
if !collectorcontrollerv1alpha1.QuotaSources.Has(s) {
return errors.Errorf("invalid quota source '%s'", s)
}
}
for _, s := range a.Sources.PV {
if !collectorcontrollerv1alpha1.PVSources.Has(s) {
return errors.Errorf("invalid pv source '%s'", s)
}
}
for _, s := range a.Sources.PVC {
if !collectorcontrollerv1alpha1.PVCSources.Has(s) {
return errors.Errorf("invalid pvc source '%s'", s)
}
}
for _, s := range a.Sources.Namespace {
if !collectorcontrollerv1alpha1.NamespaceSources.Has(s) {
return errors.Errorf("invalid namespace source '%s'", s)
}
}
if !collectorcontrollerv1alpha1.Types.Has(a.Sources.Type) {
return errors.Errorf("invalid aggregation type '%s'", a.Sources.Type)
}
if len(a.Levels) == 0 {
return errors.Errorf("aggregation %v missing levels", i)
}
if a.Sources.Type == "container" {
// the container utilization source has multiple values over time which should never be summed
sources := sets.NewString(a.Sources.GetSources()...)
if sources.Has(collectorcontrollerv1alpha1.ContainerUtilizationSource) &&
InvalidContainerUtilizationInitialLevelOperations.Has(string(a.Levels[0].Operation)) {
return errors.Errorf("initial operation for aggregation %v must not be 'sum' if utilization source is used", i)
}
}
for _, l := range a.Levels {
if !collectorcontrollerv1alpha1.Operations.Has(string(l.Operation)) {
return errors.Errorf("invalid aggregation level operation '%s'", l.Operation)
}
if l.Mask.Level == "" {
return errors.Errorf("level missing name %v", l)
}
for k := range l.HistogramBuckets {
if !resourceAlias.Has(k) {
return errors.Errorf("unknown histogramBucket key %v, must be one of %v", k, resourceAlias.List())
}
}
}
}
for _, t := range c.Extensions.NodeTaints {
if t.LabelName == "" {
return errors.Errorf("nodeTaint extension missing name '%v'", t)
}
if len(t.TaintKeys) == 0 && len(t.TaintValues) == 0 && len(t.TaintEffects) == 0 {
return errors.Errorf("nodeTaint extent requires at least one of keys, values, effects to be specified")
}
}
return nil
}
var InvalidContainerUtilizationInitialLevelOperations = sets.NewString("sum", "")