Skip to content
Open
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
190 changes: 190 additions & 0 deletions pkg/plugins/github/cmd/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package main

import (
"context"
"encoding/json"
"fmt"
// "io"
// "net/http"
// "regexp"
// "strconv"
// "strings"
// "time"

"os"

"github.com/hashicorp/go-plugin"
"github.com/google/go-github/v74/github"
commonconfig "github.com/opencost/opencost-plugins/pkg/common/config"
githubplugin "github.com/opencost/opencost-plugins/pkg/plugins/github/githubplugin"
"golang.org/x/time/rate"
"github.com/opencost/opencost/core/pkg/log"
"github.com/opencost/opencost/core/pkg/model/pb"
"github.com/opencost/opencost/core/pkg/opencost"
ocplugin "github.com/opencost/opencost/core/pkg/plugin"
)

const organisation = "opencost"

var handshakeConfig = plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "PLUGIN_NAME",
MagicCookieValue: "github",
}

type githubSource struct {
ghCtx context.Context
usageApi *github.BillingService
rateLimiter *rate.Limiter
}

func (g *githubSource) GetCustomCosts(req *pb.CustomCostRequest) []*pb.CustomCostResponse {
results := []*pb.CustomCostResponse{}

targets, err := opencost.GetWindows(req.Start.AsTime(), req.End.AsTime(), req.Resolution.AsDuration())
if err != nil {
log.Errorf("error getting windows: %v", err)
errResp := pb.CustomCostResponse{
Errors: []string{fmt.Sprintf("error getting windows: %v", err)},
}
results = append(results, &errResp)
return results
}

// This only works for whole number dates
for _, target := range targets {
fmt.Println("Calling Billing Or", target)
windowPointer := target.Start()
year := int((*windowPointer).Year())

month := int((*windowPointer).Month())

day := int((*windowPointer).Day())

hour := int((*windowPointer).Hour())

// Check for resolution level, it can be hour, day, month, year
usageReportOptions := &github.UsageReportOptions{
Year: &year,
Month: &month,
Day: &day,
Hour: &hour,
}

fmt.Println("Calling Billing Or")
actionsPricing, err := g.scrapeBillingOrg("cloudrainsdev", usageReportOptions)
// actionsPricing, _, err = g.scrapeBillingUser(user, usageReportOptions)

if err != nil {
log.Errorf("error getting dd pricing: %v", err)
errResp := pb.CustomCostResponse{
Errors: []string{fmt.Sprintf("error getting dd pricing: %v", err)},
}
results = append(results, &errResp)
return results
} else {
log.Debugf("got list pricing: %v", &actionsPricing.UsageItems[0].Quantity)
}
}
return results
}


func (g *githubSource) scrapeBillingOrg(organisation string, usageReportOptions *github.UsageReportOptions) (*github.UsageReport, error) {
orgBilling, _, err := g.usageApi.GetUsageReportOrg(g.ghCtx, organisation, usageReportOptions)
// Check for errors
// if response.StatusCode != http.StatusOk {
// return nil, fmt.Errorf("failed to retrieve actions price. Status code: %d", response.StatusCode)
// }

if err != nil {
return nil, fmt.Errorf("failed to fetch actions price: %v", err)
}
return orgBilling, nil
}

func (g *githubSource) scrapeBillingUser(user string, usageReportOptions *github.UsageReportOptions) (*github.UsageReport, error) {
userBilling, _, err := g.usageApi.GetUsageReportUser(g.ghCtx, user, usageReportOptions)
// Check for errors
// if response.StatusCode != http.StatusOk {
// return nil, fmt.Errorf("failed to retrieve actions price. Status code: %d", response.StatusCode)
// }

if err != nil {
return nil, fmt.Errorf("failed to fetch actions price: %v", err)
}
return userBilling, nil
}


func getConfigFilePath() (string, error) {
// plugins expect exactly 2 args: the executable itself,
// and a path to the config file to use
// all config for the plugin must come through the config file
if len(os.Args) != 2 {
return "", fmt.Errorf("plugins require 2 args: the plugin itself, and the full path to its config file. Got %d args", len(os.Args))
}

_, err := os.Stat(os.Args[1])
if err != nil {
return "", fmt.Errorf("error reading config file at %s: %v", os.Args[1], err)
}

return os.Args[1], nil
}

func getGithubConfig(configFilePath string) (*githubplugin.GithubConfig, error) {
var result githubplugin.GithubConfig
bytes, err := os.ReadFile(configFilePath)
if err != nil {
return nil, fmt.Errorf("error reading config file for DD config @ %s: %v", configFilePath, err)
}
err = json.Unmarshal(bytes, &result)
if err != nil {
return nil, fmt.Errorf("error marshaling json into DD config %v", err)
}

// if result.DDLogLevel == "" {
// result.DDLogLevel = "info"
// }

return &result, nil
}

func getGithubClients(ghConfig githubplugin.GithubConfig) (context.Context, *github.BillingService) {
ghCtx := context.Background()
ghClient := github.NewClient(nil).WithAuthToken(ghConfig.GithubPAT).Billing
return ghCtx, ghClient
}

func main() {

configFile, err := commonconfig.GetConfigFilePath()
if err != nil {
log.Fatalf("error opening config file: %v", err)
}

ghConfig, err := getGithubConfig(configFile)
if err != nil {
log.Fatalf("error building DD config: %v", err)
}
// set log level
rateLimiter := rate.NewLimiter(0.25, 5)

ghCostSrc := githubSource{
rateLimiter: rateLimiter,
}

ghCostSrc.ghCtx, ghCostSrc.usageApi = getGithubClients(*ghConfig)

var pluginMap = map[string]plugin.Plugin{
"CustomCostSource": &ocplugin.CustomCostPlugin{Impl: &ghCostSrc},
}

plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
GRPCServer: plugin.DefaultGRPCServer,
})
}

7 changes: 7 additions & 0 deletions pkg/plugins/github/githubplugin/githubconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package githubplugin


type GithubConfig struct {
GithubUserName string `json:"github_username"`
GithubPAT string `json:"github_pat"`
}
62 changes: 62 additions & 0 deletions pkg/plugins/github/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module github.com/opencost/opencost-plugins/pkg/plugins/github

go 1.24.2

replace github.com/opencost/opencost-plugins/pkg/common => ../../common

require (
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-github/v74 v74.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-hclog v1.6.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.6.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/opencost/opencost-plugins/pkg/common v0.0.0-20250403142249-d8eaf3258224 // indirect
github.com/opencost/opencost/core v1.117.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/rs/zerolog v1.26.1 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.8.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirect
golang.org/x/net v0.42.0 // indirect
golang.org/x/sys v0.34.0 // indirect
golang.org/x/text v0.27.0 // indirect
golang.org/x/time v0.12.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250721164621-a45f3dfb1074 // indirect
google.golang.org/grpc v1.74.2 // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.33.1 // indirect
k8s.io/apimachinery v0.33.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e // indirect
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
sigs.k8s.io/randfill v1.0.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading