-
Notifications
You must be signed in to change notification settings - Fork 8
/
jobdef.go
112 lines (103 loc) · 2.47 KB
/
jobdef.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package buildkit
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)
type JobDefinition struct {
Namespace *corev1.Namespace
StatefulSet *appsv1.StatefulSet
Service *corev1.Service
MatchingLabels map[string]string
}
func JobDef(env []corev1.EnvVar) *JobDefinition {
nsbuild := &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: "ns-build",
},
}
replicas := int32(1)
labels := map[string]string{"app": "buildkitd"}
buildkitd := &appsv1.StatefulSet{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "StatefulSet",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: nsbuild.Name,
Name: "buildkitd",
},
Spec: appsv1.StatefulSetSpec{
ServiceName: "buildkitd",
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "buildkitd",
Image: "moby/buildkit:v0.11.3",
Args: []string{
"--addr", "unix:///run/buildkit/buildkitd.sock",
"--addr", "tcp://0.0.0.0:10000",
},
Env: env,
SecurityContext: &corev1.SecurityContext{
Privileged: pointer.Bool(true),
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "var-lib-buildkit",
MountPath: "/var/lib/buildkit",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "var-lib-buildkit",
// Emptydir.
},
},
},
},
},
}
buildkitService := &corev1.Service{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Service",
},
ObjectMeta: metav1.ObjectMeta{
Name: "buildkitd",
Namespace: nsbuild.Name,
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeNodePort,
Ports: []corev1.ServicePort{{
Port: 10000,
Protocol: corev1.ProtocolTCP,
}},
Selector: labels,
},
}
return &JobDefinition{
Namespace: nsbuild,
StatefulSet: buildkitd,
Service: buildkitService,
MatchingLabels: labels,
}
}