/
cloudcostprops.go
214 lines (183 loc) · 6.05 KB
/
cloudcostprops.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
package kubecost
import (
"fmt"
"strings"
"github.com/tkennes/openghg/pkg/log"
)
const (
CloudCostInvoiceEntityIDProp string = "invoiceEntityID"
CloudCostAccountIDProp string = "accountID"
CloudCostProviderProp string = "provider"
CloudCostProviderIDProp string = "providerID"
CloudCostCategoryProp string = "category"
CloudCostServiceProp string = "service"
CloudCostLabelProp string = "label"
)
const (
// CloudCostClusterManagementCategory describes CloudCost representing Hosted Kubernetes Fees
CloudCostClusterManagementCategory string = "Cluster Management"
// CloudCostDiskCategory describes CloudCost representing Disk usage
CloudCostDiskCategory string = "Disk"
// CloudCostLoadBalancerCategory describes CloudCost representing Load Balancer usage
CloudCostLoadBalancerCategory string = "Load Balancer"
// CloudCostNetworkCategory describes CloudCost representing Network usage
CloudCostNetworkCategory string = "Network"
// CloudCostVirtualMachineCategory describes CloudCost representing VM usage
CloudCostVirtualMachineCategory string = "Virtual Machine"
// CloudCostOtherCategory describes CloudCost that do not belong to a defined category
CloudCostOtherCategory string = "Other"
)
type CloudCostLabels map[string]string
func (ccl CloudCostLabels) Clone() CloudCostLabels {
result := make(map[string]string, len(ccl))
for k, v := range ccl {
result[k] = v
}
return result
}
func (ccl CloudCostLabels) Equal(that CloudCostLabels) bool {
if len(ccl) != len(that) {
return false
}
// Maps are of equal length, so if all keys are in both maps, we don't
// have to check the keys of the other map.
for k, val := range ccl {
if thatVal, ok := that[k]; !ok || val != thatVal {
return false
}
}
return true
}
// Intersection returns the set of labels that have the same key and value in the receiver and arg
func (ccl CloudCostLabels) Intersection(that CloudCostLabels) CloudCostLabels {
intersection := make(map[string]string)
if len(ccl) == 0 || len(that) == 0 {
return intersection
}
// Pick the smaller of the two label sets
smallerLabels := ccl
largerLabels := that
if len(ccl) > len(that) {
smallerLabels = that
largerLabels = ccl
}
// Loop through the smaller label set
for k, sVal := range smallerLabels {
if lVal, ok := largerLabels[k]; ok && sVal == lVal {
intersection[k] = sVal
}
}
return intersection
}
type CloudCostProperties struct {
ProviderID string `json:"providerID,omitempty"`
Provider string `json:"provider,omitempty"`
AccountID string `json:"accountID,omitempty"`
InvoiceEntityID string `json:"invoiceEntityID,omitempty"`
Service string `json:"service,omitempty"`
Category string `json:"category,omitempty"`
Labels CloudCostLabels `json:"labels,omitempty"`
}
func (ccp *CloudCostProperties) Equal(that *CloudCostProperties) bool {
return ccp.ProviderID == that.ProviderID &&
ccp.Provider == that.Provider &&
ccp.AccountID == that.AccountID &&
ccp.InvoiceEntityID == that.InvoiceEntityID &&
ccp.Service == that.Service &&
ccp.Category == that.Category &&
ccp.Labels.Equal(that.Labels)
}
func (ccp *CloudCostProperties) Clone() *CloudCostProperties {
return &CloudCostProperties{
ProviderID: ccp.ProviderID,
Provider: ccp.Provider,
AccountID: ccp.AccountID,
InvoiceEntityID: ccp.InvoiceEntityID,
Service: ccp.Service,
Category: ccp.Category,
Labels: ccp.Labels.Clone(),
}
}
// Intersection ensure the values of two CloudCostAggregateProperties are maintain only if they are equal
func (ccp *CloudCostProperties) Intersection(that *CloudCostProperties) *CloudCostProperties {
if ccp == nil || that == nil {
return nil
}
if ccp.Equal(that) {
return ccp
}
intersectionCCP := &CloudCostProperties{}
if ccp.Equal(intersectionCCP) || that.Equal(intersectionCCP) {
return intersectionCCP
}
if ccp.Provider == that.Provider {
intersectionCCP.Provider = ccp.Provider
}
if ccp.ProviderID == that.ProviderID {
intersectionCCP.ProviderID = ccp.ProviderID
}
if ccp.AccountID == that.AccountID {
intersectionCCP.AccountID = ccp.AccountID
}
if ccp.InvoiceEntityID == that.InvoiceEntityID {
intersectionCCP.InvoiceEntityID = ccp.InvoiceEntityID
}
if ccp.Service == that.Service {
intersectionCCP.Service = ccp.Service
}
if ccp.Category == that.Category {
intersectionCCP.Category = ccp.Category
}
intersectionCCP.Labels = ccp.Labels.Intersection(that.Labels)
return intersectionCCP
}
func (ccp *CloudCostProperties) GenerateKey(props []string) string {
if len(props) == 0 {
return fmt.Sprintf("%s/%s/%s/%s/%s/%s", ccp.Provider, ccp.InvoiceEntityID, ccp.AccountID, ccp.Category, ccp.Service, ccp.ProviderID)
}
values := make([]string, len(props))
for i, prop := range props {
propVal := UnallocatedSuffix
switch true {
case prop == CloudCostProviderProp:
if ccp.Provider != "" {
propVal = ccp.Provider
}
case prop == CloudCostProviderIDProp:
if ccp.ProviderID != "" {
propVal = ccp.ProviderID
}
case prop == CloudCostCategoryProp:
if ccp.Category != "" {
propVal = ccp.Category
}
case prop == CloudCostInvoiceEntityIDProp:
if ccp.InvoiceEntityID != "" {
propVal = ccp.InvoiceEntityID
}
case prop == CloudCostAccountIDProp:
if ccp.AccountID != "" {
propVal = ccp.AccountID
}
case prop == CloudCostServiceProp:
if ccp.Service != "" {
propVal = ccp.Service
}
case strings.HasPrefix(prop, "label:"):
labels := ccp.Labels
if labels != nil {
labelName := strings.TrimPrefix(prop, "label:")
if labelValue, ok := labels[labelName]; ok && labelValue != "" {
propVal = labelValue
}
}
default:
// This case should never be reached, as input up until this point
// should be checked and rejected if invalid. But if we do get a
// value we don't recognize, log a warning.
log.Warnf("CloudCost: GenerateKey: illegal aggregation parameter: %s", prop)
}
values[i] = propVal
}
return strings.Join(values, "/")
}