Skip to content

Commit 9cf2d18

Browse files
committed
fix: service insight for cross namespace configuration
1 parent 2f0cb58 commit 9cf2d18

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

internal/configs/configurator.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ func (cnf *Configurator) AddOrUpdateIngress(ingEx *IngressEx) (Warnings, error)
311311
return warnings, nil
312312
}
313313

314+
// virtualServerExForHost takes a hostname and returns a VirtualServerEx for the given hostname.
315+
func (cnf *Configurator) virtualServerExForHost(hostname string) *VirtualServerEx {
316+
for _, vsEx := range cnf.virtualServers {
317+
if vsEx.VirtualServer.Spec.Host == hostname {
318+
return vsEx
319+
}
320+
}
321+
return nil
322+
}
323+
314324
// virtualServerForHost takes a hostname and returns a VS for the given hostname.
315325
func (cnf *Configurator) virtualServerForHost(hostname string) *conf_v1.VirtualServer {
316326
for _, vsEx := range cnf.virtualServers {
@@ -321,11 +331,13 @@ func (cnf *Configurator) virtualServerForHost(hostname string) *conf_v1.VirtualS
321331
return nil
322332
}
323333

324-
// upstreamsForVirtualServer takes VirtualServer and returns a list of associated upstreams.
325-
func (cnf *Configurator) upstreamsForVirtualServer(vs *conf_v1.VirtualServer) []string {
334+
// upstreamsForVirtualServer takes a VirtualServerEx and returns a list of associated upstreams.
335+
func (cnf *Configurator) upstreamsForVirtualServer(vsex *VirtualServerEx) []string {
326336
l := nl.LoggerFromContext(cnf.CfgParams.Context)
337+
vs := vsex.VirtualServer
338+
var upstreamNames []string
339+
327340
nl.Debugf(l, "Get upstreamName for vs: %s", vs.Spec.Host)
328-
upstreamNames := make([]string, 0, len(vs.Spec.Upstreams))
329341

330342
virtualServerUpstreamNamer := NewUpstreamNamerForVirtualServer(vs)
331343

@@ -334,16 +346,27 @@ func (cnf *Configurator) upstreamsForVirtualServer(vs *conf_v1.VirtualServer) []
334346
nl.Debugf(l, "upstream: %s, upstreamName: %s", u.Name, upstreamName)
335347
upstreamNames = append(upstreamNames, upstreamName)
336348
}
349+
350+
for _, vsr := range vsex.VirtualServerRoutes {
351+
upstreamNamer := NewUpstreamNamerForVirtualServerRoute(vs, vsr)
352+
for _, u := range vsr.Spec.Upstreams {
353+
upstreamName := upstreamNamer.GetNameForUpstream(u.Name)
354+
nl.Debugf(l, "upstream: %s, upstreamName: %s", u.Name, upstreamName)
355+
upstreamNames = append(upstreamNames, upstreamName)
356+
357+
}
358+
}
359+
337360
return upstreamNames
338361
}
339362

340363
// UpstreamsForHost takes a hostname and returns upstreams for the given hostname.
341364
func (cnf *Configurator) UpstreamsForHost(hostname string) []string {
342365
l := nl.LoggerFromContext(cnf.CfgParams.Context)
343366
nl.Debugf(l, "Get upstream for host: %s", hostname)
344-
vs := cnf.virtualServerForHost(hostname)
345-
if vs != nil {
346-
return cnf.upstreamsForVirtualServer(vs)
367+
vsex := cnf.virtualServerExForHost(hostname)
368+
if vsex != nil {
369+
return cnf.upstreamsForVirtualServer(vsex)
347370
}
348371
return nil
349372
}

internal/configs/configurator_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,24 @@ func TestUpstreamsForHost_ReturnsNilForNoVirtualServers(t *testing.T) {
15631563
}
15641564
}
15651565

1566+
func TestUpstreamsForHost_VirtualServerRoutes(t *testing.T) {
1567+
t.Parallel()
1568+
1569+
tcnf := createTestConfigurator(t)
1570+
tcnf.virtualServers = map[string]*VirtualServerEx{
1571+
"vs": validVirtualServerExWithRouteUpstreams,
1572+
}
1573+
1574+
want := []string{
1575+
"vs_default_cafe-vs_vsr_tea_tea-app_tea",
1576+
"vs_default_cafe-vs_vsr_coffee_coffee-app_coffee",
1577+
}
1578+
got := tcnf.UpstreamsForHost("cafe.example.com")
1579+
if !cmp.Equal(want, got) {
1580+
t.Error(cmp.Diff(want, got))
1581+
}
1582+
}
1583+
15661584
func TestUpstreamsForHost_DoesNotReturnUpstreamsOnBogusHostname(t *testing.T) {
15671585
t.Parallel()
15681586

@@ -1834,6 +1852,55 @@ var (
18341852
},
18351853
}
18361854

1855+
validVirtualServerExWithRouteUpstreams = &VirtualServerEx{
1856+
VirtualServerRoutes: []*conf_v1.VirtualServerRoute{
1857+
{
1858+
ObjectMeta: meta_v1.ObjectMeta{
1859+
Name: "tea-app",
1860+
Namespace: "tea",
1861+
},
1862+
Spec: conf_v1.VirtualServerRouteSpec{
1863+
Upstreams: []conf_v1.Upstream{
1864+
{
1865+
Name: "tea",
1866+
Service: "tea-svc",
1867+
},
1868+
},
1869+
},
1870+
},
1871+
{
1872+
ObjectMeta: meta_v1.ObjectMeta{
1873+
Name: "coffee-app",
1874+
Namespace: "coffee",
1875+
},
1876+
Spec: conf_v1.VirtualServerRouteSpec{
1877+
Upstreams: []conf_v1.Upstream{
1878+
{
1879+
Name: "coffee",
1880+
Service: "coffee-svc",
1881+
},
1882+
},
1883+
},
1884+
},
1885+
},
1886+
VirtualServer: &conf_v1.VirtualServer{
1887+
ObjectMeta: meta_v1.ObjectMeta{
1888+
Name: "cafe-vs",
1889+
Namespace: "default",
1890+
},
1891+
Spec: conf_v1.VirtualServerSpec{
1892+
Host: "cafe.example.com",
1893+
Routes: []conf_v1.Route{
1894+
{
1895+
Route: "tea/tea",
1896+
},
1897+
{
1898+
Route: "coffee/coffee",
1899+
},
1900+
},
1901+
},
1902+
},
1903+
}
18371904
validTransportServerExWithUpstreams = &TransportServerEx{
18381905
TransportServer: &conf_v1.TransportServer{
18391906
ObjectMeta: meta_v1.ObjectMeta{

0 commit comments

Comments
 (0)