Skip to content
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
1 change: 1 addition & 0 deletions dataclients/kubernetes/definitions/ingressv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
IngressFilterAnnotation = "zalando.org/skipper-filter"
IngressPredicateAnnotation = "zalando.org/skipper-predicate"
IngressRoutesAnnotation = "zalando.org/skipper-routes"
IngressBackendAnnotation = "zalando.org/skipper-backend"
)

var errInvalidPortType = errors.New("invalid port type")
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: zalando.org/v1
kind: RouteGroup
metadata:
name: my-test
spec:
backends:
- name: fwd
type: forward
routes:
- pathSubtree: /
backends:
- backendName: fwd
53 changes: 53 additions & 0 deletions dataclients/kubernetes/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type ingressContext struct {
logger *logger
annotationFilters []*eskip.Filter
annotationPredicate string
annotationBackend string
forwardBackendURL string
extraRoutes []*eskip.Route
backendWeights map[string]float64
pathMode PathMode
Expand All @@ -58,6 +60,7 @@ type ingress struct {
forceKubernetesService bool
backendTrafficAlgorithm BackendTrafficAlgorithm
defaultLoadBalancerAlgorithm string
forwardBackendURL string
kubernetesAnnotationPredicates []AnnotationPredicates
kubernetesAnnotationFiltersAppend []AnnotationFilters
kubernetesEastWestRangeAnnotationPredicates []AnnotationPredicates
Expand All @@ -69,9 +72,26 @@ var nonWord = regexp.MustCompile(`\W`)
var errNotAllowedExternalName = errors.New("ingress with not allowed external name service")

func (ic *ingressContext) addHostRoute(host string, route *eskip.Route) {
ic.applyBackend(route)
ic.hostRoutes[host] = append(ic.hostRoutes[host], route)
}

func (ic *ingressContext) applyBackend(route *eskip.Route) {
if ic.forwardBackendURL == "" || ic.annotationBackend == "" || route == nil {
return
}
if be, err := eskip.BackendTypeFromString(ic.annotationBackend); err != nil {
return
} else {
switch be {
case eskip.ForwardBackend:
route.BackendType = eskip.NetworkBackend
route.Backend = ic.forwardBackendURL
route.Filters = []*eskip.Filter{}
}
}
}

func newIngress(o Options) *ingress {
return &ingress{
provideHTTPSRedirect: o.ProvideHTTPSRedirect,
Expand All @@ -86,6 +106,7 @@ func newIngress(o Options) *ingress {
forceKubernetesService: o.ForceKubernetesService,
backendTrafficAlgorithm: o.BackendTrafficAlgorithm,
defaultLoadBalancerAlgorithm: o.DefaultLoadBalancerAlgorithm,
forwardBackendURL: o.ForwardBackendURL,
kubernetesAnnotationPredicates: o.KubernetesAnnotationPredicates,
kubernetesAnnotationFiltersAppend: o.KubernetesAnnotationFiltersAppend,
kubernetesEastWestRangeAnnotationPredicates: o.KubernetesEastWestRangeAnnotationPredicates,
Expand Down Expand Up @@ -262,6 +283,21 @@ func annotationFilter(m *definitions.Metadata, logger *logger) []*eskip.Filter {
return nil
}

// parse backend annotation
func annotationBackend(m *definitions.Metadata) (eskip.BackendType, error) {
if s, ok := m.Annotations[definitions.IngressBackendAnnotation]; ok {
return eskip.BackendTypeFromString(s)
}
return 0, fmt.Errorf("annotation not found")
}

func annotationBackendString(m *definitions.Metadata) string {
if be, err := annotationBackend(m); err == nil {
return be.String()
}
return ""
}

// parse predicate annotation
func annotationPredicate(m *definitions.Metadata) string {
var annotationPredicate string
Expand Down Expand Up @@ -350,6 +386,22 @@ func hasCatchAllRoutes(routes []*eskip.Route) bool {
return false
}

func (ing *ingress) applyBackend(i *definitions.IngressV1Item, r *eskip.Route) {
if ing.forwardBackendURL == "" || r == nil {
return
}
if be, err := annotationBackend(i.Metadata); err != nil {
return
} else {
switch be {
case eskip.ForwardBackend:
r.BackendType = eskip.NetworkBackend
r.Backend = ing.forwardBackendURL
r.Filters = []*eskip.Filter{}
}
}
}

// convert logs if an invalid found, but proceeds with the valid ones.
// Reporting failures in Ingress status is not possible, because
// Ingress status field only supports IP and Hostname as string.
Expand All @@ -372,6 +424,7 @@ func (ing *ingress) convert(state *clusterState, df defaultFilters, r *certregis
ewIngInfo[r.Id] = []string{i.Metadata.Namespace, i.Metadata.Name}
}
}
ing.applyBackend(i, r)
}

for host, rs := range hostRoutes {
Expand Down
1 change: 1 addition & 0 deletions dataclients/kubernetes/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ func TestIngressV1AnnotationConfig(t *testing.T) {
kubernetestest.FixturesToTest(t,
"testdata/ingressV1/annotation-predicates",
"testdata/ingressV1/annotation-filters",
"testdata/ingressV1/annotation-backends",
)
}
4 changes: 4 additions & 0 deletions dataclients/kubernetes/ingressv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ func (ing *ingress) ingressV1Route(
logger: logger,
annotationFilters: annotationFilter(i.Metadata, logger),
annotationPredicate: annotationPredicate(i.Metadata),
annotationBackend: annotationBackendString(i.Metadata),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we need annotationBackendString() to make it more clean

forwardBackendURL: ing.forwardBackendURL,
extraRoutes: extraRoutes(i.Metadata),
backendWeights: backendWeights(i.Metadata, logger),
pathMode: pathMode(i.Metadata, ing.pathMode, logger),
Expand All @@ -446,9 +448,11 @@ func (ing *ingress) ingressV1Route(
var route *eskip.Route
if r, ok, err := ing.convertDefaultBackendV1(ic, ing.forceKubernetesService); ok {
route = r
ic.applyBackend(route)
} else if err != nil {
ic.logger.Errorf("Failed to convert default backend: %v", err)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for readability, because route is not touched anymore until "return".

for _, rule := range i.Spec.Rules {
err := ing.addSpecRuleV1(ic, rule)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kube_default__myapp__example_test____myapp:
Host("^(example[.]test[.]?(:[0-9]+)?)$")
-> "http://forward.example";

kube_default__myapp__0__example_test____: Host("^(example[.]test[.]?(:[0-9]+)?)$") && Method("OPTIONS") -> <shunt>;

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp
namespace: default
annotations:
zalando.org/skipper-backend: forward
zalando.org/skipper-routes: Method("OPTIONS") -> status(200) -> <shunt>
spec:
rules:
- host: example.test
http:
paths:
- backend:
service:
name: myapp
port:
number: 80
pathType: ImplementationSpecific
---
apiVersion: v1
kind: Service
metadata:
labels:
application: myapp
name: myapp
spec:
clusterIP: 10.3.190.97
ports:
- name: main
port: 80
protocol: TCP
targetPort: 7272
selector:
application: myapp
type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
labels:
application: myapp
name: myapp
namespace: default
subsets:
- addresses:
- ip: 10.2.9.103
- ip: 10.2.9.104
ports:
- name: main
port: 7272
protocol: TCP
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kube_default__myapp__example_test____myapp:
Host("^(example[.]test[.]?(:[0-9]+)?)$")
-> "http://forward.example";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
forward-backend-url: http://forward.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp
namespace: default
annotations:
zalando.org/skipper-backend: forward
zalando.org/skipper-filter: status(254)
spec:
rules:
- host: example.test
http:
paths:
- backend:
service:
name: myapp
port:
number: 80
pathType: ImplementationSpecific
---
apiVersion: v1
kind: Service
metadata:
labels:
application: myapp
name: myapp
spec:
clusterIP: 10.3.190.97
ports:
- name: main
port: 80
protocol: TCP
targetPort: 7272
selector:
application: myapp
type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
labels:
application: myapp
name: myapp
namespace: default
subsets:
- addresses:
- ip: 10.2.9.103
- ip: 10.2.9.104
ports:
- name: main
port: 7272
protocol: TCP
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kube_default__myapp__example_test____myapp:
Host("^(example[.]test[.]?(:[0-9]+)?)$") && True()
-> "http://forward.example";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
forward-backend-url: http://forward.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp
namespace: default
annotations:
zalando.org/skipper-backend: forward
zalando.org/skipper-predicate: True()
spec:
rules:
- host: example.test
http:
paths:
- backend:
service:
name: myapp
port:
number: 80
pathType: ImplementationSpecific
---
apiVersion: v1
kind: Service
metadata:
labels:
application: myapp
name: myapp
spec:
clusterIP: 10.3.190.97
ports:
- name: main
port: 80
protocol: TCP
targetPort: 7272
selector:
application: myapp
type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
labels:
application: myapp
name: myapp
namespace: default
subsets:
- addresses:
- ip: 10.2.9.103
- ip: 10.2.9.104
ports:
- name: main
port: 7272
protocol: TCP
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kube_default__myapp__example_test____myapp:
Host("^(example[.]test[.]?(:[0-9]+)?)$")
-> "http://forward.example";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
forward-backend-url: http://forward.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp
namespace: default
annotations:
zalando.org/skipper-backend: forward
spec:
rules:
- host: example.test
http:
paths:
- backend:
service:
name: myapp
port:
number: 80
pathType: ImplementationSpecific
---
apiVersion: v1
kind: Service
metadata:
labels:
application: myapp
name: myapp
spec:
clusterIP: 10.3.190.97
ports:
- name: main
port: 80
protocol: TCP
targetPort: 7272
selector:
application: myapp
type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
labels:
application: myapp
name: myapp
namespace: default
subsets:
- addresses:
- ip: 10.2.9.103
- ip: 10.2.9.104
ports:
- name: main
port: 7272
protocol: TCP
Loading