k8s: rename timeout opt and move it out of deployment manifest

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-06-03 10:30:06 +02:00
parent 53b7cbc5cb
commit f30e143428
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
4 changed files with 99 additions and 101 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/docker/buildx/store" "github.com/docker/buildx/store"
"github.com/docker/buildx/util/platformutil" "github.com/docker/buildx/util/platformutil"
"github.com/docker/buildx/util/progress" "github.com/docker/buildx/util/progress"
"github.com/docker/go-units"
"github.com/moby/buildkit/client" "github.com/moby/buildkit/client"
"github.com/pkg/errors" "github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
@ -50,7 +51,7 @@ type Driver struct {
configMapClient clientcorev1.ConfigMapInterface configMapClient clientcorev1.ConfigMapInterface
podChooser podchooser.PodChooser podChooser podchooser.PodChooser
defaultLoad bool defaultLoad bool
provisioningTimeout time.Duration timeout time.Duration
} }
func (d *Driver) IsMobyDriver() bool { func (d *Driver) IsMobyDriver() bool {
@ -89,7 +90,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, timeout: %ds", d.minReplicas, d.provisioningTimeout/time.Second), fmt.Sprintf("waiting for %d pods to be ready, timeout: %s", d.minReplicas, units.HumanDuration(d.timeout)),
func() error { func() error {
return d.wait(ctx) return d.wait(ctx)
}) })
@ -102,19 +103,18 @@ func (d *Driver) wait(ctx context.Context) error {
err error err error
depl *appsv1.Deployment depl *appsv1.Deployment
) )
timeoutChan := time.After(d.provisioningTimeout)
timeoutChan := time.After(d.timeout)
ticker := time.NewTicker(100 * time.Millisecond) ticker := time.NewTicker(100 * time.Millisecond)
tryCounter := 0
defer ticker.Stop() defer ticker.Stop()
for { for {
select { select {
case <-ctx.Done():
return ctx.Err()
case <-timeoutChan:
return err
case <-ticker.C: 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) {
@ -122,13 +122,8 @@ func (d *Driver) wait(ctx context.Context) error {
} }
err = errors.Errorf("expected %d replicas to be ready, got %d", d.minReplicas, depl.Status.ReadyReplicas) 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()
} }
} }
} }
func (d *Driver) Info(ctx context.Context) (*driver.Info, error) { func (d *Driver) Info(ctx context.Context) (*driver.Info, error) {

View File

@ -17,8 +17,11 @@ import (
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
) )
const prioritySupported = 40 const (
const priorityUnsupported = 80 prioritySupported = 40
priorityUnsupported = 80
defaultTimeout = 120 * time.Second
)
func init() { func init() {
driver.Register(&factory{}) driver.Register(&factory{})
@ -69,12 +72,13 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
clientset: clientset, clientset: clientset,
} }
deploymentOpt, loadbalance, namespace, defaultLoad, err := f.processDriverOpts(deploymentName, namespace, cfg) deploymentOpt, loadbalance, namespace, defaultLoad, timeout, err := f.processDriverOpts(deploymentName, namespace, cfg)
if nil != err { if nil != err {
return nil, err return nil, err
} }
d.defaultLoad = defaultLoad d.defaultLoad = defaultLoad
d.timeout = timeout
d.deployment, d.configMaps, err = manifest.NewDeployment(deploymentOpt) d.deployment, d.configMaps, err = manifest.NewDeployment(deploymentOpt)
if err != nil { if err != nil {
@ -83,8 +87,6 @@ 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)
@ -105,12 +107,11 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
return d, nil return d, nil
} }
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, time.Duration, error) {
deploymentOpt := &manifest.DeploymentOpt{ deploymentOpt := &manifest.DeploymentOpt{
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,
@ -118,6 +119,7 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
} }
defaultLoad := false defaultLoad := false
timeout := defaultTimeout
deploymentOpt.Qemu.Image = bkimage.QemuImage deploymentOpt.Qemu.Image = bkimage.QemuImage
@ -135,7 +137,7 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
case "replicas": case "replicas":
deploymentOpt.Replicas, err = strconv.Atoi(v) deploymentOpt.Replicas, err = strconv.Atoi(v)
if err != nil { if err != nil {
return nil, "", "", false, err return nil, "", "", false, 0, err
} }
case "requests.cpu": case "requests.cpu":
deploymentOpt.RequestsCPU = v deploymentOpt.RequestsCPU = v
@ -152,7 +154,7 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
case "rootless": case "rootless":
deploymentOpt.Rootless, err = strconv.ParseBool(v) deploymentOpt.Rootless, err = strconv.ParseBool(v)
if err != nil { if err != nil {
return nil, "", "", false, err return nil, "", "", false, 0, err
} }
if _, isImage := cfg.DriverOpts["image"]; !isImage { if _, isImage := cfg.DriverOpts["image"]; !isImage {
deploymentOpt.Image = bkimage.DefaultRootlessImage deploymentOpt.Image = bkimage.DefaultRootlessImage
@ -164,17 +166,17 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
case "nodeselector": case "nodeselector":
deploymentOpt.NodeSelector, err = splitMultiValues(v, ",", "=") deploymentOpt.NodeSelector, err = splitMultiValues(v, ",", "=")
if err != nil { if err != nil {
return nil, "", "", false, errors.Wrap(err, "cannot parse node selector") return nil, "", "", false, 0, errors.Wrap(err, "cannot parse node selector")
} }
case "annotations": case "annotations":
deploymentOpt.CustomAnnotations, err = splitMultiValues(v, ",", "=") deploymentOpt.CustomAnnotations, err = splitMultiValues(v, ",", "=")
if err != nil { if err != nil {
return nil, "", "", false, errors.Wrap(err, "cannot parse annotations") return nil, "", "", false, 0, errors.Wrap(err, "cannot parse annotations")
} }
case "labels": case "labels":
deploymentOpt.CustomLabels, err = splitMultiValues(v, ",", "=") deploymentOpt.CustomLabels, err = splitMultiValues(v, ",", "=")
if err != nil { if err != nil {
return nil, "", "", false, errors.Wrap(err, "cannot parse labels") return nil, "", "", false, 0, errors.Wrap(err, "cannot parse labels")
} }
case "tolerations": case "tolerations":
ts := strings.Split(v, ";") ts := strings.Split(v, ";")
@ -199,12 +201,12 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
case "tolerationSeconds": case "tolerationSeconds":
c, err := strconv.Atoi(kv[1]) c, err := strconv.Atoi(kv[1])
if nil != err { if nil != err {
return nil, "", "", false, err return nil, "", "", false, 0, err
} }
c64 := int64(c) c64 := int64(c)
t.TolerationSeconds = &c64 t.TolerationSeconds = &c64
default: default:
return nil, "", "", false, errors.Errorf("invalid tolaration %q", v) return nil, "", "", false, 0, errors.Errorf("invalid tolaration %q", v)
} }
} }
} }
@ -216,13 +218,13 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
case LoadbalanceSticky: case LoadbalanceSticky:
case LoadbalanceRandom: case LoadbalanceRandom:
default: default:
return nil, "", "", false, errors.Errorf("invalid loadbalance %q", v) return nil, "", "", false, 0, errors.Errorf("invalid loadbalance %q", v)
} }
loadbalance = v loadbalance = v
case "qemu.install": case "qemu.install":
deploymentOpt.Qemu.Install, err = strconv.ParseBool(v) deploymentOpt.Qemu.Install, err = strconv.ParseBool(v)
if err != nil { if err != nil {
return nil, "", "", false, err return nil, "", "", false, 0, err
} }
case "qemu.image": case "qemu.image":
if v != "" { if v != "" {
@ -231,16 +233,19 @@ func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg
case "default-load": case "default-load":
defaultLoad, err = strconv.ParseBool(v) defaultLoad, err = strconv.ParseBool(v)
if err != nil { if err != nil {
return nil, "", "", false, err return nil, "", "", false, 0, err
}
case "timeout":
timeout, err = time.ParseDuration(v)
if err != nil {
return nil, "", "", false, 0, errors.Wrap(err, "cannot parse timeout")
} }
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, 0, errors.Errorf("invalid driver option %s for driver %s", k, DriverName)
} }
} }
return deploymentOpt, loadbalance, namespace, defaultLoad, nil return deploymentOpt, loadbalance, namespace, defaultLoad, timeout, nil
} }
func splitMultiValues(in string, itemsep string, kvsep string) (map[string]string, error) { func splitMultiValues(in string, itemsep string, kvsep string) (map[string]string, error) {

View File

@ -41,7 +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", "timeout": "300s",
"requests.cpu": "100m", "requests.cpu": "100m",
"requests.memory": "32Mi", "requests.memory": "32Mi",
"limits.cpu": "200m", "limits.cpu": "200m",
@ -56,7 +56,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
"qemu.image": "qemu:latest", "qemu.image": "qemu:latest",
"default-load": "true", "default-load": "true",
} }
r, loadbalance, ns, defaultLoad, err := f.processDriverOpts(cfg.Name, "test", cfg) r, loadbalance, ns, defaultLoad, timeout, err := f.processDriverOpts(cfg.Name, "test", cfg)
nodeSelectors := map[string]string{ nodeSelectors := map[string]string{
"selector1": "value1", "selector1": "value1",
@ -93,7 +93,6 @@ 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)
@ -107,6 +106,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
require.True(t, r.Qemu.Install) require.True(t, r.Qemu.Install)
require.Equal(t, "qemu:latest", r.Qemu.Image) require.Equal(t, "qemu:latest", r.Qemu.Image)
require.True(t, defaultLoad) require.True(t, defaultLoad)
require.Equal(t, 300*time.Second, timeout)
}, },
) )
@ -114,14 +114,13 @@ func TestFactory_processDriverOpts(t *testing.T) {
"NoOptions", func(t *testing.T) { "NoOptions", func(t *testing.T) {
cfg.DriverOpts = map[string]string{} cfg.DriverOpts = map[string]string{}
r, loadbalance, ns, defaultLoad, err := f.processDriverOpts(cfg.Name, "test", cfg) r, loadbalance, ns, defaultLoad, timeout, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.NoError(t, err) require.NoError(t, err)
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)
@ -135,6 +134,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
require.False(t, r.Qemu.Install) require.False(t, r.Qemu.Install)
require.Equal(t, bkimage.QemuImage, r.Qemu.Image) require.Equal(t, bkimage.QemuImage, r.Qemu.Image)
require.False(t, defaultLoad) require.False(t, defaultLoad)
require.Equal(t, 120*time.Second, timeout)
}, },
) )
@ -145,14 +145,13 @@ func TestFactory_processDriverOpts(t *testing.T) {
"loadbalance": "sticky", "loadbalance": "sticky",
} }
r, loadbalance, ns, defaultLoad, err := f.processDriverOpts(cfg.Name, "test", cfg) r, loadbalance, ns, defaultLoad, timeout, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.NoError(t, err) require.NoError(t, err)
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)
@ -166,6 +165,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
require.False(t, r.Qemu.Install) require.False(t, r.Qemu.Install)
require.Equal(t, bkimage.QemuImage, r.Qemu.Image) require.Equal(t, bkimage.QemuImage, r.Qemu.Image)
require.False(t, defaultLoad) require.False(t, defaultLoad)
require.Equal(t, 120*time.Second, timeout)
}, },
) )
@ -174,17 +174,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{
"replicas": "invalid", "replicas": "invalid",
} }
_, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) _, _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err)
},
)
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) require.Error(t, err)
}, },
) )
@ -194,7 +184,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{
"rootless": "invalid", "rootless": "invalid",
} }
_, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) _, _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err) require.Error(t, err)
}, },
) )
@ -204,7 +194,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{
"tolerations": "key=foo,value=bar,invalid=foo2", "tolerations": "key=foo,value=bar,invalid=foo2",
} }
_, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) _, _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err) require.Error(t, err)
}, },
) )
@ -214,7 +204,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{
"tolerations": "key=foo,value=bar,tolerationSeconds=invalid", "tolerations": "key=foo,value=bar,tolerationSeconds=invalid",
} }
_, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) _, _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err) require.Error(t, err)
}, },
) )
@ -224,7 +214,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{
"annotations": "key,value", "annotations": "key,value",
} }
_, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) _, _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err) require.Error(t, err)
}, },
) )
@ -234,7 +224,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{
"labels": "key=value=foo", "labels": "key=value=foo",
} }
_, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) _, _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err) require.Error(t, err)
}, },
) )
@ -244,7 +234,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{
"loadbalance": "invalid", "loadbalance": "invalid",
} }
_, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) _, _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err) require.Error(t, err)
}, },
) )
@ -254,7 +244,7 @@ func TestFactory_processDriverOpts(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{
"qemu.install": "invalid", "qemu.install": "invalid",
} }
_, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) _, _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err) require.Error(t, err)
}, },
) )
@ -264,7 +254,17 @@ func TestFactory_processDriverOpts(t *testing.T) {
cfg.DriverOpts = map[string]string{ cfg.DriverOpts = map[string]string{
"invalid": "foo", "invalid": "foo",
} }
_, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) _, _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err)
},
)
t.Run(
"InvalidTimeout", func(t *testing.T) {
cfg.DriverOpts = map[string]string{
"timeout": "invalid",
}
_, _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg)
require.Error(t, err) require.Error(t, err)
}, },
) )

View File

@ -4,7 +4,6 @@ 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"
@ -20,7 +19,6 @@ 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