Skip to content

Commit a60150c

Browse files
authored
Merge pull request docker#1045 from szeber/kubernetes-tolerations
feat: add tolerations handling to kubernetes driver
2 parents adafbe0 + 3f65177 commit a60150c

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

docs/reference/buildx_create.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Passes additional driver-specific options. Details for each driver:
140140
- `limits.cpu` - Sets the limit CPU value specified in units of Kubernetes CPU. Example `limits.cpu=100m`, `limits.cpu=2`
141141
- `limits.memory` - Sets the limit memory value specified in bytes or with a valid suffix. Example `limits.memory=500Mi`, `limits.memory=4G`
142142
- `nodeselector="label1=value1,label2=value2"` - Sets the kv of `Pod` nodeSelector. No Defaults. Example `nodeselector=kubernetes.io/arch=arm64`
143+
- `tolerations="key=foo,value=bar;key=foo2,operator=exists;key=foo3,effect=NoSchedule"` - Sets the `Pod` tolerations. Accepts the same values as the kube manifest tolerations. Key-value pairs are separated by `,`, tolerations are separated by `;`. No Defaults. Example `tolerations=operator=exists`
143144
- `rootless=(true|false)` - Run the container as a non-root user without `securityContext.privileged`. [Using Ubuntu host kernel is recommended](https://github.com/moby/buildkit/blob/master/docs/rootless.md). Defaults to false.
144145
- `loadbalance=(sticky|random)` - Load-balancing strategy. If set to "sticky", the pod is chosen using the hash of the context path. Defaults to "sticky"
145146
- `qemu.install=(true|false)` - Install QEMU emulation for multi platforms support.

driver/kubernetes/factory.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"strconv"
66
"strings"
77

8+
corev1 "k8s.io/api/core/v1"
9+
810
"github.com/docker/buildx/driver"
911
"github.com/docker/buildx/driver/bkimage"
1012
"github.com/docker/buildx/driver/kubernetes/manifest"
@@ -117,6 +119,48 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
117119
}
118120
}
119121
deploymentOpt.NodeSelector = s
122+
case "tolerations":
123+
u, err := strconv.Unquote(v)
124+
if nil != err {
125+
return nil, err
126+
}
127+
ts := strings.Split(u, ";")
128+
deploymentOpt.Tolerations = []corev1.Toleration{}
129+
for i := range ts {
130+
kvs := strings.Split(ts[i], ",")
131+
if len(kvs) == 0 {
132+
return nil, errors.Errorf("invalid tolaration %q", v)
133+
}
134+
135+
t := corev1.Toleration{}
136+
137+
for j := range kvs {
138+
kv := strings.Split(kvs[j], "=")
139+
if len(kv) == 2 {
140+
switch kv[0] {
141+
case "key":
142+
t.Key = kv[1]
143+
case "operator":
144+
t.Operator = corev1.TolerationOperator(kv[1])
145+
case "value":
146+
t.Value = kv[1]
147+
case "effect":
148+
t.Effect = corev1.TaintEffect(kv[1])
149+
case "tolerationSeconds":
150+
c, err := strconv.Atoi(kv[1])
151+
if nil != err {
152+
return nil, err
153+
}
154+
c64 := int64(c)
155+
t.TolerationSeconds = &c64
156+
default:
157+
return nil, errors.Errorf("invalid tolaration %q", v)
158+
}
159+
}
160+
}
161+
162+
deploymentOpt.Tolerations = append(deploymentOpt.Tolerations, t)
163+
}
120164
case "loadbalance":
121165
switch v {
122166
case LoadbalanceSticky:

driver/kubernetes/manifest/manifest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type DeploymentOpt struct {
3232

3333
Rootless bool
3434
NodeSelector map[string]string
35+
Tolerations []corev1.Toleration
3536
RequestsCPU string
3637
RequestsMemory string
3738
LimitsCPU string
@@ -159,6 +160,10 @@ func NewDeployment(opt *DeploymentOpt) (d *appsv1.Deployment, c []*corev1.Config
159160
d.Spec.Template.Spec.NodeSelector = opt.NodeSelector
160161
}
161162

163+
if len(opt.Tolerations) > 0 {
164+
d.Spec.Template.Spec.Tolerations = opt.Tolerations
165+
}
166+
162167
if opt.RequestsCPU != "" {
163168
reqCPU, err := resource.ParseQuantity(opt.RequestsCPU)
164169
if err != nil {

0 commit comments

Comments
 (0)