Path: blob/main/install/installer/pkg/common/storage.go
2500 views
// Copyright (c) 2021 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 common56import (7"fmt"89"path/filepath"1011storageconfig "github.com/gitpod-io/gitpod/content-service/api/config"12"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"13"k8s.io/utils/pointer"1415corev1 "k8s.io/api/core/v1"16)1718const StorageMount = "/mnt/secrets/storage"1920// StorageConfig produces config service configuration from the installer config2122func useMinio(context *RenderContext) bool {23// Minio is used for in-cluster storage and as a facade to non-GCP providers24return pointer.BoolDeref(context.Config.ObjectStorage.InCluster, false)25}2627func StorageConfig(context *RenderContext) storageconfig.StorageConfig {28var res *storageconfig.StorageConfig29if context.Config.ObjectStorage.CloudStorage != nil {30res = &storageconfig.StorageConfig{31Kind: storageconfig.GCloudStorage,32GCloudConfig: storageconfig.GCPConfig{33Region: context.Config.Metadata.Region,34Project: context.Config.ObjectStorage.CloudStorage.Project,35CredentialsFile: filepath.Join(StorageMount, "service-account.json"),36},37}38}3940if context.Config.ObjectStorage.S3 != nil {41res = &storageconfig.StorageConfig{42Kind: storageconfig.S3Storage,43S3Config: &storageconfig.S3Config{44Region: context.Config.Metadata.Region,45Bucket: context.Config.ObjectStorage.S3.BucketName,46},47}4849if context.Config.ObjectStorage.S3.Credentials != nil && context.Config.ObjectStorage.S3.Credentials.Kind != "" {50res.S3Config.CredentialsFile = filepath.Join(StorageMount, "credentials")51}52}5354if useMinio(context) {55res = &storageconfig.StorageConfig{56Kind: storageconfig.MinIOStorage,57MinIOConfig: storageconfig.MinIOConfig{58Endpoint: fmt.Sprintf("minio.%s.svc.cluster.local:%d", context.Namespace, MinioServiceAPIPort),59AccessKeyID: context.Values.StorageAccessKey,60SecretAccessKey: context.Values.StorageSecretKey,61Secure: false,62Region: "local", // Local Minio requires this value - workspace allocation fails if not set to this63ParallelUpload: 6,64},65}66}6768if res == nil {69panic("no valid storage configuration set")70}7172// 5 GiB73res.BlobQuota = 5 * 1024 * 1024 * 102474if context.Config.ObjectStorage.BlobQuota != nil {75res.BlobQuota = *context.Config.ObjectStorage.BlobQuota76}7778_ = context.WithExperimental(func(ucfg *experimental.Config) error {79if ucfg.Workspace != nil {80res.Stage = storageconfig.Stage(ucfg.Workspace.Stage)81}82return nil83})8485return *res86}8788// mountStorage performs the actual storage mount, which is common across all providers89func MountStorage(pod *corev1.PodSpec, secret string, container ...string) {90volumeName := "storage-volume"9192pod.Volumes = append(pod.Volumes,93corev1.Volume{94Name: volumeName,95VolumeSource: corev1.VolumeSource{96Secret: &corev1.SecretVolumeSource{97SecretName: secret,98},99},100},101)102103idx := make(map[string]struct{}, len(container))104if len(container) == 0 {105for _, c := range pod.Containers {106idx[c.Name] = struct{}{}107}108} else {109for _, c := range container {110idx[c] = struct{}{}111}112}113114for i := range pod.Containers {115if _, ok := idx[pod.Containers[i].Name]; !ok {116continue117}118119pod.Containers[i].VolumeMounts = append(pod.Containers[i].VolumeMounts,120corev1.VolumeMount{121Name: volumeName,122ReadOnly: true,123MountPath: StorageMount,124},125)126}127}128129// AddStorageMounts adds mounts and volumes to a pod which are required for130// the storage configuration to function. If a list of containers is provided,131// the mounts are only added to those containers. If the list is empty, they're132// added to all containers.133func AddStorageMounts(ctx *RenderContext, pod *corev1.PodSpec, container ...string) error {134if ctx.Config.ObjectStorage.CloudStorage != nil {135MountStorage(pod, ctx.Config.ObjectStorage.CloudStorage.ServiceAccount.Name, container...)136137return nil138}139140if ctx.Config.ObjectStorage.S3 != nil {141if ctx.Config.ObjectStorage.S3.Credentials != nil {142MountStorage(pod, ctx.Config.ObjectStorage.S3.Credentials.Name, container...)143}144145return nil146}147148if useMinio(ctx) {149// builtin storage needs no extra mounts150return nil151}152153return fmt.Errorf("no valid storage configuration set")154}155156func NewEmptyDirVolume(name string) *corev1.Volume {157return &corev1.Volume{158Name: name,159VolumeSource: corev1.VolumeSource{160EmptyDir: &corev1.EmptyDirVolumeSource{},161},162}163}164165166