Skip to content

Commit

Permalink
feat: support gateway TLSRoute (#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
lingsamuel committed Jun 28, 2022
1 parent 49991e2 commit b33d70c
Show file tree
Hide file tree
Showing 17 changed files with 1,097 additions and 57 deletions.
80 changes: 69 additions & 11 deletions pkg/ingress/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,26 @@ func (c *gatewayController) sync(ctx context.Context, ev *types.Event) error {
key := ev.Object.(string)
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
log.Errorf("found Gateway resource with invalid meta namespace key %s: %s", key, err)
log.Errorw("found Gateway resource with invalid meta namespace key",
zap.Error(err),
zap.String("key", key),
)
return err
}

gateway, err := c.controller.gatewayLister.Gateways(namespace).Get(name)
if err != nil {
if !k8serrors.IsNotFound(err) {
log.Errorf("failed to get Gateway %s: %s", key, err)
log.Errorw("failed to get Gateway",
zap.Error(err),
zap.String("key", key),
)
return err
}
if ev.Type != types.EventDelete {
log.Warnf("Gateway %s was deleted before it can be delivered", key)
log.Warnw("Gateway was deleted before it can be delivered",
zap.String("key", key),
)
// Don't need to retry.
return nil
}
Expand All @@ -107,14 +115,30 @@ func (c *gatewayController) sync(ctx context.Context, ev *types.Event) error {
// 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)
log.Warnw("discard the stale Gateway delete event since it exists",
zap.String("key", key),
)
return nil
}
gateway = ev.Tombstone.(*gatewayv1alpha2.Gateway)
//} else {
//if c.controller.HasGatewayClass(string(gateway.Spec.GatewayClassName)) {
// // TODO: Translate listeners
//}

err = c.controller.RemoveListeners(gateway.Namespace, gateway.Name)
if err != nil {
return err
}
} else {
if c.controller.HasGatewayClass(string(gateway.Spec.GatewayClassName)) {
// TODO: handle listeners
listeners, err := c.controller.translator.TranslateGatewayV1Alpha2(gateway)
if err != nil {
return err
}

err = c.controller.AddListeners(gateway.Namespace, gateway.Name, listeners)
if err != nil {
return err
}
}
}

// TODO The current implementation does not fully support the definition of Gateway.
Expand Down Expand Up @@ -152,7 +176,10 @@ func (c *gatewayController) handleSyncErr(obj interface{}, err error) {
func (c *gatewayController) onAdd(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
log.Errorf("found gateway resource with bad meta namespace key: %s", err)
log.Errorw("found gateway resource with bad meta namespace key",
zap.Error(err),
zap.Any("obj", obj),
)
return
}
if !c.controller.NamespaceProvider.IsWatchingNamespace(key) {
Expand All @@ -167,8 +194,39 @@ func (c *gatewayController) onAdd(obj interface{}) {
Object: key,
})
}
func (c *gatewayController) onUpdate(oldObj, newObj interface{}) {}
func (c *gatewayController) OnDelete(obj interface{}) {}
func (c *gatewayController) onUpdate(oldObj, newObj interface{}) {

}

func (c *gatewayController) OnDelete(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
log.Errorw("failed to handle deletion Gateway meta key",
zap.Error(err),
zap.Any("obj", obj),
)
return
}

gateway, ok := obj.(*gatewayv1alpha2.Gateway)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
log.Errorw("Gateway in bad tombstone state",
zap.String("key", key),
zap.Any("obj", obj),
)
return
}
gateway = tombstone.Obj.(*gatewayv1alpha2.Gateway)
}

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

