Skip to content

Commit e8ab39e

Browse files
easyCZroboquat
authored andcommitted
[usage] Use a config file to configure usage component
1 parent 76c52df commit e8ab39e

File tree

15 files changed

+357
-108
lines changed

15 files changed

+357
-108
lines changed

components/usage/BUILD.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ packages:
1515
packaging: app
1616
buildCommand: ["go", "build", "-trimpath", "-ldflags", "-buildid= -w -s -X 'github.com/gitpod-io/gitpod/usage/cmd.Version=commit-${__git_commit}'"]
1717

18+
- name: lib
19+
type: go
20+
deps:
21+
- components/common-go:lib
22+
srcs:
23+
- "**/*.go"
24+
- "go.mod"
25+
- "go.sum"
26+
config:
27+
packaging: library
28+
dontTest: true
29+
1830
- name: init-testdb
1931
type: generic
2032
deps:

components/usage/cmd/run.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
package cmd
66

77
import (
8-
"github.com/gitpod-io/gitpod/usage/pkg/server"
9-
"time"
10-
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
1111
"github.com/gitpod-io/gitpod/common-go/log"
12+
"github.com/gitpod-io/gitpod/usage/pkg/server"
1213
"github.com/spf13/cobra"
14+
"os"
15+
"path"
1316
)
1417

