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

Failover strategy implementation #65

Merged
merged 1 commit into from
Mar 18, 2020
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
10 changes: 9 additions & 1 deletion deploy/crds/ohmyglb.absa.oss_gslbs_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,15 @@ spec:
type: array
type: object
strategy:
type: string
properties:
primaryGeoTag:
type: string
type:
type: string
required:
- primaryGeoTag
- type
type: object
required:
- ingress
- strategy
Expand Down
3 changes: 2 additions & 1 deletion deploy/crds/ohmyglb.absa.oss_v1beta1_gslb_cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ spec:
serviceName: frontend-podinfo # Gslb should reflect Healthy status and create associated DNS records
servicePort: http
path: /
strategy: roundRobin # Use a round robin load balancing strategy, when deciding which downstream clusters to route clients too
strategy:
type: roundRobin # Use a round robin load balancing strategy, when deciding which downstream clusters to route clients too
18 changes: 18 additions & 0 deletions deploy/crds/ohmyglb.absa.oss_v1beta1_gslb_cr_failover.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: ohmyglb.absa.oss/v1beta1
kind: Gslb
metadata:
name: test-gslb
namespace: test-gslb
spec:
ingress:
rules:
- host: failover.cloud.example.com
http:
paths:
- backend:
serviceName: frontend-podinfo # Gslb should reflect Healthy status and create associated DNS records
servicePort: http
path: /
strategy:
type: failover
primaryGeoTag: eu
7 changes: 6 additions & 1 deletion pkg/apis/ohmyglb/v1beta1/gslb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import (
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

type Strategy struct {
Type string `json:"type"`
PrimaryGeoTag string `json:"primaryGeoTag"`
}

// GslbSpec defines the desired state of Gslb
// +k8s:openapi-gen=true
type GslbSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html
Ingress v1beta1.IngressSpec `json:"ingress"`
Strategy string `json:"strategy"`
Strategy Strategy `json:"strategy"`
}

// GslbStatus defines the observed state of Gslb
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/ohmyglb/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion pkg/controller/gslb/dnsupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,26 @@ func (r *ReconcileGslb) gslbDNSEndpoint(gslb *ohmyglbv1beta1.Gslb) (*externaldns
return nil, err
}
if len(externalTargets) > 0 {
switch gslb.Spec.Strategy {
switch gslb.Spec.Strategy.Type {
case "roundRobin":
finalTargets = append(finalTargets, externalTargets...)
case "failover":
clusterGeoTag := os.Getenv("CLUSTER_GEO_TAG")
// If cluster is Primary
if gslb.Spec.Strategy.PrimaryGeoTag == clusterGeoTag {
// If cluster is Primary and Healthy return only own targets
// If cluster is Primary and Unhealthy return Secondary external targets
if health != "Healthy" {
finalTargets = externalTargets
}
} else {
// If cluster is Secondary and Primary external cluster is Healthy
// then return Primary external targets.
// Return own targets by default.
if len(externalTargets) > 0 {
finalTargets = externalTargets
}
}
}
}

Expand Down
112 changes: 112 additions & 0 deletions pkg/controller/gslb/gslb_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,118 @@ func TestGslbController(t *testing.T) {
t.Errorf("got:\n %s unexpected heartbeat records,\n\n want:\n %s", got, want)
}
})

t.Run("Returns own records using Failover strategy when Primary", func(t *testing.T) {
err := os.Setenv("OVERRIDE_WITH_FAKE_EXT_DNS", "true")
if err != nil {
t.Fatalf("Can't setup env var: (%v)", err)
}
err = os.Setenv("CLUSTER_GEO_TAG", "eu")
if err != nil {
t.Fatalf("Can't setup env var: (%v)", err)
}

// Enable failover strategy
gslb.Spec.Strategy.Type = "failover"
gslb.Spec.Strategy.PrimaryGeoTag = "eu"
err = cl.Update(context.TODO(), gslb)
if err != nil {
t.Fatalf("Can't update gslb: (%v)", err)
}

reconcileAndUpdateGslb(t, r, req, cl, gslb)

dnsEndpoint := &externaldns.DNSEndpoint{}
err = cl.Get(context.TODO(), req.NamespacedName, dnsEndpoint)
if err != nil {
t.Fatalf("Failed to get expected DNSEndpoint: (%v)", err)
}

got := dnsEndpoint.Spec.Endpoints

want := []*externaldns.Endpoint{
{
DNSName: "localtargets.app3.cloud.example.com",
RecordTTL: 30,
RecordType: "A",
Targets: externaldns.Targets{"10.0.0.1", "10.0.0.2", "10.0.0.3"},
},
{
DNSName: "app3.cloud.example.com",
RecordTTL: 30,
RecordType: "A",
Targets: externaldns.Targets{"10.0.0.1", "10.0.0.2", "10.0.0.3"},
},
}

prettyGot := prettyPrint(got)
prettyWant := prettyPrint(want)

if !reflect.DeepEqual(got, want) {
t.Errorf("got:\n %s DNSEndpoint,\n\n want:\n %s", prettyGot, prettyWant)
}

err = os.Setenv("OVERRIDE_WITH_FAKE_EXT_DNS", "false")
if err != nil {
t.Fatalf("Can't setup env var: (%v)", err)
}
})

t.Run("Returns external records using Failover strategy when Secondary", func(t *testing.T) {
err := os.Setenv("OVERRIDE_WITH_FAKE_EXT_DNS", "true")
if err != nil {
t.Fatalf("Can't setup env var: (%v)", err)
}
err = os.Setenv("CLUSTER_GEO_TAG", "za")
if err != nil {
t.Fatalf("Can't setup env var: (%v)", err)
}

// Enable failover strategy
gslb.Spec.Strategy.Type = "failover"
gslb.Spec.Strategy.PrimaryGeoTag = "eu"
err = cl.Update(context.TODO(), gslb)
if err != nil {
t.Fatalf("Can't update gslb: (%v)", err)
}

reconcileAndUpdateGslb(t, r, req, cl, gslb)

dnsEndpoint := &externaldns.DNSEndpoint{}
err = cl.Get(context.TODO(), req.NamespacedName, dnsEndpoint)
if err != nil {
t.Fatalf("Failed to get expected DNSEndpoint: (%v)", err)
}

got := dnsEndpoint.Spec.Endpoints

want := []*externaldns.Endpoint{
{
DNSName: "localtargets.app3.cloud.example.com",
RecordTTL: 30,
RecordType: "A",
Targets: externaldns.Targets{"10.0.0.1", "10.0.0.2", "10.0.0.3"},
},
{
DNSName: "app3.cloud.example.com",
RecordTTL: 30,
RecordType: "A",
Targets: externaldns.Targets{"10.1.0.1", "10.1.0.2", "10.1.0.3"},
},
}

prettyGot := prettyPrint(got)
prettyWant := prettyPrint(want)

if !reflect.DeepEqual(got, want) {
t.Errorf("got:\n %s DNSEndpoint,\n\n want:\n %s", prettyGot, prettyWant)
}

err = os.Setenv("OVERRIDE_WITH_FAKE_EXT_DNS", "false")
if err != nil {
t.Fatalf("Can't setup env var: (%v)", err)
}
})
}

func reconcileAndUpdateGslb(t *testing.T,
Expand Down