Skip to content

Commit 260bf0d

Browse files
committed
feat: add timeout parameter to buildx builder creation for improved control
1 parent 5e09706 commit 260bf0d

2 files changed

Lines changed: 37 additions & 18 deletions

File tree

ci-runner/helper/DockerHelper.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ import (
2323
"encoding/json"
2424
"errors"
2525
"fmt"
26+
"io"
27+
"io/ioutil"
28+
"log"
29+
"os"
30+
"os/exec"
31+
"path"
32+
"path/filepath"
33+
"strconv"
34+
"strings"
35+
"sync"
36+
"syscall"
37+
"time"
38+
2639
"github.com/aws/aws-sdk-go/aws"
2740
"github.com/aws/aws-sdk-go/aws/credentials"
2841
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
@@ -38,18 +51,6 @@ import (
3851
"github.com/devtron-labs/common-lib/utils/dockerOperations"
3952
"github.com/devtron-labs/common-lib/utils/retryFunc"
4053
"golang.org/x/sync/errgroup"
41-
"io"
42-
"io/ioutil"
43-
"log"
44-
"os"
45-
"os/exec"
46-
"path"
47-
"path/filepath"
48-
"strconv"
49-
"strings"
50-
"sync"
51-
"syscall"
52-
"time"
5354
)
5455

5556
const (
@@ -480,7 +481,7 @@ func (impl *DockerHelperImpl) BuildArtifact(ciRequest *CommonWorkflowRequest) (s
480481
}
481482
useBuildxK8sDriver, eligibleK8sDriverNodes = dockerBuildConfig.CheckForBuildXK8sDriver()
482483
if useBuildxK8sDriver {
483-
deploymentNames, err = impl.createBuildxBuilderWithK8sDriver(ciContext, ciRequest.PropagateLabelsInBuildxPod, ciRequest.DockerConnection, dockerBuildConfig.BuildxDriverImage, eligibleK8sDriverNodes, ciRequest.PipelineId, ciRequest.WorkflowId)
484+
deploymentNames, err = impl.createBuildxBuilderWithK8sDriver(ciContext, ciRequest.PropagateLabelsInBuildxPod, ciRequest.DockerConnection, dockerBuildConfig.BuildxDriverImage, eligibleK8sDriverNodes, ciRequest.PipelineId, ciRequest.WorkflowId, builderPodWaitDuration)
484485
if err != nil {
485486
log.Println(util.DEVTRON, " error in creating buildxDriver , err : ", err.Error())
486487
return err
@@ -1129,14 +1130,14 @@ func (impl *DockerHelperImpl) createBuildxBuilderForMultiArchBuild(ciContext cic
11291130
return nil
11301131
}
11311132

1132-
func (impl *DockerHelperImpl) createBuildxBuilderWithK8sDriver(ciContext cicxt.CiContext, propagateLabelsInBuildxPod bool, dockerConnection, buildxDriverImage string, builderNodes []map[string]string, ciPipelineId, ciWorkflowId int) ([]string, error) {
1133+
func (impl *DockerHelperImpl) createBuildxBuilderWithK8sDriver(ciContext cicxt.CiContext, propagateLabelsInBuildxPod bool, dockerConnection, buildxDriverImage string, builderNodes []map[string]string, ciPipelineId, ciWorkflowId int, timeout time.Duration) ([]string, error) {
11331134
deploymentNames := make([]string, 0)
11341135
if len(builderNodes) == 0 {
11351136
return deploymentNames, errors.New("atleast one node is expected for builder with kubernetes driver")
11361137
}
11371138
for i := 0; i < len(builderNodes); i++ {
11381139
nodeOpts := builderNodes[i]
1139-
builderCmd, deploymentName, err := getBuildxK8sDriverCmd(propagateLabelsInBuildxPod, dockerConnection, buildxDriverImage, nodeOpts, ciPipelineId, ciWorkflowId)
1140+
builderCmd, deploymentName, err := getBuildxK8sDriverCmd(propagateLabelsInBuildxPod, dockerConnection, buildxDriverImage, nodeOpts, ciPipelineId, ciWorkflowId, timeout)
11401141
if err != nil {
11411142
return deploymentNames, err
11421143
}
@@ -1213,7 +1214,7 @@ func (impl *DockerHelperImpl) runCmd(cmd string) (error, *bytes.Buffer) {
12131214
return err, errBuf
12141215
}
12151216

1216-
func getBuildxK8sDriverCmd(propagateLabelsInBuildxPod bool, dockerConnection, buildxDriverImage string, driverOpts map[string]string, ciPipelineId, ciWorkflowId int) (string, string, error) {
1217+
func getBuildxK8sDriverCmd(propagateLabelsInBuildxPod bool, dockerConnection, buildxDriverImage string, driverOpts map[string]string, ciPipelineId, ciWorkflowId int, timeout time.Duration) (string, string, error) {
12171218
buildxCreate := "docker buildx create --buildkitd-flags '--allow-insecure-entitlement network.host --allow-insecure-entitlement security.insecure' --name=%s --driver=kubernetes --node=%s --bootstrap "
12181219
nodeName := driverOpts["node"]
12191220
if nodeName == "" {
@@ -1235,6 +1236,9 @@ func getBuildxK8sDriverCmd(propagateLabelsInBuildxPod bool, dockerConnection, bu
12351236
}
12361237

12371238
driverOpts["driverOptions"] = getBuildXDriverOptionsWithImage(buildxDriverImage, driverOpts["driverOptions"])
1239+
if timeout > 0 {
1240+
driverOpts["driverOptions"] = getBuildXDriverOptionsWithTimeout(timeout, driverOpts["driverOptions"])
1241+
}
12381242
if len(driverOpts["driverOptions"]) > 0 {
12391243
buildxCreate += " '--driver-opt=%s' "
12401244
buildxCreate = fmt.Sprintf(buildxCreate, driverOpts["driverOptions"])
@@ -1259,6 +1263,21 @@ func getBuildXDriverOptionsWithImage(buildxDriverImage, driverOptions string) st
12591263
return driverOptions
12601264
}
12611265

1266+
func getBuildXDriverOptionsWithTimeout(timeout time.Duration, driverOptions string) string {
1267+
if strings.HasPrefix(driverOptions, "timeout=") ||
1268+
strings.Contains(driverOptions, ",timeout=") {
1269+
// if timeout is already present in driver options then do not override it, just return the existing options
1270+
return driverOptions
1271+
}
1272+
timeoutOption := fmt.Sprintf("\"timeout=%s\"", timeout.String())
1273+
if len(driverOptions) > 0 {
1274+
driverOptions += fmt.Sprintf(",%s", timeoutOption)
1275+
} else {
1276+
driverOptions = timeoutOption
1277+
}
1278+
return driverOptions
1279+
}
1280+
12621281
func getBuildXDriverOptionsWithLabelsAndAnnotations(driverOptions string) (string, error) {
12631282
// not passing annotation as of now because --driver-opt=annotations is not supported by buildx if contains quotes
12641283
labels := make(map[string]string)

ci-runner/helper/DockerHelper_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestCreateBuildXK8sDriver(t *testing.T) {
4141
eligibleK8sNodes := dockerBuildConfig.GetEligibleK8sDriverNodes()
4242
impl := getDockerHelperImpl()
4343
ciContext := cicxt.BuildCiContext(context.Background(), true)
44-
_, err := impl.createBuildxBuilderWithK8sDriver(ciContext, false, "", "", eligibleK8sNodes, 1, 1)
44+
_, err := impl.createBuildxBuilderWithK8sDriver(ciContext, false, "", "", eligibleK8sNodes, 1, 1, 0)
4545
t.Cleanup(func() {
4646
buildxDelete := fmt.Sprintf("docker buildx rm %s", BUILDX_K8S_DRIVER_NAME)
4747
builderRemoveCmd := exec.Command("/bin/sh", "-c", buildxDelete)
@@ -64,7 +64,7 @@ func TestCleanBuildxK8sDriver(t *testing.T) {
6464
eligibleK8sNodes := dockerBuildConfig.GetEligibleK8sDriverNodes()
6565
impl := getDockerHelperImpl()
6666
ciContext := cicxt.BuildCiContext(context.Background(), true)
67-
_, err := impl.createBuildxBuilderWithK8sDriver(ciContext, false, "", "", eligibleK8sNodes, 1, 1)
67+
_, err := impl.createBuildxBuilderWithK8sDriver(ciContext, false, "", "", eligibleK8sNodes, 1, 1, 0)
6868
if err != nil {
6969
fmt.Println(err.Error())
7070
t.Fail()

0 commit comments

Comments
 (0)