Add parameter provisioningTimeout to Kubernetes driver options.

Signed-off-by: Arnold Sobanski <arnold@l4g.dev>
This commit is contained in:
Arnold Sobanski
2024-05-14 14:45:12 +01:00
committed by CrazyMax
parent bc83ecb538
commit 53b7cbc5cb
4 changed files with 85 additions and 50 deletions

View File

@ -41,15 +41,16 @@ type Driver struct {
// if you add fields, remember to update docs: // if you add fields, remember to update docs:
// https://github.com/docker/docs/blob/main/content/build/drivers/kubernetes.md // https://github.com/docker/docs/blob/main/content/build/drivers/kubernetes.md
minReplicas int minReplicas int
deployment *appsv1.Deployment deployment *appsv1.Deployment
configMaps []*corev1.ConfigMap configMaps []*corev1.ConfigMap
clientset *kubernetes.Clientset clientset *kubernetes.Clientset
deploymentClient clientappsv1.DeploymentInterface deploymentClient clientappsv1.DeploymentInterface
podClient clientcorev1.PodInterface podClient clientcorev1.PodInterface
configMapClient clientcorev1.ConfigMapInterface configMapClient clientcorev1.ConfigMapInterface
podChooser podchooser.PodChooser podChooser podchooser.PodChooser
defaultLoad bool defaultLoad bool
provisioningTimeout time.Duration
} }
func (d *Driver) IsMobyDriver() bool { func (d *Driver) IsMobyDriver() bool {
@ -88,7 +89,7 @@ func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
} }
} }
return sub.Wrap( return sub.Wrap(
fmt.Sprintf("waiting for %d pods to be ready", d.minReplicas), fmt.Sprintf("waiting for %d pods to be ready, timeout: %ds", d.minReplicas, d.provisioningTimeout/time.Second),
func() error { func() error {
return d.wait(ctx) return d.wait(ctx)
}) })
@ -101,22 +102,33 @@ func (d *Driver) wait(ctx context.Context) error {
err error err error
depl *appsv1.Deployment depl *appsv1.Deployment
) )
for try := 0; try < 100; try++ { timeoutChan := time.After(d.provisioningTimeout)
depl, err = d.deploymentClient.Get(ctx, d.deployment.Name, metav1.GetOptions{}) ticker := time.NewTicker(100 * time.Millisecond)
if err == nil { tryCounter := 0
if depl.Status.ReadyReplicas >= int32(d.minReplicas) { defer ticker.Stop()
return nil
} for {
err = errors.Errorf("expected %d replicas to be ready, got %d",
d.minReplicas, depl.Status.ReadyReplicas)
}
select { select {
case <-ticker.C:
if tryCounter < 100 {
tryCounter++
}
ticker.Stop()
ticker = time.NewTicker(time.Duration(100+tryCounter*20) * time.Millisecond)
depl, err = d.deploymentClient.Get(ctx, d.deployment.Name, metav1.GetOptions{})
if err == nil {
if depl.Status.ReadyReplicas >= int32(d.minReplicas) {
return nil
}
err = errors.Errorf("expected %d replicas to be ready, got %d", d.minReplicas, depl.Status.ReadyReplicas)
}
case <-timeoutChan:
return errors.Errorf("timeout after %s: last error: %v", d.provisioningTimeout, err)
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return ctx.Err()
case <-time.After(time.Duration(100+try*20) * time.Millisecond):
} }
} }
return err
} }
func (d *Driver) Info(ctx context.Context) (*driver.Info, error) { func (d *Driver) Info(ctx context.Context) (*driver.Info, error) {

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"strconv" "strconv"
"strings" "strings"
"time"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
@ -82,6 +83,8 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
d.minReplicas = deploymentOpt.Replicas d.minReplicas = deploymentOpt.Replicas
d.provisioningTimeout = deploymentOpt.ProvisioningTimeout
d.deploymentClient = clientset.AppsV1().Deployments(namespace) d.deploymentClient = clientset.AppsV1().Deployments(namespace)
d.podClient = clientset.CoreV1().Pods(namespace) d.podClient = clientset.CoreV1().Pods(namespace)
d.configMapClient = clientset.CoreV1().ConfigMaps(namespace) d.configMapClient = clientset.CoreV1().ConfigMaps(namespace)
@ -104,13 +107,14 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg driver.InitConfig) (*manifest.DeploymentOpt, string, string, bool, error) { func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg driver.InitConfig) (*manifest.DeploymentOpt, string, string, bool, error) {
deploymentOpt := &manifest.DeploymentOpt{ deploymentOpt := &manifest.DeploymentOpt{
Name: deploymentName, Name: deploymentName,
Image: bkimage.DefaultImage, Image: bkimage.DefaultImage,
Replicas: 1, Replicas: 1,
BuildkitFlags: cfg.BuildkitdFlags, ProvisioningTimeout: 120 * time.Second,
Rootless: false, BuildkitFlags: cfg.BuildkitdFlags,
Platforms: cfg.Platforms, Rootless: false,
ConfigFiles: cfg.Files, Platforms: cfg.Platforms,
ConfigFiles: cfg.Files,
} }
defaultLoad := false defaultLoad := false
@ -229,6 +233,8 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
if err != nil { if err != nil {
return nil, "", "", false, err return nil, "", "", false, err
} }
case "provisioningTimeout":
deploymentOpt.ProvisioningTimeout, err = time.ParseDuration(v)
default: default:
return nil, "", "", false, errors.Errorf("invalid driver option %s for driver %s", k, DriverName) return nil, "", "", false, errors.Errorf("invalid driver option %s for driver %s", k, DriverName)
} }

