Skip to content

Commit

Permalink
Merge pull request #2241 from buildpacks/fix/network-name
Browse files Browse the repository at this point in the history
Fix ephemeral bridge network name for podman
  • Loading branch information
natalieparellano authored Aug 5, 2024
2 parents 5d0687f + b44f845 commit 3a22a7f
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

1 comment on commit 3a22a7f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Go Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 3a22a7f Previous: 5d0687f Ratio
BenchmarkBuild/with_Trusted_Builder 2612231779 ns/op 1131890547 ns/op 2.31

This comment was automatically generated by workflow using github-action-benchmark.

CC: @buildpacks/platform-maintainers

Please sign in to comment.