Skip to content

Commit aa0aab4

Browse files
committed
multitenant: only count real hosts for billing
PR weaveworks#3822 made the kubernetes probe send host nodes, so we need to exclude them from counting when billing.
1 parent 63109b9 commit aa0aab4

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

app/multitenant/billing_emitter.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (e *BillingEmitter) Add(ctx context.Context, rep report.Report, buf []byte)
6464
}
6565
rowKey, colKey := calculateDynamoKeys(userID, now)
6666

67-
interval := e.reportInterval(rep)
67+
interval, nodes := e.scanReport(rep)
6868
// Cache the last-known value of interval for this user, and use
6969
// it if we didn't find one in this report.
7070
e.Lock()
@@ -78,7 +78,7 @@ func (e *BillingEmitter) Add(ctx context.Context, rep report.Report, buf []byte)
7878
}
7979
}
8080
// Billing takes an integer number of seconds, so keep track of the amount lost to rounding
81-
nodeSeconds := interval.Seconds()*float64(len(rep.Host.Nodes)) + e.rounding[userID]
81+
nodeSeconds := interval.Seconds()*float64(nodes) + e.rounding[userID]
8282
rounding := nodeSeconds - math.Floor(nodeSeconds)
8383
e.rounding[userID] = rounding
8484
e.Unlock()
@@ -151,11 +151,20 @@ func intervalFromCommand(cmd string) string {
151151
return ""
152152
}
153153

154-
// reportInterval tries to find the custom report interval of this report. If
155-
// it is malformed, or not set, it returns zero.
156-
func (e *BillingEmitter) reportInterval(r report.Report) time.Duration {
154+
// scanReport counts the nodes tries to find any custom report interval
155+
// of this report. If it is malformed, or not set, it returns zero.
156+
func (e *BillingEmitter) scanReport(r report.Report) (time.Duration, int) {
157+
nHosts := 0
158+
// We scan the host nodes looking for ones reported by a per-node probe;
159+
// the Kubernetes cluster probe also makes host nodes but they only have a few fields set
160+
for _, h := range r.Host.Nodes {
161+
// Relying here on Uptime being something that changes in each report, hence will be in a delta report
162+
if _, ok := h.Latest.Lookup(report.Uptime); ok {
163+
nHosts++
164+
}
165+
}
157166
if r.Window != 0 {
158-
return r.Window
167+
return r.Window, nHosts
159168
}
160169
var inter string
161170
for _, c := range r.Container.Nodes {
@@ -175,13 +184,13 @@ func (e *BillingEmitter) reportInterval(r report.Report) time.Duration {
175184
}
176185
}
177186
if inter == "" {
178-
return 0
187+
return 0, nHosts
179188
}
180189
d, err := time.ParseDuration(inter)
181190
if err != nil {
182-
return 0
191+
return 0, nHosts
183192
}
184-
return d
193+
return d, nHosts
185194
}
186195

187196
// Tries to determine if this report came from a host running Weave Net

0 commit comments

Comments
 (0)