Skip to content

Commit

Permalink
add policy, location snippet, error page from VS to VSR for route sel…
Browse files Browse the repository at this point in the history
…ector

Signed-off-by: Haywood Shannon <[email protected]>

Signed-off-by: Haywood Shannon <[email protected]>
  • Loading branch information
haywoodsh committed Nov 13, 2024
1 parent ba3cc97 commit d03be9f
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: k8s.nginx.org/v1
kind: Policy
metadata:
name: api-key-policy
namespace: cafe
spec:
apiKey:
suppliedIn:
header:
- "X-header-name"
query:
- "queryName"
clientSecret: api-key-client-secret
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Secret
metadata:
name: api-key-client-secret
namespace: cafe
type: nginx.org/apikey
data:
client1: cGFzc3dvcmQ= # password
client2: YW5vdGhlci1wYXNzd29yZA== # another-password
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@ spec:
host: cafe.example.com
tls:
secret: cafe-secret
server-snippets: |
# snippet defined in VS server block
proxy_set_header X-VS-Name "Cafe";
routes:
# - path: /tea
# route: tea/tea
# policies:
# - name: rate-limit-policy
# - path: /coffee
# route: coffee/coffee
# - path: /coffee
# routeSelector:
# matchLabels:
# route: coffee
- path: /
routeSelector:
matchLabels:
app: cafe
app: cafe
# route: tea
policies:
- name: api-key-policy
location-snippets: |
# snippet defined in VS
proxy_set_header X-VS-Name "Cafe";
errorPages:
- codes: [ 502, 503 ]
redirect:
code: 301
url: https://nginx.org
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ metadata:
name: coffee
namespace: coffee
labels:
route: coffee
app: cafe
route: coffee
spec:
host: cafe.example.com
upstreams:
Expand All @@ -16,3 +16,15 @@ spec:
- path: /coffee
action:
pass: coffee
policies:
- name: rate-limit-policy
location-snippets: |
# snippet defined in VSR
proxy_set_header X-VSR-Name "Coffee";
errorPages:
- codes: [404]
return:
code: 200
body: "Original resource not found, but success!"


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: k8s.nginx.org/v1
kind: Policy
metadata:
name: rate-limit-policy
namespace: coffee
spec:
rateLimit:
rate: 1r/s
key: ${binary_remote_addr}
zoneSize: 10M
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: tea
namespace: tea
labels:
# route: tea
route: tea
app: cafe
spec:
host: cafe.example.com
Expand All @@ -16,3 +16,6 @@ spec:
- path: /tea
action:
pass: tea
# location-snippets: |
# # snippet defined in VSR
# proxy_set_header X-VSR-Name "Tea";
81 changes: 61 additions & 20 deletions internal/configs/virtualserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -85,23 +86,24 @@ type PodInfo struct {

// VirtualServerEx holds a VirtualServer along with the resources that are referenced in this VirtualServer.
type VirtualServerEx struct {
VirtualServer *conf_v1.VirtualServer
HTTPPort int
HTTPSPort int
HTTPIPv4 string
HTTPIPv6 string
HTTPSIPv4 string
HTTPSIPv6 string
Endpoints map[string][]string
VirtualServerRoutes []*conf_v1.VirtualServerRoute
ExternalNameSvcs map[string]bool
Policies map[string]*conf_v1.Policy
PodsByIP map[string]PodInfo
SecretRefs map[string]*secrets.SecretReference
ApPolRefs map[string]*unstructured.Unstructured
LogConfRefs map[string]*unstructured.Unstructured
DosProtectedRefs map[string]*unstructured.Unstructured
DosProtectedEx map[string]*DosEx
VirtualServer *conf_v1.VirtualServer
HTTPPort int
HTTPSPort int
HTTPIPv4 string
HTTPIPv6 string
HTTPSIPv4 string
HTTPSIPv6 string
Endpoints map[string][]string
VirtualServerRoutes []*conf_v1.VirtualServerRoute
VirtualServerSelectorRoutes map[string][]string
ExternalNameSvcs map[string]bool
Policies map[string]*conf_v1.Policy
PodsByIP map[string]PodInfo
SecretRefs map[string]*secrets.SecretReference
ApPolRefs map[string]*unstructured.Unstructured
LogConfRefs map[string]*unstructured.Unstructured
DosProtectedRefs map[string]*unstructured.Unstructured
DosProtectedEx map[string]*DosEx
}

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

useCustomListeners := false
Expand Down Expand Up @@ -570,10 +573,48 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(

continue
} else if r.RouteSelector != nil {
selector := r.RouteSelector
glog.Infof("RouteSelector: %v", selector)

// get vsr name

selector := &metav1.LabelSelector{
MatchLabels: r.RouteSelector.MatchLabels,
}
sel, _ := metav1.LabelSelectorAsSelector(selector)

selectorKey := sel.String()
vsrKeys := vsEx.VirtualServerSelectorRoutes[selectorKey]
//nl.Infof(l, "VirtualServerRoutes: %v", vsEx.VirtualServerRoutes)
//
//nl.Infof(l, "VirtualServerSelectorRoutes: %v", vsEx.VirtualServerSelectorRoutes)
//
//nl.Infof(l, "vsrKeys: %v", vsrKeys)
//
//nl.Infof(l, "RouteSelector: %v", selector)

// store route location snippet for the referenced VirtualServerRoute in case they don't define their own
if r.LocationSnippets != "" {
for _, name := range vsrKeys {
vsrLocationSnippetsFromVs[name] = r.LocationSnippets
}
}

// store route error pages and route index for the referenced VirtualServerRoute in case they don't define their own
if len(r.ErrorPages) > 0 {
for _, name := range vsrKeys {
vsrErrorPagesFromVs[name] = errorPages.pages
vsrErrorPagesRouteIndex[name] = errorPages.index
}
}

// store route policies for the referenced VirtualServerRoute in case they don't define their own
if len(r.Policies) > 0 {
//nl.Infof(l, "Route Policies: %v", r.Policies)
for _, name := range vsrKeys {
//nl.Infof(l, "Adding policy to VSR $v: %v", name, r.Policies)
vsrPoliciesFromVs[name] = r.Policies
}
}

continue
}

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

0 comments on commit d03be9f

Please sign in to comment.