// Package `all` contains the billman CLI for calculating // log and metric billing data. package all import ( "context" "flag" "github.com/peterbourgon/ff/v3/ffcli" "edge-infra.dev/pkg/edge/monitoring/billman/cmd" "edge-infra.dev/pkg/edge/monitoring/billman/costs" "edge-infra.dev/pkg/edge/monitoring/billman/gcp" ) const ( name = "all" container = "k8s_container" node = "k8s_node" ) // config for the "all" subcommand type config struct { RootConfig *cmd.Config DisplayType string } func New(rootConfig *cmd.Config) *ffcli.Command { cfg := config{ RootConfig: rootConfig, DisplayType: name, } fs := flag.NewFlagSet("billman "+name, flag.ExitOnError) return &ffcli.Command{ Name: name, ShortUsage: "billman " + name + " [flags]", FlagSet: cmd.WithGlobalFlags(fs), Exec: cfg.Exec, } } // Exec runs the `billman all` command. Both log and // metric data will be calculated. func (c *config) Exec(context.Context, []string) error { err := cmd.CheckRequiredFlags(cmd.GlobalFlags) if err != nil { return err } err = cmd.CheckPeriod(c.RootConfig.Period) if err != nil { return err } bytesContainer, err := gcp.GetLogBytes(c.RootConfig.TopLevelProjectID, c.RootConfig.ClusterID, c.RootConfig.Period, container) if err != nil { return err } bytesNode, err := gcp.GetLogBytes(c.RootConfig.TopLevelProjectID, c.RootConfig.ClusterID, c.RootConfig.Period, node) if err != nil { return err } lcd := costs.LogContainerData{ Bytes: bytesContainer, Cost: costs.LogCosts(bytesContainer, c.RootConfig.LoggingRate), } lnd := costs.LogNodeData{ Bytes: bytesNode, Cost: costs.LogCosts(bytesNode, c.RootConfig.LoggingRate), } ld := costs.LogData{ Bytes: lcd.Bytes + lnd.Bytes, Cost: lcd.Cost + lnd.Cost, } samples, err := gcp.GetMetricSamples(c.RootConfig.TopLevelProjectID, c.RootConfig.ClusterID, c.RootConfig.Period) if err != nil { return err } md := costs.MetricData{ Samples: samples, Cost: costs.MetricCosts(samples, c.RootConfig.MetricsRate), } b := costs.Billing{ Cluster: c.RootConfig.Cluster, LogContainerData: lcd, LogNodeData: lnd, LogData: ld, MetricData: md, Options: c.RootConfig.Options, DisplayType: c.DisplayType, } costs.PrintClusterCosts(b) return nil }