-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
199 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// 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 controller | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/zap" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/client-go/util/workqueue" | ||
|
||
"github.com/apache/apisix-ingress-controller/pkg/log" | ||
"github.com/apache/apisix-ingress-controller/pkg/types" | ||
) | ||
|
||
type ingressController struct { | ||
controller *Controller | ||
workqueue workqueue.RateLimitingInterface | ||
workers int | ||
} | ||
|
||
func (c *Controller) newIngressController() *ingressController { | ||
ctl := &ingressController{ | ||
controller: c, | ||
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ingress"), | ||
workers: 1, | ||
} | ||
|
||
c.ingressInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ | ||
AddFunc: ctl.onAdd, | ||
UpdateFunc: ctl.onUpdate, | ||
DeleteFunc: ctl.OnDelete, | ||
}) | ||
return ctl | ||
} | ||
|
||
func (c *ingressController) run(ctx context.Context) { | ||
log.Info("ingress controller started") | ||
defer log.Infof("ingress controller exited") | ||
|
||
if !cache.WaitForCacheSync(ctx.Done(), c.controller.ingressInformer.HasSynced, c.controller.ingressClassInformer.HasSynced) { | ||
log.Errorf("cache sync failed") | ||
return | ||
} | ||
for i := 0; i < c.workers; i++ { | ||
go c.runWorker(ctx) | ||
} | ||
<-ctx.Done() | ||
c.workqueue.ShutDown() | ||
} | ||
|
||
func (c *ingressController) 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 *ingressController) sync(ctx context.Context, ev *types.Event) error { | ||
return nil | ||
} | ||
|
||
func (c *ingressController) handleSyncErr(obj interface{}, err error) { | ||
if err == nil { | ||
c.workqueue.Forget(obj) | ||
return | ||
} | ||
if c.workqueue.NumRequeues(obj) < _maxRetries { | ||
log.Infow("sync ingress failed, will retry", | ||
zap.Any("object", obj), | ||
) | ||
c.workqueue.AddRateLimited(obj) | ||
} else { | ||
c.workqueue.Forget(obj) | ||
log.Warnf("drop ingress %+v out of the queue", obj) | ||
} | ||
} | ||
|
||
func (c *ingressController) onAdd(obj interface{}) { | ||
key, err := cache.MetaNamespaceKeyFunc(obj) | ||
if err != nil { | ||
log.Errorf("found ingress resource with bad meta namesapce key: %s", err) | ||
return | ||
} | ||
if !c.controller.namespaceWatching(key) { | ||
return | ||
} | ||
log.Debugw("ingress add event arrived", | ||
zap.Any("object", obj)) | ||
|
||
c.workqueue.AddRateLimited(&types.Event{ | ||
Type: types.EventAdd, | ||
Object: key, | ||
}) | ||
} | ||
|
||
func (c *ingressController) onUpdate(oldObj, newObj interface{}) { | ||
prev := oldObj.(*networkingv1.Ingress) | ||
curr := newObj.(*networkingv1.Ingress) | ||
if prev.ResourceVersion >= curr.ResourceVersion { | ||
return | ||
} | ||
key, err := cache.MetaNamespaceKeyFunc(newObj) | ||
if err != nil { | ||
log.Errorf("found ingress resource with bad meta namespace key: %s", err) | ||
return | ||
} | ||
log.Debugw("ingress update event arrived", | ||
zap.Any("new object", curr), | ||
zap.Any("old object", prev), | ||
) | ||
|
||
c.workqueue.AddRateLimited(&types.Event{ | ||
Type: types.EventUpdate, | ||
Object: key, | ||
}) | ||
} | ||
|
||
func (c *ingressController) OnDelete(obj interface{}) { | ||
au, ok := obj.(*networkingv1.Ingress) | ||
if !ok { | ||
tombstone, ok := obj.(cache.DeletedFinalStateUnknown) | ||
if !ok { | ||
return | ||
} | ||
au = tombstone.Obj.(*networkingv1.Ingress) | ||
} | ||
|
||
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) | ||
if err != nil { | ||
log.Errorf("found ingress resource with bad meta namespace key: %s", err) | ||
return | ||
} | ||
if !c.controller.namespaceWatching(key) { | ||
return | ||
} | ||
log.Debugw("ingress delete event arrived", | ||
zap.Any("final state", au), | ||
) | ||
c.workqueue.AddRateLimited(&types.Event{ | ||
Type: types.EventDelete, | ||
Object: key, | ||
Tombstone: au, | ||
}) | ||
} |