Skip to content

Commit e448dd6

Browse files
authored
Remove all references to OpenCost (#36)
Now that the v0.0.5 has been tested out in production, we can remove all references to OpenCost. This removes the flags, configuration, and branches in the codebase. There is still one query that references OpenCost, but that can be removed in a future patch once cloudcost-exporter emits Azure persistent volume costs.
1 parent 658bee6 commit e448dd6

File tree

4 files changed

+30
-90
lines changed

4 files changed

+30
-90
lines changed

cmd/bot/config.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ type config struct {
3131

3232
GitHub github.Config
3333

34-
IsCI bool `envconfig:"CI"`
35-
PR int `envconfig:"GITHUB_PULL_REQUEST" required:"true"`
36-
Event string `envconfig:"GITHUB_EVENT_NAME"`
37-
LogLevel string `envconfig:"LOG_LEVEL" default:"info"`
38-
UseCloudCostExporterMetrics bool `envconfig:"USE_CLOUD_COST_EXPORTER" default:"false"`
34+
IsCI bool `envconfig:"CI"`
35+
PR int `envconfig:"GITHUB_PULL_REQUEST" required:"true"`
36+
Event string `envconfig:"GITHUB_EVENT_NAME"`
37+
LogLevel string `envconfig:"LOG_LEVEL" default:"info"`
3938
}
4039

4140
const pullRequestEvent = "pull_request"

cmd/bot/main.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,16 @@ func realMain(ctx context.Context) error {
5656

5757
prometheusClients, err := costmodel.NewClients(
5858
&costmodel.ClientConfig{
59-
Address: cfg.Prometheus.Prod.Address,
60-
HTTPConfigFile: cfg.Prometheus.Prod.HTTPConfigFile,
61-
Username: cfg.Prometheus.Prod.Username,
62-
Password: cfg.Prometheus.Prod.Password,
63-
UseCloudCostExporterMetrics: cfg.UseCloudCostExporterMetrics,
59+
Address: cfg.Prometheus.Prod.Address,
60+
HTTPConfigFile: cfg.Prometheus.Prod.HTTPConfigFile,
61+
Username: cfg.Prometheus.Prod.Username,
62+
Password: cfg.Prometheus.Prod.Password,
6463
},
6564
&costmodel.ClientConfig{
66-
Address: cfg.Prometheus.Dev.Address,
67-
HTTPConfigFile: cfg.Prometheus.Dev.HTTPConfigFile,
68-
Username: cfg.Prometheus.Dev.Username,
69-
Password: cfg.Prometheus.Dev.Password,
70-
UseCloudCostExporterMetrics: cfg.UseCloudCostExporterMetrics,
65+
Address: cfg.Prometheus.Dev.Address,
66+
HTTPConfigFile: cfg.Prometheus.Dev.HTTPConfigFile,
67+
Username: cfg.Prometheus.Dev.Username,
68+
Password: cfg.Prometheus.Dev.Password,
7169
})
7270
if err != nil {
7371
return fmt.Errorf("creating cost model client: %w", err)

cmd/estimator/main.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,25 @@ import (
1111

1212
func main() {
1313
var fromFile, toFile, prometheusAddress, httpConfigFile, reportType, username, password string
14-
var useCloudCostExporterMetrics bool
1514
flag.StringVar(&fromFile, "from", "", "The file to compare from")
1615
flag.StringVar(&toFile, "to", "", "The file to compare to")
1716
flag.StringVar(&prometheusAddress, "prometheus.address", "http://localhost:9093/prometheus", "The Address of the prometheus server")
1817
flag.StringVar(&httpConfigFile, "http.config.file", "", "The path to the http config file")
1918
flag.StringVar(&username, "username", "", "Mimir username")
2019
flag.StringVar(&password, "password", "", "Mimir password")
2120
flag.StringVar(&reportType, "report.type", "table", "The type of report to generate. Options are: table, summary")
22-
flag.BoolVar(&useCloudCostExporterMetrics, "use.cloud.cost.exporter.metrics", false, "Whether to use the cloud cost exporter metrics")
2321
flag.Parse()
2422

2523
clusters := flag.Args()
2624

2725
ctx := context.Background()
28-
if err := run(ctx, fromFile, toFile, prometheusAddress, httpConfigFile, reportType, username, password, clusters, useCloudCostExporterMetrics); err != nil {
26+
if err := run(ctx, fromFile, toFile, prometheusAddress, httpConfigFile, reportType, username, password, clusters); err != nil {
2927
fmt.Printf("Could not run: %s\n", err)
3028
os.Exit(1)
3129
}
3230
}
3331

34-
func run(ctx context.Context, fromFile, toFile, address, httpConfigFile, reportType, username, password string, clusters []string, useCloudCostExporterMetrics bool) error {
32+
func run(ctx context.Context, fromFile, toFile, address, httpConfigFile, reportType, username, password string, clusters []string) error {
3533
from, err := os.ReadFile(fromFile)
3634
if err != nil {
3735
return fmt.Errorf("could not read file: %s", err)
@@ -44,11 +42,10 @@ func run(ctx context.Context, fromFile, toFile, address, httpConfigFile, reportT
4442
}
4543

4644
client, err := costmodel.NewClient(&costmodel.ClientConfig{
47-
Address: address,
48-
HTTPConfigFile: httpConfigFile,
49-
Username: username,
50-
Password: password,
51-
UseCloudCostExporterMetrics: useCloudCostExporterMetrics,
45+
Address: address,
46+
HTTPConfigFile: httpConfigFile,
47+
Username: username,
48+
Password: password,
5249
})
5350

5451
if err != nil {

pkg/costmodel/client.go

+12-66
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,7 @@ import (
1616
)
1717

1818
const (
19-
queryCostPerCPU = `
20-
avg by (spot) (node_cpu_hourly_cost{cluster="%s"}
21-
* on (cluster, node) group_left(spot)
22-
group by (cluster, node, spot) (
23-
label_replace(
24-
label_join(kubecost_node_is_spot == 1, "node", "", "exported_instance")
25-
,"spot", "true", "", ""
26-
)
27-
or on (cluster, node)
28-
label_replace(
29-
label_join(kubecost_node_is_spot == 0, "node", "", "exported_instance")
30-
,"spot", "false", "", ""
31-
)
32-
)
33-
)
34-
`
35-
cloudcostExporterQueryCostPerCpu = `
19+
queryCostPerCpu = `
3620
avg by (price_tier) (
3721
cloudcost_aws_ec2_instance_cpu_usd_per_core_hour{cluster_name="%s"}
3822
or
@@ -41,24 +25,7 @@ avg by (spot) (node_cpu_hourly_cost{cluster="%s"}
4125
cloudcost_gcp_gke_instance_cpu_usd_per_core_hour{cluster_name="%s"}
4226
)
4327
`
44-
4528
queryMemoryCost = `
46-
avg by (spot) (node_ram_hourly_cost{cluster="%s"}
47-
* on (cluster, node) group_left(spot)
48-
group by (cluster, node, spot) (
49-
label_replace(
50-
label_join(kubecost_node_is_spot == 1, "node", "", "exported_instance")
51-
,"spot", "true", "", ""
52-
)
53-
or on (cluster, node)
54-
label_replace(
55-
label_join(kubecost_node_is_spot == 0, "node", "", "exported_instance")
56-
,"spot", "false", "", ""
57-
)
58-
)
59-
)
60-
`
61-
cloudcostExporterQueryMemoryCost = `
6229
avg by (price_tier) (
6330
cloudcost_aws_ec2_instance_memory_usd_per_gib_hour{cluster_name="%s"}
6431
or
@@ -67,13 +34,9 @@ avg by (spot) (node_ram_hourly_cost{cluster="%s"}
6734
cloudcost_gcp_gke_instance_memory_usd_per_gib_hour{cluster_name="%s"}
6835
)
6936
`
37+
38+
// TODO(@Pokom): update this query with azure's PVC's cost once https://github.com/grafana/cloudcost-exporter/issues/236 is merged in
7039
queryPersistentVolumeCost = `
71-
avg_over_time(
72-
avg(
73-
pv_hourly_cost{cluster="%s"}
74-
)[24h:1m]
75-
)`
76-
cloudcostQueryPersistentVolumeCost = `
7740
avg(
7841
cloudcost_aws_ec2_persistent_volume_usd_per_hour{persistentvolume!="", state="in-use"}
7942
/ on (persistentvolume) group_left() (
@@ -112,8 +75,7 @@ var (
11275

11376
// Client is a client for the cost model.
11477
type Client struct {
115-
client api.Client
116-
useCloudCostExporterMetrics bool
78+
client api.Client
11779
}
11880

11981
// Clients bundles the dev and prod client in one struct.
@@ -124,11 +86,10 @@ type Clients struct {
12486

12587
// ClientConfig is the configuration for the cost model client.
12688
type ClientConfig struct {
127-
Address string
128-
HTTPConfigFile string
129-
Username string
130-
Password string
131-
UseCloudCostExporterMetrics bool
89+
Address string
90+
HTTPConfigFile string
91+
Username string
92+
Password string
13293
}
13394

13495
// NewClient creates a new cost model client with the given configuration.
@@ -168,8 +129,7 @@ func NewClient(config *ClientConfig) (*Client, error) {
168129
return nil, err
169130
}
170131
return &Client{
171-
client: client,
172-
useCloudCostExporterMetrics: config.UseCloudCostExporterMetrics,
132+
client: client,
173133
}, nil
174134
}
175135

@@ -189,12 +149,7 @@ func NewClients(prodConfig, devConfig *ClientConfig) (*Clients, error) {
189149

190150
// GetCostPerCPU returns the average cost per CPU for a given cluster.
191151
func (c *Client) GetCostPerCPU(ctx context.Context, cluster string) (Cost, error) {
192-
query := fmt.Sprintf(queryCostPerCPU, cluster)
193-
// TODO: Remove this once we've removed support for OpenCost
194-
if c.useCloudCostExporterMetrics {
195-
slog.Info("GetMemoryCost", "cluster", cluster, "message", "using cloudcost exporter metrics")
196-
query = fmt.Sprintf(cloudcostExporterQueryCostPerCpu, cluster, cluster, cluster)
197-
}
152+
query := fmt.Sprintf(queryCostPerCpu, cluster, cluster, cluster)
198153
results, err := c.query(ctx, query)
199154
if err != nil {
200155
return Cost{}, err
@@ -204,12 +159,7 @@ func (c *Client) GetCostPerCPU(ctx context.Context, cluster string) (Cost, error
204159

205160
// GetMemoryCost returns the cost per memory for a given cluster
206161
func (c *Client) GetMemoryCost(ctx context.Context, cluster string) (Cost, error) {
207-
query := fmt.Sprintf(queryMemoryCost, cluster)
208-
// TODO: Remove this once we've removed support for OpenCost
209-
if c.useCloudCostExporterMetrics {
210-
slog.Info("GetMemoryCost", "cluster", cluster, "message", "using cloudcost exporter metrics")
211-
query = fmt.Sprintf(cloudcostExporterQueryMemoryCost, cluster, cluster, cluster)
212-
}
162+
query := fmt.Sprintf(queryMemoryCost, cluster, cluster, cluster)
213163
results, err := c.query(ctx, query)
214164
if err != nil {
215165
return Cost{}, err
@@ -235,11 +185,7 @@ func (c *Client) GetNodeCount(ctx context.Context, cluster string) (int, error)
235185

236186
// GetCostForPersistentVolume returns the average cost per persistent volume for a given cluster
237187
func (c *Client) GetCostForPersistentVolume(ctx context.Context, cluster string) (Cost, error) {
238-
query := fmt.Sprintf(queryPersistentVolumeCost, cluster)
239-
if c.useCloudCostExporterMetrics {
240-
slog.Info("GetCostForPersistentVolume", "cluster", cluster, "message", "using cloudcost exporter metrics")
241-
query = fmt.Sprintf(cloudcostQueryPersistentVolumeCost, cluster, cluster, cluster, cluster)
242-
}
188+
query := fmt.Sprintf(queryPersistentVolumeCost, cluster, cluster, cluster, cluster)
243189
results, err := c.query(ctx, query)
244190
if err != nil {
245191
return Cost{}, err

0 commit comments

Comments
 (0)