/
meterreport_types.go
439 lines (374 loc) · 14.1 KB
/
meterreport_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2020 IBM Corp.
//
// 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 v1alpha1
import (
"fmt"
"strconv"
"emperror.dev/errors"
"github.com/redhat-marketplace/redhat-marketplace-operator/v2/apis/marketplace/common"
"github.com/redhat-marketplace/redhat-marketplace-operator/v2/apis/marketplace/v1beta1"
status "github.com/redhat-marketplace/redhat-marketplace-operator/v2/pkg/utils/status"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
// MeterReportSpec defines the desired state of MeterReport
// +k8s:openapi-gen=true
type MeterReportSpec struct {
// ReportUUID is the generated ID for the report.
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
ReportUUID string `json:"reportUUID,omitempty"`
// StartTime of the job
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
StartTime metav1.Time `json:"startTime"`
// EndTime of the job
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
EndTime metav1.Time `json:"endTime"`
// LabelSelectors are used to filter to the correct workload.
// DEPRECATED
// +optional
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
LabelSelector metav1.LabelSelector `json:"labelSelector,omitempty"`
// PrometheusService is the definition for the service labels.
// DEPRECATED
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
// +optional
PrometheusService *common.ServiceReference `json:"prometheusService,omitempty"`
// MeterDefinitions is the list of meterDefinitions included in the report
// DEPRECATED
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
// +optional
MeterDefinitions []MeterDefinition `json:"meterDefinitions,omitempty"`
// MeterDefinitionReferences are used as the first meter definition source. Prometheus data is used to supplement.
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
// +listType:=map
// +listMapKey:=name
// +listMapKey:=namespace
// +optional
MeterDefinitionReferences []v1beta1.MeterDefinitionReference `json:"meterDefinitionReferences,omitempty"`
// ExtraArgs is a set of arguments to pass to the job
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors.x-descriptors="hidden"
// +optional
ExtraArgs []string `json:"extraJobArgs,omitempty"`
}
// MeterReportStatus defines the observed state of MeterReport
type MeterReportStatus struct {
// Conditions represent the latest available observations of an object's stateonfig
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors.x-descriptors="urn:alm:descriptor:io.kubernetes.conditions"
Conditions status.Conditions `json:"conditions,omitempty"`
// A list of pointers to currently running jobs.
// DEPRECATED
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +optional
AssociatedJob *common.JobReference `json:"jobReference,omitempty"`
// UploadStatus displays the last status for upload targets.
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
// +optional
UploadStatus UploadDetailConditions `json:"uploadStatus,omitempty"`
// WorkloadCount is the number of workloads reported on
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +optional
WorkloadCount *int `json:"workloadCount,omitempty"`
// MetricUploadCount is the number of metrics in the report
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +optional
MetricUploadCount *int `json:"metricUploadCount,omitempty"`
// UploadAttempts track the number of times a file has failed due to
// unrecoverable errors
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +optional
RetryUpload int `json:"uploadAttempts,omitempty"`
// UploadID is the ID associated with the upload
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +optional
UploadID *types.UID `json:"uploadUID,omitempty"`
// DataServiceStatus is the status of the report stored in data service
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +optional
DataServiceStatus *UploadDetails `json:"dataServiceStatus,omitempty"`
// Errors shows if there were any errors from queries
// for the report.
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +optional
Errors []ErrorDetails `json:"errors,omitempty"`
// Warnings from the job
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +optional
Warnings []ErrorDetails `json:"warnings,omitempty"`
}
func (stat *MeterReportStatus) IsStored() bool {
if cond := stat.Conditions.GetCondition(ReportConditionTypeStorageStatus); cond != nil {
return cond.IsTrue()
}
return false
}
func (stat *MeterReportStatus) IsUploaded() bool {
if cond := stat.Conditions.GetCondition(ReportConditionTypeUploadStatus); cond != nil {
return cond.IsTrue()
}
return false
}
type UploadStatus string
const (
UploadStatusSuccess UploadStatus = "success"
UploadStatusFailure UploadStatus = "failure"
)
func (a *UploadStatus) UnmarshalJSON(b []byte) error {
str, err := strconv.Unquote(string(b))
if err != nil {
return err
}
*a = UploadStatus(str)
return nil
}
func (a UploadStatus) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(string(a))), nil
}
func (a UploadStatus) String() string {
return string(a)
}
// UploadDetails provides details about uploads for the meterreport
type UploadDetails struct {
// Target is the upload target
Target string `json:"target"`
// ID is the upload id
ID string `json:"id,omitempty"`
// Status is the current status
Status UploadStatus `json:"status"`
// Error is present if an error occurred on upload
Error string `json:"error,omitempty"`
}
func (u UploadDetails) Success() bool {
return u.Status == UploadStatusSuccess
}
func (u UploadDetails) Err() error {
if u.Error == "" {
return nil
}
return errors.New(u.Error)
}
type UploadDetailConditions []*UploadDetails
func (u *UploadDetailConditions) Append(conds UploadDetailConditions) {
if u == nil {
u = &UploadDetailConditions{}
}
for j := range conds {
cond := conds[j]
u.Set(*cond)
}
}
func (u *UploadDetailConditions) Set(cond UploadDetails) {
if u == nil {
u = &UploadDetailConditions{}
}
for i := range *u {
if (*u)[i].Target == cond.Target {
(*u)[i] = &cond
return
}
}
*u = append(*u, &cond)
}
func (u UploadDetailConditions) Get(target string) *UploadDetails {
for _, status := range u {
if status != nil && status.Target == target {
return status
}
}
return nil
}
func (u UploadDetailConditions) OneSuccessOf(targets []string) bool {
for _, target := range targets {
status := u.Get(target)
if status != nil && status.Target == target {
if status.Status == UploadStatusSuccess {
return true
}
}
}
return false
}
func (u UploadDetailConditions) AllSuccesses() bool {
if len(u) == 0 {
return false
}
for _, status := range u {
if status != nil && !status.Success() {
return false
}
}
return true
}
func (u UploadDetailConditions) Errors() (err error) {
for _, status := range u {
if status != nil && !status.Success() {
err = errors.Append(err, errors.Errorf("target:%s error:%s", status.Target, status.Error))
return
}
}
return
}
// ErrorDetails provides details about errors that happen in the job
type ErrorDetails struct {
// Reason the error occurred
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +optional
Reason string `json:"reason"`
// Details of the error
// +operator-sdk:gen-csv:customresourcedefinitions.statusDescriptors=true
// +optional
Details map[string]string `json:"details,omitempty"`
}
func (e ErrorDetails) FromError(err error) ErrorDetails {
detailsMap := map[string]string{}
details := errors.GetDetails(err)
for i := 0; i < len(details); i = i + 2 {
key := fmt.Sprintf("%v", details[i])
value := fmt.Sprintf("%+v", details[i+1])
detailsMap[key] = value
}
e.Details = detailsMap
e.Reason = errors.Cause(err).Error()
return e
}
const (
ReportConditionTypeJobRunning status.ConditionType = "JobRunning"
ReportConditionReasonJobSubmitted status.ConditionReason = "Submitted"
ReportConditionReasonJobNotStarted status.ConditionReason = "NotStarted"
ReportConditionReasonJobWaiting status.ConditionReason = "Waiting"
ReportConditionReasonJobFinished status.ConditionReason = "Finished"
ReportConditionReasonJobErrored status.ConditionReason = "Errored"
ReportConditionReasonJobIsDisconnected status.ConditionReason = "Disconn"
ReportConditionReasonJobNoData status.ConditionReason = "NoData"
ReportConditionReasonJobSkipped status.ConditionReason = "Skipped"
ReportConditionReasonJobMaxRetries status.ConditionReason = "MaxRetries"
ReportConditionTypeStorageStatus status.ConditionType = "Stored"
ReportConditionTypeUploadStatus status.ConditionType = "Uploaded"
ReportConditionReasonUploadStatusFinished status.ConditionReason = "Finished"
ReportConditionReasonUploadStatusNotStarted status.ConditionReason = "NotStarted"
ReportConditionReasonUploadStatusErrored status.ConditionReason = "Errored"
)
var (
ReportConditionJobNotStarted = status.Condition{
Type: ReportConditionTypeJobRunning,
Status: corev1.ConditionFalse,
Reason: ReportConditionReasonJobNotStarted,
Message: "Job has not been started",
}
ReportConditionJobSubmitted = status.Condition{
Type: ReportConditionTypeJobRunning,
Status: corev1.ConditionTrue,
Reason: ReportConditionReasonJobSubmitted,
Message: "Job has been submitted",
}
ReportConditionJobWaiting = status.Condition{
Type: ReportConditionTypeJobRunning,
Status: corev1.ConditionFalse,
Reason: ReportConditionReasonJobWaiting,
Message: "Report end time has not progressed.",
}
ReportConditionJobFinished = status.Condition{
Type: ReportConditionTypeJobRunning,
Status: corev1.ConditionFalse,
Reason: ReportConditionReasonJobFinished,
Message: "Job has finished",
}
ReportConditionJobErrored = status.Condition{
Type: ReportConditionTypeJobRunning,
Status: corev1.ConditionFalse,
Reason: ReportConditionReasonJobErrored,
Message: "Job has errored",
}
ReportConditionStorageStatusFinished = status.Condition{
Type: ReportConditionTypeStorageStatus,
Status: corev1.ConditionTrue,
Reason: ReportConditionReasonUploadStatusFinished,
}
ReportConditionStorageStatusUnknown = status.Condition{
Type: ReportConditionTypeStorageStatus,
Status: corev1.ConditionUnknown,
Reason: ReportConditionReasonUploadStatusNotStarted,
}
ReportConditionStorageStatusErrored = status.Condition{
Type: ReportConditionTypeStorageStatus,
Status: corev1.ConditionFalse,
Reason: ReportConditionReasonUploadStatusErrored,
}
ReportConditionJobIsDisconnected = status.Condition{
Type: ReportConditionTypeUploadStatus,
Status: corev1.ConditionFalse,
Reason: ReportConditionReasonJobIsDisconnected,
Message: "Report is running in a disconnected environment",
}
ReportConditionJobHasNoData = status.Condition{
Type: ReportConditionTypeUploadStatus,
Status: corev1.ConditionFalse,
Reason: ReportConditionReasonJobNoData,
Message: "Report has no data",
}
ReportConditionJobSkipped = status.Condition{
Type: ReportConditionTypeUploadStatus,
Status: corev1.ConditionFalse,
Reason: ReportConditionReasonJobSkipped,
}
ReportConditionFailedAttempts = status.Condition{
Type: ReportConditionTypeUploadStatus,
Status: corev1.ConditionFalse,
Reason: ReportConditionReasonJobMaxRetries,
}
ReportConditionUploadStatusFinished = status.Condition{
Type: ReportConditionTypeUploadStatus,
Status: corev1.ConditionTrue,
Reason: ReportConditionReasonUploadStatusFinished,
}
ReportConditionUploadStatusUnknown = status.Condition{
Type: ReportConditionTypeUploadStatus,
Status: corev1.ConditionUnknown,
Reason: ReportConditionReasonUploadStatusNotStarted,
}
ReportConditionUploadStatusErrored = status.Condition{
Type: ReportConditionTypeUploadStatus,
Status: corev1.ConditionFalse,
Reason: ReportConditionReasonUploadStatusErrored,
}
)
// +kubebuilder:object:root=true
// MeterReport is the Schema for the meterreports API
// +kubebuilder:subresource:status
// +kubebuilder:resource:path=marketplaceconfigs,scope=Namespaced
// +kubebuilder:printcolumn:name="METRICS",type=string,JSONPath=`.status.metricUploadCount`
// +kubebuilder:printcolumn:name="STORED",type=string,JSONPath=`.status.conditions[?(@.type == "Stored")].status`
// +kubebuilder:printcolumn:name="STORED_REASON",type=string,JSONPath=`.status.conditions[?(@.type == "Stored")].reason`
// +kubebuilder:printcolumn:name="UPLOADED",type=string,JSONPath=`.status.conditions[?(@.type == "Uploaded")].status`
// +kubebuilder:printcolumn:name="UPLOADED_REASON",type=string,JSONPath=`.status.conditions[?(@.type == "Uploaded")].reason`
// +operator-sdk:gen-csv:customresourcedefinitions.displayName="Reports"
// +kubebuilder:resource:path=meterreports,scope=Namespaced
type MeterReport struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec MeterReportSpec `json:"spec,omitempty"`
Status MeterReportStatus `json:"status,omitempty"`
}
// +kubebuilder:object:root=true
// MeterReportList contains a list of MeterReport
type MeterReportList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []MeterReport `json:"items"`
}
func init() {
SchemeBuilder.Register(&MeterReport{}, &MeterReportList{})
}