Skip to content

Commit

Permalink
fix(buildkit): return the image's exposed ports in ascending order
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Jun 29, 2023
1 parent e9a0309 commit a50ec83
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
7 changes: 1 addition & 6 deletions pkg/build/buildkit/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,11 @@ func extractContainerImageConfigFromImageManifest(ctx context.Context, imageStr
return nil, err
}

var exposedPorts []string
for k := range cf.Config.ExposedPorts {
exposedPorts = append(exposedPorts, k)
}

return &pb.ContainerImageConfig{
Entrypoint: cf.Config.Entrypoint,
Cmd: cf.Config.Cmd,
WorkingDir: cf.Config.WorkingDir,
ExposedPorts: exposedPorts,
ExposedPorts: build.SortExposedPorts(cf.Config.ExposedPorts),
}, nil
}

Expand Down
30 changes: 30 additions & 0 deletions pkg/build/buildkit/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,36 @@ EXPOSE 8888/tcp
},
}, appFiles)
})

t.Run("multiple exposed ports, should ensure the ascending order of ports", func(t *testing.T) {
destImage := baseRegistry(t, "my-app", "")

dockerfile := `FROM busybox
EXPOSE 100/udp 53/udp 443/udp
EXPOSE 8080/tcp 80/tcp 8000/tcp 9090 8888
`
req := &pb.BuildRequest{
Kind: pb.BuildKind_BUILD_KIND_APP_BUILD_WITH_CONTAINER_FILE,
App: &pb.TsuruApp{
Name: "my-app",
},
DestinationImages: []string{destImage},
Containerfile: string(dockerfile),
PushOptions: &pb.PushOptions{
InsecureRegistry: registryHTTP,
},
}

appFiles, err := NewBuildKit(bc, BuildKitOptions{TempDir: t.TempDir()}).Build(context.TODO(), req, os.Stdout)
require.NoError(t, err)
assert.Equal(t, &pb.TsuruConfig{
ImageConfig: &pb.ContainerImageConfig{
Cmd: []string{"sh"},
ExposedPorts: []string{"53/udp", "80/tcp", "100/udp", "443/udp", "8000/tcp", "8080/tcp", "8888/tcp", "9090/tcp"},
},
}, appFiles)
})
}

func compressGZIP(t *testing.T, path string) []byte {
Expand Down
29 changes: 29 additions & 0 deletions pkg/build/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"fmt"
"io"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"text/template"

Expand Down Expand Up @@ -264,3 +267,29 @@ func (w *BuildResponseOutputWriter) Fd() uintptr { // required to implement cons
func (w *BuildResponseOutputWriter) Name() string { // required to implement console.File
return ""
}

func SortExposedPorts(ports map[string]struct{}) []string {
var ps []string
for p := range ports {
ps = append(ps, p)
}

sort.Slice(ps, func(i, j int) bool {
a, b := ps[i], ps[j]

// Example of exposed port: "8080/tcp"
portAStr, protoAStr, _ := strings.Cut(a, "/")
portBStr, protoBStr, _ := strings.Cut(b, "/")

portA, _ := strconv.Atoi(portAStr)
portB, _ := strconv.Atoi(portBStr)

if portA == portB {
return protoAStr < protoBStr
}

return portA < portB
})

return ps
}
25 changes: 25 additions & 0 deletions pkg/build/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,28 @@ RUN --mount=type=secret,id=tsuru-app-envvars,target=/var/run/secrets/envs.sh,uid
})
}
}

func TestSortExposedPorts(t *testing.T) {
t.Parallel()

tests := []struct {
ports map[string]struct{}
expected []string
}{
{},
{
ports: map[string]struct{}{"8888/tcp": {}},
expected: []string{"8888/tcp"},
},
{
ports: map[string]struct{}{"8888/tcp": {}, "80/tcp": {}, "53/udp": {}, "80/udp": {}, "8000/tcp": {}},
expected: []string{"53/udp", "80/tcp", "80/udp", "8000/tcp", "8888/tcp"},
},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
assert.Equal(t, tt.expected, SortExposedPorts(tt.ports))
})
}
}

0 comments on commit a50ec83

Please sign in to comment.