Skip to content

Commit

Permalink
set default TCP protocol
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Jul 2, 2024
1 parent 2bbf153 commit 3336f59
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var samplePortsConfig = []types.ServicePortConfig{
Protocol: "tcp",
},
{
Mode: "ingress",
Target: 53,
Published: "10053",
Protocol: "udp",
Expand All @@ -205,6 +206,7 @@ var samplePortsConfig = []types.ServicePortConfig{
Mode: "host",
Target: 22,
Published: "10022",
Protocol: "tcp",
},
}

Expand Down Expand Up @@ -856,7 +858,7 @@ networks:
Ports: []types.ServicePortConfig{
{Target: 555, Mode: "ingress", Protocol: "tcp"},
{Target: 34567, Mode: "ingress", Protocol: "tcp"},
{Target: 555, Published: "555", Extensions: map[string]interface{}{"x-foo-bar": true}},
{Target: 555, Mode: "ingress", Protocol: "tcp", Published: "555", Extensions: map[string]interface{}{"x-foo-bar": true}},
},
Ulimits: map[string]*types.UlimitsConfig{
"nproc": {Single: 555},
Expand Down
1 change: 1 addition & 0 deletions transform/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var defaultValues = map[tree.Path]transformFunc{}
func init() {
defaultValues["services.*.build"] = defaultBuildContext
defaultValues["services.*.secrets.*"] = defaultSecretMount
defaultValues["services.*.ports.*"] = portDefaults
}

// SetDefaultValues transforms a compose model to set default values to missing attributes
Expand Down
15 changes: 15 additions & 0 deletions transform/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,18 @@ func encode(v any) (map[string]any, error) {
err = decoder.Decode(v)
return m, err
}

func portDefaults(data any, _ tree.Path, _ bool) (any, error) {
switch v := data.(type) {
case map[string]any:
if _, ok := v["protocol"]; !ok {
v["protocol"] = "tcp"
}
if _, ok := v["mode"]; !ok {
v["mode"] = "ingress"
}
return v, nil
default:
return data, nil
}
}
37 changes: 37 additions & 0 deletions transform/ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,40 @@ func Test_transformPorts(t *testing.T) {
})
}
}

func Test_portDefaults(t *testing.T) {
tests := []struct {
name string
yaml any
ignoreParseError bool
want any
wantErr string
}{
{
name: "default port",
yaml: map[string]any{
"target": 80,
"published": 8080,
},
want: map[string]any{
"mode": "ingress",
"protocol": "tcp",
"published": 8080,
"target": 80,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := portDefaults(tt.yaml, tree.NewPath("services.foo.ports"), tt.ignoreParseError)
if tt.wantErr != "" {
assert.Error(t, err, tt.wantErr)
return
}
assert.NilError(t, err)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("transformPorts() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 3336f59

Please sign in to comment.