Skip to content

Commit

Permalink
fix: ingress do not watching any namespace when namespaceSelector is …
Browse files Browse the repository at this point in the history
…empty (#742)
  • Loading branch information
gxthrj authored Nov 26, 2021
1 parent 62d7897 commit f470867
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
11 changes: 11 additions & 0 deletions pkg/api/validation/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions pkg/api/validation/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
5 changes: 4 additions & 1 deletion pkg/ingress/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/ingress/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f470867

Please sign in to comment.