Skip to content

Commit e0a1e77

Browse files
authored
fix: route/service still exist when deleting the Gateway (#74)
1 parent 54f28ad commit e0a1e77

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

.github/workflows/conformance-test.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
run: |
3333
go install sigs.k8s.io/[email protected]
3434
35-
e2e-test:
35+
conformance-test:
3636
needs:
3737
- prepare
3838
runs-on: buildjet-2vcpu-ubuntu-2204
@@ -85,11 +85,29 @@ jobs:
8585
shell: bash
8686
env:
8787
API7_EE_LICENSE: ${{ secrets.API7_EE_LICENSE }}
88+
continue-on-error: true
8889
run: |
8990
make conformance-test
90-
91+
9192
- name: Upload Gateway API Conformance Report
93+
if: ${{ github.event_name == 'push' }}
9294
uses: actions/upload-artifact@v4
9395
with:
9496
name: api7-ingress-controller-conformance-report.yaml
9597
path: api7-ingress-controller-conformance-report.yaml
98+
99+
- name: Format Conformance Test Report
100+
if: ${{ github.event_name == 'pull_request' }}
101+
run: |
102+
echo '# conformance test report' > report.md
103+
echo '```yaml' >> report.md
104+
cat api7-ingress-controller-conformance-report.yaml >> report.md
105+
echo '```' >> report.md
106+
107+
- name: Report Conformance Test Result to PR Comment
108+
if: ${{ github.event_name == 'pull_request' }}
109+
uses: mshick/add-pr-comment@v2
110+
with:
111+
message-id: '${{ matrix.target }}'
112+
message-path: |
113+
report.md

internal/controller/httproute_controller.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"k8s.io/apimachinery/pkg/runtime"
1313
"k8s.io/apimachinery/pkg/types"
1414
ctrl "sigs.k8s.io/controller-runtime"
15+
"sigs.k8s.io/controller-runtime/pkg/builder"
1516
"sigs.k8s.io/controller-runtime/pkg/client"
17+
"sigs.k8s.io/controller-runtime/pkg/event"
1618
"sigs.k8s.io/controller-runtime/pkg/handler"
1719
"sigs.k8s.io/controller-runtime/pkg/predicate"
1820
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -47,6 +49,25 @@ func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
4749
Watches(&v1alpha1.PluginConfig{},
4850
handler.EnqueueRequestsFromMapFunc(r.listHTTPRoutesByExtensionRef),
4951
).
52+
Watches(&gatewayv1.Gateway{},
53+
handler.EnqueueRequestsFromMapFunc(r.listHTTPRoutesForGateway),
54+
builder.WithPredicates(
55+
predicate.Funcs{
56+
GenericFunc: func(e event.GenericEvent) bool {
57+
return false
58+
},
59+
DeleteFunc: func(e event.DeleteEvent) bool {
60+
return false
61+
},
62+
CreateFunc: func(e event.CreateEvent) bool {
63+
return true
64+
},
65+
UpdateFunc: func(e event.UpdateEvent) bool {
66+
return true
67+
},
68+
},
69+
),
70+
).
5071
Complete(r)
5172
}
5273

@@ -190,6 +211,30 @@ func (r *HTTPRouteReconciler) listHTTPRoutesByExtensionRef(ctx context.Context,
190211
return requests
191212
}
192213

214+
func (r *HTTPRouteReconciler) listHTTPRoutesForGateway(ctx context.Context, obj client.Object) []reconcile.Request {
215+
gateway, ok := obj.(*gatewayv1.Gateway)
216+
if !ok {
217+
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to Gateway")
218+
}
219+
hrList := &gatewayv1.HTTPRouteList{}
220+
if err := r.List(ctx, hrList, client.MatchingFields{
221+
indexer.ParentRefs: indexer.GenIndexKey(gateway.Namespace, gateway.Name),
222+
}); err != nil {
223+
r.Log.Error(err, "failed to list httproutes by gateway", "gateway", gateway.Name)
224+
return nil
225+
}
226+
requests := make([]reconcile.Request, 0, len(hrList.Items))
227+
for _, hr := range hrList.Items {
228+
requests = append(requests, reconcile.Request{
229+
NamespacedName: client.ObjectKey{
230+
Namespace: hr.Namespace,
231+
Name: hr.Name,
232+
},
233+
})
234+
}
235+
return requests
236+
}
237+
193238
func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.TranslateContext) error {
194239
var terr error
195240
for _, backend := range tctx.BackendRefs {

internal/provider/adc/adc.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,18 @@ func (d *adcClient) Delete(ctx context.Context, obj client.Object) error {
8585
log.Debugw("deleting object", zap.Any("object", obj))
8686

8787
var resourceTypes []string
88+
var labels map[string]string
8889
switch obj.(type) {
8990
case *gatewayv1.HTTPRoute:
9091
resourceTypes = append(resourceTypes, "service")
92+
labels = label.GenLabel(obj)
9193
case *gatewayv1.Gateway:
92-
resourceTypes = append(resourceTypes, "global_rule", "ssl", "plugin_metadata")
94+
// delete all resources
9395
}
9496

9597
return d.sync(Task{
9698
Name: obj.GetName(),
97-
Labels: label.GenLabel(obj),
99+
Labels: labels,
98100
ResourceTypes: resourceTypes,
99101
})
100102
}

test/e2e/gatewayapi/httproute.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ spec:
173173
})
174174

175175
Context("HTTPRoute Base", func() {
176-
177176
var exactRouteByGet = `
178177
apiVersion: gateway.networking.k8s.io/v1
179178
kind: HTTPRoute
@@ -223,6 +222,29 @@ spec:
223222
Expect().
224223
Status(404)
225224
})
225+
226+
It("Delete Gateway after apply HTTPRoute", func() {
227+
By("create HTTPRoute")
228+
ResourceApplied("HTTPRoute", "httpbin", exactRouteByGet, 1)
229+
230+
By("access daataplane to check the HTTPRoute")
231+
s.NewAPISIXClient().
232+
GET("/get").
233+
WithHost("httpbin.example").
234+
Expect().
235+
Status(200)
236+
237+
By("delete Gateway")
238+
err := s.DeleteResource("Gateway", "api7ee")
239+
Expect(err).NotTo(HaveOccurred(), "deleting Gateway")
240+
time.Sleep(5 * time.Second)
241+
242+
s.NewAPISIXClient().
243+
GET("/get").
244+
WithHost("httpbin.example").
245+
Expect().
246+
Status(404)
247+
})
226248
})
227249
Context("HTTPRoute Rule Match", func() {
228250
var exactRouteByGet = `

0 commit comments

Comments
 (0)