Skip to content

Commit

Permalink
feat: support gateway API HTTPRoute (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
lingsamuel committed May 31, 2022
1 parent 5477fb0 commit 6c7452f
Show file tree
Hide file tree
Showing 7 changed files with 886 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -6,6 +6,7 @@ require (
github.com/gin-gonic/gin v1.7.7
github.com/hashicorp/go-memdb v1.3.2
github.com/hashicorp/go-multierror v1.1.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/client_model v0.2.0
github.com/slok/kubewebhook/v2 v2.2.0
Expand Down
29 changes: 22 additions & 7 deletions pkg/ingress/controller.go
Expand Up @@ -119,15 +119,18 @@ type Controller struct {
apisixPluginConfigLister kube.ApisixPluginConfigLister
gatewayInformer cache.SharedIndexInformer
gatewayLister gatewaylistersv1alpha2.GatewayLister
gatewayHttpRouteInformer cache.SharedIndexInformer
gatewayHttpRouteLister gatewaylistersv1alpha2.HTTPRouteLister

// resource controllers
namespaceController *namespaceController
podController *podController
endpointsController *endpointsController
endpointSliceController *endpointSliceController
ingressController *ingressController
secretController *secretController
gatewayController *gatewayController
namespaceController *namespaceController
podController *podController
endpointsController *endpointsController
endpointSliceController *endpointSliceController
ingressController *ingressController
secretController *secretController
gatewayController *gatewayController
gatewayHTTPRouteController *gatewayHTTPRouteController

apisixUpstreamController *apisixUpstreamController
apisixRouteController *apisixRouteController
Expand Down Expand Up @@ -264,6 +267,9 @@ func (c *Controller) initWhenStartLeading() {
c.gatewayLister = gatewayFactory.Gateway().V1alpha2().Gateways().Lister()
c.gatewayInformer = gatewayFactory.Gateway().V1alpha2().Gateways().Informer()

c.gatewayHttpRouteLister = gatewayFactory.Gateway().V1alpha2().HTTPRoutes().Lister()
c.gatewayHttpRouteInformer = gatewayFactory.Gateway().V1alpha2().HTTPRoutes().Informer()

switch c.cfg.Kubernetes.ApisixRouteVersion {
case config.ApisixRouteV2beta2:
apisixRouteInformer = apisixFactory.Apisix().V2beta2().ApisixRoutes().Informer()
Expand Down Expand Up @@ -328,6 +334,7 @@ func (c *Controller) initWhenStartLeading() {
c.apisixConsumerController = c.newApisixConsumerController()
c.apisixPluginConfigController = c.newApisixPluginConfigController()
c.gatewayController = c.newGatewayController()
c.gatewayHTTPRouteController = c.newGatewayHTTPRouteController()
}

// recorderEvent recorder events for resources
Expand Down Expand Up @@ -550,9 +557,17 @@ func (c *Controller) run(ctx context.Context) {
c.gatewayInformer.Run(ctx.Done())
})

c.goAttach(func() {
c.gatewayHttpRouteInformer.Run(ctx.Done())
})

c.goAttach(func() {
c.gatewayController.run(ctx)
})

c.goAttach(func() {
c.gatewayHTTPRouteController.run(ctx)
})
}

c.goAttach(func() {
Expand Down
216 changes: 216 additions & 0 deletions pkg/ingress/gateway_httproute.go
@@ -0,0 +1,216 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ingress

import (
"context"
"time"

"go.uber.org/zap"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

"github.com/apache/apisix-ingress-controller/pkg/kube/translation"
"github.com/apache/apisix-ingress-controller/pkg/log"
"github.com/apache/apisix-ingress-controller/pkg/types"
)

type gatewayHTTPRouteController struct {
controller *Controller
workqueue workqueue.RateLimitingInterface
workers int
}

func (c *Controller) newGatewayHTTPRouteController() *gatewayHTTPRouteController {
ctrl := &gatewayHTTPRouteController{
controller: c,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.NewItemFastSlowRateLimiter(1*time.Second, 60*time.Second, 5), "GatewayHTTPRoute"),
workers: 1,
}

ctrl.controller.gatewayHttpRouteInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ctrl.onAdd,
UpdateFunc: ctrl.onUpdate,
DeleteFunc: ctrl.OnDelete,
})
return ctrl
}

func (c *gatewayHTTPRouteController) run(ctx context.Context) {
log.Info("gateway HTTPRoute controller started")
defer log.Info("gateway HTTPRoute controller exited")
defer c.workqueue.ShutDown()

if !cache.WaitForCacheSync(ctx.Done(), c.controller.gatewayHttpRouteInformer.HasSynced) {
log.Error("sync Gateway HTTPRoute cache failed")
return
}

for i := 0; i < c.workers; i++ {
go c.runWorker(ctx)
}
<-ctx.Done()
}

func (c *gatewayHTTPRouteController) runWorker(ctx context.Context) {
for {
obj, quit := c.workqueue.Get()
if quit {
return
}
err := c.sync(ctx, obj.(*types.Event))
c.workqueue.Done(obj)
c.handleSyncErr(obj, err)
}
}

func (c *gatewayHTTPRouteController) sync(ctx context.Context, ev *types.Event) error {
key := ev.Object.(string)
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
log.Errorw("found Gateway HTTPRoute resource with invalid key",
zap.Error(err),
zap.String("key", key),
)
return err
}

httpRoute, err := c.controller.gatewayHttpRouteLister.HTTPRoutes(namespace).Get(name)
if err != nil {
if !k8serrors.IsNotFound(err) {
log.Errorw("failed to get Gateway HTTPRoute",
zap.Error(err),
zap.String("key", key),
)
return err
}
if ev.Type != types.EventDelete {
log.Warnw("Gateway HTTPRoute was deleted before process",
zap.String("key", key),
)
// Don't need to retry.
return nil
}
}

if ev.Type == types.EventDelete {
if httpRoute != nil {
// We still find the resource while we are processing the DELETE event,
// that means object with same namespace and name was created, discarding
// this stale DELETE event.
log.Warnf("discard the stale Gateway delete event since the %s exists", key)
return nil
}
httpRoute = ev.Tombstone.(*gatewayv1alpha2.HTTPRoute)
}

tctx, err := c.controller.translator.TranslateGatewayHTTPRouteV1Alpha2(httpRoute)

if err != nil {
log.Errorw("failed to translate gateway HTTPRoute",
zap.Error(err),
zap.Any("object", httpRoute),
)
return err
}

log.Debugw("translated HTTPRoute",
zap.Any("routes", tctx.Routes),
zap.Any("upstreams", tctx.Upstreams),
)
m := &manifest{
routes: tctx.Routes,
upstreams: tctx.Upstreams,
}

var (
added *manifest
updated *manifest
deleted *manifest
)

if ev.Type == types.EventDelete {
deleted = m
} else if ev.Type == types.EventAdd {
added = m
} else {
var oldCtx *translation.TranslateContext
oldObj := ev.OldObject.(*gatewayv1alpha2.HTTPRoute)
oldCtx, err = c.controller.translator.TranslateGatewayHTTPRouteV1Alpha2(oldObj)
if err != nil {
log.Errorw("failed to translate old HTTPRoute",
zap.String("version", oldObj.APIVersion),
zap.String("event_type", "update"),
zap.Any("HTTPRoute", oldObj),
zap.Error(err),
)
return err
}

om := &manifest{
routes: oldCtx.Routes,
upstreams: oldCtx.Upstreams,
}
added, updated, deleted = m.diff(om)
}

return c.controller.syncManifests(ctx, added, updated, deleted)
}

func (c *gatewayHTTPRouteController) handleSyncErr(obj interface{}, err error) {
if err == nil {
c.workqueue.Forget(obj)
c.controller.MetricsCollector.IncrSyncOperation("gateway_httproute", "success")
return
}
event := obj.(*types.Event)
if k8serrors.IsNotFound(err) && event.Type != types.EventDelete {
log.Infow("sync gateway HTTPRoute but not found, ignore",
zap.String("event_type", event.Type.String()),
zap.String("HTTPRoute ", event.Object.(string)),
)
c.workqueue.Forget(event)
return
}
log.Warnw("sync gateway HTTPRoute failed, will retry",
zap.Any("object", obj),
zap.Error(err),
)
c.workqueue.AddRateLimited(obj)
c.controller.MetricsCollector.IncrSyncOperation("gateway_httproute", "failure")
}

func (c *gatewayHTTPRouteController) onAdd(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
log.Errorf("found gateway HTTPRoute resource with bad meta namespace key: %s", err)
return
}
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("gateway HTTPRoute add event arrived",
zap.Any("object", obj),
)

c.workqueue.Add(&types.Event{
Type: types.EventAdd,
Object: key,
})
}
func (c *gatewayHTTPRouteController) onUpdate(oldObj, newObj interface{}) {}
func (c *gatewayHTTPRouteController) OnDelete(obj interface{}) {}

0 comments on commit 6c7452f

Please sign in to comment.