Skip to content

Commit

Permalink
CLOUDP-250918: Add test to reproduce issue #1515 (#1621)
Browse files Browse the repository at this point in the history
* Add reproducing test

Signed-off-by: jose.vazquez <jose.vazquez@mongodb.com>

* Fix cache and predicate setup

* test/e2e/cache_watch_test.go: improve e2e test

* Fix gets labels and ns names

Signed-off-by: jose.vazquez <jose.vazquez@mongodb.com>

* Rename controller predicates helper

* Move trim to env reading line

---------

Signed-off-by: jose.vazquez <jose.vazquez@mongodb.com>
Co-authored-by: Sergiusz Urbaniak <sergiusz.urbaniak@gmail.com>
  • Loading branch information
josvazg and s-urbaniak committed Jun 3, 2024
1 parent 50325b3 commit 01511da
Show file tree
Hide file tree
Showing 10 changed files with 404 additions and 47 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ jobs:
"deletion-protection",
"atlas-search-nodes",
"atlas-search-index",
"cache-watch",
"reconcile-all",
"reconcile-one",
"reconcile-two",
]
steps:
- name: Get repo files from cache
Expand Down
27 changes: 12 additions & 15 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func main() {
syncPeriod := time.Hour * 3

var cacheFunc cache.NewCacheFunc
if len(config.WatchedNamespaces) > 1 {
if len(config.WatchedNamespaces) > 0 {
var namespaces []string
for ns := range config.WatchedNamespaces {
namespaces = append(namespaces, ns)
Expand All @@ -124,8 +124,7 @@ func main() {
Port: 9443,
}),
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{config.Namespace: {}},
SyncPeriod: &syncPeriod,
SyncPeriod: &syncPeriod,
},
HealthProbeBindAddress: config.ProbeAddr,
LeaderElection: config.EnableLeaderElection,
Expand All @@ -142,9 +141,10 @@ func main() {
// globalPredicates should be used for general controller Predicates
// that should be applied to all controllers in order to limit the
// resources they receive events for.
predicateNamespaces := controller.NamespacesOrAllPredicate(config.WatchedNamespaces)
globalPredicates := []predicate.Predicate{
watch.CommonPredicates(), // ignore spurious changes. status changes etc.
watch.SelectNamespacesPredicate(config.WatchedNamespaces), // select only desired namespaces
watch.CommonPredicates(), // ignore spurious changes. status changes etc.
watch.SelectNamespacesPredicate(predicateNamespaces), // select only desired namespaces
}

atlasProvider := atlas.NewProductionProvider(config.AtlasDomain, config.GlobalAPISecret, mgr.GetClient())
Expand Down Expand Up @@ -294,7 +294,6 @@ type Config struct {
AtlasDomain string
EnableLeaderElection bool
MetricsAddr string
Namespace string
WatchedNamespaces map[string]bool
ProbeAddr string
GlobalAPISecret client.ObjectKey
Expand Down Expand Up @@ -335,15 +334,13 @@ func parseConfiguration() Config {

// dev note: we pass the watched namespace as the env variable to use the Kubernetes Downward API. Unfortunately
// there is no way to use it for container arguments
watchedNamespace := os.Getenv("WATCH_NAMESPACE")
config.WatchedNamespaces = make(map[string]bool)
for _, namespace := range strings.Split(watchedNamespace, ",") {
namespace = strings.TrimSpace(namespace)
config.WatchedNamespaces[namespace] = true
}

if len(config.WatchedNamespaces) == 1 {
config.Namespace = watchedNamespace
watchedNamespace := strings.TrimSpace(os.Getenv("WATCH_NAMESPACE"))
if watchedNamespace != "" {
config.WatchedNamespaces = make(map[string]bool)
for _, namespace := range strings.Split(watchedNamespace, ",") {
namespace = strings.TrimSpace(namespace)
config.WatchedNamespaces[namespace] = true
}
}

configureDeletionProtection(&config)
Expand Down
8 changes: 8 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ func CustomLabelSelectorCacheBuilder(obj client.Object, labelsSelector labels.Se
return cache.New(config, opts)
}
}

func NamespacesOrAllPredicate(namespaceMap map[string]bool) map[string]bool {
if len(namespaceMap) > 0 {
return namespaceMap
}
// if no namespaces where specified it must check all namespaces
return map[string]bool{cache.AllNamespaces: true}
}
Loading

0 comments on commit 01511da

Please sign in to comment.