-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Weight Round Robin (3/4) - annotations
related to #50 implemented code that adds an annotation to the local dnsEndpoint. - covered by unit-tests - the annotation is as follows: ```json { "eu":{ "weight":35, "targets":[ "10.10.0.1", "10.10.0.2" ] }, "us":{ "weight":50, "targets":[ "10.0.0.1", "10.0.0.2" ] }, "za":{ "weight":15, "targets":[ "10.22.0.1", "10.22.0.2", "10.22.1.1" ] } } ``` Signed-off-by: kuritka <kuritka@gmail.com>
- Loading branch information
Showing
5 changed files
with
272 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package controllers | ||
|
||
/* | ||
Copyright 2022 The k8gb Contributors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic | ||
*/ | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/k8gb-io/k8gb/controllers/depresolver" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
k8gbv1beta1 "github.com/k8gb-io/k8gb/api/v1beta1" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/k8gb-io/k8gb/controllers/providers/assistant" | ||
"github.com/k8gb-io/k8gb/controllers/providers/dns" | ||
"github.com/stretchr/testify/assert" | ||
externaldns "sigs.k8s.io/external-dns/endpoint" | ||
) | ||
|
||
func TestWeight(t *testing.T) { | ||
// arrange | ||
type wrr struct { | ||
weight string | ||
targets []string | ||
} | ||
var tests = []struct { | ||
name string | ||
data map[string]wrr | ||
injectWeights bool | ||
host string | ||
annotation string | ||
}{ | ||
{ | ||
name: "eu35-us50-za15", | ||
injectWeights: true, | ||
host: "roundrobin.cloud.example.com", | ||
data: map[string]wrr{ | ||
"eu": {weight: "35%", targets: []string{"10.10.0.1", "10.10.0.2"}}, | ||
"us": {weight: "50%", targets: []string{"10.0.0.1", "10.0.0.2"}}, | ||
"za": {weight: "15%", targets: []string{"10.22.0.1", "10.22.0.2", "10.22.1.1"}}, | ||
}, | ||
annotation: `{"eu":{"weight":35,"targets":["10.10.0.1","10.10.0.2"]},` + | ||
`"us":{"weight":50,"targets":["10.0.0.1","10.0.0.2"]},` + | ||
`"za":{"weight":15,"targets":["10.22.0.1","10.22.0.2","10.22.1.1"]}}`, | ||
}, | ||
|
||
{ | ||
name: "eu100-us0-za0", | ||
injectWeights: true, | ||
host: "roundrobin.cloud.example.com", | ||
data: map[string]wrr{ | ||
"eu": {weight: "100%", targets: []string{"10.10.0.1", "10.10.0.2"}}, | ||
"us": {weight: "0%", targets: []string{"10.0.0.1", "10.0.0.2"}}, | ||
"za": {weight: "0%", targets: []string{"10.22.0.1", "10.22.0.2", "10.22.1.1"}}, | ||
}, | ||
annotation: `{"eu":{"weight":100,"targets":["10.10.0.1","10.10.0.2"]},` + | ||
`"us":{"weight":0,"targets":["10.0.0.1","10.0.0.2"]},` + | ||
`"za":{"weight":0,"targets":["10.22.0.1","10.22.0.2","10.22.1.1"]}}`, | ||
}, | ||
|
||
{ | ||
name: "no weights without external targets", | ||
injectWeights: false, | ||
host: "roundrobin.cloud.example.com", | ||
data: map[string]wrr{}, | ||
}, | ||
|
||
{ | ||
name: "no weights with external targets", | ||
injectWeights: false, | ||
host: "roundrobin.cloud.example.com", | ||
data: map[string]wrr{ | ||
"eu": {weight: "100%", targets: []string{"10.10.0.1", "10.10.0.2"}}, | ||
"us": {weight: "0%", targets: []string{"10.0.0.1", "10.0.0.2"}}, | ||
"za": {weight: "0%", targets: []string{"10.22.0.1", "10.22.0.2", "10.22.1.1"}}, | ||
}, | ||
}, | ||
|
||
{ | ||
name: "us100", | ||
injectWeights: true, | ||
host: "roundrobin.cloud.example.com", | ||
data: map[string]wrr{ | ||
"us": {weight: "0%", targets: []string{"10.0.0.1", "10.0.0.2"}}, | ||
}, | ||
annotation: `{"us":{"weight":0,"targets":["10.0.0.1","10.0.0.2"]}}`, | ||
}, | ||
|
||
{ | ||
name: "weights0", | ||
injectWeights: true, | ||
host: "roundrobin.cloud.example.com", | ||
data: map[string]wrr{}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
|
||
assertAnnotation := func(gslb *k8gbv1beta1.Gslb, ep *externaldns.DNSEndpoint) error { | ||
assert.NotNil(t, ep) | ||
assert.NotNil(t, gslb) | ||
assert.Equal(t, ep.ObjectMeta.Annotations["k8gb.absa.oss/dnstype"], "local") | ||
str, found := ep.ObjectMeta.Annotations["k8gb.absa.oss/weight-round-robin"] | ||
// annotation doesnt exist | ||
assert.Equal(t, len(test.annotation) != 0, found) | ||
// annotation is equal to tested value | ||
assert.Equal(t, test.annotation, str) | ||
return nil | ||
} | ||
|
||
injectWeight := func(ctx context.Context, gslb *k8gbv1beta1.Gslb, client client.Client) error { | ||
if !test.injectWeights { | ||
return nil | ||
} | ||
gslb.Spec.Strategy.Weight = make(map[string]k8gbv1beta1.Percentage, 0) | ||
for k, w := range test.data { | ||
gslb.Spec.Strategy.Weight[k] = k8gbv1beta1.Percentage(w.weight) | ||
} | ||
return nil | ||
} | ||
|
||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
settings := provideSettings(t, predefinedConfig) | ||
m := dns.NewMockProvider(ctrl) | ||
r := depresolver.NewMockResolver(ctrl) | ||
m.EXPECT().GslbIngressExposedIPs(gomock.Any()).Return([]string{}, nil).Times(1) | ||
m.EXPECT().SaveDNSEndpoint(gomock.Any(), gomock.Any()).Do(assertAnnotation).Return(fmt.Errorf("save DNS error")).Times(1) | ||
m.EXPECT().CreateZoneDelegationForExternalDNS(gomock.Any()).Return(nil).AnyTimes() | ||
r.EXPECT().ResolveGslbSpec(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(injectWeight).AnyTimes() | ||
|
||
ts := assistant.Targets{} | ||
for k, w := range test.data { | ||
ts[k] = assistant.Target{IPs: w.targets} | ||
} | ||
m.EXPECT().GetExternalTargets("roundrobin.cloud.example.com").Return(ts).Times(1) | ||
m.EXPECT().GetExternalTargets("notfound.cloud.example.com").Return(assistant.Targets{}).Times(1) | ||
m.EXPECT().GetExternalTargets("unhealthy.cloud.example.com").Return(assistant.Targets{}).Times(1) | ||
|
||
settings.reconciler.DNSProvider = m | ||
settings.reconciler.DepResolver = r | ||
|
||
// act, assert | ||
_, _ = settings.reconciler.Reconcile(context.TODO(), settings.request) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
apiVersion: k8gb.absa.oss/v1beta1 | ||
kind: Gslb | ||
metadata: | ||
name: test-gslb | ||
namespace: test-gslb | ||
spec: | ||
ingress: | ||
ingressClassName: nginx | ||
rules: | ||
- host: notfound.cloud.example.com # This is the GSLB enabled host that clients would use | ||
http: # This section mirrors the same structure as that of an Ingress resource and will be used verbatim when creating the corresponding Ingress resource that will match the GSLB host | ||
paths: | ||
- path: / | ||
pathType: Prefix | ||
backend: | ||
service: | ||
name: non-existing-app # Gslb should reflect NotFound status | ||
port: | ||
name: http | ||
- host: unhealthy.cloud.example.com | ||
http: | ||
paths: | ||
- path: / | ||
pathType: Prefix | ||
backend: | ||
service: | ||
name: unhealthy-app # Gslb should reflect Unhealthy status | ||
port: | ||
name: http | ||
- host: roundrobin.cloud.example.com | ||
http: | ||
paths: | ||
- path: / | ||
pathType: Prefix | ||
backend: | ||
service: | ||
name: frontend-podinfo # Gslb should reflect Healthy status and create associated DNS records | ||
port: | ||
name: http | ||
strategy: | ||
type: roundRobin # Use a round robin load balancing strategy, when deciding which downstream clusters to route clients too | ||
splitBrainThresholdSeconds: 300 # Threshold after which external cluster is filtered out from delegated zone when it doesn't look alive | ||
dnsTtlSeconds: 30 # TTL value for automatically created DNS records | ||
weight: | ||
eu: 35% | ||
us: 50% | ||
za: 15% |