Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/pkg/components/usage/deployment.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
10
"github.com/gitpod-io/gitpod/common-go/baseserver"
11
"github.com/gitpod-io/gitpod/installer/pkg/cluster"
12
"github.com/gitpod-io/gitpod/installer/pkg/common"
13
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"
14
appsv1 "k8s.io/api/apps/v1"
15
corev1 "k8s.io/api/core/v1"
16
"k8s.io/apimachinery/pkg/api/resource"
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
"k8s.io/apimachinery/pkg/runtime"
19
"k8s.io/apimachinery/pkg/util/intstr"
20
"k8s.io/utils/pointer"
21
)
22
23
const (
24
usageConfigmapVolume = "config"
25
usageConfigMountPath = "/config.json"
26
)
27
28
func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
29
labels := common.CustomizeLabel(ctx, Component, common.TypeMetaDeployment)
30
31
args := []string{
32
"run",
33
fmt.Sprintf("--config=%s", usageConfigMountPath),
34
}
35
36
volumes := []corev1.Volume{
37
{
38
Name: usageConfigmapVolume,
39
VolumeSource: corev1.VolumeSource{
40
ConfigMap: &corev1.ConfigMapVolumeSource{
41
LocalObjectReference: corev1.LocalObjectReference{
42
Name: Component,
43
},
44
},
45
},
46
},
47
}
48
volumeMounts := []corev1.VolumeMount{
49
{
50
Name: usageConfigmapVolume,
51
ReadOnly: true,
52
MountPath: usageConfigMountPath,
53
SubPath: configJSONFilename,
54
},
55
}
56
_ = ctx.WithExperimental(func(cfg *experimental.Config) error {
57
volume, mount, _, ok := getStripeConfig(cfg)
58
if !ok {
59
return nil
60
}
61
62
volumes = append(volumes, volume)
63
volumeMounts = append(volumeMounts, mount)
64
return nil
65
})
66
67
configHash, err := common.ObjectHash(configmap(ctx))
68
if err != nil {
69
return nil, err
70
}
71
72
return []runtime.Object{
73
&appsv1.Deployment{
74
TypeMeta: common.TypeMetaDeployment,
75
ObjectMeta: metav1.ObjectMeta{
76
Name: Component,
77
Namespace: ctx.Namespace,
78
Labels: labels,
79
Annotations: common.CustomizeAnnotation(ctx, Component, common.TypeMetaDeployment, func() map[string]string {
80
return map[string]string{
81
common.AnnotationConfigChecksum: configHash,
82
}
83
}),
84
},
85
Spec: appsv1.DeploymentSpec{
86
Selector: &metav1.LabelSelector{MatchLabels: common.DefaultLabels(Component)},
87
Replicas: common.Replicas(ctx, Component),
88
Strategy: common.DeploymentStrategy,
89
Template: corev1.PodTemplateSpec{
90
ObjectMeta: metav1.ObjectMeta{
91
Name: Component,
92
Namespace: ctx.Namespace,
93
Labels: labels,
94
Annotations: common.CustomizeAnnotation(ctx, Component, common.TypeMetaDeployment),
95
},
96
Spec: corev1.PodSpec{
97
Affinity: cluster.WithNodeAffinityHostnameAntiAffinity(Component, cluster.AffinityLabelMeta),
98
TopologySpreadConstraints: cluster.WithHostnameTopologySpread(Component),
99
ServiceAccountName: Component,
100
EnableServiceLinks: pointer.Bool(false),
101
DNSPolicy: corev1.DNSClusterFirst,
102
RestartPolicy: corev1.RestartPolicyAlways,
103
TerminationGracePeriodSeconds: pointer.Int64(30),
104
InitContainers: []corev1.Container{*common.DatabaseMigrationWaiterContainer(ctx)},
105
Volumes: volumes,
106
Containers: []corev1.Container{{
107
Name: Component,
108
Image: ctx.ImageName(ctx.Config.Repository, Component, ctx.VersionManifest.Components.Usage.Version),
109
Args: args,
110
ImagePullPolicy: corev1.PullIfNotPresent,
111
Resources: common.ResourceRequirements(ctx, Component, Component, corev1.ResourceRequirements{
112
Requests: corev1.ResourceList{
113
"cpu": resource.MustParse("100m"),
114
"memory": resource.MustParse("64Mi"),
115
},
116
}),
117
Ports: []corev1.ContainerPort{},
118
SecurityContext: &corev1.SecurityContext{
119
Privileged: pointer.Bool(false),
120
AllowPrivilegeEscalation: pointer.Bool(false),
121
},
122
Env: common.CustomizeEnvvar(ctx, Component, common.MergeEnv(
123
common.DefaultEnv(&ctx.Config),
124
common.DatabaseEnv(&ctx.Config),
125
common.ConfigcatEnv(ctx),
126
)),
127
VolumeMounts: volumeMounts,
128
LivenessProbe: &corev1.Probe{
129
ProbeHandler: corev1.ProbeHandler{
130
HTTPGet: &corev1.HTTPGetAction{
131
Path: "/live",
132
Port: intstr.IntOrString{IntVal: baseserver.BuiltinHealthPort},
133
Scheme: corev1.URISchemeHTTP,
134
},
135
},
136
FailureThreshold: 3,
137
SuccessThreshold: 1,
138
TimeoutSeconds: 1,
139
},
140
ReadinessProbe: &corev1.Probe{
141
ProbeHandler: corev1.ProbeHandler{
142
HTTPGet: &corev1.HTTPGetAction{
143
Path: "/ready",
144
Port: intstr.IntOrString{IntVal: baseserver.BuiltinHealthPort},
145
Scheme: corev1.URISchemeHTTP,
146
},
147
},
148
FailureThreshold: 3,
149
SuccessThreshold: 1,
150
TimeoutSeconds: 1,
151
},
152
},
153
*common.KubeRBACProxyContainerWithConfig(ctx),
154
},
155
Tolerations: common.WithTolerationWorkspaceComponentNotReady(ctx),
156
},
157
},
158
},
159
},
160
}, nil
161
}
162
163