-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathworkload.go
186 lines (157 loc) · 6.36 KB
/
workload.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package builder
import (
"fmt"
"strings"
"github.com/tendermint/tendermint/libs/log"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
manifesttypes "github.com/ovrclk/akash/manifest/v2beta1"
clusterUtil "github.com/ovrclk/akash/provider/cluster/util"
"github.com/ovrclk/akash/sdl"
mtypes "github.com/ovrclk/akash/x/market/types/v1beta2"
)
type workloadBase interface {
builderBase
Name() string
}
type workload struct {
builder
service *manifesttypes.Service
runtimeClassName string
}
var _ workloadBase = (*workload)(nil)
func newWorkloadBuilder(log log.Logger, settings Settings, lid mtypes.LeaseID, group *manifesttypes.Group, service *manifesttypes.Service) workload {
return workload{
builder: builder{
settings: settings,
log: log.With("module", "kube-builder"),
lid: lid,
group: group,
},
service: service,
runtimeClassName: settings.DeploymentRuntimeClass,
}
}
func (b *workload) container() corev1.Container {
falseValue := false
kcontainer := corev1.Container{
Name: b.service.Name,
Image: b.service.Image,
Command: b.service.Command,
Args: b.service.Args,
Resources: corev1.ResourceRequirements{
Limits: make(corev1.ResourceList),
Requests: make(corev1.ResourceList),
},
ImagePullPolicy: corev1.PullIfNotPresent,
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: &falseValue,
Privileged: &falseValue,
AllowPrivilegeEscalation: &falseValue,
},
}
if cpu := b.service.Resources.CPU; cpu != nil {
requestedCPU := clusterUtil.ComputeCommittedResources(b.settings.CPUCommitLevel, cpu.Units)
kcontainer.Resources.Requests[corev1.ResourceCPU] = resource.NewScaledQuantity(int64(requestedCPU.Value()), resource.Milli).DeepCopy()
kcontainer.Resources.Limits[corev1.ResourceCPU] = resource.NewScaledQuantity(int64(cpu.Units.Value()), resource.Milli).DeepCopy()
}
if mem := b.service.Resources.Memory; mem != nil {
requestedMem := clusterUtil.ComputeCommittedResources(b.settings.MemoryCommitLevel, mem.Quantity)
kcontainer.Resources.Requests[corev1.ResourceMemory] = resource.NewQuantity(int64(requestedMem.Value()), resource.DecimalSI).DeepCopy()
kcontainer.Resources.Limits[corev1.ResourceMemory] = resource.NewQuantity(int64(mem.Quantity.Value()), resource.DecimalSI).DeepCopy()
}
for _, ephemeral := range b.service.Resources.Storage {
attr := ephemeral.Attributes.Find(sdl.StorageAttributePersistent)
if persistent, _ := attr.AsBool(); !persistent {
requestedStorage := clusterUtil.ComputeCommittedResources(b.settings.StorageCommitLevel, ephemeral.Quantity)
kcontainer.Resources.Requests[corev1.ResourceEphemeralStorage] = resource.NewQuantity(int64(requestedStorage.Value()), resource.DecimalSI).DeepCopy()
kcontainer.Resources.Limits[corev1.ResourceEphemeralStorage] = resource.NewQuantity(int64(ephemeral.Quantity.Value()), resource.DecimalSI).DeepCopy()
break
}
}
if b.service.Params != nil {
for _, params := range b.service.Params.Storage {
kcontainer.VolumeMounts = append(kcontainer.VolumeMounts, corev1.VolumeMount{
// matches VolumeName in persistentVolumeClaims below
Name: fmt.Sprintf("%s-%s", b.service.Name, params.Name),
ReadOnly: params.ReadOnly,
MountPath: params.Mount,
})
}
}
envVarsAdded := make(map[string]int)
for _, env := range b.service.Env {
parts := strings.SplitN(env, "=", 2)
switch len(parts) {
case 2:
kcontainer.Env = append(kcontainer.Env, corev1.EnvVar{Name: parts[0], Value: parts[1]})
case 1:
kcontainer.Env = append(kcontainer.Env, corev1.EnvVar{Name: parts[0]})
}
envVarsAdded[parts[0]] = 0
}
kcontainer.Env = b.addEnvVarsForDeployment(envVarsAdded, kcontainer.Env)
for _, expose := range b.service.Expose {
kcontainer.Ports = append(kcontainer.Ports, corev1.ContainerPort{
ContainerPort: int32(expose.Port),
})
}
return kcontainer
}
func (b *workload) persistentVolumeClaims() []corev1.PersistentVolumeClaim {
var pvcs []corev1.PersistentVolumeClaim // nolint:prealloc
for _, storage := range b.service.Resources.Storage {
attr := storage.Attributes.Find(sdl.StorageAttributePersistent)
if persistent, valid := attr.AsBool(); !valid || !persistent {
continue
}
volumeMode := corev1.PersistentVolumeFilesystem
pvc := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s", b.service.Name, storage.Name),
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.ResourceRequirements{
Limits: make(corev1.ResourceList),
Requests: make(corev1.ResourceList),
},
VolumeMode: &volumeMode,
StorageClassName: nil,
DataSource: nil, // bind to existing pvc. akash does not support it. yet
},
}
pvc.Spec.Resources.Requests[corev1.ResourceStorage] = resource.NewQuantity(int64(storage.Quantity.Value()), resource.DecimalSI).DeepCopy()
attr = storage.Attributes.Find(sdl.StorageAttributeClass)
if class, valid := attr.AsString(); valid && class != sdl.StorageClassDefault {
pvc.Spec.StorageClassName = &class
}
pvcs = append(pvcs, pvc)
}
return pvcs
}
func (b *workload) Name() string {
return b.service.Name
}
func (b *workload) labels() map[string]string {
obj := b.builder.labels()
obj[AkashManifestServiceLabelName] = b.service.Name
return obj
}
func (b *workload) imagePullSecrets() []corev1.LocalObjectReference {
if b.settings.DockerImagePullSecretsName == "" {
return nil
}
return []corev1.LocalObjectReference{{Name: b.settings.DockerImagePullSecretsName}}
}
func (b *workload) addEnvVarsForDeployment(envVarsAlreadyAdded map[string]int, env []corev1.EnvVar) []corev1.EnvVar {
// Add each env. var. if it is not already set by the SDL
env = addIfNotPresent(envVarsAlreadyAdded, env, envVarAkashGroupSequence, b.lid.GetGSeq())
env = addIfNotPresent(envVarsAlreadyAdded, env, envVarAkashDeploymentSequence, b.lid.GetDSeq())
env = addIfNotPresent(envVarsAlreadyAdded, env, envVarAkashOrderSequence, b.lid.GetOSeq())
env = addIfNotPresent(envVarsAlreadyAdded, env, envVarAkashOwner, b.lid.Owner)
env = addIfNotPresent(envVarsAlreadyAdded, env, envVarAkashProvider, b.lid.Provider)
env = addIfNotPresent(envVarsAlreadyAdded, env, envVarAkashClusterPublicHostname, b.settings.ClusterPublicHostname)
return env
}