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

Commit

Permalink
Merge pull request #40 from TykTechnologies/further-update-enhancements
Browse files Browse the repository at this point in the history
Extend update test for annotations
  • Loading branch information
joshblakeley authored Jul 1, 2020
2 parents 1193b04 + 48fd6b8 commit 21d094c
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 45 deletions.
48 changes: 32 additions & 16 deletions ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,33 +337,49 @@ func (c *ControlServer) handleIngressUpdate(oldObj interface{}, newObj interface
}

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

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

// New or deleted paths
if len(r0.HTTP.Paths) != len(old.Spec.Rules[0].HTTP.Paths) {
return true
}
if len(new.Spec.Rules) > 0 {
for ruleNum := range new.Spec.Rules {

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

if old.Spec.Rules[0].HTTP.Paths[i] != r0.HTTP.Paths[i] {
// If hostname changed, re-create
if hName != old.Spec.Rules[0].Host {
return true
}
// check for changed service names and ports
if old.Spec.Rules[0].HTTP.Paths[i].Backend.ServiceName != r0.HTTP.Paths[i].Backend.ServiceName ||
old.Spec.Rules[0].HTTP.Paths[i].Backend.ServicePort != r0.HTTP.Paths[i].Backend.ServicePort {

// New or deleted paths
if len(newRule.HTTP.Paths) != len(oldRule.HTTP.Paths) {
return true
}
}

// Handle if a path is changed
for pathNum := range oldRule.HTTP.Paths {

if oldRule.HTTP.Paths[pathNum] != newRule.HTTP.Paths[pathNum] {
return true
}
// check for changed service names and ports
if oldRule.HTTP.Paths[ruleNum].Backend.ServiceName != newRule.HTTP.Paths[pathNum].Backend.ServiceName ||
oldRule.HTTP.Paths[pathNum].Backend.ServicePort != newRule.HTTP.Paths[pathNum].Backend.ServicePort {
return true
}
}
}
}

return false
Expand Down
107 changes: 81 additions & 26 deletions ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"net/http"
"testing"

yaml "gopkg.in/yaml.v2"

"github.com/TykTechnologies/tyk-k8s/tyk"
"github.com/TykTechnologies/tyk-sync/clients/objects"
"gopkg.in/yaml.v2"
"k8s.io/api/networking/v1beta1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

var lastResponse = ""
var lastResponse string
var running = false

func serverSetup() {
Expand Down Expand Up @@ -63,12 +64,12 @@ func TestControlServer_noPaths(t *testing.T) {
apiVersion: networking/v1beta1
kind: Ingress
metadata:
name: cafe-ingress
annotations:
kubernetes.io/ingress.class: tyk
name: cafe-ingress
annotations:
kubernetes.io/ingress.class: tyk
spec:
rules:
- host: cafe.example.com
rules:
- host: cafe.example.com
`

go serverSetup()
Expand Down Expand Up @@ -177,7 +178,7 @@ func TestControlServer_doAddWithCustomTemplate(t *testing.T) {
HTTP: &v1beta1.HTTPIngressRuleValue{
Paths: []v1beta1.HTTPIngressPath{
{
Path: "/",
Path: "/foo",
Backend: v1beta1.IngressBackend{
ServiceName: "foo-service",
ServicePort: intstr.IntOrString{IntVal: 80, StrVal: "80"},
Expand All @@ -202,7 +203,6 @@ func TestControlServer_doAddWithCustomTemplate(t *testing.T) {
if err != nil {
t.Fatal(err)
}

def := &objects.DBApiDefinition{}
err = json.Unmarshal([]byte(lastResponse), def)
if err != nil {
Expand All @@ -225,12 +225,11 @@ func TestControlServer_doAddWithCustomTemplate(t *testing.T) {
lastResponse = ""
}

var ingTests = []struct {
name string
in *v1beta1.Ingress
out *v1beta1.Ingress
var ingTests = map[string]struct {
in *v1beta1.Ingress
out *v1beta1.Ingress
}{
{"changed host", &v1beta1.Ingress{
"changed host": {&v1beta1.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: "foo-name",
Namespace: "bar-namespace",
Expand Down Expand Up @@ -287,8 +286,7 @@ var ingTests = []struct {
},
},
},
},
{"changed path", &v1beta1.Ingress{
}, "changed path": {&v1beta1.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: "foo-name",
Namespace: "bar-namespace",
Expand Down Expand Up @@ -345,9 +343,7 @@ var ingTests = []struct {
},
},
},
},
// changed service name
{"changed service name", &v1beta1.Ingress{
}, "changed service name": {&v1beta1.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: "foo-name",
Namespace: "bar-namespace",
Expand Down Expand Up @@ -404,9 +400,7 @@ var ingTests = []struct {
},
},
},
},
//changed service port
{"changed service port", &v1beta1.Ingress{
}, "changed service port": {&v1beta1.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: "foo-name",
Namespace: "bar-namespace",
Expand Down Expand Up @@ -463,18 +457,79 @@ var ingTests = []struct {
},
},
},
}, "changed annotation": {&v1beta1.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: "foo-name",
Namespace: "bar-namespace",
Annotations: map[string]string{
IngressAnnotation: IngressAnnotationValue,
"bool.service.tyk.io/use-keyless": "false",
},
},
Spec: v1beta1.IngressSpec{
Rules: []v1beta1.IngressRule{
{
Host: "foo.com",
IngressRuleValue: v1beta1.IngressRuleValue{
HTTP: &v1beta1.HTTPIngressRuleValue{
Paths: []v1beta1.HTTPIngressPath{
{
Path: "/",
Backend: v1beta1.IngressBackend{
ServiceName: "foo-service",
ServicePort: intstr.IntOrString{IntVal: 80, StrVal: "80"},
},
},
},
},
},
},
},
},
}, &v1beta1.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: "foo-name",
Namespace: "bar-namespace",
Annotations: map[string]string{
IngressAnnotation: IngressAnnotationValue,
"bool.service.tyk.io/use-keyless": "true",
},
},
Spec: v1beta1.IngressSpec{
Rules: []v1beta1.IngressRule{
{
Host: "foo.com",
IngressRuleValue: v1beta1.IngressRuleValue{
HTTP: &v1beta1.HTTPIngressRuleValue{
Paths: []v1beta1.HTTPIngressPath{
{
Path: "/",
Backend: v1beta1.IngressBackend{
ServiceName: "foo-service",
ServicePort: intstr.IntOrString{IntVal: 80, StrVal: "80"},
},
},
},
},
},
},
},
},
},
},
}

func TestIngressChanged(t *testing.T) {

t.Parallel()
x := NewController()

for _, tt := range ingTests {
t.Run(tt.name, func(t *testing.T) {
changed := x.ingressChanged(tt.in, tt.out)
for name, tc := range ingTests {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
changed := x.ingressChanged(tc.in, tc.out)
if !changed {
t.Errorf("Wanted change but none detected for: %s", tt.name)
t.Errorf("Wanted change but none detected for: %s", name)
}
})
}
Expand Down
17 changes: 14 additions & 3 deletions tyk/tyk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/TykTechnologies/tyk/apidef"
"path"
"regexp"
"strings"
"text/template"

"github.com/TykTechnologies/tyk/apidef"

"github.com/TykTechnologies/tyk-k8s/logger"
"github.com/TykTechnologies/tyk-k8s/processor"
"github.com/TykTechnologies/tyk-sync/clients/dashboard"
"github.com/TykTechnologies/tyk-sync/clients/gateway"
"github.com/TykTechnologies/tyk-sync/clients/interfaces"
"github.com/TykTechnologies/tyk-sync/clients/objects"
"github.com/TykTechnologies/tyk-k8s/logger"
"github.com/TykTechnologies/tyk-k8s/processor"
"github.com/satori/go.uuid"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -293,6 +294,16 @@ func UpdateAPIs(svcs map[string]*APIDefOptions) error {
continue
}

postProcessedDef := string(adBytes)
log.Debug(postProcessedDef)
if opts.Annotations != nil {
postProcessedDef, err = processor.Process(opts.Annotations, string(adBytes))
if err != nil {
errs = append(errs, err)
continue
}
}

apiDef := objects.NewDefinition()
err = json.Unmarshal(adBytes, apiDef)
if err != nil {
Expand Down

0 comments on commit 21d094c

Please sign in to comment.