Skip to content

Commit 9c02a74

Browse files
committed
Merge branch 'main' of github.com:kgateway-dev/ingress2gateway into backend-tls
Signed-off-by: omar <omar.hammami@solo.io>
2 parents df873bf + 9d49265 commit 9c02a74

12 files changed

Lines changed: 526 additions & 265 deletions

File tree

pkg/i2gw/implementations/kgateway/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ The command should generate Gateway API and Kgateway resources.
6262

6363
### Backend Behavior
6464

65-
- `nginx.ingress.kubernetes.io/proxy-connect-timeout`
65+
- `nginx.ingress.kubernetes.io/proxy-connect-timeout`: Sets the timeout for establishing a connection with a proxied server. It should be noted that this timeout
66+
cannot usually exceed 75 seconds.
67+
- `nginx.ingress.kubernetes.io/load-balance`: Sets the algorithm to use for load balancing to a proxied server. The only supported value is `round_robin`.
6668
- `nginx.ingress.kubernetes.io/affinity`: Enables session affinity (only "cookie" type is supported). Maps to `BackendConfigPolicy.spec.loadBalancer.ringHash.hashPolicies`.
6769
- `nginx.ingress.kubernetes.io/session-cookie-name`: Specifies the name of the cookie used for session affinity. Maps to `BackendConfigPolicy.spec.loadBalancer.ringHash.hashPolicies[].cookie.name`.
6870
- `nginx.ingress.kubernetes.io/session-cookie-path`: Defines the path that will be set on the cookie. Maps to `BackendConfigPolicy.spec.loadBalancer.ringHash.hashPolicies[].cookie.path`.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kgateway
18+
19+
import (
20+
"fmt"
21+
"net"
22+
"net/url"
23+
"strings"
24+
25+
"github.com/kgateway-dev/ingress2gateway/pkg/i2gw/intermediate"
26+
"github.com/kgateway-dev/kgateway/v2/api/v1alpha1/kgateway"
27+
"github.com/kgateway-dev/kgateway/v2/api/v1alpha1/shared"
28+
"k8s.io/utils/ptr"
29+
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
32+
)
33+
34+
// parsedAuthURL contains the fields you can use to build a BackendObjectReference.
35+
type parsedAuthURL struct {
36+
service string
37+
namespace string
38+
port int32
39+
path string
40+
external bool // true if host is not a Kubernetes service
41+
}
42+
43+
// parseAuthURL parses an nginx.ingress.kubernetes.io/auth-url value into a ParsedAuthURL.
44+
// ingressNS = namespace of the Ingress (used when namespace is omitted).
45+
func parseAuthURL(raw string, ingressNS string) (*parsedAuthURL, error) {
46+
if raw == "" {
47+
return nil, fmt.Errorf("auth-url is empty")
48+
}
49+
50+
u, err := url.Parse(raw)
51+
if err != nil {
52+
return nil, fmt.Errorf("invalid auth-url: %w", err)
53+
}
54+
55+
// Default path
56+
path := u.Path
57+
if path == "" {
58+
path = "/"
59+
}
60+
61+
host := u.Host
62+
63+
// Split host and port
64+
var hostname, portStr string
65+
if h, p, err := net.SplitHostPort(host); err == nil {
66+
hostname = h
67+
portStr = p
68+
} else {
69+
hostname = host
70+
}
71+
72+
// Detect external hostname (not a Kubernetes service)
73+
if !strings.Contains(hostname, ".svc") {
74+
return &parsedAuthURL{
75+
external: true,
76+
path: path,
77+
}, nil
78+
}
79+
80+
// Normalize cluster-local suffixes
81+
hostname = strings.TrimSuffix(hostname, ".cluster.local")
82+
hostname = strings.TrimSuffix(hostname, ".svc")
83+
84+
parts := strings.Split(hostname, ".")
85+
if len(parts) < 1 {
86+
return nil, fmt.Errorf("unable to extract service from hostname %q", hostname)
87+
}
88+
89+
service := parts[0]
90+
91+
// Determine namespace
92+
namespace := ingressNS
93+
if len(parts) >= 2 {
94+
namespace = parts[1]
95+
}
96+
97+
// Port
98+
var port int32
99+
if portStr != "" {
100+
var parsed int
101+
fmt.Sscanf(portStr, "%d", &parsed)
102+
port = int32(parsed)
103+
} else {
104+
switch u.Scheme {
105+
case "https":
106+
port = 443
107+
default:
108+
port = 80
109+
}
110+
}
111+
112+
return &parsedAuthURL{
113+
service: service,
114+
namespace: namespace,
115+
port: port,
116+
path: path,
117+
external: false,
118+
}, nil
119+
}
120+
121+
// applyExtAuthPolicy projects the ExtAuth IR policy into a GatewayExtension
122+
// and ExtAuthPolicy in TrafficPolicy.
123+
//
124+
// Semantics:
125+
// - We create one GatewayExtension per unique auth-url.
126+
// - That GatewayExtension's Spec.ExtAuth.HttpService references an existing Service
127+
// (parsed from the auth URL).
128+
// - An ExtAuthPolicy is added to TrafficPolicy that references the GatewayExtension.
129+
func applyExtAuthPolicy(
130+
pol intermediate.Policy,
131+
ingressName, namespace string,
132+
tp map[string]*kgateway.TrafficPolicy,
133+
gatewayExtensions map[string]*kgateway.GatewayExtension,
134+
) bool {
135+
if pol.ExtAuth == nil || pol.ExtAuth.AuthURL == "" {
136+
return false
137+
}
138+
139+
authURL := pol.ExtAuth.AuthURL
140+
141+
// Parse the auth URL to extract service information.
142+
parsed, err := parseAuthURL(authURL, namespace)
143+
if err != nil {
144+
// Invalid URL, skip it.
145+
return false
146+
}
147+
148+
// Skip external URLs as we can only reference Kubernetes Services.
149+
if parsed.external {
150+
return false
151+
}
152+
153+
// Create GatewayExtension with ExtAuth using HttpService.
154+
extHttpService := &kgateway.ExtHttpService{
155+
BackendRef: gwv1.BackendRef{
156+
BackendObjectReference: gwv1.BackendObjectReference{
157+
Name: gwv1.ObjectName(parsed.service),
158+
Namespace: ptr.To(gwv1.Namespace(parsed.namespace)), // TODO: confirm that different namespace works
159+
Port: ptr.To(gwv1.PortNumber(parsed.port)),
160+
},
161+
},
162+
PathPrefix: parsed.path,
163+
}
164+
165+
// Set AuthorizationResponse if response headers are specified.
166+
if len(pol.ExtAuth.ResponseHeaders) > 0 {
167+
extHttpService.AuthorizationResponse = &kgateway.AuthorizationResponse{
168+
HeadersToBackend: pol.ExtAuth.ResponseHeaders,
169+
}
170+
}
171+
172+
ge := &kgateway.GatewayExtension{
173+
ObjectMeta: metav1.ObjectMeta{
174+
Name: fmt.Sprintf("%s-extauth", ingressName),
175+
Namespace: namespace,
176+
},
177+
Spec: kgateway.GatewayExtensionSpec{
178+
ExtAuth: &kgateway.ExtAuthProvider{
179+
HttpService: extHttpService,
180+
},
181+
},
182+
}
183+
ge.SetGroupVersionKind(GatewayExtensionGVK)
184+
185+
// Add ExtAuthPolicy to TrafficPolicy.
186+
t := ensureTrafficPolicy(tp, ingressName, namespace)
187+
188+
t.Spec.ExtAuth = &kgateway.ExtAuthPolicy{
189+
ExtensionRef: &shared.NamespacedObjectReference{
190+
Name: gwv1.ObjectName(ge.Name),
191+
Namespace: ptr.To(gwv1.Namespace(ge.Namespace)),
192+
},
193+
}
194+
195+
gatewayExtensions[ingressName] = ge
196+
return true
197+
}

0 commit comments

Comments
 (0)