Skip to content
This repository was archived by the owner on Jan 22, 2021. It is now read-only.

Commit 21d094c

Browse files
authored
Merge pull request #40 from TykTechnologies/further-update-enhancements
Extend update test for annotations
2 parents 1193b04 + 48fd6b8 commit 21d094c

File tree

3 files changed

+127
-45
lines changed

3 files changed

+127
-45
lines changed

ingress/ingress.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -337,33 +337,49 @@ func (c *ControlServer) handleIngressUpdate(oldObj interface{}, newObj interface
337337
}
338338

339339
func (c *ControlServer) ingressChanged(old *netv1beta1.Ingress, new *netv1beta1.Ingress) bool {
340-
if len(new.Spec.Rules) > 0 {
341-
r0 := new.Spec.Rules[0]
342-
hName := r0.Host
343340

344-
// If hostname changed, re-create
345-
if hName != old.Spec.Rules[0].Host {
341+
//first check top level changes like annotations
342+
// try and get out early with a simple length check
343+
if len(old.Annotations) != len(new.Annotations) {
344+
return true
345+
}
346+
// check regular string annotations
347+
for k, v := range new.Annotations {
348+
if old.Annotations[k] != v && k != tyk.TemplateNameKey && strings.Contains(k, "service.tyk.io") {
346349
return true
347350
}
351+
}
348352

349-
// New or deleted paths
350-
if len(r0.HTTP.Paths) != len(old.Spec.Rules[0].HTTP.Paths) {
351-
return true
352-
}
353+
if len(new.Spec.Rules) > 0 {
354+
for ruleNum := range new.Spec.Rules {
353355

354-
// Handle if a path is changed
355-
for i := 0; i < len(old.Spec.Rules[0].HTTP.Paths); i++ {
356+
newRule := new.Spec.Rules[ruleNum]
357+
oldRule := old.Spec.Rules[ruleNum]
358+
hName := newRule.Host
356359

357-
if old.Spec.Rules[0].HTTP.Paths[i] != r0.HTTP.Paths[i] {
360+
// If hostname changed, re-create
361+
if hName != old.Spec.Rules[0].Host {
358362
return true
359363
}
360-
// check for changed service names and ports
361-
if old.Spec.Rules[0].HTTP.Paths[i].Backend.ServiceName != r0.HTTP.Paths[i].Backend.ServiceName ||
362-
old.Spec.Rules[0].HTTP.Paths[i].Backend.ServicePort != r0.HTTP.Paths[i].Backend.ServicePort {
364+
365+
// New or deleted paths
366+
if len(newRule.HTTP.Paths) != len(oldRule.HTTP.Paths) {
363367
return true
364368
}
365-
}
366369

370+
// Handle if a path is changed
371+
for pathNum := range oldRule.HTTP.Paths {
372+
373+
if oldRule.HTTP.Paths[pathNum] != newRule.HTTP.Paths[pathNum] {
374+
return true
375+
}
376+
// check for changed service names and ports
377+
if oldRule.HTTP.Paths[ruleNum].Backend.ServiceName != newRule.HTTP.Paths[pathNum].Backend.ServiceName ||
378+
oldRule.HTTP.Paths[pathNum].Backend.ServicePort != newRule.HTTP.Paths[pathNum].Backend.ServicePort {
379+
return true
380+
}
381+
}
382+
}
367383
}
368384

369385
return false

ingress/ingress_test.go

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import (
77
"net/http"
88
"testing"
99

10+
yaml "gopkg.in/yaml.v2"
11+
1012
"github.com/TykTechnologies/tyk-k8s/tyk"
1113
"github.com/TykTechnologies/tyk-sync/clients/objects"
12-
"gopkg.in/yaml.v2"
1314
"k8s.io/api/networking/v1beta1"
1415
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516
"k8s.io/apimachinery/pkg/util/intstr"
1617
)
1718

18-
var lastResponse = ""
19+
var lastResponse string
1920
var running = false
2021

2122
func serverSetup() {
@@ -63,12 +64,12 @@ func TestControlServer_noPaths(t *testing.T) {
6364
apiVersion: networking/v1beta1
6465
kind: Ingress
6566
metadata:
66-
name: cafe-ingress
67-
annotations:
68-
kubernetes.io/ingress.class: tyk
67+
name: cafe-ingress
68+
annotations:
69+
kubernetes.io/ingress.class: tyk
6970
spec:
70-
rules:
71-
- host: cafe.example.com
71+
rules:
72+
- host: cafe.example.com
7273
`
7374

7475
go serverSetup()
@@ -177,7 +178,7 @@ func TestControlServer_doAddWithCustomTemplate(t *testing.T) {
177178
HTTP: &v1beta1.HTTPIngressRuleValue{
178179
Paths: []v1beta1.HTTPIngressPath{
179180
{
180-
Path: "/",
181+
Path: "/foo",
181182
Backend: v1beta1.IngressBackend{
182183
ServiceName: "foo-service",
183184
ServicePort: intstr.IntOrString{IntVal: 80, StrVal: "80"},
@@ -202,7 +203,6 @@ func TestControlServer_doAddWithCustomTemplate(t *testing.T) {
202203
if err != nil {
203204
t.Fatal(err)
204205
}
205-
206206
def := &objects.DBApiDefinition{}
207207
err = json.Unmarshal([]byte(lastResponse), def)
208208
if err != nil {
@@ -225,12 +225,11 @@ func TestControlServer_doAddWithCustomTemplate(t *testing.T) {
225225
lastResponse = ""
226226
}
227227

228-
var ingTests = []struct {
229-
name string
230-
in *v1beta1.Ingress
231-
out *v1beta1.Ingress
228+
var ingTests = map[string]struct {
229+
in *v1beta1.Ingress
230+
out *v1beta1.Ingress
232231
}{
233-
{"changed host", &v1beta1.Ingress{
232+
"changed host": {&v1beta1.Ingress{
234233
ObjectMeta: v1.ObjectMeta{
235234
Name: "foo-name",
236235
Namespace: "bar-namespace",
@@ -287,8 +286,7 @@ var ingTests = []struct {
287286
},
288287
},
289288
},
290-
},
291-
{"changed path", &v1beta1.Ingress{
289+
}, "changed path": {&v1beta1.Ingress{
292290
ObjectMeta: v1.ObjectMeta{
293291
Name: "foo-name",
294292
Namespace: "bar-namespace",
@@ -345,9 +343,7 @@ var ingTests = []struct {
345343
},
346344
},
347345
},
348-
},
349-
// changed service name
350-
{"changed service name", &v1beta1.Ingress{
346+
}, "changed service name": {&v1beta1.Ingress{
351347
ObjectMeta: v1.ObjectMeta{
352348
Name: "foo-name",
353349
Namespace: "bar-namespace",
@@ -404,9 +400,7 @@ var ingTests = []struct {
404400
},
405401
},
406402
},
407-
},
408-
//changed service port
409-
{"changed service port", &v1beta1.Ingress{
403+
}, "changed service port": {&v1beta1.Ingress{
410404
ObjectMeta: v1.ObjectMeta{
411405
Name: "foo-name",
412406
Namespace: "bar-namespace",
@@ -463,18 +457,79 @@ var ingTests = []struct {
463457
},
464458
},
465459
},
460+
}, "changed annotation": {&v1beta1.Ingress{
461+
ObjectMeta: v1.ObjectMeta{
462+
Name: "foo-name",
463+
Namespace: "bar-namespace",
464+
Annotations: map[string]string{
465+
IngressAnnotation: IngressAnnotationValue,
466+
"bool.service.tyk.io/use-keyless": "false",
467+
},
468+
},
469+
Spec: v1beta1.IngressSpec{
470+
Rules: []v1beta1.IngressRule{
471+
{
472+
Host: "foo.com",
473+
IngressRuleValue: v1beta1.IngressRuleValue{
474+
HTTP: &v1beta1.HTTPIngressRuleValue{
475+
Paths: []v1beta1.HTTPIngressPath{
476+
{
477+
Path: "/",
478+
Backend: v1beta1.IngressBackend{
479+
ServiceName: "foo-service",
480+
ServicePort: intstr.IntOrString{IntVal: 80, StrVal: "80"},
481+
},
482+
},
483+
},
484+
},
485+
},
486+
},
487+
},
488+
},
489+
}, &v1beta1.Ingress{
490+
ObjectMeta: v1.ObjectMeta{
491+
Name: "foo-name",
492+
Namespace: "bar-namespace",
493+
Annotations: map[string]string{
494+
IngressAnnotation: IngressAnnotationValue,
495+
"bool.service.tyk.io/use-keyless": "true",
496+
},
497+
},
498+
Spec: v1beta1.IngressSpec{
499+
Rules: []v1beta1.IngressRule{
500+
{
501+
Host: "foo.com",
502+
IngressRuleValue: v1beta1.IngressRuleValue{
503+
HTTP: &v1beta1.HTTPIngressRuleValue{
504+
Paths: []v1beta1.HTTPIngressPath{
505+
{
506+
Path: "/",
507+
Backend: v1beta1.IngressBackend{
508+
ServiceName: "foo-service",
509+
ServicePort: intstr.IntOrString{IntVal: 80, StrVal: "80"},
510+
},
511+
},
512+
},
513+
},
514+
},
515+
},
516+
},
517+
},
518+
},
466519
},
467520
}
468521

469522
func TestIngressChanged(t *testing.T) {
470523

524+
t.Parallel()
471525
x := NewController()
472526

473-
for _, tt := range ingTests {
474-
t.Run(tt.name, func(t *testing.T) {
475-
changed := x.ingressChanged(tt.in, tt.out)
527+
for name, tc := range ingTests {
528+
name, tc := name, tc
529+
t.Run(name, func(t *testing.T) {
530+
changed := x.ingressChanged(tc.in, tc.out)
476531
if !changed {
477-
t.Errorf("Wanted change but none detected for: %s", tt.name)
532+
t.Errorf("Wanted change but none detected for: %s", name)
478533
}
479534
})
480535
}

tyk/tyk.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"github.com/TykTechnologies/tyk/apidef"
98
"path"
109
"regexp"
1110
"strings"
1211
"text/template"
1312

13+
"github.com/TykTechnologies/tyk/apidef"
14+
15+
"github.com/TykTechnologies/tyk-k8s/logger"
16+
"github.com/TykTechnologies/tyk-k8s/processor"
1417
"github.com/TykTechnologies/tyk-sync/clients/dashboard"
1518
"github.com/TykTechnologies/tyk-sync/clients/gateway"
1619
"github.com/TykTechnologies/tyk-sync/clients/interfaces"
1720
"github.com/TykTechnologies/tyk-sync/clients/objects"
18-
"github.com/TykTechnologies/tyk-k8s/logger"
19-
"github.com/TykTechnologies/tyk-k8s/processor"
2021
"github.com/satori/go.uuid"
2122
"github.com/spf13/viper"
2223
)
@@ -293,6 +294,16 @@ func UpdateAPIs(svcs map[string]*APIDefOptions) error {
293294
continue
294295
}
295296

297+
postProcessedDef := string(adBytes)
298+
log.Debug(postProcessedDef)
299+
if opts.Annotations != nil {
300+
postProcessedDef, err = processor.Process(opts.Annotations, string(adBytes))
301+
if err != nil {
302+
errs = append(errs, err)
303+
continue
304+
}
305+
}
306+
296307
apiDef := objects.NewDefinition()
297308
err = json.Unmarshal(adBytes, apiDef)
298309
if err != nil {

0 commit comments

Comments
 (0)