@@ -14,6 +14,7 @@ import (
1414
1515const (
1616 HostContainersInternal = "host.containers.internal"
17+ HostGateway = "host-gateway"
1718 localhost = "localhost"
1819)
1920
@@ -98,7 +99,7 @@ func Remove(file string, entries HostEntries) error {
9899
99100// new see comment on New()
100101func newHost (params * Params ) error {
101- entries , err := parseExtraHosts (params .ExtraHosts )
102+ entries , err := parseExtraHosts (params .ExtraHosts , params . HostContainersInternalIP )
102103 if err != nil {
103104 return err
104105 }
@@ -230,7 +231,7 @@ func checkIfEntryExists(current HostEntry, entries HostEntries) bool {
230231// parseExtraHosts converts a slice of "name:ip" string to entries.
231232// Because podman and buildah both store the extra hosts in this format
232233// we convert it here instead of having to this on the caller side.
233- func parseExtraHosts (extraHosts []string ) (HostEntries , error ) {
234+ func parseExtraHosts (extraHosts []string , hostContainersInternalIP string ) (HostEntries , error ) {
234235 entries := make (HostEntries , 0 , len (extraHosts ))
235236 for _ , entry := range extraHosts {
236237 values := strings .SplitN (entry , ":" , 2 )
@@ -243,7 +244,14 @@ func parseExtraHosts(extraHosts []string) (HostEntries, error) {
243244 if values [1 ] == "" {
244245 return nil , fmt .Errorf ("IP address in host entry %q is empty" , entry )
245246 }
246- e := HostEntry {IP : values [1 ], Names : []string {values [0 ]}}
247+ ip := values [1 ]
248+ if values [1 ] == HostGateway {
249+ if hostContainersInternalIP == "" {
250+ return nil , fmt .Errorf ("unable to replace %q of host entry %q: host containers internal IP address is empty" , HostGateway , entry )
251+ }
252+ ip = hostContainersInternalIP
253+ }
254+ e := HostEntry {IP : ip , Names : []string {values [0 ]}}
247255 entries = append (entries , e )
248256 }
249257 return entries , nil
0 commit comments