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 Jun 25, 2024
1 parent 9d0d133 commit 2f114f2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
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, p tree.Path, _ bool) (any, error) {

Check failure on line 91 in transform/ports.go

View workflow job for this annotation

GitHub Actions / test (1.22, ubuntu-latest)

unused-parameter: parameter 'p' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 91 in transform/ports.go

View workflow job for this annotation

GitHub Actions / test (1.21, ubuntu-latest)

unused-parameter: parameter 'p' seems to be unused, consider removing or renaming it as _ (revive)
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 2f114f2

Please sign in to comment.