Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
GH-496 addresses check
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymvavilov committed Nov 9, 2023
1 parent db29367 commit 456a9b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
41 changes: 23 additions & 18 deletions pkg/controllers/dnspolicy/dnspolicy_dnsrecords.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/gateway"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/traffic"
)

const (
Expand Down Expand Up @@ -45,42 +46,47 @@ func (r *DNSPolicyReconciler) reconcileDNSRecords(ctx context.Context, dnsPolicy
return nil
}

func (r *DNSPolicyReconciler) reconcileGatewayDNSRecords(ctx context.Context, gateway *gatewayv1beta1.Gateway, dnsPolicy *v1alpha1.DNSPolicy) error {
func (r *DNSPolicyReconciler) reconcileGatewayDNSRecords(ctx context.Context, gw *gatewayv1beta1.Gateway, dnsPolicy *v1alpha1.DNSPolicy) error {
log := crlog.FromContext(ctx)

if err := r.dnsHelper.removeDNSForDeletedListeners(ctx, gateway); err != nil {
trafficGateway, err := traffic.NewGateway(gw)
if err != nil {
return err
}

if err := r.dnsHelper.removeDNSForDeletedListeners(ctx, trafficGateway.Gateway); err != nil {
log.V(3).Info("error removing DNS for deleted listeners")
return err
}

clusterGatewayAddresses := getClusterGatewayAddresses(gateway)
clusterGatewayAddresses := getClusterGatewayAddresses(trafficGateway)

log.V(3).Info("checking gateway for attached routes ", "gateway", gateway.Name, "clusters", clusterGatewayAddresses)
log.V(3).Info("checking gateway for attached routes ", "gateway", trafficGateway.Name, "clusters", clusterGatewayAddresses)

for _, listener := range gateway.Spec.Listeners {
for _, listener := range trafficGateway.Spec.Listeners {
var clusterGateways []dns.ClusterGateway
var mz, err = r.dnsHelper.getManagedZoneForListener(ctx, gateway.Namespace, listener)
var mz, err = r.dnsHelper.getManagedZoneForListener(ctx, trafficGateway.Namespace, listener)
if err != nil {
return err
}
listenerHost := *listener.Hostname
if listenerHost == "" {
log.Info("skipping listener no hostname assigned", listener.Name, "in ns ", gateway.Namespace)
log.Info("skipping listener no hostname assigned", listener.Name, "in ns ", trafficGateway.Namespace)
continue
}
for clusterName, gatewayAddresses := range clusterGatewayAddresses {
// Only consider host for dns if there's at least 1 attached route to the listener for this host in *any* gateway

log.V(3).Info("checking downstream", "listener ", listener.Name)
attached := listenerTotalAttachedRoutes(gateway, clusterName, listener, gatewayAddresses)
attached := listenerTotalAttachedRoutes(trafficGateway, clusterName, listener)

if attached == 0 {
log.V(1).Info("no attached routes for ", "listener", listener, "cluster ", clusterName)
continue
}
log.V(3).Info("hostHasAttachedRoutes", "host", listener.Name, "hostHasAttachedRoutes", attached)

cg, err := r.buildClusterGateway(ctx, clusterName, gatewayAddresses, gateway)
cg, err := r.buildClusterGateway(ctx, clusterName, gatewayAddresses, trafficGateway.Gateway)
if err != nil {
return fmt.Errorf("get cluster gateway failed: %s", err)
}
Expand All @@ -91,23 +97,23 @@ func (r *DNSPolicyReconciler) reconcileGatewayDNSRecords(ctx context.Context, ga
if len(clusterGateways) == 0 {
// delete record
log.V(3).Info("no cluster gateways, deleting DNS record", " for listener ", listener.Name)
if err := r.dnsHelper.deleteDNSRecordForListener(ctx, gateway, listener); client.IgnoreNotFound(err) != nil {
if err := r.dnsHelper.deleteDNSRecordForListener(ctx, trafficGateway, listener); client.IgnoreNotFound(err) != nil {
return fmt.Errorf("failed to delete dns record for listener %s : %s", listener.Name, err)
}
return nil
}
dnsRecord, err := r.dnsHelper.createDNSRecordForListener(ctx, gateway, dnsPolicy, mz, listener)
dnsRecord, err := r.dnsHelper.createDNSRecordForListener(ctx, trafficGateway.Gateway, dnsPolicy, mz, listener)
if err := client.IgnoreAlreadyExists(err); err != nil {
return fmt.Errorf("failed to create dns record for listener host %s : %s ", *listener.Hostname, err)
}
if k8serrors.IsAlreadyExists(err) {
dnsRecord, err = r.dnsHelper.getDNSRecordForListener(ctx, listener, gateway)
dnsRecord, err = r.dnsHelper.getDNSRecordForListener(ctx, listener, trafficGateway)
if err != nil {
return fmt.Errorf("failed to get dns record for host %s : %s ", listener.Name, err)
}
}

mcgTarget, err := dns.NewMultiClusterGatewayTarget(gateway, clusterGateways, dnsPolicy.Spec.LoadBalancing)
mcgTarget, err := dns.NewMultiClusterGatewayTarget(trafficGateway.Gateway, clusterGateways, dnsPolicy.Spec.LoadBalancing)
if err != nil {
return fmt.Errorf("failed to create multi cluster gateway target for listener %s : %s ", listener.Name, err)
}
Expand Down Expand Up @@ -185,7 +191,7 @@ func (r *DNSPolicyReconciler) buildClusterGateway(ctx context.Context, clusterNa
return target, nil
}

func getClusterGatewayAddresses(gw *gatewayv1beta1.Gateway) map[string][]gatewayv1beta1.GatewayAddress {
func getClusterGatewayAddresses(gw *traffic.Gateway) map[string][]gatewayv1beta1.GatewayAddress {
clusterAddrs := make(map[string][]gatewayv1beta1.GatewayAddress, len(gw.Status.Addresses))

for _, address := range gw.Status.Addresses {
Expand All @@ -194,7 +200,7 @@ func getClusterGatewayAddresses(gw *gatewayv1beta1.Gateway) map[string][]gateway
addressValue := address.Value

//Check for Multi Cluster (MGC Gateway Status)
if *address.Type == gateway.MultiClusterIPAddressType || *address.Type == gateway.MultiClusterHostnameAddressType {
if gw.IsMultiCluster() {
tmpCluster, tmpAddress, found := strings.Cut(address.Value, "/")
//If this fails something is wrong and the value hasn't been set correctly
if found {
Expand All @@ -216,11 +222,10 @@ func getClusterGatewayAddresses(gw *gatewayv1beta1.Gateway) map[string][]gateway
return clusterAddrs
}

func listenerTotalAttachedRoutes(upstreamGateway *gatewayv1beta1.Gateway, downstreamCluster string, specListener gatewayv1beta1.Listener, addresses []gatewayv1beta1.GatewayAddress) int {
func listenerTotalAttachedRoutes(upstreamGateway *traffic.Gateway, downstreamCluster string, specListener gatewayv1beta1.Listener) int {
for _, statusListener := range upstreamGateway.Status.Listeners {
// assuming all adresses of the same type on the gateway
// for Multi Cluster (MGC Gateway Status)
if *addresses[0].Type == gateway.MultiClusterIPAddressType || *addresses[0].Type == gateway.MultiClusterHostnameAddressType {
if upstreamGateway.IsMultiCluster() {
clusterName, listenerName, found := strings.Cut(string(statusListener.Name), ".")
if !found {
return 0
Expand Down
27 changes: 25 additions & 2 deletions pkg/traffic/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,38 @@ import (
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/slice"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/gateway"
)

type GatewayInterface interface {
Interface
GetListenerByHost(host string) *gatewayv1beta1.Listener
}

func NewGateway(g *gatewayv1beta1.Gateway) GatewayInterface {
return &Gateway{Gateway: g}
func NewGateway(g *gatewayv1beta1.Gateway) (*Gateway, error) {
gw := &Gateway{Gateway: g, isMultiCluster: false}

for i, address := range gw.Status.Addresses {
if i == 0 {
gw.isMultiCluster = isMultiClusterAddressType(*address.Type)
continue
}
if gw.isMultiCluster == isMultiClusterAddressType(*address.Type) {
continue
}
return nil, fmt.Errorf("gateway is invalid: inconsistent status addresses")

}
return gw, nil
}

func isMultiClusterAddressType(addressType gatewayv1beta1.AddressType) bool {
return addressType == gateway.MultiClusterIPAddressType || addressType == gateway.MultiClusterHostnameAddressType
}

type Gateway struct {
*gatewayv1beta1.Gateway
isMultiCluster bool
}

func (a *Gateway) GetKind() string {
Expand Down Expand Up @@ -122,3 +141,7 @@ func (a *Gateway) GetListenerByHost(host string) *gatewayv1beta1.Listener {
func (a *Gateway) ExposesOwnController() bool {
return false
}

func (a *Gateway) IsMultiCluster() bool {
return a.isMultiCluster
}

0 comments on commit 456a9b7

Please sign in to comment.