Skip to content

Commit

Permalink
fix: remove namespace from Module.includedNamespaces (#644)
Browse files Browse the repository at this point in the history
# Description

As title.

## Related Issue

#81 

## Checklist

- [x] I have read the [contributing
documentation](https://retina.sh/docs/contributing).
- [x] I signed and signed-off the commits (`git commit -S -s ...`). See
[this
documentation](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification)
on signing commits.
- [x] I have correctly attributed the author(s) of the code.
- [x] I have tested the changes locally.
- [x] I have followed the project's style guidelines.
- [ ] I have updated the documentation, if necessary.
- [x] I have added tests, if applicable.

## Screenshots (if applicable) or Testing Completed


![image](https://github.com/user-attachments/assets/f5d828a4-6415-49be-8d40-a5981ace427a)

## Additional Notes

Add any additional notes or context about the pull request here.

---

Please refer to the [CONTRIBUTING.md](../CONTRIBUTING.md) file for more
information on how to contribute to this project.

Signed-off-by: Alex Castilio dos Santos <alexsantos@microsoft.com>
  • Loading branch information
alexcastilio committed Sep 3, 2024
1 parent ff807d5 commit 34d8452
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/module/metrics/metrics_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ func (m *Module) updateNamespaceLists(spec *api.MetricsSpec) {
m.l.Error("Both included and excluded namespaces are specified. Cannot reconcile.")
}

if len(spec.Namespaces.Include) == 0 {
m.appendIncludeList([]string{})
m.appendExcludeList([]string{})
}

if len(spec.Namespaces.Exclude) == 0 {
m.appendIncludeList([]string{})
m.appendExcludeList([]string{})
}

if len(spec.Namespaces.Include) > 0 {
m.l.Info("Including namespaces", zap.Strings("namespaces", spec.Namespaces.Include))
m.appendIncludeList(spec.Namespaces.Include)
Expand Down
77 changes: 77 additions & 0 deletions pkg/module/metrics/metrics_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/microsoft/retina/pkg/managers/filtermanager"
"github.com/microsoft/retina/pkg/pubsub"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"go.uber.org/zap"
)
Expand All @@ -40,6 +41,7 @@ func TestAppendIncludeList(t *testing.T) {
c := cache.NewMockCacheInterface(ctrl) //nolint:typecheck
c.EXPECT().GetIPsByNamespace(gomock.Any()).Return([]net.IP{}).AnyTimes()
fm.EXPECT().AddIPs(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
fm.EXPECT().DeleteIPs(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()

me := InitModule(
context.Background(),
Expand All @@ -54,6 +56,81 @@ func TestAppendIncludeList(t *testing.T) {
me.appendIncludeList([]string{"test"})
}

func TestUpdateNamespaceLists(t *testing.T) {
_, err := log.SetupZapLogger(log.GetDefaultLogOpts())
require.NoError(t, err)
cfg, err := kcfg.GetConfig(testCfgFile)
assert.NotNil(t, cfg)
require.NoError(t, err)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

p := pubsub.NewMockPubSubInterface(ctrl)
e := enricher.NewMockEnricherInterface(ctrl)
fm := filtermanager.NewMockIFilterManager(ctrl)
c := cache.NewMockCacheInterface(ctrl)
c.EXPECT().GetIPsByNamespace(gomock.Any()).Return([]net.IP{}).AnyTimes()
fm.EXPECT().AddIPs(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
fm.EXPECT().DeleteIPs(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()

me := InitModule(
context.Background(),
cfg,
p,
e,
fm,
c,
)

assert.NotNil(t, me)

testcases := []struct {
description string
namespaces []string
wantIncludedNamespaces map[string]struct{}
}{
{
"input 0 namespaces",
[]string{},
map[string]struct{}{},
},
{
"input 1 namespace (add)",
[]string{"ns1"},
map[string]struct{}{"ns1": {}},
},
{
"input 1 namespace different than previous (add 1 & remove 1)",
[]string{"ns2"},
map[string]struct{}{"ns2": {}},
},
{
"input 2 namespaces (add 1)",
[]string{"ns1", "ns2"},
map[string]struct{}{"ns1": {}, "ns2": {}},
},
{
"input 2 namespaces different than previous 2 (add 2 & remove 2)",
[]string{"ns3", "ns4"},
map[string]struct{}{"ns3": {}, "ns4": {}},
},
{
"input 0 namespaces (remove 2)",
[]string{},
map[string]struct{}{},
},
}

for _, test := range testcases {
t.Run(test.description, func(t *testing.T) {
spec := (&api.MetricsSpec{}).
WithIncludedNamespaces(test.namespaces)
me.updateNamespaceLists(spec)
assert.Equal(t, test.wantIncludedNamespaces, me.includedNamespaces)
})
}
}

func TestPodCallBack(t *testing.T) {
cfg, err := kcfg.GetConfig(testCfgFile)
cfg.EnableAnnotations = true
Expand Down

0 comments on commit 34d8452

Please sign in to comment.