1518
func init() {
@@ -19,8 +22,7 @@ func init() {
1922
func run() *cobra.Command {
2023
var (
2124
verbose bool
22-
apiKeyFile string
23-
schedule time.Duration
25+
configPath string
2426
)
2527

2628
cmd := &cobra.Command{
@@ -30,19 +32,39 @@ func run() *cobra.Command {
3032
Run: func(cmd *cobra.Command, args []string) {
3133
log.Init(ServiceName, Version, true, verbose)
3234

33-
err := server.Start(server.Config{
34-
ControllerSchedule: schedule,
35-
StripeCredentialsFile: apiKeyFile,
36-
})
35+
cfg, err := parseConfig(configPath)
36+
if err != nil {
37+
log.WithError(err).Fatal("Failed to get config. Did you specify --config correctly?")
38+
}
39+
40+
err = server.Start(cfg)
3741
if err != nil {
3842
log.WithError(err).Fatal("Failed to start usage server.")
3943
}
4044
},
4145
}
4246

47+
localConfig := path.Join(os.ExpandEnv("GOMOD"), "..", "config.json")
48+
4349
cmd.Flags().BoolVar(&verbose, "verbose", false, "Toggle verbose logging (debug level)")
44-
cmd.Flags().DurationVar(&schedule, "schedule", 1*time.Hour, "The schedule on which the reconciler should run")
45-
cmd.Flags().StringVar(&apiKeyFile, "stripe-secret-path", "", "Location of the Stripe credentials file on disk")
50+
cmd.Flags().StringVar(&configPath, "config", localConfig, "Configuration file for running usage component")
4651

4752
return cmd
4853
}
54+
55+
func parseConfig(path string) (server.Config, error) {
56+
raw, err := os.ReadFile(path)
57+
if err != nil {
58+
return server.Config{}, fmt.Errorf("failed to read config from %s: %w", path, err)
59+
}
60+
61+
var cfg server.Config
62+
dec := json.NewDecoder(bytes.NewReader(raw))
63+
dec.DisallowUnknownFields()
64+
err = dec.Decode(&cfg)
65+
if err != nil {
66+
return server.Config{}, fmt.Errorf("failed to parse config from %s: %w", path, err)
67+
}
68+
69+
return cfg, nil
70+
}

components/usage/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"controllerSchedule": "1h"
3+
}

components/usage/go.mod

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,53 @@ module github.com/gitpod-io/gitpod/usage
22

33
go 1.18
44

5+
require (
6+
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
7+
github.com/go-sql-driver/mysql v1.6.0
8+
github.com/google/uuid v1.1.2
9+
github.com/prometheus/client_golang v1.12.1
10+
github.com/relvacode/iso8601 v1.1.0
11+
github.com/robfig/cron v1.2.0
12+
github.com/sirupsen/logrus v1.8.1
13+
github.com/spf13/cobra v1.4.0
14+
github.com/stretchr/testify v1.7.0
15+
github.com/stripe/stripe-go/v72 v72.114.0
16+
gorm.io/datatypes v1.0.6
17+
gorm.io/driver/mysql v1.3.3
18+
gorm.io/gorm v1.23.5
19+
)
20+
21+
require (
22+
github.com/beorn7/perks v1.0.1 // indirect
23+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
24+
github.com/davecgh/go-spew v1.1.1 // indirect
25+
github.com/golang/protobuf v1.5.2 // indirect
26+
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
27+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
28+
github.com/hashicorp/golang-lru v0.5.1 // indirect
29+
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect
30+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
31+
github.com/jinzhu/inflection v1.0.0 // indirect
32+
github.com/jinzhu/now v1.1.4 // indirect
33+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
34+
github.com/opentracing/opentracing-go v1.2.0 // indirect
35+
github.com/pmezard/go-difflib v1.0.0 // indirect
36+
github.com/prometheus/client_model v0.2.0 // indirect
37+
github.com/prometheus/common v0.32.1 // indirect
38+
github.com/prometheus/procfs v0.7.3 // indirect
39+
github.com/spf13/pflag v1.0.5 // indirect
40+
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
41+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
42+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
43+
golang.org/x/text v0.3.7 // indirect
44+
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
45+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
46+
google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect
47+
google.golang.org/grpc v1.45.0 // indirect
48+
google.golang.org/protobuf v1.28.0 // indirect
49+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
50+
)
51+
552
replace github.com/gitpod-io/gitpod/common-go => ../common-go // leeway
653

754
replace k8s.io/api => k8s.io/api v0.23.5 // leeway indirect from components/common-go:lib
@@ -53,50 +100,3 @@ replace k8s.io/kubectl => k8s.io/kubectl v0.23.5 // leeway indirect from compone
53100
replace k8s.io/mount-utils => k8s.io/mount-utils v0.23.5 // leeway indirect from components/common-go:lib
54101

55102
replace k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.23.5 // leeway indirect from components/common-go:lib
56-
57-
require (
58-
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
59-
github.com/go-sql-driver/mysql v1.6.0
60-
github.com/google/uuid v1.1.2
61-
github.com/prometheus/client_golang v1.12.1
62-
github.com/relvacode/iso8601 v1.1.0
63-
github.com/robfig/cron v1.2.0
64-
github.com/sirupsen/logrus v1.8.1
65-
github.com/spf13/cobra v1.4.0
66-
github.com/stretchr/testify v1.7.0
67-
github.com/stripe/stripe-go/v72 v72.114.0
68-
gorm.io/datatypes v1.0.6
69-
gorm.io/driver/mysql v1.3.3
70-
gorm.io/gorm v1.23.5
71-
)
72-
73-
require (
74-
github.com/beorn7/perks v1.0.1 // indirect
75-
github.com/cespare/xxhash/v2 v2.1.2 // indirect
76-
github.com/davecgh/go-spew v1.1.1 // indirect
77-
github.com/golang/protobuf v1.5.2 // indirect
78-
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
79-
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
80-
github.com/hashicorp/golang-lru v0.5.1 // indirect
81-
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect
82-
github.com/inconshreveable/mousetrap v1.0.0 // indirect
83-
github.com/jinzhu/inflection v1.0.0 // indirect
84-
github.com/jinzhu/now v1.1.4 // indirect
85-
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
86-
github.com/opentracing/opentracing-go v1.2.0 // indirect
87-
github.com/pmezard/go-difflib v1.0.0 // indirect
88-
github.com/prometheus/client_model v0.2.0 // indirect
89-
github.com/prometheus/common v0.32.1 // indirect
90-
github.com/prometheus/procfs v0.7.3 // indirect
91-
github.com/spf13/pflag v1.0.5 // indirect
92-
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
93-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
94-
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
95-
golang.org/x/text v0.3.7 // indirect
96-
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
97-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
98-
google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect
99-
google.golang.org/grpc v1.45.0 // indirect
100-
google.golang.org/protobuf v1.28.0 // indirect
101-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
102-
)

components/usage/pkg/server/server.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
type Config struct {
2020
// ControllerSchedule determines how frequently to run the Usage/Billing controller
21-
ControllerSchedule time.Duration `json:"controllerSchedule,omitempty"`
21+
ControllerSchedule string `json:"controllerSchedule,omitempty"`
2222

2323
StripeCredentialsFile string `json:"stripeCredentialsFile,omitempty"`
2424

@@ -53,7 +53,12 @@ func Start(cfg Config) error {
5353
billingController = controller.NewStripeBillingController(c, controller.DefaultWorkspacePricer)
5454
}
5555

56-
ctrl, err := controller.New(cfg.ControllerSchedule, controller.NewUsageReconciler(conn, billingController))
56+
schedule, err := time.ParseDuration(cfg.ControllerSchedule)
57+
if err != nil {
58+
return fmt.Errorf("failed to parse schedule duration: %w", err)
59+
}
60+
61+
ctrl, err := controller.New(schedule, controller.NewUsageReconciler(conn, billingController))
5762
if err != nil {
5863
return fmt.Errorf("failed to initialize usage controller: %w", err)
5964
}

install/installer/BUILD.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ packages:
2828
- components/ws-daemon:lib
2929
- components/ws-manager-api/go:lib
3030
- components/ws-proxy:lib
31+
- components/usage:lib
3132
env:
3233
- CGO_ENABLED=0
3334
prep:

install/installer/go.mod

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/gitpod-io/gitpod/openvsx-proxy v0.0.0-00010101000000-000000000000
1515
github.com/gitpod-io/gitpod/public-api v0.0.0-00010101000000-000000000000
1616
github.com/gitpod-io/gitpod/registry-facade/api v0.0.0-00010101000000-000000000000
17+
github.com/gitpod-io/gitpod/usage v0.0.0-00010101000000-000000000000
1718
github.com/gitpod-io/gitpod/ws-daemon v0.0.0-00010101000000-000000000000
1819
github.com/gitpod-io/gitpod/ws-daemon/api v0.0.0-00010101000000-000000000000
1920
github.com/gitpod-io/gitpod/ws-manager/api v0.0.0-00010101000000-000000000000
@@ -23,7 +24,7 @@ require (
2324
github.com/jetstack/cert-manager v1.4.4
2425
github.com/replicatedhq/kots v1.67.0
2526
github.com/sirupsen/logrus v1.8.1
26-
github.com/spf13/cobra v1.2.1
27+
github.com/spf13/cobra v1.4.0
2728
github.com/stretchr/testify v1.7.1
2829
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898
2930
helm.sh/helm/v3 v3.7.1
@@ -111,6 +112,7 @@ require (
111112
github.com/go-playground/universal-translator v0.18.0 // indirect
112113
github.com/go-redis/redis/v7 v7.4.1 // indirect
113114
github.com/go-redis/redis/v8 v8.11.5 // indirect
115+
github.com/go-sql-driver/mysql v1.6.0 // indirect
114116
github.com/gobwas/glob v0.2.3 // indirect
115117
github.com/godbus/dbus/v5 v5.0.6 // indirect
116118
github.com/gogo/googleapis v1.4.0 // indirect
@@ -163,6 +165,8 @@ require (
163165
github.com/ipld/go-codec-dagpb v1.3.0 // indirect
164166
github.com/ipld/go-ipld-prime v0.11.0 // indirect
165167
github.com/jbenet/goprocess v0.1.4 // indirect
168+
github.com/jinzhu/inflection v1.0.0 // indirect
169+
github.com/jinzhu/now v1.1.4 // indirect
166170
github.com/jmoiron/sqlx v1.3.1 // indirect
167171
github.com/josharian/intern v1.0.0 // indirect
168172
github.com/json-iterator/go v1.1.12 // indirect
@@ -223,7 +227,9 @@ require (
223227
github.com/prometheus/client_model v0.2.0 // indirect
224228
github.com/prometheus/common v0.32.1 // indirect
225229
github.com/prometheus/procfs v0.7.3 // indirect
230+
github.com/relvacode/iso8601 v1.1.0 // indirect
226231
github.com/rivo/uniseg v0.2.0 // indirect
232+
github.com/robfig/cron v1.2.0 // indirect
227233
github.com/rs/cors v1.7.0 // indirect
228234
github.com/rs/xid v1.2.1 // indirect
229235
github.com/rubenv/sql-migrate v0.0.0-20210614095031-55d5740dbbcc // indirect
@@ -233,6 +239,7 @@ require (
233239
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
234240
github.com/spf13/cast v1.4.1 // indirect
235241
github.com/spf13/pflag v1.0.5 // indirect
242+
github.com/stripe/stripe-go/v72 v72.114.0 // indirect
236243
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
237244
github.com/uber/jaeger-client-go v2.29.1+incompatible // indirect
238245
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
@@ -267,6 +274,9 @@ require (
267274
gopkg.in/ini.v1 v1.63.2 // indirect
268275
gopkg.in/yaml.v2 v2.4.0 // indirect
269276
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
277+
gorm.io/datatypes v1.0.6 // indirect
278+
gorm.io/driver/mysql v1.3.3 // indirect
279+
gorm.io/gorm v1.23.5 // indirect
270280
k8s.io/apiextensions-apiserver v0.23.5 // indirect
271281
k8s.io/apiserver v0.23.5 // indirect
272282
k8s.io/cli-runtime v0.23.5 // indirect
@@ -309,6 +319,8 @@ replace github.com/gitpod-io/gitpod/registry-facade/api => ../../components/regi
309319

310320
replace github.com/gitpod-io/gitpod/supervisor/api => ../../components/supervisor-api/go // leeway
311321

322+
replace github.com/gitpod-io/gitpod/usage => ../../components/usage // leeway
323+
312324
replace github.com/gitpod-io/gitpod/ws-daemon => ../../components/ws-daemon // leeway
313325

314326
replace github.com/gitpod-io/gitpod/ws-daemon/api => ../../components/ws-daemon-api/go // leeway

0 commit comments

Comments
 (0)