-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dns): Use generated vips instead of rebuilding it
This avoids computing the same thing twice. Now that allocator persists the correct configuration we can just reuse this Signed-off-by: Charly Molter <charly.molter@konghq.com>
- Loading branch information
Showing
7 changed files
with
142 additions
and
465 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,65 @@ | ||
package topology | ||
|
||
import ( | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/asaskevich/govalidator" | ||
"github.com/pkg/errors" | ||
|
||
mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1" | ||
core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh" | ||
"github.com/kumahq/kuma/pkg/core/resources/model" | ||
"github.com/kumahq/kuma/pkg/core/xds" | ||
"github.com/kumahq/kuma/pkg/dns/vips" | ||
) | ||
|
||
const VIPListenPort = uint32(80) | ||
|
||
func VIPOutbounds( | ||
resourceKey model.ResourceKey, | ||
dataplanes []*core_mesh.DataplaneResource, | ||
zoneIngresses []*core_mesh.ZoneIngressResource, | ||
vipList vips.List, | ||
virtualOutboundView *vips.VirtualOutboundView, | ||
tldomain string, | ||
externalServices []*core_mesh.ExternalServiceResource, | ||
) ([]xds.VIPDomains, []*mesh_proto.Dataplane_Networking_Outbound) { | ||
type vipEntry struct { | ||
ip string | ||
port uint32 | ||
entryType vips.EntryType | ||
} | ||
serviceVIPMap := map[string][]vipEntry{} | ||
services := []string{} | ||
for _, dataplane := range dataplanes { | ||
// backwards compatibility | ||
if dataplane.Spec.IsIngress() { | ||
for _, service := range dataplane.Spec.GetNetworking().GetIngress().GetAvailableServices() { | ||
if service.Mesh == resourceKey.Mesh { | ||
// Only add outbounds for services in the same mesh | ||
inService := service.Tags[mesh_proto.ServiceTag] | ||
if _, found := serviceVIPMap[inService]; !found { | ||
vip, err := ForwardLookup(vipList, vips.NewServiceEntry(inService)) | ||
if err == nil { | ||
serviceVIPMap[inService] = append(serviceVIPMap[inService], vipEntry{vip, VIPListenPort, vips.Service}) | ||
services = append(services, inService) | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
for _, inbound := range dataplane.Spec.GetNetworking().GetInbound() { | ||
inService := inbound.GetTags()[mesh_proto.ServiceTag] | ||
if _, found := serviceVIPMap[inService]; !found { | ||
vip, err := ForwardLookup(vipList, vips.NewServiceEntry(inService)) | ||
if err == nil { | ||
serviceVIPMap[inService] = append(serviceVIPMap[inService], vipEntry{vip, VIPListenPort, vips.Service}) | ||
services = append(services, inService) | ||
} | ||
} | ||
} | ||
var vipDomains []xds.VIPDomains | ||
var outbounds []*mesh_proto.Dataplane_Networking_Outbound | ||
for _, key := range virtualOutboundView.Keys() { | ||
voutbound := virtualOutboundView.Get(key) | ||
if voutbound.Address == "" { | ||
continue | ||
} | ||
} | ||
|
||
for _, zi := range zoneIngresses { | ||
for _, service := range zi.Spec.GetAvailableServices() { | ||
if service.Mesh == resourceKey.Mesh { | ||
// Only add outbounds for services in the same mesh | ||
inService := service.Tags[mesh_proto.ServiceTag] | ||
if _, found := serviceVIPMap[inService]; !found { | ||
vip, err := ForwardLookup(vipList, vips.NewServiceEntry(inService)) | ||
if err == nil { | ||
serviceVIPMap[inService] = append(serviceVIPMap[inService], vipEntry{vip, VIPListenPort, vips.Service}) | ||
services = append(services, inService) | ||
domain := xds.VIPDomains{Address: voutbound.Address} | ||
switch key.Type { | ||
case vips.Host, vips.FullyQualifiedDomain: | ||
for _, ob := range voutbound.Outbounds { | ||
if govalidator.IsDNSName(key.Name) { | ||
domain.Domains = []string{key.Name} | ||
if ob.Port != 0 { | ||
outbounds = append(outbounds, &mesh_proto.Dataplane_Networking_Outbound{ | ||
Address: voutbound.Address, | ||
Port: ob.Port, | ||
Tags: ob.TagSet, | ||
}) | ||
} | ||
// TODO remove the `vips.Host` on the next major version it's there for backward compatibility | ||
if key.Type == vips.Host { | ||
outbounds = append(outbounds, &mesh_proto.Dataplane_Networking_Outbound{ | ||
Address: voutbound.Address, | ||
Port: VIPListenPort, | ||
Tags: ob.TagSet, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
externalServicesByServiceName := map[string]*core_mesh.ExternalServiceResource{} | ||
for _, externalService := range externalServices { | ||
inService := externalService.Spec.Tags[mesh_proto.ServiceTag] | ||
externalServicesByServiceName[inService] = externalService | ||
host := externalService.Spec.GetHost() | ||
if _, found := serviceVIPMap[inService]; !found { | ||
vip1, err := ForwardLookup(vipList, vips.NewHostEntry(host)) | ||
if err == nil { | ||
port := externalService.Spec.GetPort() | ||
var p32 uint32 | ||
if p64, err := strconv.ParseUint(port, 10, 32); err != nil { | ||
p32 = VIPListenPort | ||
} else { | ||
p32 = uint32(p64) | ||
} | ||
serviceVIPMap[inService] = append(serviceVIPMap[inService], vipEntry{vip1, p32, vips.Host}) | ||
services = append(services, inService) | ||
} | ||
vip2, err := ForwardLookup(vipList, vips.NewServiceEntry(inService)) | ||
if err == nil { | ||
port := externalService.Spec.GetPort() | ||
var p32 uint32 | ||
if p64, err := strconv.ParseUint(port, 10, 32); err != nil { | ||
p32 = VIPListenPort | ||
} else { | ||
p32 = uint32(p64) | ||
} | ||
serviceVIPMap[inService] = append(serviceVIPMap[inService], vipEntry{vip2, p32, vips.Service}) | ||
services = append(services, inService) | ||
case vips.Service: | ||
service := voutbound.Outbounds[0].TagSet[mesh_proto.ServiceTag] | ||
domain.Domains = []string{service + "." + tldomain} | ||
cleanedDomain := strings.ReplaceAll(service, "_", ".") + "." + tldomain | ||
if cleanedDomain != domain.Domains[0] { | ||
domain.Domains = append(domain.Domains, cleanedDomain) | ||
} | ||
} | ||
} | ||
|
||
sort.Strings(services) | ||
var vipDomains []xds.VIPDomains | ||
var outbounds []*mesh_proto.Dataplane_Networking_Outbound | ||
for _, service := range services { | ||
entries := serviceVIPMap[service] | ||
for _, entry := range entries { | ||
outbounds = append(outbounds, &mesh_proto.Dataplane_Networking_Outbound{ | ||
Address: entry.ip, | ||
Tags: map[string]string{mesh_proto.ServiceTag: service}, | ||
Port: entry.port, | ||
Address: voutbound.Address, | ||
Port: VIPListenPort, | ||
Tags: voutbound.Outbounds[0].TagSet, | ||
}) | ||
vip := xds.VIPDomains{ | ||
Address: entry.ip, | ||
} | ||
switch entry.entryType { | ||
case vips.Service: | ||
// add regular .mesh domain | ||
vip.Domains = []string{service + "." + tldomain} | ||
cleanedDomain := strings.ReplaceAll(service, "_", ".") + "." + tldomain | ||
if cleanedDomain != vip.Domains[0] { | ||
vip.Domains = append(vip.Domains, cleanedDomain) | ||
} | ||
// todo (lobkovilya): backwards compatibility, could be deleted in the next major release Kuma 1.2.x | ||
if entry.port != VIPListenPort { | ||
outbounds = append(outbounds, &mesh_proto.Dataplane_Networking_Outbound{ | ||
Address: entry.ip, | ||
Tags: map[string]string{mesh_proto.ServiceTag: service}, | ||
Port: VIPListenPort, | ||
}) | ||
} | ||
case vips.Host: | ||
host := externalServicesByServiceName[service].Spec.GetHost() | ||
if govalidator.IsDNSName(host) { | ||
vip.Domains = append(vip.Domains, host) | ||
} | ||
} | ||
vipDomains = append(vipDomains, vip) | ||
} | ||
vipDomains = append(vipDomains, domain) | ||
} | ||
|
||
return vipDomains, outbounds | ||
} | ||
|
||
func ForwardLookup(vips vips.List, entry vips.Entry) (string, error) { | ||
ip, found := vips[entry] | ||
if !found { | ||
return "", errors.Errorf("entry name [%s] not found", entry.Name) | ||
} | ||
return ip, nil | ||
} |
Oops, something went wrong.