Skip to content

Commit d03be9f

Browse files
committed
add policy, location snippet, error page from VS to VSR for route selector
Signed-off-by: Haywood Shannon <[email protected]> Signed-off-by: Haywood Shannon <[email protected]>
1 parent ba3cc97 commit d03be9f

File tree

11 files changed

+247
-82
lines changed

11 files changed

+247
-82
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: k8s.nginx.org/v1
2+
kind: Policy
3+
metadata:
4+
name: api-key-policy
5+
namespace: cafe
6+
spec:
7+
apiKey:
8+
suppliedIn:
9+
header:
10+
- "X-header-name"
11+
query:
12+
- "queryName"
13+
clientSecret: api-key-client-secret
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: api-key-client-secret
5+
namespace: cafe
6+
type: nginx.org/apikey
7+
data:
8+
client1: cGFzc3dvcmQ= # password
9+
client2: YW5vdGhlci1wYXNzd29yZA== # another-password

examples/custom-resources/cross-namespace-configuration/cafe-virtual-server.yaml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,28 @@ spec:
77
host: cafe.example.com
88
tls:
99
secret: cafe-secret
10+
server-snippets: |
11+
# snippet defined in VS server block
12+
proxy_set_header X-VS-Name "Cafe";
1013
routes:
1114
# - path: /tea
1215
# route: tea/tea
16+
# policies:
17+
# - name: rate-limit-policy
1318
# - path: /coffee
1419
# route: coffee/coffee
15-
# - path: /coffee
16-
# routeSelector:
17-
# matchLabels:
18-
# route: coffee
1920
- path: /
2021
routeSelector:
2122
matchLabels:
22-
app: cafe
23+
app: cafe
24+
# route: tea
25+
policies:
26+
- name: api-key-policy
27+
location-snippets: |
28+
# snippet defined in VS
29+
proxy_set_header X-VS-Name "Cafe";
30+
errorPages:
31+
- codes: [ 502, 503 ]
32+
redirect:
33+
code: 301
34+
url: https://nginx.org

examples/custom-resources/cross-namespace-configuration/coffee-virtual-server-route.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ metadata:
44
name: coffee
55
namespace: coffee
66
labels:
7-
route: coffee
87
app: cafe
8+
route: coffee
99
spec:
1010
host: cafe.example.com
1111
upstreams:
@@ -16,3 +16,15 @@ spec:
1616
- path: /coffee
1717
action:
1818
pass: coffee
19+
policies:
20+
- name: rate-limit-policy
21+
location-snippets: |
22+
# snippet defined in VSR
23+
proxy_set_header X-VSR-Name "Coffee";
24+
errorPages:
25+
- codes: [404]
26+
return:
27+
code: 200
28+
body: "Original resource not found, but success!"
29+
30+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: k8s.nginx.org/v1
2+
kind: Policy
3+
metadata:
4+
name: rate-limit-policy
5+
namespace: coffee
6+
spec:
7+
rateLimit:
8+
rate: 1r/s
9+
key: ${binary_remote_addr}
10+
zoneSize: 10M

examples/custom-resources/cross-namespace-configuration/tea-virtual-server-route.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: tea
55
namespace: tea
66
labels:
7-
# route: tea
7+
route: tea
88
app: cafe
99
spec:
1010
host: cafe.example.com
@@ -16,3 +16,6 @@ spec:
1616
- path: /tea
1717
action:
1818
pass: tea
19+
# location-snippets: |
20+
# # snippet defined in VSR
21+
# proxy_set_header X-VSR-Name "Tea";

