mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-12 06:27:07 +08:00
Add parameter provisioningTimeout to Kubernetes driver options.
Signed-off-by: Arnold Sobanski <arnold@l4g.dev>
This commit is contained in:

committed by
CrazyMax

parent
bc83ecb538
commit
53b7cbc5cb
@ -50,6 +50,7 @@ type Driver struct {
|
|||||||
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)
|
||||||
|
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{})
|
depl, err = d.deploymentClient.Get(ctx, d.deployment.Name, metav1.GetOptions{})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if depl.Status.ReadyReplicas >= int32(d.minReplicas) {
|
if depl.Status.ReadyReplicas >= int32(d.minReplicas) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = errors.Errorf("expected %d replicas to be ready, got %d",
|
err = errors.Errorf("expected %d replicas to be ready, got %d", d.minReplicas, depl.Status.ReadyReplicas)
|
||||||
d.minReplicas, depl.Status.ReadyReplicas)
|
|
||||||
}
|
}
|
||||||
select {
|
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) {
|
||||||
|
@ -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)
|
||||||
@ -107,6 +110,7 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
|
|||||||
Name: deploymentName,
|
Name: deploymentName,
|
||||||
Image: bkimage.DefaultImage,
|
Image: bkimage.DefaultImage,
|
||||||
Replicas: 1,
|
Replicas: 1,
|
||||||
|
ProvisioningTimeout: 120 * time.Second,
|
||||||
BuildkitFlags: cfg.BuildkitdFlags,
|
BuildkitFlags: cfg.BuildkitdFlags,
|
||||||
Rootless: false,
|
Rootless: false,
|
||||||
Platforms: cfg.Platforms,
|
Platforms: cfg.Platforms,
|
||||||
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
@ -40,6 +41,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
|
|||||||
"namespace": "test-ns",
|
"namespace": "test-ns",
|
||||||
"image": "test:latest",
|
"image": "test:latest",
|
||||||
"replicas": "2",
|
"replicas": "2",
|
||||||
|
"provisioningTimeout": "300s",
|
||||||
"requests.cpu": "100m",
|
"requests.cpu": "100m",
|
||||||
"requests.memory": "32Mi",
|
"requests.memory": "32Mi",
|
||||||
"limits.cpu": "200m",
|
"limits.cpu": "200m",
|
||||||
@ -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{
|
||||||
|
@ -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"
|
||||||
@ -19,6 +20,7 @@ type DeploymentOpt struct {
|
|||||||
Name string
|
Name string
|
||||||
Image string
|
Image string
|
||||||
Replicas int
|
Replicas int
|
||||||
|
ProvisioningTimeout time.Duration
|
||||||
ServiceAccountName string
|
ServiceAccountName string
|
||||||
SchedulerName string
|
SchedulerName string
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user