Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ IMAGE_PREFIX=grafana
IMAGE_NAME=kost
IMAGE_NAME_LATEST=${IMAGE_PREFIX}/${IMAGE_NAME}:latest
IMAGE_NAME_VERSION=$(IMAGE_PREFIX)/$(IMAGE_NAME):$(VERSION)
DIVE_HIGHEST_USER_WASTED_PERCENT := 0.15

PROM_VERSION_PKG ?= github.com/prometheus/common/version
BUILD_USER ?= $(shell whoami)@$(shell hostname)
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Specifically, the following assumptions need to be met:
- K8s resources defined in a standalone repository
- We use [jsonnet](https://jsonnet.org/) to define resources + [tanka](https://tanka.dev/) to generate the k8s manifest files and commit them to a `kube-manifest` repo + [flux](https://fluxcd.io/) to deploy them to clusters
- Mimir to store [opencost](https://github.com/opencost/opencost) + [cloudcost-exporter](https://github.com/grafana/cloudcost-exporter) metrics for cost data
- Drone(soon to be GitHub Actions) to detect changes and run the cost report
- GitHub Actions to detect changes and run the cost report

While these are what we use internally and require, in theory the bot should work so long as you have:
1. Two manifest files that you can compare changes
2. Prometheus complient backend with cost metrics
2. Prometheus compliant backend with cost metrics
3. A CI system to run the bot when changes happen


Expand All @@ -45,7 +45,7 @@ There are two entrypoints that you can run:
- bot

Estimator is a simple cli that accepts two manifest files and a set of clusters to generate the cost estimator for.
Bot is what is ran in Drone today and requires the `kube-manifest` repository to be available locally.
Bot is what is ran in GitHub Actions today and requires the `kube-manifest` repository to be available locally.

## Estimator

Expand Down Expand Up @@ -76,8 +76,8 @@ Set the following environment variables:
- `KUBE_MANIFESTS_PATH`: path to `grafana/kube-manifests`
- `HTTP_CONFIG_FILE`: path to configuration created in [Prereqs](#prerequisites)
- `PROMETHEUS_ADDRESS`: mimir endpoint
- `DRONE_PULL_REQUEST`: GitHub PR to create comment on
- `DRONE_BUILD_EVENT `: set to `pull_request`
- `GITHUB_PULL_REQUEST`: GitHub PR to create comment on
- `GITHUB_EVENT_NAME`: set to `pull_request`
- `GITHUB_TOKEN`: set to a token that is able to comment on PRs
- `CI`: set to `true`

Expand All @@ -87,4 +87,4 @@ go run ./cmd/bot/
## Debugging

- checkout the change in `kube-manifests` that you want to generate a cost estimate report for.
This also includes to set `master` to the right hash.
This also includes to set `master` to the right hash.
17 changes: 13 additions & 4 deletions cmd/bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"errors"
"fmt"
"log/slog"
"os"

"github.com/kelseyhightower/envconfig"

Expand All @@ -29,9 +31,10 @@ type config struct {

GitHub github.Config

IsCI bool `envconfig:"CI"`
PR int `envconfig:"DRONE_PULL_REQUEST" required:"true"`
Event string `envconfig:"DRONE_BUILD_EVENT"`
IsCI bool `envconfig:"CI"`
PR int `envconfig:"GITHUB_PULL_REQUEST" required:"true"`
Event string `envconfig:"GITHUB_EVENT_NAME"`
LogLevel string `envconfig:"LOG_LEVEL" default:"info"`
}

const pullRequestEvent = "pull_request"
Expand All @@ -53,13 +56,19 @@ func (c config) validate() error {
}

if c.PR == 0 || c.Event != pullRequestEvent {
return errors.New("expecting DRONE_PULL_REQUEST and DRONE_BUILD_EVENT to be set")
return errors.New("expecting GITHUB_PULL_REQUEST and GITHUB_EVENT_NAME to be set")
}

if err := c.GitHub.Validate(); err != nil {
return fmt.Errorf("github configuration: %w", err)
}

var level slog.Level
if err := level.UnmarshalText([]byte(c.LogLevel)); err != nil {
return fmt.Errorf("parsing log level: %w", err)
}
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo}))
slog.SetDefault(logger)
// TODO check repo exists

return nil
Expand Down
13 changes: 8 additions & 5 deletions cmd/bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ func realMain(ctx context.Context) error {

repo := git.NewRepository(cfg.Manifests.RepoPath)

oldCommit := "master"
newCommit, err := repo.GetCurrentCommit(ctx)
oldCommit, err := repo.GetCommit(ctx, "HEAD^")
if err != nil {
return fmt.Errorf("getting current commit: %w", err)
return fmt.Errorf("getting commit: %w", err)
}

rev := oldCommit + "..." + newCommit
cf, err := repo.ChangedFiles(ctx, rev)
newCommit, err := repo.GetCommit(ctx, "HEAD")
if err != nil {
return fmt.Errorf("getting commit: %w", err)
}

cf, err := repo.ChangedFiles(ctx, oldCommit, newCommit)
if err != nil {
return err
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"strings"
Expand All @@ -29,27 +30,31 @@ func (r Repository) git(ctx context.Context, args ...string) ([]byte, error) {

out, err := cmd.Output()
if err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) {
return nil, fmt.Errorf("running %v: %w\n%s", cmd, ee, ee.Stderr)
}
return nil, fmt.Errorf("running %v: %w", cmd, err)
}

return out, nil
}

func (r Repository) GetCurrentCommit(ctx context.Context) (string, error) {
head, err := r.git(ctx, "rev-parse", "HEAD")
func (r Repository) GetCommit(ctx context.Context, ref string) (string, error) {
head, err := r.git(ctx, "rev-parse", ref)
if err != nil {
return "", err
}

return strings.TrimSpace(string(head)), nil
}

func (r Repository) ChangedFiles(ctx context.Context, rev string) (ChangedFiles, error) {
func (r Repository) ChangedFiles(ctx context.Context, oldCommit string, newCommit string) (ChangedFiles, error) {
cf := ChangedFiles{
Renamed: make(map[string]string),
}

out, err := r.git(ctx, "diff", "--name-status", rev)
out, err := r.git(ctx, "diff", "--name-status", oldCommit, newCommit)
if err != nil {
return cf, err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"log/slog"
"net/http"
"strings"

Expand Down Expand Up @@ -32,6 +33,7 @@ func NewClient(ctx context.Context, cfg Config) (Client, error) {
var token = cfg.Token

if cfg.AppID > 0 {
slog.Info("Using GitHub app authentication")
t, err := ghinstallation.NewAppsTransport(
httpcache.NewMemoryCacheTransport(),
cfg.AppID,
Expand All @@ -52,6 +54,8 @@ func NewClient(ctx context.Context, cfg Config) (Client, error) {
}

token = tok.GetToken()
} else {
slog.Info("Using GitHub token authentication")
}

ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
Expand Down
Loading