-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add watching for projects in data federation
- Loading branch information
1 parent
47827f8
commit 059e602
Showing
5 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package indexer | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1" | ||
) | ||
|
||
const ( | ||
AtlasDataFederationByProject = "atlasdatafederation.spec.project" | ||
) | ||
|
||
type AtlasDataFederationByProjectIndexer struct { | ||
logger *zap.SugaredLogger | ||
} | ||
|
||
func NewAtlasDataFederationByProjectIndexer(logger *zap.Logger) *AtlasDataFederationByProjectIndexer { | ||
return &AtlasDataFederationByProjectIndexer{ | ||
logger: logger.Named(AtlasDatabaseUserByProject).Sugar(), | ||
} | ||
} | ||
|
||
func (*AtlasDataFederationByProjectIndexer) Object() client.Object { | ||
return &akov2.AtlasDataFederation{} | ||
} | ||
|
||
func (*AtlasDataFederationByProjectIndexer) Name() string { | ||
return AtlasDataFederationByProject | ||
} | ||
|
||
func (a *AtlasDataFederationByProjectIndexer) Keys(object client.Object) []string { | ||
datafederation, ok := object.(*akov2.AtlasDataFederation) | ||
if !ok { | ||
a.logger.Errorf("expected *AtlasDataFederation but got %T", object) | ||
return nil | ||
} | ||
|
||
if datafederation.Spec.Project.IsEmpty() { | ||
return nil | ||
} | ||
|
||
return []string{datafederation.Spec.Project.GetObject(datafederation.Namespace).String()} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package indexer | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap/zaptest" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1" | ||
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/common" | ||
) | ||
|
||
func TestAtlasDataFederationByProjectIndexer(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
object client.Object | ||
wantKeys []string | ||
}{ | ||
{ | ||
name: "should return nil on wrong type", | ||
object: &akov2.AtlasDeployment{}, | ||
}, | ||
{ | ||
name: "should return nil when there are no references", | ||
object: &akov2.AtlasDataFederation{}, | ||
}, | ||
{ | ||
name: "should return nil when there is an empty reference", | ||
object: &akov2.AtlasDataFederation{ | ||
Spec: akov2.DataFederationSpec{ | ||
Project: common.ResourceRefNamespaced{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "should return project namespace if name is set only", | ||
object: &akov2.AtlasDataFederation{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "name", Namespace: "ns"}, | ||
Spec: akov2.DataFederationSpec{ | ||
Project: common.ResourceRefNamespaced{Name: "someProject"}, | ||
}, | ||
}, | ||
wantKeys: []string{"ns/someProject"}, | ||
}, | ||
{ | ||
name: "should return secret namespace and name if set", | ||
object: &akov2.AtlasDataFederation{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "name", Namespace: "ns"}, | ||
Spec: akov2.DataFederationSpec{ | ||
Project: common.ResourceRefNamespaced{Name: "someProject", Namespace: "otherNs"}, | ||
}, | ||
}, | ||
wantKeys: []string{"otherNs/someProject"}, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
indexer := NewAtlasDataFederationByProjectIndexer(zaptest.NewLogger(t)) | ||
keys := indexer.Keys(tc.object) | ||
sort.Strings(keys) | ||
assert.Equal(t, tc.wantKeys, keys) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters