Skip to content

Commit 43b7a90

Browse files
committed
converting identifier to name and supporting similar pattern employed by PortForwardOpts
1 parent 39d9cd7 commit 43b7a90

File tree

1 file changed

+79
-22
lines changed

1 file changed

+79
-22
lines changed

pkg/fwdIp/fwdIp.go

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ type ForwardConfiguration struct {
3939
}
4040

4141
type ServiceConfiguration struct {
42-
Identifier string `yaml:"identifier"`
43-
IP string `yaml:"ip"`
42+
Name string `yaml:"name"`
43+
IP string `yaml:"ip"`
4444
}
4545

4646
var ipRegistry *Registry
@@ -160,37 +160,28 @@ func getBaseUnreservedIP(opts ForwardIPOpts) []byte {
160160
func getConfigurationForService(opts ForwardIPOpts) *ServiceConfiguration {
161161
fwdCfg := getForwardConfiguration(opts)
162162

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
167166
}
168167
}
169168
return nil
170169
}
171170

172171
func applyCLIPassedReservations(opts ForwardIPOpts, f *ForwardConfiguration) *ForwardConfiguration {
173172
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+
181175
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
185179
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)
187181
}
188182
}
189183
if !overridden {
190-
f.ServiceConfigurations = append(f.ServiceConfigurations, &ServiceConfiguration{
191-
Identifier: identifier,
192-
IP: ipStr,
193-
})
184+
f.ServiceConfigurations = append(f.ServiceConfigurations, res)
194185
}
195186
}
196187
return f
@@ -227,6 +218,72 @@ func getForwardConfiguration(opts ForwardIPOpts) *ForwardConfiguration {
227218
return applyCLIPassedReservations(opts, forwardConfiguration)
228219
}
229220

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+
230273
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
232289
}

0 commit comments

Comments
 (0)