View File

@ -2,6 +2,7 @@ package kubernetes
import ( import (
"testing" "testing"
"time"
"github.com/docker/buildx/driver" "github.com/docker/buildx/driver"
"github.com/docker/buildx/driver/bkimage" "github.com/docker/buildx/driver/bkimage"
@ -37,22 +38,23 @@ func TestFactory_processDriverOpts(t *testing.T) {
t.Run( t.Run(
"ValidOptions", func(t *testing.T) { "ValidOptions", func(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{
"namespace": "test-ns", "namespace": "test-ns",
"image": "test:latest", "image": "test:latest",
"replicas": "2", "replicas": "2",
"requests.cpu": "100m", "provisioningTimeout": "300s",
"requests.memory": "32Mi", "requests.cpu": "100m",
"limits.cpu": "200m", "requests.memory": "32Mi",
"limits.memory": "64Mi", "limits.cpu": "200m",
"rootless": "true", "limits.memory": "64Mi",
"nodeselector": "selector1=value1,selector2=value2", "rootless": "true",
"tolerations": "key=tolerationKey1,value=tolerationValue1,operator=Equal,effect=NoSchedule,tolerationSeconds=60;key=tolerationKey2,operator=Exists", "nodeselector": "selector1=value1,selector2=value2",
"annotations": "example.com/expires-after=annotation1,example.com/other=annotation2", "tolerations": "key=tolerationKey1,value=tolerationValue1,operator=Equal,effect=NoSchedule,tolerationSeconds=60;key=tolerationKey2,operator=Exists",
"labels": "example.com/owner=label1,example.com/other=label2", "annotations": "example.com/expires-after=annotation1,example.com/other=annotation2",
"loadbalance": "random", "labels": "example.com/owner=label1,example.com/other=label2",
"qemu.install": "true", "loadbalance": "random",
"qemu.image": "qemu:latest", "qemu.install": "true",
"default-load": "true", "qemu.image": "qemu:latest",
"default-load": "true",
} }
r, loadbalance, ns, defaultLoad, err := f.processDriverOpts(cfg.Name, "test", cfg) r, loadbalance, ns, defaultLoad, err := f.processDriverOpts(cfg.Name, "test", cfg)
@ -91,6 +93,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
require.Equal(t, "test-ns", ns) require.Equal(t, "test-ns", ns)
require.Equal(t, "test:latest", r.Image) require.Equal(t, "test:latest", r.Image)
require.Equal(t, 2, r.Replicas) require.Equal(t, 2, r.Replicas)
require.Equal(t, 300*time.Second, r.ProvisioningTimeout)
require.Equal(t, "100m", r.RequestsCPU) require.Equal(t, "100m", r.RequestsCPU)
require.Equal(t, "32Mi", r.RequestsMemory) require.Equal(t, "32Mi", r.RequestsMemory)
require.Equal(t, "200m", r.LimitsCPU) require.Equal(t, "200m", r.LimitsCPU)
@ -118,6 +121,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
require.Equal(t, "test", ns) require.Equal(t, "test", ns)
require.Equal(t, bkimage.DefaultImage, r.Image) require.Equal(t, bkimage.DefaultImage, r.Image)
require.Equal(t, 1, r.Replicas) require.Equal(t, 1, r.Replicas)
require.Equal(t, 120*time.Second, r.ProvisioningTimeout)
require.Equal(t, "", r.RequestsCPU) require.Equal(t, "", r.RequestsCPU)
require.Equal(t, "", r.RequestsMemory) require.Equal(t, "", r.RequestsMemory)
require.Equal(t, "", r.LimitsCPU) require.Equal(t, "", r.LimitsCPU)
@ -148,6 +152,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
require.Equal(t, "test", ns) require.Equal(t, "test", ns)
require.Equal(t, bkimage.DefaultRootlessImage, r.Image) require.Equal(t, bkimage.DefaultRootlessImage, r.Image)
require.Equal(t, 1, r.Replicas) require.Equal(t, 1, r.Replicas)
require.Equal(t, 120*time.Second, r.ProvisioningTimeout)
require.Equal(t, "", r.RequestsCPU) require.Equal(t, "", r.RequestsCPU)
require.Equal(t, "", r.RequestsMemory) require.Equal(t, "", r.RequestsMemory)
require.Equal(t, "", r.LimitsCPU) require.Equal(t, "", r.LimitsCPU)
@ -174,6 +179,16 @@ func TestFactory_processDriverOpts(t *testing.T) {
}, },
) )
t.Run(
"InvalidProvisioningTimeout", func(t *testing.T) {
cfg.DriverOpts = map[string]string{
"ProvisioningTimeout": "invalid",
}
_, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err)
},
)
t.Run( t.Run(
"InvalidRootless", func(t *testing.T) { "InvalidRootless", func(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"path" "path"
"strings" "strings"
"time"
"github.com/docker/buildx/util/platformutil" "github.com/docker/buildx/util/platformutil"
v1 "github.com/opencontainers/image-spec/specs-go/v1" v1 "github.com/opencontainers/image-spec/specs-go/v1"
@ -15,12 +16,13 @@ import (
) )
type DeploymentOpt struct { type DeploymentOpt struct {
Namespace string Namespace string
Name string Name string
Image string Image string
Replicas int Replicas int
ServiceAccountName string ProvisioningTimeout time.Duration
SchedulerName string ServiceAccountName string
SchedulerName string
// Qemu // Qemu
Qemu struct { Qemu struct {