Skip to content

Commit e261a56

Browse files
committed
fix: r
Signed-off-by: Ashing Zheng <[email protected]>
1 parent c024696 commit e261a56

File tree

1 file changed

+167
-140
lines changed

1 file changed

+167
-140
lines changed

internal/provider/api7ee/status.go

Lines changed: 167 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -68,162 +68,189 @@ func (d *api7eeProvider) handleStatusUpdate(statusUpdateMap map[types.Namespaced
6868
d.statusUpdateMap = statusUpdateMap
6969
}
7070

71+
//nolint:gocyclo
7172
func (d *api7eeProvider) updateStatus(nnk types.NamespacedNameKind, condition metav1.Condition) {
7273
switch nnk.Kind {
7374
case types.KindApisixRoute:
74-
d.updateApisixCRDStatus(nnk, &apiv2.ApisixRoute{}, condition)
75-
case types.KindApisixGlobalRule:
76-
d.updateApisixCRDStatus(nnk, &apiv2.ApisixGlobalRule{}, condition)
77-
case types.KindApisixTls:
78-
d.updateApisixCRDStatus(nnk, &apiv2.ApisixTls{}, condition)
79-
case types.KindApisixConsumer:
80-
d.updateApisixCRDStatus(nnk, &apiv2.ApisixConsumer{}, condition)
81-
case types.KindHTTPRoute:
82-
d.updateGatewayRouteStatus(nnk, &gatewayv1.HTTPRoute{}, condition)
83-
case types.KindTCPRoute:
84-
log.Debugw("updating TCPRoute status",
85-
zap.Any("parentRefs", d.client.ConfigManager.GetConfigRefsByResourceKey(nnk)))
86-
d.updateGatewayRouteStatus(nnk, &gatewayv1alpha2.TCPRoute{}, condition)
87-
case types.KindUDPRoute:
88-
log.Debugw("updating UDPRoute status",
89-
zap.Any("parentRefs", d.client.ConfigManager.GetConfigRefsByResourceKey(nnk)))
90-
d.updateGatewayRouteStatus(nnk, &gatewayv1alpha2.UDPRoute{}, condition)
91-
case types.KindGRPCRoute:
92-
log.Debugw("updating GRPCRoute status",
93-
zap.Any("parentRefs", d.client.ConfigManager.GetConfigRefsByResourceKey(nnk)))
94-
d.updateGatewayRouteStatus(nnk, &gatewayv1.GRPCRoute{}, condition)
95-
}
96-
}
97-
98-
// updateApisixCRDStatus updates the status of APISIX CRD resources.
99-
func (d *api7eeProvider) updateApisixCRDStatus(
100-
nnk types.NamespacedNameKind,
101-
resource client.Object,
102-
condition metav1.Condition,
103-
) {
104-
d.updater.Update(status.Update{
105-
NamespacedName: nnk.NamespacedName(),
106-
Resource: resource,
107-
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
108-
switch v := obj.(type) {
109-
case *apiv2.ApisixRoute:
110-
cp := v.DeepCopy()
75+
d.updater.Update(status.Update{
76+
NamespacedName: nnk.NamespacedName(),
77+
Resource: &apiv2.ApisixRoute{},
78+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
79+
cp := obj.(*apiv2.ApisixRoute).DeepCopy()
11180
cutils.SetApisixCRDConditionWithGeneration(&cp.Status, cp.GetGeneration(), condition)
11281
return cp
113-
case *apiv2.ApisixGlobalRule:
114-
cp := v.DeepCopy()
82+
}),
83+
})
84+
case types.KindApisixGlobalRule:
85+
d.updater.Update(status.Update{
86+
NamespacedName: nnk.NamespacedName(),
87+
Resource: &apiv2.ApisixGlobalRule{},
88+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
89+
cp := obj.(*apiv2.ApisixGlobalRule).DeepCopy()
11590
cutils.SetApisixCRDConditionWithGeneration(&cp.Status, cp.GetGeneration(), condition)
11691
return cp
117-
case *apiv2.ApisixTls:
118-
cp := v.DeepCopy()
92+
}),
93+
})
94+
case types.KindApisixTls:
95+
d.updater.Update(status.Update{
96+
NamespacedName: nnk.NamespacedName(),
97+
Resource: &apiv2.ApisixTls{},
98+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
99+
cp := obj.(*apiv2.ApisixTls).DeepCopy()
119100
cutils.SetApisixCRDConditionWithGeneration(&cp.Status, cp.GetGeneration(), condition)
120101
return cp
121-
case *apiv2.ApisixConsumer:
122-
cp := v.DeepCopy()
102+
}),
103+
})
104+
case types.KindApisixConsumer:
105+
d.updater.Update(status.Update{
106+
NamespacedName: nnk.NamespacedName(),
107+
Resource: &apiv2.ApisixConsumer{},
108+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
109+
cp := obj.(*apiv2.ApisixConsumer).DeepCopy()
123110
cutils.SetApisixCRDConditionWithGeneration(&cp.Status, cp.GetGeneration(), condition)
124111
return cp
125-
default:
126-
return obj
112+
}),
113+
})
114+
case types.KindHTTPRoute:
115+
parentRefs := d.client.ConfigManager.GetConfigRefsByResourceKey(nnk)
116+
gatewayRefs := map[types.NamespacedNameKind]struct{}{}
117+
for _, parentRef := range parentRefs {
118+
if parentRef.Kind == types.KindGateway {
119+
gatewayRefs[parentRef] = struct{}{}
127120
}
128-
}),
129-
})
130-
}
131-
132-
// updateGatewayRouteStatus updates the status of Gateway API route resources.
133-
func (d *api7eeProvider) updateGatewayRouteStatus(
134-
nnk types.NamespacedNameKind,
135-
resource client.Object,
136-
condition metav1.Condition,
137-
) {
138-
parentRefs := d.client.ConfigManager.GetConfigRefsByResourceKey(nnk)
139-
gatewayRefs := d.extractGatewayRefs(parentRefs)
140-
141-
d.updater.Update(status.Update{
142-
NamespacedName: nnk.NamespacedName(),
143-
Resource: resource,
144-
Mutator: status.MutatorFunc(d.createRouteMutator(gatewayRefs, condition)),
145-
})
146-
}
147-
148-
// extractGatewayRefs extracts gateway references from parent references.
149-
func (d *api7eeProvider) extractGatewayRefs(parentRefs []types.NamespacedNameKind) map[types.NamespacedNameKind]struct{} {
150-
gatewayRefs := map[types.NamespacedNameKind]struct{}{}
151-
for _, parentRef := range parentRefs {
152-
if parentRef.Kind == types.KindGateway {
153-
gatewayRefs[parentRef] = struct{}{}
154121
}
155-
}
156-
return gatewayRefs
157-
}
158-
159-
// createRouteMutator creates a mutator function for updating route parent status.
160-
func (d *api7eeProvider) createRouteMutator(
161-
gatewayRefs map[types.NamespacedNameKind]struct{},
162-
condition metav1.Condition,
163-
) func(obj client.Object) client.Object {
164-
return func(obj client.Object) client.Object {
165-
switch route := obj.(type) {
166-
case *gatewayv1.HTTPRoute:
167-
cp := route.DeepCopy()
168-
d.updateParentStatus(&cp.Status.RouteStatus, cp.GetNamespace(), gatewayRefs, condition)
169-
return cp
170-
case *gatewayv1alpha2.TCPRoute:
171-
cp := route.DeepCopy()
172-
d.updateParentStatus(&cp.Status.RouteStatus, cp.GetNamespace(), gatewayRefs, condition)
173-
return cp
174-
case *gatewayv1alpha2.UDPRoute:
175-
cp := route.DeepCopy()
176-
d.updateParentStatus(&cp.Status.RouteStatus, cp.GetNamespace(), gatewayRefs, condition)
177-
return cp
178-
case *gatewayv1.GRPCRoute:
179-
cp := route.DeepCopy()
180-
d.updateParentStatus(&cp.Status.RouteStatus, cp.GetNamespace(), gatewayRefs, condition)
181-
return cp
182-
default:
183-
return obj
122+
d.updater.Update(status.Update{
123+
NamespacedName: nnk.NamespacedName(),
124+
Resource: &gatewayv1.HTTPRoute{},
125+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
126+
cp := obj.(*gatewayv1.HTTPRoute).DeepCopy()
127+
gatewayNs := cp.GetNamespace()
128+
for i, ref := range cp.Status.Parents {
129+
ns := gatewayNs
130+
if ref.ParentRef.Namespace != nil {
131+
ns = string(*ref.ParentRef.Namespace)
132+
}
133+
if ref.ParentRef.Kind == nil || *ref.ParentRef.Kind == types.KindGateway {
134+
nnk := types.NamespacedNameKind{
135+
Name: string(ref.ParentRef.Name),
136+
Namespace: ns,
137+
Kind: types.KindGateway,
138+
}
139+
if _, ok := gatewayRefs[nnk]; ok {
140+
ref.Conditions = cutils.MergeCondition(ref.Conditions, condition)
141+
cp.Status.Parents[i] = ref
142+
}
143+
}
144+
}
145+
return cp
146+
}),
147+
})
148+
case types.KindUDPRoute:
149+
parentRefs := d.client.ConfigManager.GetConfigRefsByResourceKey(nnk)
150+
log.Debugw("updating UDPRoute status", zap.Any("parentRefs", parentRefs))
151+
gatewayRefs := map[types.NamespacedNameKind]struct{}{}
152+
for _, parentRef := range parentRefs {
153+
if parentRef.Kind == types.KindGateway {
154+
gatewayRefs[parentRef] = struct{}{}
155+
}
184156
}
185-
}
186-
}
187-
188-
// updateParentStatus updates the parent status for route resources.
189-
func (d *api7eeProvider) updateParentStatus(
190-
routeStatus *gatewayv1.RouteStatus,
191-
defaultNamespace string,
192-
gatewayRefs map[types.NamespacedNameKind]struct{},
193-
condition metav1.Condition,
194-
) {
195-
for i, ref := range routeStatus.Parents {
196-
if !d.shouldUpdateParentRef(ref.ParentRef, defaultNamespace, gatewayRefs) {
197-
continue
157+
d.updater.Update(status.Update{
158+
NamespacedName: nnk.NamespacedName(),
159+
Resource: &gatewayv1alpha2.UDPRoute{},
160+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
161+
cp := obj.(*gatewayv1alpha2.UDPRoute).DeepCopy()
162+
gatewayNs := cp.GetNamespace()
163+
for i, ref := range cp.Status.Parents {
164+
ns := gatewayNs
165+
if ref.ParentRef.Namespace != nil {
166+
ns = string(*ref.ParentRef.Namespace)
167+
}
168+
if ref.ParentRef.Kind == nil || *ref.ParentRef.Kind == types.KindGateway {
169+
nnk := types.NamespacedNameKind{
170+
Name: string(ref.ParentRef.Name),
171+
Namespace: ns,
172+
Kind: types.KindGateway,
173+
}
174+
if _, ok := gatewayRefs[nnk]; ok {
175+
ref.Conditions = cutils.MergeCondition(ref.Conditions, condition)
176+
cp.Status.Parents[i] = ref
177+
}
178+
}
179+
}
180+
return cp
181+
}),
182+
})
183+
case types.KindTCPRoute:
184+
parentRefs := d.client.ConfigManager.GetConfigRefsByResourceKey(nnk)
185+
log.Debugw("updating TCPRoute status", zap.Any("parentRefs", parentRefs))
186+
gatewayRefs := map[types.NamespacedNameKind]struct{}{}
187+
for _, parentRef := range parentRefs {
188+
if parentRef.Kind == types.KindGateway {
189+
gatewayRefs[parentRef] = struct{}{}
190+
}
198191
}
199-
ref.Conditions = cutils.MergeCondition(ref.Conditions, condition)
200-
routeStatus.Parents[i] = ref
201-
}
202-
}
203-
204-
// shouldUpdateParentRef checks if a parent reference should be updated.
205-
func (d *api7eeProvider) shouldUpdateParentRef(
206-
parentRef gatewayv1.ParentReference,
207-
defaultNamespace string,
208-
gatewayRefs map[types.NamespacedNameKind]struct{},
209-
) bool {
210-
if parentRef.Kind != nil && *parentRef.Kind != types.KindGateway {
211-
return false
212-
}
213-
214-
ns := defaultNamespace
215-
if parentRef.Namespace != nil {
216-
ns = string(*parentRef.Namespace)
217-
}
218-
219-
nnk := types.NamespacedNameKind{
220-
Name: string(parentRef.Name),
221-
Namespace: ns,
222-
Kind: types.KindGateway,
192+
d.updater.Update(status.Update{
193+
NamespacedName: nnk.NamespacedName(),
194+
Resource: &gatewayv1alpha2.TCPRoute{},
195+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
196+
cp := obj.(*gatewayv1alpha2.TCPRoute).DeepCopy()
197+
gatewayNs := cp.GetNamespace()
198+
for i, ref := range cp.Status.Parents {
199+
ns := gatewayNs
200+
if ref.ParentRef.Namespace != nil {
201+
ns = string(*ref.ParentRef.Namespace)
202+
}
203+
if ref.ParentRef.Kind == nil || *ref.ParentRef.Kind == types.KindGateway {
204+
nnk := types.NamespacedNameKind{
205+
Name: string(ref.ParentRef.Name),
206+
Namespace: ns,
207+
Kind: types.KindGateway,
208+
}
209+
if _, ok := gatewayRefs[nnk]; ok {
210+
ref.Conditions = cutils.MergeCondition(ref.Conditions, condition)
211+
cp.Status.Parents[i] = ref
212+
}
213+
}
214+
}
215+
return cp
216+
}),
217+
})
218+
case types.KindGRPCRoute:
219+
parentRefs := d.client.ConfigManager.GetConfigRefsByResourceKey(nnk)
220+
log.Debugw("updating GRPCRoute status", zap.Any("parentRefs", parentRefs))
221+
gatewayRefs := map[types.NamespacedNameKind]struct{}{}
222+
for _, parentRef := range parentRefs {
223+
if parentRef.Kind == types.KindGateway {
224+
gatewayRefs[parentRef] = struct{}{}
225+
}
226+
}
227+
d.updater.Update(status.Update{
228+
NamespacedName: nnk.NamespacedName(),
229+
Resource: &gatewayv1.GRPCRoute{},
230+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
231+
cp := obj.(*gatewayv1.GRPCRoute).DeepCopy()
232+
gatewayNs := cp.GetNamespace()
233+
for i, ref := range cp.Status.Parents {
234+
ns := gatewayNs
235+
if ref.ParentRef.Namespace != nil {
236+
ns = string(*ref.ParentRef.Namespace)
237+
}
238+
if ref.ParentRef.Kind == nil || *ref.ParentRef.Kind == types.KindGateway {
239+
nnk := types.NamespacedNameKind{
240+
Name: string(ref.ParentRef.Name),
241+
Namespace: ns,
242+
Kind: types.KindGateway,
243+
}
244+
if _, ok := gatewayRefs[nnk]; ok {
245+
ref.Conditions = cutils.MergeCondition(ref.Conditions, condition)
246+
cp.Status.Parents[i] = ref
247+
}
248+
}
249+
}
250+
return cp
251+
}),
252+
})
223253
}
224-
225-
_, exists := gatewayRefs[nnk]
226-
return exists
227254
}
228255

229256
func (d *api7eeProvider) resolveADCExecutionErrors(

0 commit comments

Comments
 (0)