Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/pkg/common/objects.go
2500 views
1
// Copyright (c) 2021 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 common
6
7
import (
8
corev1 "k8s.io/api/core/v1"
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
"k8s.io/apimachinery/pkg/runtime"
11
"k8s.io/apimachinery/pkg/util/intstr"
12
"k8s.io/utils/pointer"
13
)
14
15
func DefaultServiceAccount(component string) RenderFunc {
16
return func(cfg *RenderContext) ([]runtime.Object, error) {
17
pullSecrets := make([]corev1.LocalObjectReference, 0)
18
19
if len(cfg.Config.ImagePullSecrets) > 0 {
20
for _, i := range cfg.Config.ImagePullSecrets {
21
pullSecrets = append(pullSecrets, corev1.LocalObjectReference{
22
Name: i.Name,
23
})
24
}
25
}
26
27
return []runtime.Object{
28
&corev1.ServiceAccount{
29
TypeMeta: TypeMetaServiceAccount,
30
ObjectMeta: metav1.ObjectMeta{
31
Name: component,
32
Namespace: cfg.Namespace,
33
Labels: CustomizeLabel(cfg, component, TypeMetaConfigmap),
34
Annotations: CustomizeAnnotation(cfg, component, TypeMetaConfigmap),
35
},
36
AutomountServiceAccountToken: pointer.Bool(true),
37
ImagePullSecrets: pullSecrets,
38
},
39
}, nil
40
}
41
}
42
43
type ServicePort struct {
44
Name string
45
ContainerPort int32
46
ServicePort int32
47
}
48
49
func GenerateService(component string, ports []ServicePort, mod ...func(spec *corev1.Service)) RenderFunc {
50
return func(cfg *RenderContext) ([]runtime.Object, error) {
51
var servicePorts []corev1.ServicePort
52
for _, port := range ports {
53
servicePorts = append(servicePorts, corev1.ServicePort{
54
Protocol: *TCPProtocol,
55
Name: port.Name,
56
Port: port.ServicePort,
57
TargetPort: intstr.IntOrString{IntVal: port.ContainerPort},
58
})
59
}
60
61
// kind=service is required for services. It allows Gitpod to find them
62
serviceLabels := DefaultLabels(component)
63
serviceLabels["kind"] = "service"
64
65
service := &corev1.Service{
66
TypeMeta: TypeMetaService,
67
ObjectMeta: metav1.ObjectMeta{
68
Name: component,
69
Namespace: cfg.Namespace,
70
Labels: serviceLabels,
71
Annotations: make(map[string]string),
72
},
73
Spec: corev1.ServiceSpec{
74
Ports: servicePorts,
75
Selector: DefaultLabels(component),
76
Type: corev1.ServiceTypeClusterIP,
77
},
78
}
79
80
for _, m := range mod {
81
// Apply any custom modifications to the spec
82
m(service)
83
}
84
85
// Add in the customizations for labels and annotations
86
service.ObjectMeta.Labels = CustomizeLabel(cfg, component, TypeMetaService, func() map[string]string {
87
return service.ObjectMeta.Labels
88
})
89
service.ObjectMeta.Annotations = CustomizeAnnotation(cfg, component, TypeMetaService, func() map[string]string {
90
return service.ObjectMeta.Annotations
91
})
92
93
return []runtime.Object{service}, nil
94
}
95
}
96
97
// DockerRegistryHash creates a sample pod spec that can be converted into a hash for annotations
98
func DockerRegistryHash(ctx *RenderContext) ([]runtime.Object, error) {
99
if !pointer.BoolDeref(ctx.Config.ContainerRegistry.InCluster, false) {
100
return nil, nil
101
}
102
103
return []runtime.Object{&corev1.Pod{Spec: corev1.PodSpec{
104
Containers: []corev1.Container{{
105
Env: []corev1.EnvVar{{
106
Name: "DOCKER_REGISTRY_USERNAME",
107
Value: ctx.Values.InternalRegistryUsername,
108
}, {
109
Name: "DOCKER_REGISTRY_PASSWORD",
110
Value: ctx.Values.InternalRegistryPassword,
111
}},
112
}},
113
}}}, nil
114
}
115
116