Skip to content

Commit cb69e36

Browse files
committed
Merge branch origin main
2 parents 80820d6 + d7fbe44 commit cb69e36

File tree

19 files changed

+837
-203
lines changed

19 files changed

+837
-203
lines changed

api/adc/types.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ type Route struct {
126126
}
127127

128128
type Timeout struct {
129-
Connect float64 `json:"connect"`
130-
Read float64 `json:"read"`
131-
Send float64 `json:"send"`
129+
Connect int `json:"connect"`
130+
Read int `json:"read"`
131+
Send int `json:"send"`
132132
}
133133

134134
type StreamRoute struct {
@@ -149,7 +149,7 @@ type Upstream struct {
149149
HashOn string `json:"hash_on,omitempty" yaml:"hash_on,omitempty"`
150150
Key string `json:"key,omitempty" yaml:"key,omitempty"`
151151
Nodes UpstreamNodes `json:"nodes" yaml:"nodes"`
152-
PassHost *PassHost `json:"pass_host,omitempty" yaml:"pass_host,omitempty"`
152+
PassHost string `json:"pass_host,omitempty" yaml:"pass_host,omitempty"`
153153
Retries *int64 `json:"retries,omitempty" yaml:"retries,omitempty"`
154154
RetryTimeout *float64 `json:"retry_timeout,omitempty" yaml:"retry_timeout,omitempty"`
155155
Scheme string `json:"scheme,omitempty" yaml:"scheme,omitempty"`
@@ -202,14 +202,6 @@ const (
202202
Trace Method = "TRACE"
203203
)
204204

205-
type PassHost string
206-
207-
const (
208-
Node PassHost = "node"
209-
Pass PassHost = "pass"
210-
Rewrite PassHost = "rewrite"
211-
)
212-
213205
type Scheme string
214206

215207
const (

api/v1alpha1/backendtrafficpolicy_types.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ type BackendTrafficPolicySpec struct {
4444
//
4545
// +kubebuilder:validation:Enum=pass;node;rewrite;
4646
// +kubebuilder:default=pass
47-
PassHost string `json:"pass_host,omitempty" yaml:"pass_host,omitempty"`
47+
PassHost string `json:"passHost,omitempty" yaml:"passHost,omitempty"`
4848

4949
// Specifies the host of the Upstream request. This is only valid if
50-
// the pass_host is set to rewrite
51-
Host Hostname `json:"upstream_host,omitempty" yaml:"upstream_host,omitempty"`
50+
// the passHost is set to rewrite
51+
Host Hostname `json:"upstreamHost,omitempty" yaml:"upstreamHost,omitempty"`
5252
}
5353

5454
// LoadBalancer describes the load balancing parameters.
@@ -69,10 +69,16 @@ type LoadBalancer struct {
6969

7070
type Timeout struct {
7171
// +kubebuilder:default="60s"
72+
// +kubebuilder:validation:Pattern=`^[0-9]+s$`
73+
// +kubebuilder:validation:Type=string
7274
Connect metav1.Duration `json:"connect,omitempty" yaml:"connect,omitempty"`
7375
// +kubebuilder:default="60s"
76+
// +kubebuilder:validation:Pattern=`^[0-9]+s$`
77+
// +kubebuilder:validation:Type=string
7478
Send metav1.Duration `json:"send,omitempty" yaml:"send,omitempty"`
7579
// +kubebuilder:default="60s"
80+
// +kubebuilder:validation:Pattern=`^[0-9]+s$`
81+
// +kubebuilder:validation:Type=string
7682
Read metav1.Duration `json:"read,omitempty" yaml:"read,omitempty"`
7783
}
7884

config/crd/bases/gateway.apisix.io_backendtrafficpolicies.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ spec:
7070
type: object
7171
x-kubernetes-validations:
7272
- rule: '!(has(self.key) && self.type != ''chash'')'
73-
pass_host:
73+
passHost:
7474
default: pass
7575
description: |-
7676
Configures the host when the request is forwarded to the upstream.
@@ -164,18 +164,21 @@ spec:
164164
properties:
165165
connect:
166166
default: 60s
167+
pattern: ^[0-9]+s$
167168
type: string
168169
read:
169170
default: 60s
171+
pattern: ^[0-9]+s$
170172
type: string
171173
send:
172174
default: 60s
175+
pattern: ^[0-9]+s$
173176
type: string
174177
type: object
175-
upstream_host:
178+
upstreamHost:
176179
description: |-
177180
Specifies the host of the Upstream request. This is only valid if
178-
the pass_host is set to rewrite
181+
the passHost is set to rewrite
179182
maxLength: 253
180183
minLength: 1
181184
pattern: ^(\*\.)?[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$

config/samples/config.yaml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
log_level: "debug" # The log level of the API7 Ingress Controller.
1+
log_level: "info" # The log level of the API7 Ingress Controller.
22
# the default value is "info".
33

44
controller_name: gateway.apisix.io/api7-ingress-controller # The controller name of the API7 Ingress Controller,
@@ -15,16 +15,3 @@ leader_election:
1515
retry_period: 2s # retry_period is the time in seconds that the acting controller
1616
# will wait between tries of actions with the controller.
1717
disable: false # Whether to disable leader election.
18-
19-
# ingress_class: api7 # The ingress class name of the API7 Ingress Controller.
20-
# ingress_publish_service: "" # The service name of the ingress publish service.
21-
# ingress_status_address: [] # The status address of the ingress.
22-
# gateway_configs: # The configuration of the API7 Gateway.
23-
# - name: api7 # The name of the Gateway in the Gateway API.
24-
# control_plane:
25-
# admin_key: "${ADMIN_KEY}" # The admin key of the control plane.
26-
# endpoints:
27-
# - ${ENDPOINT} # The endpoint of the control plane.
28-
# tls_verify: false
29-
# addresses: # record the status address of the gateway-api gateway
30-
# - "172.18.0.4" # The LB IP of the gateway service.

internal/controller/config/config.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,6 @@ func (c *Config) Validate() error {
8989
return nil
9090
}
9191

92-
//nolint:unused
93-
func (c *Config) validateGatewayConfig(gc *GatewayConfig) error {
94-
95-
if gc.Name == "" {
96-
return fmt.Errorf("control_planesp[].gateway_name is required")
97-
}
98-
if gc.ControlPlane.AdminKey == "" {
99-
return fmt.Errorf("control_planes[].admin_api.admin_key is required")
100-
}
101-
if len(gc.ControlPlane.Endpoints) == 0 {
102-
return fmt.Errorf("control_planes[].admin_api.endpoints is required")
103-
}
104-
if gc.ControlPlane.TLSVerify == nil {
105-
gc.ControlPlane.TLSVerify = new(bool)
106-
*gc.ControlPlane.TLSVerify = true
107-
}
108-
109-
return nil
110-
}
111-
11292
func GetControllerName() string {
11393
return ControllerConfig.ControllerName
11494
}

internal/controller/gateway_controller.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
7575
if err := r.Provider.Delete(ctx, gateway); err != nil {
7676
return ctrl.Result{}, err
7777
}
78+
return ctrl.Result{}, nil
7879
}
7980
return ctrl.Result{}, err
8081
}
@@ -234,6 +235,9 @@ func (r *GatewayReconciler) listGatewaysForGatewayProxy(ctx context.Context, obj
234235

235236
recs := make([]reconcile.Request, 0, len(gatewayList.Items))
236237
for _, gateway := range gatewayList.Items {
238+
if !r.checkGatewayClass(&gateway) {
239+
continue
240+
}
237241
recs = append(recs, reconcile.Request{
238242
NamespacedName: client.ObjectKey{
239243
Namespace: gateway.GetNamespace(),
@@ -244,7 +248,7 @@ func (r *GatewayReconciler) listGatewaysForGatewayProxy(ctx context.Context, obj
244248
return recs
245249
}
246250

247-
func (r *GatewayReconciler) listGatewaysForHTTPRoute(_ context.Context, obj client.Object) []reconcile.Request {
251+
func (r *GatewayReconciler) listGatewaysForHTTPRoute(ctx context.Context, obj client.Object) []reconcile.Request {
248252
httpRoute, ok := obj.(*gatewayv1.HTTPRoute)
249253
if !ok {
250254
r.Log.Error(
@@ -268,6 +272,18 @@ func (r *GatewayReconciler) listGatewaysForHTTPRoute(_ context.Context, obj clie
268272
gatewayNamespace = string(*parentRef.Namespace)
269273
}
270274

275+
gateway := new(gatewayv1.Gateway)
276+
if err := r.Get(ctx, client.ObjectKey{
277+
Namespace: gatewayNamespace,
278+
Name: string(parentRef.Name),
279+
}, gateway); err != nil {
280+
continue
281+
}
282+
283+
if !r.checkGatewayClass(gateway) {
284+
continue
285+
}
286+
271287
recs = append(recs, reconcile.Request{
272288
NamespacedName: client.ObjectKey{
273289
Namespace: gatewayNamespace,

internal/controller/httproute_controller.go

Lines changed: 134 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,54 @@ func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
7272
},
7373
),
7474
).
75+
Watches(&v1alpha1.BackendTrafficPolicy{},
76+
handler.EnqueueRequestsFromMapFunc(r.listHTTPRoutesForBackendTrafficPolicy),
77+
builder.WithPredicates(
78+
predicate.Funcs{
79+
GenericFunc: func(e event.GenericEvent) bool {
80+
return false
81+
},
82+
DeleteFunc: func(e event.DeleteEvent) bool {
83+
return true
84+
},
85+
CreateFunc: func(e event.CreateEvent) bool {
86+
return true
87+
},
88+
UpdateFunc: func(e event.UpdateEvent) bool {
89+
oldObj, ok := e.ObjectOld.(*v1alpha1.BackendTrafficPolicy)
90+
newObj, ok2 := e.ObjectNew.(*v1alpha1.BackendTrafficPolicy)
91+
if !ok || !ok2 {
92+
return false
93+
}
94+
oldRefs := oldObj.Spec.TargetRefs
95+
newRefs := newObj.Spec.TargetRefs
96+
97+
oldRefMap := make(map[string]v1alpha1.BackendPolicyTargetReferenceWithSectionName)
98+
for _, ref := range oldRefs {
99+
key := fmt.Sprintf("%s/%s/%s", ref.Group, ref.Kind, ref.Name)
100+
oldRefMap[key] = ref
101+
}
102+
103+
for _, ref := range newRefs {
104+
key := fmt.Sprintf("%s/%s/%s", ref.Group, ref.Kind, ref.Name)
105+
delete(oldRefMap, key)
106+
}
107+
if len(oldRefMap) > 0 {
108+
targetRefs := make([]v1alpha1.BackendPolicyTargetReferenceWithSectionName, 0, len(oldRefs))
109+
for _, ref := range oldRefMap {
110+
targetRefs = append(targetRefs, ref)
111+
}
112+
dump := oldObj.DeepCopy()
113+
dump.Spec.TargetRefs = targetRefs
114+
r.genericEvent <- event.GenericEvent{
115+
Object: dump,
116+
}
117+
}
118+
return true
119+
},
120+
},
121+
),
122+
).
75123
Watches(&v1alpha1.HTTPRoutePolicy{},
76124
handler.EnqueueRequestsFromMapFunc(r.listHTTPRouteByHTTPRoutePolicy),
77125
builder.WithPredicates(
@@ -144,6 +192,7 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
144192

145193
tctx := provider.NewDefaultTranslateContext(ctx)
146194

195+
tctx.RouteParentRefs = hr.Spec.ParentRefs
147196
rk := provider.ResourceKind{
148197
Kind: hr.Kind,
149198
Namespace: hr.Namespace,
@@ -173,6 +222,8 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
173222
}
174223
}
175224

225+
ProcessBackendTrafficPolicy(r.Client, r.Log, tctx)
226+
176227
if err := r.Provider.Update(ctx, tctx, hr); err != nil {
177228
acceptStatus.status = false
178229
acceptStatus.msg = err.Error()
@@ -256,6 +307,54 @@ func (r *HTTPRouteReconciler) listHTTPRoutesByExtensionRef(ctx context.Context,
256307
return requests
257308
}
258309

310+
func (r *HTTPRouteReconciler) listHTTPRoutesForBackendTrafficPolicy(ctx context.Context, obj client.Object) []reconcile.Request {
311+
policy, ok := obj.(*v1alpha1.BackendTrafficPolicy)
312+
if !ok {
313+
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to BackendTrafficPolicy")
314+
return nil
315+
}
316+
317+
httprouteList := []gatewayv1.HTTPRoute{}
318+
for _, targetRef := range policy.Spec.TargetRefs {
319+
service := &corev1.Service{}
320+
if err := r.Get(ctx, client.ObjectKey{
321+
Namespace: policy.Namespace,
322+
Name: string(targetRef.Name),
323+
}, service); err != nil {
324+
if client.IgnoreNotFound(err) != nil {
325+
r.Log.Error(err, "failed to get service", "namespace", policy.Namespace, "name", targetRef.Name)
326+
}
327+
continue
328+
}
329+
hrList := &gatewayv1.HTTPRouteList{}
330+
if err := r.List(ctx, hrList, client.MatchingFields{
331+
indexer.ServiceIndexRef: indexer.GenIndexKey(policy.Namespace, string(targetRef.Name)),
332+
}); err != nil {
333+
r.Log.Error(err, "failed to list httproutes by service reference", "service", targetRef.Name)
334+
return nil
335+
}
336+
httprouteList = append(httprouteList, hrList.Items...)
337+
}
338+
var namespacedNameMap = make(map[types.NamespacedName]struct{})
339+
requests := make([]reconcile.Request, 0, len(httprouteList))
340+
for _, hr := range httprouteList {
341+
key := types.NamespacedName{
342+
Namespace: hr.Namespace,
343+
Name: hr.Name,
344+
}
345+
if _, ok := namespacedNameMap[key]; !ok {
346+
namespacedNameMap[key] = struct{}{}
347+
requests = append(requests, reconcile.Request{
348+
NamespacedName: client.ObjectKey{
349+
Namespace: hr.Namespace,
350+
Name: hr.Name,
351+
},
352+
})
353+
}
354+
}
355+
return requests
356+
}
357+
259358
func (r *HTTPRouteReconciler) listHTTPRoutesForGateway(ctx context.Context, obj client.Object) []reconcile.Request {
260359
gateway, ok := obj.(*gatewayv1.Gateway)
261360
if !ok {
@@ -330,27 +429,53 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
330429
return requests
331430
}
332431

333-
func (r *HTTPRouteReconciler) listHTTPRouteForGenericEvent(ctx context.Context, obj client.Object) []reconcile.Request {
432+
func (r *HTTPRouteReconciler) listHTTPRouteForGenericEvent(ctx context.Context, obj client.Object) (requests []reconcile.Request) {
433+
var namespacedNameMap = make(map[types.NamespacedName]struct{})
434+
334435
switch v := obj.(type) {
436+
case *v1alpha1.BackendTrafficPolicy:
437+
httprouteAll := []gatewayv1.HTTPRoute{}
438+
for _, ref := range v.Spec.TargetRefs {
439+
httprouteList := &gatewayv1.HTTPRouteList{}
440+
if err := r.List(ctx, httprouteList, client.MatchingFields{
441+
indexer.ServiceIndexRef: indexer.GenIndexKey(v.GetNamespace(), string(ref.Name)),
442+
}); err != nil {
443+
r.Log.Error(err, "failed to list HTTPRoutes for BackendTrafficPolicy", "namespace", v.GetNamespace(), "ref", ref.Name)
444+
return nil
445+
}
446+
httprouteAll = append(httprouteAll, httprouteList.Items...)
447+
}
448+
for _, hr := range httprouteAll {
449+
key := types.NamespacedName{
450+
Namespace: hr.Namespace,
451+
Name: hr.Name,
452+
}
453+
if _, ok := namespacedNameMap[key]; !ok {
454+
namespacedNameMap[key] = struct{}{}
455+
requests = append(requests, reconcile.Request{
456+
NamespacedName: client.ObjectKey{
457+
Namespace: hr.Namespace,
458+
Name: hr.Name,
459+
},
460+
})
461+
}
462+
}
335463
case *v1alpha1.HTTPRoutePolicy:
336-
var (
337-
namespacedNames = make(map[types.NamespacedName]struct{})
338-
requests []reconcile.Request
339-
)
340464
for _, ref := range v.Spec.TargetRefs {
341465
namespacedName := types.NamespacedName{Namespace: v.GetNamespace(), Name: string(ref.Name)}
342-
if _, ok := namespacedNames[namespacedName]; !ok {
343-
namespacedNames[namespacedName] = struct{}{}
466+
if _, ok := namespacedNameMap[namespacedName]; !ok {
467+
namespacedNameMap[namespacedName] = struct{}{}
344468
if err := r.Get(ctx, namespacedName, new(gatewayv1.HTTPRoute)); err != nil {
345469
r.Log.Info("failed to Get HTTPRoute", "namespace", namespacedName.Namespace, "name", namespacedName.Name)
346470
continue
347471
}
348472
requests = append(requests, reconcile.Request{NamespacedName: namespacedName})
349473
}
350474
}
351-
return requests
475+
default:
476+
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to BackendTrafficPolicy")
352477
}
353-
return nil
478+
return requests
354479
}
355480

356481
func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.TranslateContext) error {

0 commit comments

Comments
 (0)