Skip to content

Commit

Permalink
feat: support update and delete of HTTPRoute (#1315)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinsRan authored Sep 7, 2022
1 parent 94dbbed commit 5c0ea2b
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 7 deletions.
55 changes: 52 additions & 3 deletions pkg/providers/gateway/gateway_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,63 @@ func (c *gatewayHTTPRouteController) onAdd(obj interface{}) {
return
}
log.Debugw("gateway HTTPRoute add event arrived",
zap.String("key", key),
zap.Any("object", obj),
)

log.Debugw("add HTTPRoute", zap.String("key", key))
c.workqueue.Add(&types.Event{
Type: types.EventAdd,
Object: key,
})
}
func (c *gatewayHTTPRouteController) onUpdate(oldObj, newObj interface{}) {}
func (c *gatewayHTTPRouteController) OnDelete(obj interface{}) {}

func (c *gatewayHTTPRouteController) onUpdate(oldObj, newObj interface{}) {
oldHTTPRoute := oldObj.(*gatewayv1alpha2.HTTPRoute)
newHTTPRoute := newObj.(*gatewayv1alpha2.HTTPRoute)
if oldHTTPRoute.ResourceVersion >= newHTTPRoute.ResourceVersion {
return
}
key, err := cache.MetaNamespaceKeyFunc(oldObj)
if err != nil {
log.Errorw("found gateway HTTPRoute resource with bad meta namespace key",
zap.Error(err),
)
return
}
if !c.controller.NamespaceProvider.IsWatchingNamespace(key) {
return
}
log.Debugw("Gateway HTTPRoute update event arrived",
zap.String("key", key),
zap.Any("old object", oldObj),
zap.Any("new object", newObj),
)

c.workqueue.Add(&types.Event{
Type: types.EventUpdate,
Object: key,
})
}

func (c *gatewayHTTPRouteController) OnDelete(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
log.Errorw("found Gateway HTTPRoute resource with bad meta namespace key",
zap.Error(err),
)
return
}
if !c.controller.NamespaceProvider.IsWatchingNamespace(key) {
return
}
log.Debugw("Gateway HTTPRoute delete event arrived",
zap.String("key", key),
zap.Any("object", obj),
)

c.workqueue.Add(&types.Event{
Type: types.EventDelete,
Object: key,
Tombstone: obj,
})
}
8 changes: 5 additions & 3 deletions test/e2e/scaffold/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ func (s *Scaffold) CreateApisixRoute(name string, rules []ApisixRouteRule) {

// CreateResourceFromString creates resource from a loaded yaml string.
func (s *Scaffold) CreateResourceFromString(yaml string) error {
err := k8s.KubectlApplyFromStringE(s.t, s.kubectlOptions, yaml)
time.Sleep(5 * time.Second)
return err
return k8s.KubectlApplyFromStringE(s.t, s.kubectlOptions, yaml)
}

func (s *Scaffold) DeleteResourceFromString(yaml string) error {
return k8s.KubectlDeleteFromStringE(s.t, s.kubectlOptions, yaml)
}

func (s *Scaffold) GetOutputFromString(shell ...string) (string, error) {
Expand Down
92 changes: 91 additions & 1 deletion test/e2e/suite-gateway/gateway_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ spec:

ginkgo.It("Basic HTTPRoute with 1 Hosts 1 Rule 2 Match 1 BackendRef", func() {
backendSvc, backendPorts := s.DefaultHTTPBackend()
time.Sleep(time.Second * 15)
route := fmt.Sprintf(`
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: HTTPRoute
Expand Down Expand Up @@ -146,4 +145,95 @@ spec:
Expect().
Status(http.StatusNotFound)
})

ginkgo.It("Update HTTPRoute", func() {
backendSvc, backendPorts := s.DefaultHTTPBackend()
route := fmt.Sprintf(`
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: HTTPRoute
metadata:
name: basic-http-route
spec:
hostnames: ["httpbin.org"]
rules:
- matches:
- path:
type: PathPrefix
value: /ip
backendRefs:
- name: %s
port: %d
`, backendSvc, backendPorts[0])
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(route), "creating HTTPRoute")
time.Sleep(time.Second * 6)
assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(1), "Checking number of routes")
assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixUpstreamsCreated(1), "Checking number of upstreams")

route = fmt.Sprintf(`
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: HTTPRoute
metadata:
name: basic-http-route
spec:
hostnames: ["httpbin.org"]
rules:
- matches:
- path:
type: PathPrefix
value: /get
backendRefs:
- name: %s
port: %d
`, backendSvc, backendPorts[0])
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(route), "update HTTPRoute")
assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(1), "Checking number of routes")

time.Sleep(6 * time.Second)

_ = s.NewAPISIXClient().GET("/get").
WithHeader("Host", "httpbin.org").
Expect().
Status(http.StatusOK)
_ = s.NewAPISIXClient().GET("/ip").
WithHeader("Host", "httpbin.org").
Expect().
Status(http.StatusNotFound)
})

ginkgo.It("Delete HTTPRoute", func() {
backendSvc, backendPorts := s.DefaultHTTPBackend()
route := fmt.Sprintf(`
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: HTTPRoute
metadata:
name: basic-http-route
spec:
hostnames: ["httpbin.org"]
rules:
- matches:
- path:
type: PathPrefix
value: /ip
backendRefs:
- name: %s
port: %d
`, backendSvc, backendPorts[0])
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(route), "creating HTTPRoute")
time.Sleep(time.Second * 6)
assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(1), "Checking number of routes")
assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixUpstreamsCreated(1), "Checking number of upstreams")

_ = s.NewAPISIXClient().GET("/ip").
WithHeader("Host", "httpbin.org").
Expect().
Status(http.StatusOK)

assert.Nil(ginkgo.GinkgoT(), s.DeleteResourceFromString(route), "delete HTTPRoute")
assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(0), "Checking number of routes")

_ = s.NewAPISIXClient().GET("/ip").
WithHeader("Host", "httpbin.org").
Expect().
Status(http.StatusNotFound)
})
})
1 change: 1 addition & 0 deletions test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ spec:
protocol: TCP
`))
s.EnsureNumEndpointsReady(ginkgo.GinkgoT(), "mosquito", 1)
time.Sleep(30 * time.Second)
// setup Apisix Route for mqtt proxy
apisixRoute := `
apiVersion: apisix.apache.org/v2
Expand Down

0 comments on commit 5c0ea2b

Please sign in to comment.