Skip to content

Commit b2ac268

Browse files
author
Andrew Farries
committed
[usage] Make Stripe payment integration optional
Enable stripe integration iff a Stripe secret is configured.
1 parent 015090a commit b2ac268

File tree

5 files changed

+88
-15
lines changed

5 files changed

+88
-15
lines changed

components/usage/cmd/run.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ func init() {
2222

2323
func run() *cobra.Command {
2424
var (
25-
verbose bool
26-
apiKeyFile string
27-
schedule time.Duration
25+
verbose bool
26+
paymentEnabled bool
27+
apiKeyFile string
28+
schedule time.Duration
2829
)
2930

3031
cmd := &cobra.Command{
@@ -44,12 +45,15 @@ func run() *cobra.Command {
4445
log.WithError(err).Fatal("Failed to establish database connection.")
4546
}
4647

47-
err = stripe.Authenticate(apiKeyFile)
48-
if err != nil {
49-
log.WithError(err).Fatal("Failed to initialize stripe client.")
48+
log.Infof("payment integration with Stripe is set to %v", paymentEnabled)
49+
if paymentEnabled {
50+
err = stripe.Authenticate(apiKeyFile)
51+
if err != nil {
52+
log.WithError(err).Fatal("Failed to initialize stripe client.")
53+
}
5054
}
5155

52-
ctrl, err := controller.New(schedule, controller.NewUsageReconciler(conn))
56+
ctrl, err := controller.New(schedule, controller.NewUsageReconciler(conn, paymentEnabled))
5357
if err != nil {
5458
log.WithError(err).Fatal("Failed to initialize usage controller.")
5559
}
@@ -74,7 +78,8 @@ func run() *cobra.Command {
7478

7579
cmd.Flags().BoolVar(&verbose, "verbose", false, "Toggle verbose logging (debug level)")
7680
cmd.Flags().DurationVar(&schedule, "schedule", 1*time.Hour, "The schedule on which the reconciler should run")
77-
cmd.Flags().StringVar(&apiKeyFile, "api-key-file", "/stripe-secret/apikeys", "Location of the stripe credentials file on disk")
81+
cmd.Flags().BoolVar(&paymentEnabled, "enable-payment", false, "Whether or not payment integration with Stripe should be enabled")
82+
cmd.Flags().StringVar(&apiKeyFile, "api-key-file", "/stripe-secret/apikeys", "Location of the Stripe credentials file on disk")
7883

7984
return cmd
8085
}

components/usage/pkg/controller/reconciler.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ func (f ReconcilerFunc) Reconcile() error {
3030
}
3131

3232
type UsageReconciler struct {
33-
nowFunc func() time.Time
34-
conn *gorm.DB
33+
nowFunc func() time.Time
34+
paymentEnabled bool
35+
conn *gorm.DB
3536
}
3637

37-
func NewUsageReconciler(conn *gorm.DB) *UsageReconciler {
38-
return &UsageReconciler{conn: conn, nowFunc: time.Now}
38+
func NewUsageReconciler(conn *gorm.DB, paymentEnabled bool) *UsageReconciler {
39+
return &UsageReconciler{conn: conn, paymentEnabled: paymentEnabled, nowFunc: time.Now}
3940
}
4041

4142
type UsageReconcileStatus struct {
@@ -126,7 +127,9 @@ func (u *UsageReconciler) ReconcileTimeRange(ctx context.Context, from, to time.
126127
}
127128
status.Report = report
128129

129-
submitUsageReport(status.Report)
130+
if u.paymentEnabled {
131+
submitUsageReport(status.Report)
132+
}
130133

131134
return status, nil
132135
}

install/installer/pkg/components/usage/deployment.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
2424

2525
var volumes []corev1.Volume
2626
var volumeMounts []corev1.VolumeMount
27+
enablePayment := false
2728
_ = ctx.WithExperimental(func(cfg *experimental.Config) error {
2829
if cfg.WebApp != nil && cfg.WebApp.Server != nil && cfg.WebApp.Server.StripeSecret != "" {
2930
stripeSecret := cfg.WebApp.Server.StripeSecret
31+
enablePayment = true
3032

3133
volumes = append(volumes,
3234
corev1.Volume{
@@ -79,8 +81,8 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
7981
Image: ctx.ImageName(ctx.Config.Repository, Component, ctx.VersionManifest.Components.Usage.Version),
8082
Args: []string{
8183
"run",
82-
"--schedule",
83-
"$(RECONCILER_SCHEDULE)",
84+
"--schedule=$(RECONCILER_SCHEDULE)",
85+
fmt.Sprintf("--enable-payment=%v", enablePayment),
8486
},
8587
ImagePullPolicy: corev1.PullIfNotPresent,
8688
Resources: common.ResourceRequirements(ctx, Component, Component, corev1.ResourceRequirements{

install/installer/pkg/components/usage/deployment_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,35 @@ func TestDeployment_ContainsDBEnvVars(t *testing.T) {
4848
}},
4949
})
5050
}
51+
52+
func TestDeployment_EnablesPaymentWhenAStripeSecretIsPresent(t *testing.T) {
53+
ctx := renderContextWithStripeSecretSet(t)
54+
55+
objs, err := deployment(ctx)
56+
require.NoError(t, err)
57+
58+
dpl, ok := objs[0].(*appsv1.Deployment)
59+
require.True(t, ok)
60+
61+
containers := dpl.Spec.Template.Spec.Containers
62+
require.Len(t, containers, 2)
63+
64+
usageContainer := containers[0]
65+
require.Contains(t, usageContainer.Args, "--enable-payment=true")
66+
}
67+
68+
func TestDeployment_DisablesPaymentWhenAStripeSecretIsNotPresent(t *testing.T) {
69+
ctx := renderContextWithUsageEnabled(t)
70+
71+
objs, err := deployment(ctx)
72+
require.NoError(t, err)
73+
74+
dpl, ok := objs[0].(*appsv1.Deployment)
75+
require.True(t, ok)
76+
77+
containers := dpl.Spec.Template.Spec.Containers
78+
require.Len(t, containers, 2)
79+
80+
usageContainer := containers[0]
81+
require.Contains(t, usageContainer.Args, "--enable-payment=false")
82+
}

install/installer/pkg/components/usage/objects_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,34 @@ func renderContextWithUsageConfig(t *testing.T, usage *experimental.UsageConfig)
6161
func renderContextWithUsageEnabled(t *testing.T) *common.RenderContext {
6262
return renderContextWithUsageConfig(t, &experimental.UsageConfig{Enabled: true})
6363
}
64+
65+
func renderContextWithStripeSecretSet(t *testing.T) *common.RenderContext {
66+
ctx, err := common.NewRenderContext(config.Config{
67+
Domain: "test.domain.everything.awesome.is",
68+
Experimental: &experimental.Config{
69+
WebApp: &experimental.WebAppConfig{
70+
Server: &experimental.ServerConfig{StripeSecret: "some-stripe-secret"},
71+
Usage: &experimental.UsageConfig{Enabled: true},
72+
},
73+
},
74+
Database: config.Database{
75+
CloudSQL: &config.DatabaseCloudSQL{
76+
ServiceAccount: config.ObjectRef{
77+
Name: "gcp-db-creds-service-account-name",
78+
},
79+
},
80+
},
81+
}, versions.Manifest{
82+
Components: versions.Components{
83+
Usage: versions.Versioned{
84+
Version: "commit-test-latest",
85+
},
86+
ServiceWaiter: versions.Versioned{
87+
Version: "commit-test-latest",
88+
},
89+
},
90+
}, "test-namespace")
91+
require.NoError(t, err)
92+
93+
return ctx
94+
}

0 commit comments

Comments
 (0)