Skip to content

Commit bdac2d1

Browse files
authored
Merge pull request #245 from ndeloof/merge_extra_hosts
make HostsLists a map[string]string to better handle merging config
2 parents 95e6c14 + acc9418 commit bdac2d1

File tree

5 files changed

+49
-46
lines changed

5 files changed

+49
-46
lines changed

loader/full-struct_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
178178
"project_db_1:mysql",
179179
"project_db_1:postgresql",
180180
},
181-
ExtraHosts: []string{
182-
"somehost:162.242.195.82",
183-
"otherhost:50.31.209.229",
181+
ExtraHosts: types.HostsList{
182+
"somehost": "162.242.195.82",
183+
"otherhost": "50.31.209.229",
184184
},
185185
Extensions: map[string]interface{}{
186186
"x-bar": "baz",
@@ -700,8 +700,8 @@ services:
700700
- project_db_1:mysql
701701
- project_db_1:postgresql
702702
extra_hosts:
703-
- somehost:162.242.195.82
704-
- otherhost:50.31.209.229
703+
otherhost: 50.31.209.229
704+
somehost: 162.242.195.82
705705
hostname: foo
706706
healthcheck:
707707
test:
@@ -1270,10 +1270,10 @@ func fullExampleJSON(workingDir, homeDir string) string {
12701270
"project_db_1:mysql",
12711271
"project_db_1:postgresql"
12721272
],
1273-
"extra_hosts": [
1274-
"somehost:162.242.195.82",
1275-
"otherhost:50.31.209.229"
1276-
],
1273+
"extra_hosts": {
1274+
"otherhost": "50.31.209.229",
1275+
"somehost": "162.242.195.82"
1276+
},
12771277
"hostname": "foo",
12781278
"healthcheck": {
12791279
"test": [

loader/loader.go

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"path/filepath"
2525
"reflect"
2626
"regexp"
27-
"sort"
2827
"strconv"
2928
"strings"
3029
"time"
@@ -385,7 +384,7 @@ func createTransformHook(additionalTransformers ...Transformer) mapstructure.Dec
385384
reflect.TypeOf(types.MappingWithEquals{}): transformMappingOrListFunc("=", true),
386385
reflect.TypeOf(types.Labels{}): transformMappingOrListFunc("=", false),
387386
reflect.TypeOf(types.MappingWithColon{}): transformMappingOrListFunc(":", false),
388-
reflect.TypeOf(types.HostsList{}): transformListOrMappingFunc(":", false),
387+
reflect.TypeOf(types.HostsList{}): transformMappingOrListFunc(":", false),
389388
reflect.TypeOf(types.ServiceVolumeConfig{}): transformServiceVolumeConfig,
390389
reflect.TypeOf(types.BuildConfig{}): transformBuildConfig,
391390
reflect.TypeOf(types.Duration(0)): transformStringToDuration,
@@ -1060,22 +1059,6 @@ func transformMappingOrListFunc(sep string, allowNil bool) TransformerFunc {
10601059
}
10611060
}
10621061

1063-
func transformListOrMappingFunc(sep string, allowNil bool) TransformerFunc {
1064-
return func(data interface{}) (interface{}, error) {
1065-
return transformListOrMapping(data, sep, allowNil)
1066-
}
1067-
}
1068-
1069-
func transformListOrMapping(listOrMapping interface{}, sep string, allowNil bool) (interface{}, error) {
1070-
switch value := listOrMapping.(type) {
1071-
case map[string]interface{}:
1072-
return toStringList(value, sep, allowNil), nil
1073-
case []interface{}:
1074-
return listOrMapping, nil
1075-
}
1076-
return nil, errors.Errorf("expected a map or a list, got %T: %#v", listOrMapping, listOrMapping)
1077-
}
1078-
10791062
func transformMappingOrList(mappingOrList interface{}, sep string, allowNil bool) (interface{}, error) {
10801063
switch value := mappingOrList.(type) {
10811064
case map[string]interface{}:
@@ -1168,15 +1151,3 @@ func toString(value interface{}, allowNil bool) interface{} {
11681151
return ""
11691152
}
11701153
}
1171-
1172-
func toStringList(value map[string]interface{}, separator string, allowNil bool) []string {
1173-
var output []string
1174-
for key, value := range value {
1175-
if value == nil && !allowNil {
1176-
continue
1177-
}
1178-
output = append(output, fmt.Sprintf("%s%s%s", key, separator, value))
1179-
}
1180-
sort.Strings(output)
1181-
return output
1182-
}

loader/loader_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,8 +1232,8 @@ services:
12321232
assert.NilError(t, err)
12331233

12341234
expected := types.HostsList{
1235-
"alpha:50.31.209.229",
1236-
"zulu:162.242.195.82",
1235+
"alpha": "50.31.209.229",
1236+
"zulu": "162.242.195.82",
12371237
}
12381238

12391239
assert.Assert(t, is.Len(config.Services, 1))
@@ -1246,16 +1246,14 @@ services:
12461246
web:
12471247
image: busybox
12481248
extra_hosts:
1249-
- "zulu:162.242.195.82"
12501249
- "alpha:50.31.209.229"
12511250
- "zulu:ff02::1"
12521251
`)
12531252
assert.NilError(t, err)
12541253

12551254
expected := types.HostsList{
1256-
"zulu:162.242.195.82",
1257-
"alpha:50.31.209.229",
1258-
"zulu:ff02::1",
1255+
"alpha": "50.31.209.229",
1256+
"zulu": "ff02::1",
12591257
}
12601258

12611259
assert.Assert(t, is.Len(config.Services, 1))

loader/merge_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,3 +1256,28 @@ func TestMergeEnvironments(t *testing.T) {
12561256
assert.Assert(t, *env["NAME"] == "DEV")
12571257
assert.Assert(t, env["VALUE"] == nil)
12581258
}
1259+
1260+
func TestMergeExtraHosts(t *testing.T) {
1261+
base := types.HostsList{
1262+
"kept": "192.168.1.100",
1263+
"extra1.domain.org": "192.168.1.101",
1264+
"extra2.domain.org": "192.168.1.102",
1265+
}
1266+
override := types.HostsList{
1267+
"extra1.domain.org": "10.0.0.1",
1268+
"extra2.domain.org": "10.0.0.2",
1269+
"added": "10.0.0.3",
1270+
}
1271+
err := mergo.Merge(&base, &override, mergo.WithOverride)
1272+
assert.NilError(t, err)
1273+
assert.DeepEqual(
1274+
t,
1275+
base,
1276+
types.HostsList{
1277+
"kept": "192.168.1.100",
1278+
"extra1.domain.org": "10.0.0.1",
1279+
"extra2.domain.org": "10.0.0.2",
1280+
"added": "10.0.0.3",
1281+
},
1282+
)
1283+
}

types/types.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,16 @@ func (s SSHKey) MarshalJSON() ([]byte, error) {
468468
type MappingWithColon map[string]string
469469

470470
// HostsList is a list of colon-separated host-ip mappings
471-
type HostsList []string
471+
type HostsList map[string]string
472+
473+
// AsList return host-ip mappings as a list of colon-separated strings
474+
func (h HostsList) AsList() []string {
475+
l := make([]string, 0, len(h))
476+
for k, v := range h {
477+
l = append(l, fmt.Sprintf("%s:%s", k, v))
478+
}
479+
return l
480+
}
472481

473482
// LoggingConfig the logging configuration for a service
474483
type LoggingConfig struct {

0 commit comments

Comments
 (0)