Skip to content

Commit

Permalink
Merge branch 'main' into issue_#1283
Browse files Browse the repository at this point in the history
  • Loading branch information
jjbustamante authored Aug 15, 2024
2 parents ae9a04a + 3a22a7f commit 3e91bc4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strconv"
"time"

"github.com/BurntSushi/toml"
"github.com/buildpacks/lifecycle/api"
Expand Down Expand Up @@ -164,6 +165,8 @@ func (l *LifecycleExecution) PrevImageName() string {
return l.opts.PreviousImage
}

const maxNetworkRemoveRetries = 2

func (l *LifecycleExecution) Run(ctx context.Context, phaseFactoryCreator PhaseFactoryCreator) error {
phaseFactory := phaseFactoryCreator(l)

Expand Down Expand Up @@ -211,15 +214,21 @@ func (l *LifecycleExecution) Run(ctx context.Context, phaseFactoryCreator PhaseF
if l.os == "windows" {
driver = "nat"
}
networkName := fmt.Sprintf("pack.local/network/%x", randString(10))
networkName := fmt.Sprintf("pack.local-network-%x", randString(10))
resp, err := l.docker.NetworkCreate(ctx, networkName, types.NetworkCreate{
Driver: driver,
})
if err != nil {
return fmt.Errorf("failed to create ephemeral %s network: %w", driver, err)
}
defer func() {
_ = l.docker.NetworkRemove(ctx, networkName)
for i := 0; i <= maxNetworkRemoveRetries; i++ {
time.Sleep(100 * time.Duration(i) * time.Millisecond) // wait if retrying
if err = l.docker.NetworkRemove(ctx, networkName); err != nil {
continue
}
break
}
}()
l.logger.Debugf("Created ephemeral bridge network %s with ID %s", networkName, resp.ID)
if resp.Warning != "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/build/lifecycle_execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)

for _, entry := range fakePhaseFactory.NewCalledWithProvider {
h.AssertContains(t, string(entry.HostConfig().NetworkMode), "pack.local/network/")
h.AssertContains(t, string(entry.HostConfig().NetworkMode), "pack.local-network-")
h.AssertEq(t, entry.HostConfig().NetworkMode.IsDefault(), false)
h.AssertEq(t, entry.HostConfig().NetworkMode.IsHost(), false)
h.AssertEq(t, entry.HostConfig().NetworkMode.IsNone(), false)
Expand Down

0 comments on commit 3e91bc4

Please sign in to comment.