Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weight Round Robin (3/4) - Endpoint #914

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions controllers/dnsupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,17 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic

import (
"fmt"
"sort"
"strings"

"github.com/k8gb-io/k8gb/controllers/depresolver"
"github.com/k8gb-io/k8gb/controllers/providers/assistant"

k8gbv1beta1 "github.com/k8gb-io/k8gb/api/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
externaldns "sigs.k8s.io/external-dns/endpoint"
)

func sortTargets(targets []string) []string {
sort.Slice(targets, func(i, j int) bool {
return targets[i] < targets[j]
})
return targets
}

func (r *GslbReconciler) gslbDNSEndpoint(gslb *k8gbv1beta1.Gslb) (*externaldns.DNSEndpoint, error) {
var gslbHosts []*externaldns.Endpoint
var ttl = externaldns.TTL(gslb.Spec.Strategy.DNSTtlSeconds)
Expand All @@ -53,7 +46,7 @@ func (r *GslbReconciler) gslbDNSEndpoint(gslb *k8gbv1beta1.Gslb) (*externaldns.D
}

for host, health := range serviceHealth {
var finalTargets []string
var finalTargets = assistant.NewTargets()

if !strings.Contains(host, r.Config.EdgeDNSZone) {
return nil, fmt.Errorf("ingress host %s does not match delegated zone %s", host, r.Config.EdgeDNSZone)
Expand All @@ -63,7 +56,7 @@ func (r *GslbReconciler) gslbDNSEndpoint(gslb *k8gbv1beta1.Gslb) (*externaldns.D
isHealthy := health == k8gbv1beta1.Healthy

if isHealthy {
finalTargets = append(finalTargets, localTargets...)
finalTargets.Append(r.Config.ClusterGeoTag, localTargets)
localTargetsHost := fmt.Sprintf("localtargets-%s", host)
dnsRecord := &externaldns.Endpoint{
DNSName: localTargetsHost,
Expand All @@ -75,14 +68,13 @@ func (r *GslbReconciler) gslbDNSEndpoint(gslb *k8gbv1beta1.Gslb) (*externaldns.D
}

// Check if host is alive on external Gslb
externalTargets := r.DNSProvider.GetExternalTargets(host).GetIPs()

sortTargets(externalTargets)
externalTargets := r.DNSProvider.GetExternalTargets(host)
externalTargets.Sort()

if len(externalTargets) > 0 {
switch gslb.Spec.Strategy.Type {
case depresolver.RoundRobinStrategy, depresolver.GeoStrategy:
finalTargets = append(finalTargets, externalTargets...)
finalTargets.AppendTargets(externalTargets)
case depresolver.FailoverStrategy:
// If cluster is Primary
if isPrimary {
Expand All @@ -93,7 +85,7 @@ func (r *GslbReconciler) gslbDNSEndpoint(gslb *k8gbv1beta1.Gslb) (*externaldns.D
log.Info().
Str("gslb", gslb.Name).
Str("cluster", gslb.Spec.Strategy.PrimaryGeoTag).
Strs("targets", finalTargets).
Strs("targets", finalTargets.GetIPs()).
Str("workload", k8gbv1beta1.Unhealthy.String()).
Msg("Executing failover strategy for primary cluster")
}
Expand All @@ -105,7 +97,7 @@ func (r *GslbReconciler) gslbDNSEndpoint(gslb *k8gbv1beta1.Gslb) (*externaldns.D
log.Info().
Str("gslb", gslb.Name).
Str("cluster", gslb.Spec.Strategy.PrimaryGeoTag).
Strs("targets", finalTargets).
Strs("targets", finalTargets.GetIPs()).
Str("workload", k8gbv1beta1.Healthy.String()).
Msg("Executing failover strategy for secondary cluster")
}
Expand All @@ -116,22 +108,25 @@ func (r *GslbReconciler) gslbDNSEndpoint(gslb *k8gbv1beta1.Gslb) (*externaldns.D
Msg("No external targets have been found for host")
}

r.updateRuntimeStatus(gslb, isPrimary, health, finalTargets)
r.updateRuntimeStatus(gslb, isPrimary, health, finalTargets.GetIPs())
log.Info().
Str("gslb", gslb.Name).
Strs("targets", finalTargets).
Strs("targets", finalTargets.GetIPs()).
Msg("Final target list")

if len(finalTargets) > 0 {
dnsRecord := &externaldns.Endpoint{
DNSName: host,
RecordTTL: ttl,
RecordType: "A",
Targets: finalTargets,
Targets: finalTargets.GetIPs(),
Labels: externaldns.Labels{
"strategy": gslb.Spec.Strategy.Type,
},
}
for k, v := range r.getLabels(gslb, finalTargets) {
dnsRecord.Labels[k] = v
}
gslbHosts = append(gslbHosts, dnsRecord)
}
}
Expand Down Expand Up @@ -166,3 +161,19 @@ func (r *GslbReconciler) updateRuntimeStatus(gslb *k8gbv1beta1.Gslb, isPrimary b
m.UpdateFailoverStatus(gslb, isPrimary, isHealthy, finalTargets)
}
}

// getLabels map of where key identifies region and weight, value identifies IP.
func (r *GslbReconciler) getLabels(gslb *k8gbv1beta1.Gslb, targets assistant.Targets) (labels map[string]string) {
labels = make(map[string]string, 0)
for k, v := range gslb.Spec.Strategy.Weight {
t, found := targets[k]
if !found {
continue
}
for i, ip := range t.IPs {
l := fmt.Sprintf("weight-%s-%v-%v", k, i, v.Int())
labels[l] = ip
}
}
return labels
}
6 changes: 3 additions & 3 deletions controllers/providers/assistant/gslb.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ func dnsQuery(host string, nameservers utils.DNSList) (*dns.Msg, error) {
}

func (r *Gslb) GetExternalTargets(host string, extClusterNsNames map[string]string) (targets Targets) {
targets = Targets{}
for _, cluster := range extClusterNsNames {
targets = NewTargets()
for tag, cluster := range extClusterNsNames {
// Use edgeDNSServer for resolution of NS names and fallback to local nameservers
log.Info().
Str("cluster", cluster).
Expand Down Expand Up @@ -327,7 +327,7 @@ func (r *Gslb) GetExternalTargets(host string, extClusterNsNames map[string]stri
}
clusterTargets := getARecords(a)
if len(clusterTargets) > 0 {
targets = append(targets, Target{cluster, clusterTargets})
targets[tag] = &Target{clusterTargets}
log.Info().
Strs("clusterTargets", clusterTargets).
Str("cluster", cluster).
Expand Down
45 changes: 38 additions & 7 deletions controllers/providers/assistant/target.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package assistant

import "sort"

/*
Copyright 2022 The k8gb Contributors.

Expand All @@ -19,17 +21,46 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

type Target struct {
Region string
IPs []string
IPs []string
}

type Targets []Target
type Targets map[string]*Target

func NewTargets() Targets {
return make(map[string]*Target, 0)
}

func (t Targets) GetIPs() (targets []string) {
func (t Targets) GetIPs() (ips []string) {
// initializing targets to avoid possible nil reference errors (serialization etc.)
targets = []string{}
ips = []string{}
for _, v := range t {
ips = append(ips, v.IPs...)
}
return ips
}

func (t Targets) Append(tag string, ips []string) {
if target, found := t[tag]; found {
target.IPs = append(target.IPs, ips...)
return
}
t[tag] = &Target{IPs: ips}
}

func (t Targets) AppendTargets(targets Targets) {
for k, v := range targets {
t.Append(k, v.IPs)
}
}

func (t Targets) Sort() {
sort := func(targets []string) []string {
sort.Slice(targets, func(i, j int) bool {
return targets[i] < targets[j]
})
return targets
}
for _, v := range t {
targets = append(targets, v.IPs...)
v.IPs = sort(v.IPs)
}
return targets
}
Loading