@@ -39,8 +39,8 @@ type ForwardConfiguration struct {
39
39
}
40
40
41
41
type ServiceConfiguration struct {
42
- Identifier string `yaml:"identifier "`
43
- IP string `yaml:"ip"`
42
+ Name string `yaml:"name "`
43
+ IP string `yaml:"ip"`
44
44
}
45
45
46
46
var ipRegistry * Registry
@@ -160,37 +160,28 @@ func getBaseUnreservedIP(opts ForwardIPOpts) []byte {
160
160
func getConfigurationForService (opts ForwardIPOpts ) * ServiceConfiguration {
161
161
fwdCfg := getForwardConfiguration (opts )
162
162
163
- for _ , c := range fwdCfg .ServiceConfigurations {
164
- toMatch := fmt .Sprintf ("%s.%s.%s" , opts .Context , opts .Namespace , opts .ServiceName )
165
- if c .Identifier == toMatch {
166
- return c
163
+ for _ , svcCfg := range fwdCfg .ServiceConfigurations {
164
+ if svcCfg .Matches (opts ) {
165
+ return svcCfg
167
166
}
168
167
}
169
168
return nil
170
169
}
171
170
172
171
func applyCLIPassedReservations (opts ForwardIPOpts , f * ForwardConfiguration ) * ForwardConfiguration {
173
172
for _ , resStr := range opts .ForwardIPReservations {
174
- parts := strings .Split (resStr , ":" )
175
- if len (parts ) != 2 {
176
- continue // invalid syntax
177
- }
178
- // find any existing
179
- identifier := parts [0 ]
180
- ipStr := parts [1 ]
173
+ res := ServiceConfigurationFromReservation (resStr )
174
+
181
175
overridden := false
182
- for _ , c := range f .ServiceConfigurations {
183
- if c . Identifier == identifier {
184
- c .IP = ipStr
176
+ for _ , svcCfg := range f .ServiceConfigurations {
177
+ if svcCfg . MatchesName ( res ) {
178
+ svcCfg .IP = res . IP
185
179
overridden = true
186
- log .Infof ("cli reservation flag overriding config for %s now %s" , c . Identifier , c .IP )
180
+ log .Infof ("cli reservation flag overriding config for %s now %s" , svcCfg . Name , svcCfg .IP )
187
181
}
188
182
}
189
183
if ! overridden {
190
- f .ServiceConfigurations = append (f .ServiceConfigurations , & ServiceConfiguration {
191
- Identifier : identifier ,
192
- IP : ipStr ,
193
- })
184
+ f .ServiceConfigurations = append (f .ServiceConfigurations , res )
194
185
}
195
186
}
196
187
return f
@@ -227,6 +218,72 @@ func getForwardConfiguration(opts ForwardIPOpts) *ForwardConfiguration {
227
218
return applyCLIPassedReservations (opts , forwardConfiguration )
228
219
}
229
220
221
+ func (o ForwardIPOpts ) MatchList () []string {
222
+ if o .ClusterN == 0 && o .NamespaceN == 0 {
223
+ return []string {
224
+ fmt .Sprintf ("%s" , o .ServiceName ),
225
+ fmt .Sprintf ("%s.%s" , o .ServiceName , o .Namespace ),
226
+ fmt .Sprintf ("%s.%s" , o .ServiceName , o .Context ),
227
+ fmt .Sprintf ("%s.%s.svc" , o .ServiceName , o .Namespace ),
228
+ fmt .Sprintf ("%s.%s.svc.cluster.local" , o .ServiceName , o .Namespace ),
229
+ fmt .Sprintf ("%s.%s.%s" , o .ServiceName , o .Namespace , o .Context ),
230
+ fmt .Sprintf ("%s.%s.svc.%s" , o .ServiceName , o .Namespace , o .Context ),
231
+ fmt .Sprintf ("%s.%s.svc.cluster.%s" , o .ServiceName , o .Namespace , o .Context ),
232
+ }
233
+ }
234
+
235
+ if o .ClusterN > 0 && o .NamespaceN == 0 {
236
+ return []string {
237
+ fmt .Sprintf ("%s.%s" , o .ServiceName , o .Context ),
238
+ fmt .Sprintf ("%s.%s.%s" , o .ServiceName , o .Namespace , o .Context ),
239
+ fmt .Sprintf ("%s.%s.svc.%s" , o .ServiceName , o .Namespace , o .Context ),
240
+ fmt .Sprintf ("%s.%s.svc.cluster.%s" , o .ServiceName , o .Namespace , o .Context ),
241
+ }
242
+ }
243
+
244
+ if o .ClusterN == 0 && o .NamespaceN > 0 {
245
+ return []string {
246
+ fmt .Sprintf ("%s.%s" , o .ServiceName , o .Namespace ),
247
+ fmt .Sprintf ("%s.%s.svc" , o .ServiceName , o .Namespace ),
248
+ fmt .Sprintf ("%s.%s.%s" , o .ServiceName , o .Namespace , o .Context ),
249
+ fmt .Sprintf ("%s.%s.svc.%s" , o .ServiceName , o .Namespace , o .Context ),
250
+ fmt .Sprintf ("%s.%s.svc.cluster.local" , o .ServiceName , o .Namespace ),
251
+ fmt .Sprintf ("%s.%s.svc.cluster.%s" , o .ServiceName , o .Namespace , o .Context ),
252
+ }
253
+ }
254
+
255
+ return []string {
256
+ fmt .Sprintf ("%s.%s.%s" , o .ServiceName , o .Namespace , o .Context ),
257
+ fmt .Sprintf ("%s.%s.svc.%s" , o .ServiceName , o .Namespace , o .Context ),
258
+ fmt .Sprintf ("%s.%s.svc.cluster.%s" , o .ServiceName , o .Namespace , o .Context ),
259
+ }
260
+ }
261
+
262
+ func ServiceConfigurationFromReservation (reservation string ) * ServiceConfiguration {
263
+ parts := strings .Split (reservation , ":" )
264
+ if len (parts ) != 2 {
265
+ return nil
266
+ }
267
+ return & ServiceConfiguration {
268
+ Name : parts [0 ],
269
+ IP : parts [1 ],
270
+ }
271
+ }
272
+
230
273
func (c ServiceConfiguration ) String () string {
231
- return fmt .Sprintf ("ID: %s IP:%s" , c .Identifier , c .IP )
274
+ return fmt .Sprintf ("Name: %s IP:%s" , c .Name , c .IP )
275
+ }
276
+
277
+ func (c ServiceConfiguration ) Matches (opts ForwardIPOpts ) bool {
278
+ matchList := opts .MatchList ()
279
+ for _ , toMatch := range matchList {
280
+ if c .Name == toMatch {
281
+ return true
282
+ }
283
+ }
284
+ return false
285
+ }
286
+
287
+ func (c ServiceConfiguration ) MatchesName (otherCfg * ServiceConfiguration ) bool {
288
+ return c .Name == otherCfg .Name
232
289
}
0 commit comments