Path: blob/main/install/installer/pkg/components/usage/configmap.go
2501 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1/// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package usage56import (7"fmt"8"time"910"github.com/gitpod-io/gitpod/common-go/baseserver"11"github.com/gitpod-io/gitpod/usage/pkg/server"12"github.com/gitpod-io/gitpod/usage/pkg/stripe"1314db "github.com/gitpod-io/gitpod/components/gitpod-db/go"15"github.com/gitpod-io/gitpod/installer/pkg/common"16"github.com/gitpod-io/gitpod/installer/pkg/components/redis"17"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"18corev1 "k8s.io/api/core/v1"19metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"20"k8s.io/apimachinery/pkg/runtime"21)2223func configmap(ctx *common.RenderContext) ([]runtime.Object, error) {24cfg := server.Config{25LedgerSchedule: "", // By default controller is disabled26ResetUsageSchedule: time.Duration(15 * time.Minute).String(),27Server: &baseserver.Configuration{28Services: baseserver.ServicesConfiguration{29GRPC: &baseserver.ServerConfiguration{30Address: fmt.Sprintf("0.0.0.0:%d", gRPCContainerPort),31},32},33},34DefaultSpendingLimit: db.DefaultSpendingLimit{35// because we only want spending limits in SaaS, if not configured we go with a very high (i.e. no) spending limit36ForTeams: 1_000_000_000,37ForUsers: 1_000_000_000,38MinForUsersOnStripe: 0,39},40Redis: server.RedisConfiguration{41Address: common.ClusterAddress(redis.Component, ctx.Namespace, redis.Port),42},43ServerAddress: common.ClusterAddress(common.ServerComponent, ctx.Namespace, common.ServerGRPCAPIPort),44GitpodHost: "https://" + ctx.Config.Domain,45}4647expWebAppConfig := common.ExperimentalWebappConfig(ctx)48if expWebAppConfig != nil && expWebAppConfig.Stripe != nil {49cfg.StripePrices = stripe.StripePrices{50IndividualUsagePriceIDs: stripe.PriceConfig{51EUR: expWebAppConfig.Stripe.IndividualUsagePriceIDs.EUR,52USD: expWebAppConfig.Stripe.IndividualUsagePriceIDs.USD,53},54TeamUsagePriceIDs: stripe.PriceConfig{55EUR: expWebAppConfig.Stripe.TeamUsagePriceIDs.EUR,56USD: expWebAppConfig.Stripe.TeamUsagePriceIDs.USD,57},58}59}6061expUsageConfig := getExperimentalUsageConfig(ctx)6263if expUsageConfig != nil {64if expUsageConfig.Schedule != "" {65cfg.LedgerSchedule = expUsageConfig.Schedule66}67cfg.ResetUsageSchedule = expUsageConfig.ResetUsageSchedule68if expUsageConfig.DefaultSpendingLimit != nil {69cfg.DefaultSpendingLimit = *expUsageConfig.DefaultSpendingLimit70}71}7273workspaceClassConfig := getExperimentalWorkspaceClassConfig(ctx)7475cfg.CreditsPerMinuteByWorkspaceClass = make(map[string]float64)76for _, v := range workspaceClassConfig {77if v.Credits != nil {78cfg.CreditsPerMinuteByWorkspaceClass[v.Id] = v.Credits.PerMinute79}80}8182_ = ctx.WithExperimental(func(ucfg *experimental.Config) error {83_, _, path, ok := getStripeConfig(ucfg)84if !ok {85return nil86}8788cfg.StripeCredentialsFile = path89return nil90})9192serialized, err := common.ToJSONString(cfg)93if err != nil {94return nil, fmt.Errorf("failed to marshal usage config: %w", err)95}9697return []runtime.Object{98&corev1.ConfigMap{99TypeMeta: common.TypeMetaConfigmap,100ObjectMeta: metav1.ObjectMeta{101Name: Component,102Namespace: ctx.Namespace,103Labels: common.CustomizeLabel(ctx, Component, common.TypeMetaConfigmap),104Annotations: common.CustomizeAnnotation(ctx, Component, common.TypeMetaConfigmap),105},106Data: map[string]string{107configJSONFilename: string(serialized),108},109},110}, nil111}112113114