updateHosts performs a diff between existing host entries, entries to be removed, and entries to be added. Host entries preserve the order in which they were added, as the specification mentions that in case multiple entries for a host exist, the first entry should be used (by default). Note that,
(flags *pflag.FlagSet, hosts *[]string)
| 1190 | // "127.0.0.3 host1", |
| 1191 | // } |
| 1192 | func updateHosts(flags *pflag.FlagSet, hosts *[]string) error { |
| 1193 | var toRemove []hostMapping |
| 1194 | if flags.Changed(flagHostRemove) { |
| 1195 | extraHostsToRemove := flags.Lookup(flagHostRemove).Value.(*opts.ListOpts).GetSlice() |
| 1196 | for _, entry := range extraHostsToRemove { |
| 1197 | hostName, ipAddr, _ := strings.Cut(entry, ":") |
| 1198 | toRemove = append(toRemove, hostMapping{IPAddr: ipAddr, Host: hostName}) |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | var newHosts []string |
| 1203 | for _, entry := range *hosts { |
| 1204 | // Since this is in SwarmKit format, we need to find the key, which is canonical_hostname of: |
| 1205 | // IP_address canonical_hostname [aliases...] |
| 1206 | parts := strings.Fields(entry) |
| 1207 | if len(parts) == 0 { |
| 1208 | continue |
| 1209 | } |
| 1210 | ip := parts[0] |
| 1211 | hostNames := parts[1:] |
| 1212 | for _, rm := range toRemove { |
| 1213 | if rm.IPAddr != "" && rm.IPAddr != ip { |
| 1214 | continue |
| 1215 | } |
| 1216 | for i, h := range hostNames { |
| 1217 | if h == rm.Host { |
| 1218 | hostNames = append(hostNames[:i], hostNames[i+1:]...) |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | if len(hostNames) > 0 { |
| 1223 | newHosts = append(newHosts, fmt.Sprintf("%s %s", ip, strings.Join(hostNames, " "))) |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | // Append new hosts (in SwarmKit format) |
| 1228 | if flags.Changed(flagHostAdd) { |
| 1229 | values := convertExtraHostsToSwarmHosts(flags.Lookup(flagHostAdd).Value.(*opts.ListOpts).GetSlice()) |
| 1230 | newHosts = append(newHosts, values...) |
| 1231 | } |
| 1232 | *hosts = removeDuplicates(newHosts) |
| 1233 | return nil |
| 1234 | } |
| 1235 | |
| 1236 | // updateLogDriver updates the log driver only if the log driver flag is set. |
| 1237 | // All options will be replaced with those provided on the command line. |
searching dependent graphs…