Skip to content

Commit 3336f59

Browse files
committed
set default TCP protocol
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 2bbf153 commit 3336f59

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

loader/loader_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ var samplePortsConfig = []types.ServicePortConfig{
197197
Protocol: "tcp",
198198
},
199199
{
200+
Mode: "ingress",
200201
Target: 53,
201202
Published: "10053",
202203
Protocol: "udp",
@@ -205,6 +206,7 @@ var samplePortsConfig = []types.ServicePortConfig{
205206
Mode: "host",
206207
Target: 22,
207208
Published: "10022",
209+
Protocol: "tcp",
208210
},
209211
}
210212

@@ -856,7 +858,7 @@ networks:
856858
Ports: []types.ServicePortConfig{
857859
{Target: 555, Mode: "ingress", Protocol: "tcp"},
858860
{Target: 34567, Mode: "ingress", Protocol: "tcp"},
859-
{Target: 555, Published: "555", Extensions: map[string]interface{}{"x-foo-bar": true}},
861+
{Target: 555, Mode: "ingress", Protocol: "tcp", Published: "555", Extensions: map[string]interface{}{"x-foo-bar": true}},
860862
},
861863
Ulimits: map[string]*types.UlimitsConfig{
862864
"nproc": {Single: 555},

transform/defaults.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var defaultValues = map[tree.Path]transformFunc{}
2525
func init() {
2626
defaultValues["services.*.build"] = defaultBuildContext
2727
defaultValues["services.*.secrets.*"] = defaultSecretMount
28+
defaultValues["services.*.ports.*"] = portDefaults
2829
}
2930

3031
// SetDefaultValues transforms a compose model to set default values to missing attributes

transform/ports.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,18 @@ func encode(v any) (map[string]any, error) {
8787
err = decoder.Decode(v)
8888
return m, err
8989
}
90+
91+
func portDefaults(data any, _ tree.Path, _ bool) (any, error) {
92+
switch v := data.(type) {
93+
case map[string]any:
94+
if _, ok := v["protocol"]; !ok {
95+
v["protocol"] = "tcp"
96+
}
97+
if _, ok := v["mode"]; !ok {
98+
v["mode"] = "ingress"
99+
}
100+
return v, nil
101+
default:
102+
return data, nil
103+
}
104+
}

transform/ports_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,40 @@ func Test_transformPorts(t *testing.T) {
8787
})
8888
}
8989
}
90+
91+
func Test_portDefaults(t *testing.T) {
92+
tests := []struct {
93+
name string
94+
yaml any
95+
ignoreParseError bool
96+
want any
97+
wantErr string
98+
}{
99+
{
100+
name: "default port",
101+
yaml: map[string]any{
102+
"target": 80,
103+
"published": 8080,
104+
},
105+
want: map[string]any{
106+
"mode": "ingress",
107+
"protocol": "tcp",
108+
"published": 8080,
109+
"target": 80,
110+
},
111+
},
112+
}
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
got, err := portDefaults(tt.yaml, tree.NewPath("services.foo.ports"), tt.ignoreParseError)
116+
if tt.wantErr != "" {
117+
assert.Error(t, err, tt.wantErr)
118+
return
119+
}
120+
assert.NilError(t, err)
121+
if !reflect.DeepEqual(got, tt.want) {
122+
t.Errorf("transformPorts() got = %v, want %v", got, tt.want)
123+
}
124+
})
125+
}
126+
}

0 commit comments

Comments
 (0)