This is an automated email from the ASF dual-hosted git repository.
kvn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-ingress-controller.git
The following commit(s) were added to refs/heads/master by this push:
new f470867 fix: ingress do not watching any namespace when
namespaceSelector is empty (#742)
f470867 is described below
commit f4708675c6304ad019881ad7e0ac7a0affd3e6bd
Author: kv <[email protected]>
AuthorDate: Fri Nov 26 11:14:06 2021 +0800
fix: ingress do not watching any namespace when namespaceSelector is empty
(#742)
---
pkg/api/validation/utils.go | 11 +++++++++++
pkg/api/validation/utils_test.go | 9 +++++++++
pkg/ingress/compare.go | 5 ++++-
pkg/ingress/controller.go | 3 ++-
pkg/types/labels.go | 4 ++--
pkg/types/labels_test.go | 2 +-
6 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/pkg/api/validation/utils.go b/pkg/api/validation/utils.go
index 5d2145c..84180be 100644
--- a/pkg/api/validation/utils.go
+++ b/pkg/api/validation/utils.go
@@ -99,3 +99,14 @@ func validateSchema(schemaLoader *gojsonschema.JSONLoader,
obj interface{}) (boo
return false, resultErr
}
+
+func HasValueInSyncMap(m *sync.Map) bool {
+ hasValue := false
+ if m != nil {
+ m.Range(func(k, v interface{}) bool {
+ hasValue = true
+ return false
+ })
+ }
+ return hasValue
+}
diff --git a/pkg/api/validation/utils_test.go b/pkg/api/validation/utils_test.go
index 17c3961..59f538a 100644
--- a/pkg/api/validation/utils_test.go
+++ b/pkg/api/validation/utils_test.go
@@ -16,8 +16,10 @@
package validation
import (
+ "sync"
"testing"
+ "github.com/stretchr/testify/assert"
"github.com/xeipuuv/gojsonschema"
v1
"github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v1"
@@ -46,3 +48,10 @@ func Test_validateSchema(t *testing.T) {
})
}
}
+
+func TestHasValueInSyncMap(t *testing.T) {
+ m := new(sync.Map)
+ assert.False(t, HasValueInSyncMap(m), "sync.Map should be empty")
+ m.Store("hello", "test")
+ assert.True(t, HasValueInSyncMap(m), "sync.Map should not be empty")
+}
diff --git a/pkg/ingress/compare.go b/pkg/ingress/compare.go
index b5829a9..5d5747b 100644
--- a/pkg/ingress/compare.go
+++ b/pkg/ingress/compare.go
@@ -20,12 +20,15 @@ import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+ "github.com/apache/apisix-ingress-controller/pkg/api/validation"
"github.com/apache/apisix-ingress-controller/pkg/log"
)
// CompareResources used to compare the object IDs in resources and APISIX
// Find out the rest of objects in APISIX
// AND warn them in log.
+// This func is NOT concurrency safe.
+// cc
https://github.com/apache/apisix-ingress-controller/pull/742#discussion_r757197791
func (c *Controller) CompareResources(ctx context.Context) error {
var (
wg sync.WaitGroup
@@ -42,7 +45,7 @@ func (c *Controller) CompareResources(ctx context.Context)
error {
consumerMapA6 = make(map[string]string)
)
// watchingNamespace == nil means to monitor all namespaces
- if c.watchingNamespace == nil {
+ if !validation.HasValueInSyncMap(c.watchingNamespace) {
opts := v1.ListOptions{}
// list all namespaces
nsList, err :=
c.kubeClient.Client.CoreV1().Namespaces().List(ctx, opts)
diff --git a/pkg/ingress/controller.go b/pkg/ingress/controller.go
index fe2c812..ec3deb6 100644
--- a/pkg/ingress/controller.go
+++ b/pkg/ingress/controller.go
@@ -37,6 +37,7 @@ import (
"k8s.io/client-go/tools/record"
"github.com/apache/apisix-ingress-controller/pkg/api"
+ "github.com/apache/apisix-ingress-controller/pkg/api/validation"
"github.com/apache/apisix-ingress-controller/pkg/apisix"
apisixcache
"github.com/apache/apisix-ingress-controller/pkg/apisix/cache"
"github.com/apache/apisix-ingress-controller/pkg/config"
@@ -518,7 +519,7 @@ func (c *Controller) run(ctx context.Context) {
// namespaceWatching 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 c.watchingNamespace == nil {
+ if !validation.HasValueInSyncMap(c.watchingNamespace) {
ok = true
return
}
diff --git a/pkg/types/labels.go b/pkg/types/labels.go
index 2ff9c12..f99a943 100644
--- a/pkg/types/labels.go
+++ b/pkg/types/labels.go
@@ -21,8 +21,8 @@ type Labels map[string]string
// the passed Labels.
func (s Labels) IsSubsetOf(f Labels) bool {
if len(s) == 0 {
- // Empty labels matches nothing not everything.
- return false
+ // Empty labels matches everything.
+ return true
}
for k, v := range s {
if vv, ok := f[k]; !ok || vv != v {
diff --git a/pkg/types/labels_test.go b/pkg/types/labels_test.go
index 970811c..7ba1e4c 100644
--- a/pkg/types/labels_test.go
+++ b/pkg/types/labels_test.go
@@ -26,7 +26,7 @@ func TestLabelsIsSubsetOf(t *testing.T) {
"version": "v1",
"env": "prod",
}
- assert.Equal(t, l.IsSubsetOf(f), false)
+ assert.Equal(t, l.IsSubsetOf(f), true)
l["env"] = "prod"
assert.Equal(t, l.IsSubsetOf(f), true)
l["env"] = "qa"