Merge pull request #3789 from weaveworks/billing-use-container-info

multitenant: scan container command-lines as well as process
This commit is contained in:
Bryan Boreham
2020-05-19 12:17:08 +01:00
committed by GitHub
2 changed files with 52 additions and 15 deletions

View File

@@ -1,6 +1,7 @@
package multitenant
import (
"context"
"crypto/sha256"
"encoding/base64"
"flag"
@@ -9,7 +10,6 @@ import (
"sync"
"time"
"context"
log "github.com/sirupsen/logrus"
billing "github.com/weaveworks/billing-client"
@@ -119,6 +119,19 @@ func (e *BillingEmitter) Add(ctx context.Context, rep report.Report, buf []byte)
return e.Collector.Add(ctx, rep, buf)
}
func intervalFromCommand(cmd string) string {
if strings.Contains(cmd, "scope") &&
strings.Contains(cmd, "probe.publish.interval") {
cmds := strings.SplitAfter(cmd, "probe.publish.interval")
aft := strings.Split(cmds[1], " ")
if aft[0] == "" {
return aft[1]
}
return aft[0][1:]
}
return ""
}
// reportInterval tries to find the custom report interval of this report. If
// it is malformed, or not set, it returns zero.
func (e *BillingEmitter) reportInterval(r report.Report) time.Duration {
@@ -126,21 +139,20 @@ func (e *BillingEmitter) reportInterval(r report.Report) time.Duration {
return r.Window
}
var inter string
for _, c := range r.Process.Nodes {
cmd, ok := c.Latest.Lookup("cmdline")
if !ok {
continue
}
if strings.Contains(cmd, "scope") &&
strings.Contains(cmd, "probe.publish.interval") {
cmds := strings.SplitAfter(cmd, "probe.publish.interval")
aft := strings.Split(cmds[1], " ")
if aft[0] == "" {
inter = aft[1]
} else {
inter = aft[0][1:]
for _, c := range r.Container.Nodes {
if cmd, ok := c.Latest.Lookup(report.DockerContainerCommand); ok {
if inter = intervalFromCommand(cmd); inter != "" {
break
}
}
}
if inter == "" { // not found in containers: look in processes
for _, c := range r.Process.Nodes {
if cmd, ok := c.Latest.Lookup(report.Cmdline); ok {
if inter = intervalFromCommand(cmd); inter != "" {
break
}
}
}
}
if inter == "" {

View File

@@ -0,0 +1,25 @@
package multitenant
import "testing"
func Test_intervalFromCommand(t *testing.T) {
tests := []struct {
name string
cmd string
want string
}{
{cmd: "/home/weave/scope --mode=probe --probe-only --probe.kubernetes=true --probe.spy.interval=3s --probe.publish.interval=5s --probe.processes=false --probe.conntrack=false --probe.ebpf.connections=false --probe.docker.bridge=docker0 --probe.docker=true https://redacted@cloud.weave.works.", want: "5s", name: "seconds"},
{cmd: "/home/weave/scope --mode=probe --probe-only --probe.kubernetes=true --probe.spy.interval=3s --probe.publish.interval 5s --probe.processes=false --probe.conntrack=false --probe.ebpf.connections=false --probe.docker.bridge=docker0 --probe.docker=true https://redacted@cloud.weave.works.", want: "5s", name: "space"},
{cmd: "/home/weave/scope --mode=probe --no-app --probe.docker=true --probe.kubernetes.role=host --weave=false --probe.publish.interval=4500ms --probe.spy.interval=2s --probe.http.listen=:4041 --probe.conntrack.buffersize=4194304 https://redacted@cloud.weave.works scope.weave.svc.cluster.local:80", want: "4500ms", name: "miliseconds"},
{cmd: "/home/weave/scope --mode=probe --no-app --probe.docker=true --probe.kubernetes.role=host --weave=false --probe.spy.interval=2s --probe.http.listen=:4041 --probe.conntrack.buffersize=4194304 https://redacted@cloud.weave.works scope.weave.svc.cluster.local:80", want: "", name: "notset"},
{cmd: "/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --web.listen-address=:8080 --storage.tsdb.retention.time=2h --web.enable-lifecycle", want: "", name: "notscope"},
{cmd: "", want: "", name: "blank"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := intervalFromCommand(tt.cmd); got != tt.want {
t.Errorf("intervalFromCommand() = %v, want %v", got, tt.want)
}
})
}
}