internal/configs/virtualserver.go

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/sha256"
66
"encoding/hex"
77
"fmt"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
89
"net/url"
910
"os"
1011
"path"
@@ -85,23 +86,24 @@ type PodInfo struct {
8586

8687
// VirtualServerEx holds a VirtualServer along with the resources that are referenced in this VirtualServer.
8788
type VirtualServerEx struct {
88-
VirtualServer *conf_v1.VirtualServer
89-
HTTPPort int
90-
HTTPSPort int
91-
HTTPIPv4 string
92-
HTTPIPv6 string
93-
HTTPSIPv4 string
94-
HTTPSIPv6 string
95-
Endpoints map[string][]string
96-
VirtualServerRoutes []*conf_v1.VirtualServerRoute
97-
ExternalNameSvcs map[string]bool
98-
Policies map[string]*conf_v1.Policy
99-
PodsByIP map[string]PodInfo
100-
SecretRefs map[string]*secrets.SecretReference
101-
ApPolRefs map[string]*unstructured.Unstructured
102-
LogConfRefs map[string]*unstructured.Unstructured
103-
DosProtectedRefs map[string]*unstructured.Unstructured
104-
DosProtectedEx map[string]*DosEx
89+
VirtualServer *conf_v1.VirtualServer
90+
HTTPPort int
91+
HTTPSPort int
92+
HTTPIPv4 string
93+
HTTPIPv6 string
94+
HTTPSIPv4 string
95+
HTTPSIPv6 string
96+
Endpoints map[string][]string
97+
VirtualServerRoutes []*conf_v1.VirtualServerRoute
98+
VirtualServerSelectorRoutes map[string][]string
99+
ExternalNameSvcs map[string]bool
100+
Policies map[string]*conf_v1.Policy
101+
PodsByIP map[string]PodInfo
102+
SecretRefs map[string]*secrets.SecretReference
103+
ApPolRefs map[string]*unstructured.Unstructured
104+
LogConfRefs map[string]*unstructured.Unstructured
105+
DosProtectedRefs map[string]*unstructured.Unstructured
106+
DosProtectedEx map[string]*DosEx
105107
}
106108

107109
func (vsx *VirtualServerEx) String() string {
@@ -398,6 +400,7 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
398400
apResources *appProtectResourcesForVS,
399401
dosResources map[string]*appProtectDosResource,
400402
) (version2.VirtualServerConfig, Warnings) {
403+
//l := nl.LoggerFromContext(vsc.cfgParams.Context)
401404
vsc.clearWarnings()
402405

403406
useCustomListeners := false
@@ -570,10 +573,48 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
570573

571574
continue
572575
} else if r.RouteSelector != nil {
573-
selector := r.RouteSelector
574-
glog.Infof("RouteSelector: %v", selector)
575576

576577
// get vsr name
578+
579+
selector := &metav1.LabelSelector{
580+
MatchLabels: r.RouteSelector.MatchLabels,
581+
}
582+
sel, _ := metav1.LabelSelectorAsSelector(selector)
583+
584+
selectorKey := sel.String()
585+
vsrKeys := vsEx.VirtualServerSelectorRoutes[selectorKey]
586+
//nl.Infof(l, "VirtualServerRoutes: %v", vsEx.VirtualServerRoutes)
587+
//
588+
//nl.Infof(l, "VirtualServerSelectorRoutes: %v", vsEx.VirtualServerSelectorRoutes)
589+
//
590+
//nl.Infof(l, "vsrKeys: %v", vsrKeys)
591+
//
592+
//nl.Infof(l, "RouteSelector: %v", selector)
593+
594+
// store route location snippet for the referenced VirtualServerRoute in case they don't define their own
595+
if r.LocationSnippets != "" {
596+
for _, name := range vsrKeys {
597+
vsrLocationSnippetsFromVs[name] = r.LocationSnippets
598+
}
599+
}
600+
601+
// store route error pages and route index for the referenced VirtualServerRoute in case they don't define their own
602+
if len(r.ErrorPages) > 0 {
603+
for _, name := range vsrKeys {
604+
vsrErrorPagesFromVs[name] = errorPages.pages
605+
vsrErrorPagesRouteIndex[name] = errorPages.index
606+
}
607+
}
608+
609+
// store route policies for the referenced VirtualServerRoute in case they don't define their own
610+
if len(r.Policies) > 0 {
611+
//nl.Infof(l, "Route Policies: %v", r.Policies)
612+
for _, name := range vsrKeys {
613+
//nl.Infof(l, "Adding policy to VSR $v: %v", name, r.Policies)
614+
vsrPoliciesFromVs[name] = r.Policies
615+
}
616+
}
617+
577618
continue
578619
}
579620

@@ -687,7 +728,7 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
687728
}
688729
errorPageLocations = append(errorPageLocations, generateErrorPageLocations(errorPages.index, errorPages.pages)...)
689730
vsrNamespaceName := fmt.Sprintf("%v/%v", vsr.Namespace, vsr.Name)
690-
glog.Infof("vsrNamespaceName: %v", vsrNamespaceName)
731+
//glog.Infof("vsrNamespaceName: %v", vsrNamespaceName)
691732
// use the VirtualServer error pages if the route does not define any
692733
if r.ErrorPages == nil {
693734
if vsErrorPages, ok := vsrErrorPagesFromVs[vsrNamespaceName]; ok {

0 commit comments

Comments
 (0)