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

Commit

Permalink
GH-496 GW target own cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymvavilov committed Oct 25, 2023
1 parent fcfb58b commit 93aca53
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
8 changes: 5 additions & 3 deletions pkg/controllers/dnspolicy/dnspolicy_dnsrecords.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ func (r *DNSPolicyReconciler) buildClusterGateway(ctx context.Context, downstrea
singleClusterAddresses := make([]gatewayv1beta1.GatewayAddress, len(clusterAddress))

mc := &clusterv1.ManagedCluster{}
if err := r.Client().Get(ctx, client.ObjectKey{Name: downstreamClusterName}, mc, &client.GetOptions{}); err != nil {
return target, err
if downstreamClusterName != dns.NoneCluster {
if err := r.Client().Get(ctx, client.ObjectKey{Name: downstreamClusterName}, mc, &client.GetOptions{}); err != nil {
return target, err
}
}

for i, addr := range clusterAddress {
Expand All @@ -174,7 +176,7 @@ func getClusterGatewayAddresses(gw *gatewayv1beta1.Gateway) map[string][]gateway
var gatewayAddresses []gatewayv1beta1.GatewayAddress

//Default to Single Cluster (Normal Gateway Status)
cluster := "none"
cluster := dns.NoneCluster
addressValue := address.Value

//Check for Multi Cluster (MGC Gateway Status)
Expand Down
22 changes: 20 additions & 2 deletions pkg/dns/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package dns
import (
"crypto/sha256"
"fmt"
"reflect"
"strings"

"github.com/martinlindhe/base36"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
clusterv1 "open-cluster-management.io/api/cluster/v1"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
Expand All @@ -19,6 +21,7 @@ const (
DefaultGeo GeoCode = "default"
WildcardGeo GeoCode = "*"
LabelLBAttributeGeoCode = "kuadrant.io/lb-attribute-geo-code"
NoneCluster = "none"
)

// MultiClusterGatewayTarget represents a Gateway that is placed on multiple clusters (ClusterGateway).
Expand Down Expand Up @@ -83,6 +86,7 @@ func (t *MultiClusterGatewayTarget) setClusterGatewayTargets(clusterGateways []C
}

// ClusterGateway contains the addresses of a Gateway on a single cluster and the attributes of the target cluster.
// If represent a single-cluster implementation of a gateway Cluster is nil
type ClusterGateway struct {
Cluster metav1.Object
GatewayAddresses []gatewayv1beta1.GatewayAddress
Expand Down Expand Up @@ -134,7 +138,10 @@ func (t *ClusterGatewayTarget) GetWeight() int {
}

func (t *ClusterGatewayTarget) GetName() string {
return t.Cluster.GetName()
if !t.noneCluster() {
return t.Cluster.GetName()
}
return NoneCluster
}

func (t *ClusterGatewayTarget) GetShortCode() string {
Expand All @@ -143,7 +150,7 @@ func (t *ClusterGatewayTarget) GetShortCode() string {

func (t *ClusterGatewayTarget) setGeo(defaultGeo GeoCode) {
geoCode := defaultGeo
if geoCode == DefaultGeo {
if geoCode == DefaultGeo || t.noneCluster() {
t.Geo = &geoCode
return
}
Expand Down Expand Up @@ -212,6 +219,10 @@ func dnsHealthCheckProbeName(address, gatewayName, listenerName string) string {

func (t *ClusterGatewayTarget) setWeight(defaultWeight int, customWeights []*v1alpha1.CustomWeight) error {
weight := defaultWeight
if t.noneCluster() {
t.Weight = &weight
return nil
}
for k := range customWeights {
cw := customWeights[k]
selector, err := metav1.LabelSelectorAsSelector(cw.Selector)
Expand All @@ -228,6 +239,13 @@ func (t *ClusterGatewayTarget) setWeight(defaultWeight int, customWeights []*v1a
return nil
}

func (t *ClusterGatewayTarget) noneCluster() bool {
if reflect.DeepEqual(t.Cluster, &clusterv1.ManagedCluster{}) {
return true
}
return false
}

func ToBase36hash(s string) string {
hash := sha256.Sum224([]byte(s))
// convert the hash to base36 (alphanumeric) to decrease collision probabilities
Expand Down

0 comments on commit 93aca53

Please sign in to comment.