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 and small test
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymvavilov committed Nov 10, 2023
1 parent 910af6e commit 4da904f
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 211 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/dnspolicy/dns_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (dh *dnsHelper) buildDNSRecordForListener(gateway *gatewayv1beta1.Gateway,
}

// getDNSRecordForListener returns a v1alpha1.DNSRecord, if one exists, for the given listener in the given v1alpha1.ManagedZone.
// It needs a reference string to enforce DNS record serving a single traffic.Interface owner
// It needs a reference string to enforce DNS record serving a single utils.Interface owner
func (dh *dnsHelper) getDNSRecordForListener(ctx context.Context, listener gatewayv1beta1.Listener, owner metav1.Object) (*v1alpha1.DNSRecord, error) {
recordName := dnsRecordName(owner.GetName(), string(listener.Name))
dnsRecord := &v1alpha1.DNSRecord{}
Expand Down
52 changes: 27 additions & 25 deletions pkg/controllers/dnspolicy/dnspolicy_dnsrecords.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/kuadrant/kuadrant-operator/pkg/reconcilers"

"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/utils"
)

const (
Expand Down Expand Up @@ -45,42 +45,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 {
gatewayWrapper, err := utils.NewGatewayWrapper(gw)
if err != nil {
return err
}

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

clusterGatewayAddresses := getClusterGatewayAddresses(gateway)
clusterGatewayAddresses := getClusterGatewayAddresses(gatewayWrapper)

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

for _, listener := range gateway.Spec.Listeners {
for _, listener := range gatewayWrapper.Spec.Listeners {
var clusterGateways []dns.ClusterGateway
var mz, err = r.dnsHelper.getManagedZoneForListener(ctx, gateway.Namespace, listener)
var mz, err = r.dnsHelper.getManagedZoneForListener(ctx, gatewayWrapper.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 ", gatewayWrapper.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(gatewayWrapper, 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, gatewayWrapper.Gateway)
if err != nil {
return fmt.Errorf("get cluster gateway failed: %s", err)
}
Expand All @@ -91,23 +96,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, gatewayWrapper, 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, gatewayWrapper.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, gatewayWrapper)
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(gatewayWrapper.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 @@ -167,12 +172,10 @@ func (r *DNSPolicyReconciler) buildClusterGateway(ctx context.Context, clusterNa
}

for i, addr := range gatewayAddresses {
addrType := *addr.Type
if addrType == gateway.MultiClusterHostnameAddressType {
addrType = gatewayv1beta1.HostnameAddressType
}
if addrType == gateway.MultiClusterIPAddressType {
addrType = gatewayv1beta1.IPAddressType
addrType, multicluster := utils.AddressTypeToSingleCluster(addr)

if !multicluster {
addrType = *addr.Type
}

singleClusterAddresses[i] = gatewayv1beta1.GatewayAddress{
Expand All @@ -185,7 +188,7 @@ func (r *DNSPolicyReconciler) buildClusterGateway(ctx context.Context, clusterNa
return target, nil
}

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

for _, address := range gw.Status.Addresses {
Expand All @@ -194,7 +197,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 +219,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 *utils.GatewayWrapper, 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
19 changes: 7 additions & 12 deletions pkg/controllers/gateway/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ import (
"github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/slice"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/policysync"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/utils"
)

const (
GatewayClusterLabelSelectorAnnotation = "kuadrant.io/gateway-cluster-label-selector"
GatewayClustersAnnotation = "kuadrant.io/gateway-clusters"
GatewayFinalizer = "kuadrant.io/gateway"
ManagedLabel = "kuadrant.io/managed"
MultiClusterIPAddressType gatewayv1beta1.AddressType = "kuadrant.io/MultiClusterIPAddress"
MultiClusterHostnameAddressType gatewayv1beta1.AddressType = "kuadrant.io/MultiClusterHostnameAddress"
GatewayClusterLabelSelectorAnnotation = "kuadrant.io/gateway-cluster-label-selector"
GatewayClustersAnnotation = "kuadrant.io/gateway-clusters"
GatewayFinalizer = "kuadrant.io/gateway"
ManagedLabel = "kuadrant.io/managed"
)

type GatewayPlacer interface {
Expand Down Expand Up @@ -214,12 +213,8 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
for _, address := range addresses {
log.V(3).Info("checking address type for mapping", "address.Type", address.Type)
var addressType gatewayv1beta1.AddressType
if *address.Type == gatewayv1beta1.IPAddressType {
addressType = MultiClusterIPAddressType
} else if *address.Type == gatewayv1beta1.HostnameAddressType {
addressType = MultiClusterHostnameAddressType
} else {
addressType, supported := utils.AddressTypeToMultiCluster(address)
if !supported {
continue // ignore address type gatewayv1beta1.NamedAddressType. Unsupported for multi cluster gateway
}
allAddresses = append(allAddresses, gatewayv1beta1.GatewayAddress{
Expand Down
19 changes: 0 additions & 19 deletions pkg/dns/fake/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
. "github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/traffic"
. "github.com/Kuadrant/multicluster-gateway-controller/test/util"
)

Expand All @@ -27,14 +26,6 @@ func (h *FakeHostService) SetEndpoints(_ context.Context, _ *MultiClusterGateway
return nil
}

func (h *FakeHostService) GetDNSRecordsFor(_ context.Context, _ traffic.Interface) ([]*v1alpha1.DNSRecord, error) {
return nil, nil
}

func (h *FakeHostService) CleanupDNSRecords(_ context.Context, _ traffic.Interface) error {
return nil
}

func (h *FakeHostService) CreateDNSRecord(_ context.Context, subDomain string, _ *v1alpha1.ManagedZone, _ metav1.Object) (*v1alpha1.DNSRecord, error) {
if subDomain == Cluster {
return nil, fmt.Errorf(FailCreateDNSSubdomain)
Expand All @@ -59,13 +50,3 @@ func (h *FakeHostService) GetDNSRecord(ctx context.Context, subDomain string, ma
}
return record, nil
}

func (h *FakeHostService) AddEndpoints(_ context.Context, gateway traffic.Interface, _ *v1alpha1.DNSRecord) error {
hosts := gateway.GetHosts()
for _, host := range hosts {
if host == FailEndpointsHostname {
return fmt.Errorf(FailEndpointsHostname)
}
}
return nil
}
124 changes: 0 additions & 124 deletions pkg/traffic/gateway.go

This file was deleted.

27 changes: 0 additions & 27 deletions pkg/traffic/traffic.go

This file was deleted.

Loading

0 comments on commit 4da904f

Please sign in to comment.