Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/pkg/components/usage/configmap.go
2501 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
/// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package usage
6
7
import (
8
"fmt"
9
"time"
10
11
"github.com/gitpod-io/gitpod/common-go/baseserver"
12
"github.com/gitpod-io/gitpod/usage/pkg/server"
13
"github.com/gitpod-io/gitpod/usage/pkg/stripe"
14
15
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
16
"github.com/gitpod-io/gitpod/installer/pkg/common"
17
"github.com/gitpod-io/gitpod/installer/pkg/components/redis"
18
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"
19
corev1 "k8s.io/api/core/v1"
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21
"k8s.io/apimachinery/pkg/runtime"
22
)
23
24
func configmap(ctx *common.RenderContext) ([]runtime.Object, error) {
25
cfg := server.Config{
26
LedgerSchedule: "", // By default controller is disabled
27
ResetUsageSchedule: time.Duration(15 * time.Minute).String(),
28
Server: &baseserver.Configuration{
29
Services: baseserver.ServicesConfiguration{
30
GRPC: &baseserver.ServerConfiguration{
31
Address: fmt.Sprintf("0.0.0.0:%d", gRPCContainerPort),
32
},
33
},
34
},
35
DefaultSpendingLimit: db.DefaultSpendingLimit{
36
// because we only want spending limits in SaaS, if not configured we go with a very high (i.e. no) spending limit
37
ForTeams: 1_000_000_000,
38
ForUsers: 1_000_000_000,
39
MinForUsersOnStripe: 0,
40
},
41
Redis: server.RedisConfiguration{
42
Address: common.ClusterAddress(redis.Component, ctx.Namespace, redis.Port),
43
},
44
ServerAddress: common.ClusterAddress(common.ServerComponent, ctx.Namespace, common.ServerGRPCAPIPort),
45
GitpodHost: "https://" + ctx.Config.Domain,
46
}
47
48
expWebAppConfig := common.ExperimentalWebappConfig(ctx)
49
if expWebAppConfig != nil && expWebAppConfig.Stripe != nil {
50
cfg.StripePrices = stripe.StripePrices{
51
IndividualUsagePriceIDs: stripe.PriceConfig{
52
EUR: expWebAppConfig.Stripe.IndividualUsagePriceIDs.EUR,
53
USD: expWebAppConfig.Stripe.IndividualUsagePriceIDs.USD,
54
},
55
TeamUsagePriceIDs: stripe.PriceConfig{
56
EUR: expWebAppConfig.Stripe.TeamUsagePriceIDs.EUR,
57
USD: expWebAppConfig.Stripe.TeamUsagePriceIDs.USD,
58
},
59
}
60
}
61
62
expUsageConfig := getExperimentalUsageConfig(ctx)
63
64
if expUsageConfig != nil {
65
if expUsageConfig.Schedule != "" {
66
cfg.LedgerSchedule = expUsageConfig.Schedule
67
}
68
cfg.ResetUsageSchedule = expUsageConfig.ResetUsageSchedule
69
if expUsageConfig.DefaultSpendingLimit != nil {
70
cfg.DefaultSpendingLimit = *expUsageConfig.DefaultSpendingLimit
71
}
72
}
73
74
workspaceClassConfig := getExperimentalWorkspaceClassConfig(ctx)
75
76
cfg.CreditsPerMinuteByWorkspaceClass = make(map[string]float64)
77
for _, v := range workspaceClassConfig {
78
if v.Credits != nil {
79
cfg.CreditsPerMinuteByWorkspaceClass[v.Id] = v.Credits.PerMinute
80
}
81
}
82
83
_ = ctx.WithExperimental(func(ucfg *experimental.Config) error {
84
_, _, path, ok := getStripeConfig(ucfg)
85
if !ok {
86
return nil
87
}
88
89
cfg.StripeCredentialsFile = path
90
return nil
91
})
92
93
serialized, err := common.ToJSONString(cfg)
94
if err != nil {
95
return nil, fmt.Errorf("failed to marshal usage config: %w", err)
96
}
97
98
return []runtime.Object{
99
&corev1.ConfigMap{
100
TypeMeta: common.TypeMetaConfigmap,
101
ObjectMeta: metav1.ObjectMeta{
102
Name: Component,
103
Namespace: ctx.Namespace,
104
Labels: common.CustomizeLabel(ctx, Component, common.TypeMetaConfigmap),
105
Annotations: common.CustomizeAnnotation(ctx, Component, common.TypeMetaConfigmap),
106
},
107
Data: map[string]string{
108
configJSONFilename: string(serialized),
109
},
110
},
111
}, nil
112
}
113
114