Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tolerations handling to kubernetes driver #1045

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/buildx_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Passes additional driver-specific options. Details for each driver:
- `limits.cpu` - Sets the limit CPU value specified in units of Kubernetes CPU. Example `limits.cpu=100m`, `limits.cpu=2`
- `limits.memory` - Sets the limit memory value specified in bytes or with a valid suffix. Example `limits.memory=500Mi`, `limits.memory=4G`
- `nodeselector="label1=value1,label2=value2"` - Sets the kv of `Pod` nodeSelector. No Defaults. Example `nodeselector=kubernetes.io/arch=arm64`
- `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`
- `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.
- `loadbalance=(sticky|random)` - Load-balancing strategy. If set to "sticky", the pod is chosen using the hash of the context path. Defaults to "sticky"
- `qemu.install=(true|false)` - Install QEMU emulation for multi platforms support.
Expand Down
44 changes: 44 additions & 0 deletions driver/kubernetes/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strconv"
"strings"

corev1 "k8s.io/api/core/v1"

"github.com/docker/buildx/driver"
"github.com/docker/buildx/driver/bkimage"
"github.com/docker/buildx/driver/kubernetes/manifest"
Expand Down Expand Up @@ -117,6 +119,48 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
}
}
deploymentOpt.NodeSelector = s
case "tolerations":
u, err := strconv.Unquote(v)
if nil != err {
return nil, err
}
ts := strings.Split(u, ";")
deploymentOpt.Tolerations = []corev1.Toleration{}
for i := range ts {
kvs := strings.Split(ts[i], ",")
if len(kvs) == 0 {
return nil, errors.Errorf("invalid tolaration %q", v)
}

t := corev1.Toleration{}

for j := range kvs {
kv := strings.Split(kvs[j], "=")
if len(kv) == 2 {
switch kv[0] {
case "key":
t.Key = kv[1]
case "operator":
t.Operator = corev1.TolerationOperator(kv[1])
case "value":
t.Value = kv[1]
case "effect":
t.Effect = corev1.TaintEffect(kv[1])
case "tolerationSeconds":
c, err := strconv.Atoi(kv[1])
if nil != err {
return nil, err
}
c64 := int64(c)
t.TolerationSeconds = &c64
default:
return nil, errors.Errorf("invalid tolaration %q", v)
}
}
}

deploymentOpt.Tolerations = append(deploymentOpt.Tolerations, t)
}
case "loadbalance":
switch v {
case LoadbalanceSticky:
Expand Down
5 changes: 5 additions & 0 deletions driver/kubernetes/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type DeploymentOpt struct {

Rootless bool
NodeSelector map[string]string
Tolerations []corev1.Toleration
RequestsCPU string
RequestsMemory string
LimitsCPU string
Expand Down Expand Up @@ -159,6 +160,10 @@ func NewDeployment(opt *DeploymentOpt) (d *appsv1.Deployment, c []*corev1.Config
d.Spec.Template.Spec.NodeSelector = opt.NodeSelector
}

if len(opt.Tolerations) > 0 {
d.Spec.Template.Spec.Tolerations = opt.Tolerations
}

if opt.RequestsCPU != "" {
reqCPU, err := resource.ParseQuantity(opt.RequestsCPU)
if err != nil {
Expand Down