mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-18 00:47:48 +08:00
Add parameter provisioningTimeout to Kubernetes driver options.
Signed-off-by: Arnold Sobanski <arnold@l4g.dev>
This commit is contained in:
parent
bc83ecb538
commit
53b7cbc5cb
@ -41,15 +41,16 @@ type Driver struct {
|
||||
|
||||
// if you add fields, remember to update docs:
|
||||
// https://github.com/docker/docs/blob/main/content/build/drivers/kubernetes.md
|
||||
minReplicas int
|
||||
deployment *appsv1.Deployment
|
||||
configMaps []*corev1.ConfigMap
|
||||
clientset *kubernetes.Clientset
|
||||
deploymentClient clientappsv1.DeploymentInterface
|
||||
podClient clientcorev1.PodInterface
|
||||
configMapClient clientcorev1.ConfigMapInterface
|
||||
podChooser podchooser.PodChooser
|
||||
defaultLoad bool
|
||||
minReplicas int
|
||||
deployment *appsv1.Deployment
|
||||
configMaps []*corev1.ConfigMap
|
||||
clientset *kubernetes.Clientset
|
||||
deploymentClient clientappsv1.DeploymentInterface
|
||||
podClient clientcorev1.PodInterface
|
||||
configMapClient clientcorev1.ConfigMapInterface
|
||||
podChooser podchooser.PodChooser
|
||||
defaultLoad bool
|
||||
provisioningTimeout time.Duration
|
||||
}
|
||||
|
||||
func (d *Driver) IsMobyDriver() bool {
|
||||
@ -88,7 +89,7 @@ func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
|
||||
}
|
||||
}
|
||||
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 {
|
||||
return d.wait(ctx)
|
||||
})
|
||||
@ -101,22 +102,33 @@ func (d *Driver) wait(ctx context.Context) error {
|
||||
err error
|
||||
depl *appsv1.Deployment
|
||||
)
|
||||
for try := 0; try < 100; try++ {
|
||||
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)
|
||||
}
|
||||
timeoutChan := time.After(d.provisioningTimeout)
|
||||
ticker := time.NewTicker(100 * time.Millisecond)
|
||||
tryCounter := 0
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
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():
|
||||
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) {
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
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.provisioningTimeout = deploymentOpt.ProvisioningTimeout
|
||||
|
||||
d.deploymentClient = clientset.AppsV1().Deployments(namespace)
|
||||
d.podClient = clientset.CoreV1().Pods(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) {
|
||||
deploymentOpt := &manifest.DeploymentOpt{
|
||||
Name: deploymentName,
|
||||
Image: bkimage.DefaultImage,
|
||||
Replicas: 1,
|
||||
BuildkitFlags: cfg.BuildkitdFlags,
|
||||
Rootless: false,
|
||||
Platforms: cfg.Platforms,
|
||||
ConfigFiles: cfg.Files,
|
||||
Name: deploymentName,
|
||||
Image: bkimage.DefaultImage,
|
||||
Replicas: 1,
|
||||
ProvisioningTimeout: 120 * time.Second,
|
||||
BuildkitFlags: cfg.BuildkitdFlags,
|
||||
Rootless: false,
|
||||
Platforms: cfg.Platforms,
|
||||
ConfigFiles: cfg.Files,
|
||||
}
|
||||
|
||||
defaultLoad := false
|
||||
@ -229,6 +233,8 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
|
||||
if err != nil {
|
||||
return nil, "", "", false, err
|
||||
}
|
||||
case "provisioningTimeout":
|
||||
deploymentOpt.ProvisioningTimeout, err = time.ParseDuration(v)
|
||||
default:
|
||||
return nil, "", "", false, errors.Errorf("invalid driver option %s for driver %s", k, DriverName)
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package kubernetes
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/buildx/driver"
|
||||
"github.com/docker/buildx/driver/bkimage"
|
||||
@ -37,22 +38,23 @@ func TestFactory_processDriverOpts(t *testing.T) {
|
||||
t.Run(
|
||||
"ValidOptions", func(t *testing.T) {
|
||||
cfg.DriverOpts = map[string]string{
|
||||
"namespace": "test-ns",
|
||||
"image": "test:latest",
|
||||
"replicas": "2",
|
||||
"requests.cpu": "100m",
|
||||
"requests.memory": "32Mi",
|
||||
"limits.cpu": "200m",
|
||||
"limits.memory": "64Mi",
|
||||
"rootless": "true",
|
||||
"nodeselector": "selector1=value1,selector2=value2",
|
||||
"tolerations": "key=tolerationKey1,value=tolerationValue1,operator=Equal,effect=NoSchedule,tolerationSeconds=60;key=tolerationKey2,operator=Exists",
|
||||
"annotations": "example.com/expires-after=annotation1,example.com/other=annotation2",
|
||||
"labels": "example.com/owner=label1,example.com/other=label2",
|
||||
"loadbalance": "random",
|
||||
"qemu.install": "true",
|
||||
"qemu.image": "qemu:latest",
|
||||
"default-load": "true",
|
||||
"namespace": "test-ns",
|
||||
"image": "test:latest",
|
||||
"replicas": "2",
|
||||
"provisioningTimeout": "300s",
|
||||
"requests.cpu": "100m",
|
||||
"requests.memory": "32Mi",
|
||||
"limits.cpu": "200m",
|
||||
"limits.memory": "64Mi",
|
||||
"rootless": "true",
|
||||
"nodeselector": "selector1=value1,selector2=value2",
|
||||
"tolerations": "key=tolerationKey1,value=tolerationValue1,operator=Equal,effect=NoSchedule,tolerationSeconds=60;key=tolerationKey2,operator=Exists",
|
||||
"annotations": "example.com/expires-after=annotation1,example.com/other=annotation2",
|
||||
"labels": "example.com/owner=label1,example.com/other=label2",
|
||||
"loadbalance": "random",
|
||||
"qemu.install": "true",
|
||||
"qemu.image": "qemu:latest",
|
||||
"default-load": "true",
|
||||
}
|
||||
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:latest", r.Image)
|
||||
require.Equal(t, 2, r.Replicas)
|
||||
require.Equal(t, 300*time.Second, r.ProvisioningTimeout)
|
||||
require.Equal(t, "100m", r.RequestsCPU)
|
||||
require.Equal(t, "32Mi", r.RequestsMemory)
|
||||
require.Equal(t, "200m", r.LimitsCPU)
|
||||
@ -118,6 +121,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
|
||||
require.Equal(t, "test", ns)
|
||||
require.Equal(t, bkimage.DefaultImage, r.Image)
|
||||
require.Equal(t, 1, r.Replicas)
|
||||
require.Equal(t, 120*time.Second, r.ProvisioningTimeout)
|
||||
require.Equal(t, "", r.RequestsCPU)
|
||||
require.Equal(t, "", r.RequestsMemory)
|
||||
require.Equal(t, "", r.LimitsCPU)
|
||||
@ -148,6 +152,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
|
||||
require.Equal(t, "test", ns)
|
||||
require.Equal(t, bkimage.DefaultRootlessImage, r.Image)
|
||||
require.Equal(t, 1, r.Replicas)
|
||||
require.Equal(t, 120*time.Second, r.ProvisioningTimeout)
|
||||
require.Equal(t, "", r.RequestsCPU)
|
||||
require.Equal(t, "", r.RequestsMemory)
|
||||
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(
|
||||
"InvalidRootless", func(t *testing.T) {
|
||||
cfg.DriverOpts = map[string]string{
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/buildx/util/platformutil"
|
||||
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
@ -15,12 +16,13 @@ import (
|
||||
)
|
||||
|
||||
type DeploymentOpt struct {
|
||||
Namespace string
|
||||
Name string
|
||||
Image string
|
||||
Replicas int
|
||||
ServiceAccountName string
|
||||
SchedulerName string
|
||||
Namespace string
|
||||
Name string
|
||||
Image string
|
||||
Replicas int
|
||||
ProvisioningTimeout time.Duration
|
||||
ServiceAccountName string
|
||||
SchedulerName string
|
||||
|
||||
// Qemu
|
||||
Qemu struct {
|
||||
|
Loading…
x
Reference in New Issue
Block a user