// recordStatus record resources status
func (c *gatewayController) recordStatus(v *gatewayv1alpha2.Gateway, reason string, status metav1.ConditionStatus, generation int64) {
Expand Down
50 changes: 36 additions & 14 deletions pkg/ingress/gateway/gateway_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

const (
GatewayClassName = "apisix-ingress-controller"
GatewayClassName = "apisix.apache.org/gateway-controller"
)

type gatewayClassController struct {
Expand Down Expand Up @@ -127,12 +127,12 @@ func (c *gatewayClassController) markAsUpdated(gatewayClass *v1alpha2.GatewayCla
}

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

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

Expand All @@ -155,8 +155,8 @@ func (c *gatewayClassController) runWorker(ctx context.Context) {
}

func (c *gatewayClassController) sync(ctx context.Context, ev *types.Event) error {
key := ev.Object.(string)
if ev.Type == types.EventAdd {
key := ev.Object.(string)
gatewayClass, err := c.controller.gatewayClassLister.Get(key)
if err != nil {
return err
Expand All @@ -166,8 +166,7 @@ func (c *gatewayClassController) sync(ctx context.Context, ev *types.Event) erro
return c.markAsUpdated(gatewayClass)
}
} else if ev.Type == types.EventDelete {
key := ev.Object.(string)
c.controller.RemoveGatewayClass(key)
c.controller.RemoveGatewayClass(ev.Tombstone.(*v1alpha2.GatewayClass).Name)
}

return nil
Expand All @@ -181,14 +180,14 @@ func (c *gatewayClassController) handleSyncErr(obj interface{}, err error) {
}
event := obj.(*types.Event)
if k8serrors.IsNotFound(err) && event.Type != types.EventDelete {
log.Infow("sync gateway HTTPRoute but not found, ignore",
log.Infow("sync gateway class but not found, ignore",
zap.String("event_type", event.Type.String()),
zap.String("HTTPRoute ", event.Object.(string)),
zap.String("GatewayClass", event.Object.(string)),
)
c.workqueue.Forget(event)
return
}
log.Warnw("sync gateway HTTPRoute failed, will retry",
log.Warnw("sync gateway class failed, will retry",
zap.Any("object", obj),
zap.Error(err),
)
Expand All @@ -199,13 +198,15 @@ func (c *gatewayClassController) handleSyncErr(obj interface{}, err error) {
func (c *gatewayClassController) onAdd(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
log.Errorf("found gateway HTTPRoute resource with bad meta namespace key: %s", err)
log.Errorw("found gateway class resource with bad meta namespace key",
zap.Error(err),
)
return
}
if !c.controller.NamespaceProvider.IsWatchingNamespace(key) {
return
}
log.Debugw("gateway HTTPRoute add event arrived",
log.Debugw("gateway class add event arrived",
zap.Any("object", obj),
)

Expand All @@ -220,10 +221,31 @@ func (c *gatewayClassController) onUpdate(oldObj, newObj interface{}) {
}

func (c *gatewayClassController) onDelete(obj interface{}) {
gatewayClass := obj.(*v1alpha2.GatewayClass)
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
log.Errorw("failed to handle deletion GatewayClass meta key",
zap.Error(err),
zap.Any("obj", obj),
)
return
}

gatewayClass, ok := obj.(*v1alpha2.GatewayClass)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
log.Errorw("GatewayClass in bad tombstone state",
zap.String("key", key),
zap.Any("obj", obj),
)
return
}
gatewayClass = tombstone.Obj.(*v1alpha2.GatewayClass)
}

c.workqueue.Add(&types.Event{
Type: types.EventDelete,
Object: gatewayClass.Name,
Object: key,
Tombstone: gatewayClass,
})
}
8 changes: 6 additions & 2 deletions pkg/ingress/gateway/gateway_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ func (c *gatewayHTTPRouteController) sync(ctx context.Context, ev *types.Event)
// 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)
log.Warnw("discard the stale Gateway delete event since it exists",
zap.String("key", key),
)
return nil
}
httpRoute = ev.Tombstone.(*gatewayv1alpha2.HTTPRoute)
Expand Down Expand Up @@ -200,7 +202,9 @@ func (c *gatewayHTTPRouteController) handleSyncErr(obj interface{}, err error) {
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)
log.Errorw("found gateway HTTPRoute resource with bad meta namespace key",
zap.Error(err),
)
return
}
if !c.controller.NamespaceProvider.IsWatchingNamespace(key) {
Expand Down
Loading

0 comments on commit b33d70c

Please sign in to comment.