Skip to content

Commit

Permalink
fix: endless retry if namespace doesn't exist (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
lingsamuel authored Feb 23, 2022
1 parent 27f0cfa commit d30bef5
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 88 deletions.
6 changes: 3 additions & 3 deletions pkg/ingress/apisix_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (c *apisixConsumerController) onAdd(obj interface{}) {
log.Errorf("found ApisixConsumer resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixConsumer add event arrived",
Expand All @@ -184,7 +184,7 @@ func (c *apisixConsumerController) onUpdate(oldObj, newObj interface{}) {
log.Errorf("found ApisixConsumer resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixConsumer update event arrived",
Expand Down Expand Up @@ -215,7 +215,7 @@ func (c *apisixConsumerController) onDelete(obj interface{}) {
log.Errorf("found ApisixConsumer resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixConsumer delete event arrived",
Expand Down
6 changes: 3 additions & 3 deletions pkg/ingress/apisix_pluginconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (c *apisixPluginConfigController) onAdd(obj interface{}) {
log.Errorf("found ApisixPluginConfig resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixPluginConfig add event arrived",
Expand Down Expand Up @@ -277,7 +277,7 @@ func (c *apisixPluginConfigController) onUpdate(oldObj, newObj interface{}) {
log.Errorf("found ApisixPluginConfig resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixPluginConfig update event arrived",
Expand Down Expand Up @@ -310,7 +310,7 @@ func (c *apisixPluginConfigController) onDelete(obj interface{}) {
log.Errorf("found ApisixPluginConfig resource with bad meta namesapce key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixPluginConfig delete event arrived",
Expand Down
6 changes: 3 additions & 3 deletions pkg/ingress/apisix_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (c *apisixRouteController) onAdd(obj interface{}) {
log.Errorf("found ApisixRoute resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixRoute add event arrived",
Expand Down Expand Up @@ -366,7 +366,7 @@ func (c *apisixRouteController) onUpdate(oldObj, newObj interface{}) {
log.Errorf("found ApisixRoute resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixRoute update event arrived",
Expand Down Expand Up @@ -399,7 +399,7 @@ func (c *apisixRouteController) onDelete(obj interface{}) {
log.Errorf("found ApisixRoute resource with bad meta namesapce key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixRoute delete event arrived",
Expand Down
6 changes: 3 additions & 3 deletions pkg/ingress/apisix_tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (c *apisixTlsController) onAdd(obj interface{}) {
log.Errorf("found ApisixTls object with bad namespace/name: %s, ignore it", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixTls add event arrived",
Expand All @@ -214,7 +214,7 @@ func (c *apisixTlsController) onUpdate(prev, curr interface{}) {
log.Errorf("found ApisixTls object with bad namespace/name: %s, ignore it", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixTls update event arrived",
Expand Down Expand Up @@ -246,7 +246,7 @@ func (c *apisixTlsController) onDelete(obj interface{}) {
log.Errorf("found ApisixTls resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixTls delete event arrived",
Expand Down
6 changes: 3 additions & 3 deletions pkg/ingress/apisix_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (c *apisixUpstreamController) onAdd(obj interface{}) {
log.Errorf("found ApisixUpstream resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixUpstream add event arrived",
Expand All @@ -246,7 +246,7 @@ func (c *apisixUpstreamController) onUpdate(oldObj, newObj interface{}) {
log.Errorf("found ApisixUpstream resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixUpstream update event arrived",
Expand Down Expand Up @@ -277,7 +277,7 @@ func (c *apisixUpstreamController) onDelete(obj interface{}) {
log.Errorf("found ApisixUpstream resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("ApisixUpstream delete event arrived",
Expand Down
8 changes: 4 additions & 4 deletions pkg/ingress/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (c *Controller) CompareResources(ctx context.Context) error {
consumerMapA6 = make(map[string]string)
pluginConfigMapA6 = make(map[string]string)
)
// watchingNamespace == nil means to monitor all namespaces
if !validation.HasValueInSyncMap(c.watchingNamespace) {
// watchingNamespaces == nil means to monitor all namespaces
if !validation.HasValueInSyncMap(c.watchingNamespaces) {
opts := v1.ListOptions{}
// list all namespaces
nsList, err := c.kubeClient.Client.CoreV1().Namespaces().List(ctx, opts)
Expand All @@ -59,11 +59,11 @@ func (c *Controller) CompareResources(ctx context.Context) error {
for _, v := range nsList.Items {
wns.Store(v.Name, struct{}{})
}
c.watchingNamespace = wns
c.watchingNamespaces = wns
}
}

c.watchingNamespace.Range(func(key, value interface{}) bool {
c.watchingNamespaces.Range(func(key, value interface{}) bool {
wg.Add(1)
go func(ns string) {
defer wg.Done()
Expand Down
58 changes: 29 additions & 29 deletions pkg/ingress/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ const (

// Controller is the ingress apisix controller object.
type Controller struct {
name string
namespace string
cfg *config.Config
wg sync.WaitGroup
watchingNamespace *sync.Map
watchingLabels types.Labels
apisix apisix.APISIX
podCache types.PodCache
translator translation.Translator
apiServer *api.Server
MetricsCollector metrics.Collector
kubeClient *kube.KubeClient
name string
namespace string
cfg *config.Config
wg sync.WaitGroup
watchingNamespaces *sync.Map
watchingLabels types.Labels
apisix apisix.APISIX
podCache types.PodCache
translator translation.Translator
apiServer *api.Server
MetricsCollector metrics.Collector
kubeClient *kube.KubeClient
// recorder event
recorder record.EventRecorder
// this map enrolls which ApisixTls objects refer to a Kubernetes
Expand Down Expand Up @@ -181,17 +181,17 @@ func NewController(cfg *config.Config) (*Controller, error) {
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeClient.Client.CoreV1().Events("")})

c := &Controller{
name: podName,
namespace: podNamespace,
cfg: cfg,
apiServer: apiSrv,
apisix: client,
MetricsCollector: metrics.NewPrometheusCollector(),
kubeClient: kubeClient,
watchingNamespace: watchingNamespace,
watchingLabels: watchingLabels,
secretSSLMap: new(sync.Map),
recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: _component}),
name: podName,
namespace: podNamespace,
cfg: cfg,
apiServer: apiSrv,
apisix: client,
MetricsCollector: metrics.NewPrometheusCollector(),
kubeClient: kubeClient,
watchingNamespaces: watchingNamespace,
watchingLabels: watchingLabels,
secretSSLMap: new(sync.Map),
recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: _component}),

podCache: types.NewPodCache(),
}
Expand Down Expand Up @@ -442,8 +442,8 @@ func (c *Controller) run(ctx context.Context) {

c.initWhenStartLeading()

// list namesapce and init watchingNamespace
if err := c.initWatchingNamespaceByLabels(ctx); err != nil {
// list namespaces and init watchingNamespaces
if err := c.initWatchingNamespacesByLabels(ctx); err != nil {
ctx.Done()
return
}
Expand Down Expand Up @@ -552,10 +552,10 @@ func (c *Controller) run(ctx context.Context) {
c.wg.Wait()
}

// namespaceWatching accepts a resource key, getting the namespace part
// isWatchingNamespace accepts a resource key, getting the namespace part
// and checking whether the namespace is being watched.
func (c *Controller) namespaceWatching(key string) (ok bool) {
if !validation.HasValueInSyncMap(c.watchingNamespace) {
func (c *Controller) isWatchingNamespace(key string) (ok bool) {
if !validation.HasValueInSyncMap(c.watchingNamespaces) {
ok = true
return
}
Expand All @@ -566,7 +566,7 @@ func (c *Controller) namespaceWatching(key string) (ok bool) {
log.Warnf("resource %s was ignored since: %s", key, err)
return
}
_, ok = c.watchingNamespace.Load(ns)
_, ok = c.watchingNamespaces.Load(ns)
return
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/ingress/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (c *endpointsController) onAdd(obj interface{}) {
log.Errorf("found endpoints object with bad namespace/name: %s, ignore it", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("endpoints add event arrived",
Expand All @@ -133,7 +133,7 @@ func (c *endpointsController) onUpdate(prev, curr interface{}) {
log.Errorf("found endpoints object with bad namespace/name: %s, ignore it", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("endpoints update event arrived",
Expand All @@ -160,10 +160,10 @@ func (c *endpointsController) onDelete(obj interface{}) {
ep = tombstone.Obj.(*corev1.Endpoints)
}

// FIXME Refactor Controller.namespaceWatching to just use
// FIXME Refactor Controller.isWatchingNamespace to just use
// namespace after all controllers use the same way to fetch
// the object.
if !c.controller.namespaceWatching(ep.Namespace + "/" + ep.Name) {
if !c.controller.isWatchingNamespace(ep.Namespace + "/" + ep.Name) {
return
}
log.Debugw("endpoints delete event arrived",
Expand Down
6 changes: 3 additions & 3 deletions pkg/ingress/endpointslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c *endpointSliceController) onAdd(obj interface{}) {
if err != nil {
log.Errorf("found endpointSlice object with bad namespace")
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
ep := obj.(*discoveryv1.EndpointSlice)
Expand Down Expand Up @@ -165,7 +165,7 @@ func (c *endpointSliceController) onUpdate(prev, curr interface{}) {
log.Errorf("found endpointSlice object with bad namespace/name: %s, ignore it", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
if currEp.Labels[discoveryv1.LabelManagedBy] != _endpointSlicesManagedBy {
Expand Down Expand Up @@ -209,7 +209,7 @@ func (c *endpointSliceController) onDelete(obj interface{}) {
log.Errorf("found endpointSlice object with bad namespace/name: %s, ignore it", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
if ep.Labels[discoveryv1.LabelManagedBy] != _endpointSlicesManagedBy {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ingress/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *gatewayController) onAdd(obj interface{}) {
log.Errorf("found gateway resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
log.Debugw("gateway add event arrived",
Expand Down
4 changes: 2 additions & 2 deletions pkg/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (c *ingressController) onAdd(obj interface{}) {
log.Errorf("found ingress resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}

Expand Down Expand Up @@ -340,7 +340,7 @@ func (c *ingressController) OnDelete(obj interface{}) {
log.Errorf("found ingress resource with bad meta namespace key: %s", err)
return
}
if !c.controller.namespaceWatching(key) {
if !c.controller.isWatchingNamespace(key) {
return
}
valid := c.isIngressEffective(ing)
Expand Down
Loading

0 comments on commit d30bef5

Please sign in to comment.