Skip to content

Commit 9223a02

Browse files
committed
fix
1 parent 0a99a0b commit 9223a02

File tree

6 files changed

+55
-35
lines changed

6 files changed

+55
-35
lines changed

api/adc/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ type Route struct {
128128
}
129129

130130
type Timeout struct {
131-
Connect *int64 `json:"connect,omitempty"`
132-
Read *int64 `json:"read,omitempty"`
133-
Send *int64 `json:"send,omitempty"`
131+
Connect int64 `json:"connect"`
132+
Read int64 `json:"read"`
133+
Send int64 `json:"send"`
134134
}
135135

136136
type StreamRoute struct {

api/v1alpha1/backendtrafficpolicy_types.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ type LoadBalancer struct {
7272
}
7373

7474
type Timeout struct {
75+
// +kubebuilder:default="60s"
7576
Connect metav1.Duration `json:"connect,omitempty" yaml:"connect,omitempty"`
76-
Send metav1.Duration `json:"send,omitempty" yaml:"send,omitempty"`
77-
Read metav1.Duration `json:"read,omitempty" yaml:"read,omitempty"`
77+
// +kubebuilder:default="60s"
78+
Send metav1.Duration `json:"send,omitempty" yaml:"send,omitempty"`
79+
// +kubebuilder:default="60s"
80+
Read metav1.Duration `json:"read,omitempty" yaml:"read,omitempty"`
7881
}
7982

8083
// +kubebuilder:object:root=true

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,13 @@ spec:
168168
upstream.
169169
properties:
170170
connect:
171+
default: 60s
171172
type: string
172173
read:
174+
default: 60s
173175
type: string
174176
send:
177+
default: 60s
175178
type: string
176179
type: object
177180
upstream_host:

internal/controller/indexer/indexer.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ func SetupIndexer(mgr ctrl.Manager) error {
3737
if err := setupConsumerIndexer(mgr); err != nil {
3838
return err
3939
}
40+
if err := setupBackendTrafficPolicyIndexer(mgr); err != nil {
41+
return err
42+
}
4043
return nil
4144
}
4245

@@ -178,6 +181,18 @@ func setupIngressIndexer(mgr ctrl.Manager) error {
178181
return nil
179182
}
180183

184+
func setupBackendTrafficPolicyIndexer(mgr ctrl.Manager) error {
185+
if err := mgr.GetFieldIndexer().IndexField(
186+
context.Background(),
187+
&v1alpha1.BackendTrafficPolicy{},
188+
PolicyTargetRefs,
189+
BackendTrafficPolicyIndexFunc,
190+
); err != nil {
191+
return err
192+
}
193+
return nil
194+
}
195+
181196
func IngressClassIndexFunc(rawObj client.Object) []string {
182197
ingressClass := rawObj.(*networkingv1.IngressClass)
183198
if ingressClass.Spec.Controller == "" {
@@ -314,7 +329,7 @@ func BackendTrafficPolicyIndexFunc(rawObj client.Object) []string {
314329
for _, ref := range btp.Spec.TargetRefs {
315330
keys = append(keys,
316331
GenIndexKeyWithGK(
317-
v1alpha1.GroupVersion.Group,
332+
string(ref.Group),
318333
string(ref.Kind),
319334
btp.GetNamespace(),
320335
string(ref.Name),

internal/controller/policies.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/api7/api7-ingress-controller/internal/controller/config"
1111
"github.com/api7/api7-ingress-controller/internal/controller/indexer"
1212
"github.com/api7/api7-ingress-controller/internal/provider"
13+
"github.com/go-logr/logr"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415
"k8s.io/apimachinery/pkg/runtime/schema"
1516
"k8s.io/apimachinery/pkg/types"
@@ -25,7 +26,9 @@ func (p PolicyTargetKey) String() string {
2526
return p.NsName.String() + "/" + p.GroupKind.String()
2627
}
2728

28-
func ProcessBackendTrafficPolicy(c client.Client, tctx *provider.TranslateContext) {
29+
func ProcessBackendTrafficPolicy(c client.Client,
30+
log logr.Logger,
31+
tctx *provider.TranslateContext) {
2932
conflicts := map[string]v1alpha1.BackendTrafficPolicy{}
3033
for _, service := range tctx.Services {
3134
backendTrafficPolicyList := &v1alpha1.BackendTrafficPolicyList{}
@@ -34,10 +37,8 @@ func ProcessBackendTrafficPolicy(c client.Client, tctx *provider.TranslateContex
3437
indexer.PolicyTargetRefs: indexer.GenIndexKeyWithGK("", "Service", service.Namespace, service.Name),
3538
},
3639
); err != nil {
37-
if client.IgnoreNotFound(err) == nil {
38-
continue
39-
}
40-
return
40+
log.Error(err, "failed to list BackendTrafficPolicy for Service")
41+
continue
4142
}
4243
if len(backendTrafficPolicyList.Items) == 0 {
4344
continue

internal/provider/adc/translator/policies.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,34 @@ package translator
33
import (
44
"github.com/api7/api7-ingress-controller/api/adc"
55
"github.com/api7/api7-ingress-controller/api/v1alpha1"
6+
"github.com/api7/gopkg/pkg/log"
7+
"go.uber.org/zap"
68
"k8s.io/apimachinery/pkg/types"
9+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
710

811
adctypes "github.com/api7/api7-ingress-controller/api/adc"
912
)
1013

11-
func (t *Translator) AttachBackendTrafficPolicyToUpstream(policies map[types.NamespacedName]*v1alpha1.BackendTrafficPolicy, upstream *adctypes.Upstream) {
14+
func (t *Translator) AttachBackendTrafficPolicyToUpstream(ref gatewayv1.BackendRef, policies map[types.NamespacedName]*v1alpha1.BackendTrafficPolicy, upstream *adctypes.Upstream) {
1215
if len(policies) == 0 {
1316
return
1417
}
15-
for _, policy := range policies {
16-
t.attachBackendTrafficPolicyToUpstream(policy, upstream)
18+
var policy *v1alpha1.BackendTrafficPolicy
19+
for _, po := range policies {
20+
for _, targetRef := range po.Spec.TargetRefs {
21+
if ref.Name == targetRef.Name &&
22+
(ref.Namespace != nil && string(*ref.Namespace) == po.Namespace) {
23+
policy = po
24+
break
25+
}
26+
}
1727
}
18-
28+
if policy == nil {
29+
return
30+
}
31+
log.Errorw("attach backend traffic policy to upstream",
32+
zap.String("policy", policy.Name))
33+
t.attachBackendTrafficPolicyToUpstream(policy, upstream)
1934
}
2035

2136
func (t *Translator) attachBackendTrafficPolicyToUpstream(policy *v1alpha1.BackendTrafficPolicy, upstream *adctypes.Upstream) {
@@ -29,27 +44,10 @@ func (t *Translator) attachBackendTrafficPolicyToUpstream(policy *v1alpha1.Backe
2944
*upstream.Retries = int64(*policy.Spec.Retries)
3045
}
3146
if policy.Spec.Timeout != nil {
32-
var (
33-
connect *int64
34-
read *int64
35-
send *int64
36-
)
37-
if policy.Spec.Timeout.Connect.Duration > 0 {
38-
connect = new(int64)
39-
*connect = policy.Spec.Timeout.Connect.Duration.Milliseconds()
40-
}
41-
if policy.Spec.Timeout.Read.Duration > 0 {
42-
read = new(int64)
43-
*read = policy.Spec.Timeout.Read.Duration.Milliseconds()
44-
}
45-
if policy.Spec.Timeout.Send.Duration > 0 {
46-
send = new(int64)
47-
*send = policy.Spec.Timeout.Send.Duration.Milliseconds()
48-
}
4947
upstream.Timeout = &adctypes.Timeout{
50-
Connect: connect,
51-
Read: read,
52-
Send: send,
48+
Connect: policy.Spec.Timeout.Connect.Duration.Milliseconds(),
49+
Read: policy.Spec.Timeout.Read.Duration.Milliseconds(),
50+
Send: policy.Spec.Timeout.Send.Duration.Milliseconds(),
5351
}
5452
}
5553
if policy.Spec.LoadBalancer != nil {

0 commit comments

Comments
 (0)