Skip to content

Commit 7fca641

Browse files
committed
Merge branch 'search/public-preview' into fealebenpae/enterprise-search-snippets
2 parents d71f112 + ef40120 commit 7fca641

File tree

2 files changed

+112
-53
lines changed

2 files changed

+112
-53
lines changed

pkg/telemetry/collector.go

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
mdbmultiv1 "github.com/mongodb/mongodb-kubernetes/api/v1/mdbmulti"
2222
omv1 "github.com/mongodb/mongodb-kubernetes/api/v1/om"
2323
searchv1 "github.com/mongodb/mongodb-kubernetes/api/v1/search"
24+
userv1 "github.com/mongodb/mongodb-kubernetes/api/v1/user"
2425
mcov1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1"
2526
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/util/envvar"
2627
"github.com/mongodb/mongodb-kubernetes/pkg/images"
@@ -364,25 +365,50 @@ func addCommunityEvents(ctx context.Context, operatorClusterClient kubeclient.Cl
364365
return events
365366
}
366367

368+
func resolveSearchSource(ctx context.Context, operatorClusterClient kubeclient.Client, source *userv1.MongoDBResourceRef) (architecture string, isEnterprise bool, ok bool) {
369+
if source == nil {
370+
return "external", false, true // we cheat and hijack the Architecture field to indicate this Search resource is configured with an external MongoDB source
371+
}
372+
373+
key := kubeclient.ObjectKey{Namespace: source.Namespace, Name: source.Name}
374+
375+
mdb := &mdbv1.MongoDB{}
376+
if err := operatorClusterClient.Get(ctx, key, mdb); err == nil {
377+
return string(architectures.GetArchitecture(mdb.Annotations)), true, true
378+
}
379+
380+
mdbc := &mcov1.MongoDBCommunity{}
381+
if err := operatorClusterClient.Get(ctx, key, mdbc); err == nil {
382+
return "static", false, true // Community is always static
383+
}
384+
385+
return "", false, false // likely the database resource doesn't exist yet, skip telemetry for this item for now
386+
}
387+
367388
func addSearchEvents(ctx context.Context, operatorClusterClient kubeclient.Client, operatorUUID string, now time.Time) []Event {
368389
var events []Event
369390
searchList := &searchv1.MongoDBSearchList{}
370391

371392
if err := operatorClusterClient.List(ctx, searchList); err != nil {
372393
Logger.Warnf("failed to fetch MongoDBSearchList from Kubernetes: %v", err)
373-
} else {
374-
for _, item := range searchList.Items {
375-
properties := DeploymentUsageSnapshotProperties{
376-
DeploymentUID: string(item.UID),
377-
OperatorID: operatorUUID,
378-
Architecture: string(architectures.Static), // Community Search is always static
379-
IsMultiCluster: false, // Community Search doesn't support multi-cluster
380-
Type: "Search",
381-
IsRunningEnterpriseImage: false, // Community search doesn't run enterprise
382-
}
383-
if event := createEvent(properties, now, Deployments); event != nil {
384-
events = append(events, *event)
385-
}
394+
return nil
395+
}
396+
397+
for _, item := range searchList.Items {
398+
architecture, isEnterprise, ok := resolveSearchSource(ctx, operatorClusterClient, item.GetMongoDBResourceRef())
399+
if !ok { // search source doesn't exist yet, don't generate a telemetry event
400+
continue
401+
}
402+
properties := DeploymentUsageSnapshotProperties{
403+
DeploymentUID: string(item.UID),
404+
OperatorID: operatorUUID,
405+
Architecture: architecture,
406+
IsMultiCluster: false, // Search doesn't support multi-cluster
407+
Type: "Search",
408+
IsRunningEnterpriseImage: isEnterprise,
409+
}
410+
if event := createEvent(properties, now, Deployments); event != nil {
411+
events = append(events, *event)
386412
}
387413
}
388414
return events

pkg/telemetry/collector_test.go

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package telemetry
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
7+
"reflect"
68
"runtime"
79
"testing"
810
"time"
@@ -21,6 +23,7 @@ import (
2123
"github.com/mongodb/mongodb-kubernetes/api/v1/mdbmulti"
2224
omv1 "github.com/mongodb/mongodb-kubernetes/api/v1/om"
2325
searchv1 "github.com/mongodb/mongodb-kubernetes/api/v1/search"
26+
userv1 "github.com/mongodb/mongodb-kubernetes/api/v1/user"
2427
"github.com/mongodb/mongodb-kubernetes/controllers/operator/mock"
2528
mcov1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1"
2629
mockClient "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/kube/client"
@@ -1157,12 +1160,17 @@ func findEventWithDeploymentUID(events []Event, deploymentUID string) *Event {
11571160
type MockClient struct {
11581161
client.Client
11591162
MockList func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error
1163+
MockGet func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
11601164
}
11611165

11621166
func (m *MockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
11631167
return m.MockList(ctx, list, opts...)
11641168
}
11651169

1170+
func (m *MockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
1171+
return m.MockGet(ctx, key, obj, opts...)
1172+
}
1173+
11661174
func TestAddCommunityEvents(t *testing.T) {
11671175
operatorUUID := "test-operator-uuid"
11681176

@@ -1261,55 +1269,67 @@ func TestAddCommunityEvents(t *testing.T) {
12611269

12621270
func TestAddSearchEvents(t *testing.T) {
12631271
operatorUUID := "test-operator-uuid"
1264-
12651272
now := time.Now()
12661273

1274+
mdbStatic := &mdbv1.MongoDB{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "mdb-static", Annotations: map[string]string{architectures.ArchitectureAnnotation: string(architectures.Static)}}}
1275+
mdbNonStatic := &mdbv1.MongoDB{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "mdb-nonstatic", Annotations: map[string]string{architectures.ArchitectureAnnotation: string(architectures.NonStatic)}}}
1276+
community := &mcov1.MongoDBCommunity{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "community-db"}}
1277+
12671278
testCases := []struct {
1268-
name string
1269-
resources searchv1.MongoDBSearchList
1270-
events []DeploymentUsageSnapshotProperties
1279+
name string
1280+
searchItems []searchv1.MongoDBSearch
1281+
events []DeploymentUsageSnapshotProperties
1282+
sources map[reflect.Type][]client.Object
12711283
}{
12721284
{
1273-
name: "With resources",
1274-
resources: searchv1.MongoDBSearchList{
1275-
Items: []searchv1.MongoDBSearch{
1276-
{
1277-
ObjectMeta: metav1.ObjectMeta{
1278-
UID: types.UID("search-1"),
1279-
Name: "test-search-1",
1280-
},
1281-
},
1282-
{
1283-
ObjectMeta: metav1.ObjectMeta{
1284-
UID: types.UID("search-2"),
1285-
Name: "test-search-2",
1286-
},
1287-
},
1288-
},
1285+
name: "External source",
1286+
searchItems: []searchv1.MongoDBSearch{
1287+
{ObjectMeta: metav1.ObjectMeta{UID: types.UID("search-external"), Name: "search-external", Namespace: "default"}, Spec: searchv1.MongoDBSearchSpec{Source: &searchv1.MongoDBSource{ExternalMongoDBSource: &searchv1.ExternalMongoDBSource{}}}},
1288+
},
1289+
events: []DeploymentUsageSnapshotProperties{{
1290+
DeploymentUID: "search-external",
1291+
OperatorID: operatorUUID,
1292+
Architecture: "external",
1293+
IsMultiCluster: false,
1294+
Type: "Search",
1295+
IsRunningEnterpriseImage: false,
1296+
}},
1297+
},
1298+
{
1299+
name: "Enterprise static and non-static",
1300+
searchItems: []searchv1.MongoDBSearch{
1301+
{ObjectMeta: metav1.ObjectMeta{UID: types.UID("search-static"), Name: "search-static", Namespace: "default"}, Spec: searchv1.MongoDBSearchSpec{Source: &searchv1.MongoDBSource{MongoDBResourceRef: &userv1.MongoDBResourceRef{Name: "mdb-static"}}}},
1302+
{ObjectMeta: metav1.ObjectMeta{UID: types.UID("search-nonstatic"), Name: "search-nonstatic", Namespace: "default"}, Spec: searchv1.MongoDBSearchSpec{Source: &searchv1.MongoDBSource{MongoDBResourceRef: &userv1.MongoDBResourceRef{Name: "mdb-nonstatic"}}}},
12891303
},
12901304
events: []DeploymentUsageSnapshotProperties{
1291-
{
1292-
DeploymentUID: "search-1",
1293-
OperatorID: operatorUUID,
1294-
Architecture: string(architectures.Static),
1295-
IsMultiCluster: false,
1296-
Type: "Search",
1297-
IsRunningEnterpriseImage: false,
1298-
},
1299-
{
1300-
DeploymentUID: "search-2",
1301-
OperatorID: operatorUUID,
1302-
Architecture: string(architectures.Static),
1303-
IsMultiCluster: false,
1304-
Type: "Search",
1305-
IsRunningEnterpriseImage: false,
1306-
},
1305+
{DeploymentUID: "search-static", OperatorID: operatorUUID, Architecture: string(architectures.Static), IsMultiCluster: false, Type: "Search", IsRunningEnterpriseImage: true},
1306+
{DeploymentUID: "search-nonstatic", OperatorID: operatorUUID, Architecture: string(architectures.NonStatic), IsMultiCluster: false, Type: "Search", IsRunningEnterpriseImage: true},
1307+
},
1308+
sources: map[reflect.Type][]client.Object{
1309+
reflect.TypeOf(&mdbv1.MongoDB{}): {mdbStatic, mdbNonStatic},
13071310
},
13081311
},
13091312
{
1310-
name: "With no resources",
1311-
resources: searchv1.MongoDBSearchList{},
1312-
events: []DeploymentUsageSnapshotProperties{},
1313+
name: "Community source",
1314+
searchItems: []searchv1.MongoDBSearch{
1315+
{ObjectMeta: metav1.ObjectMeta{UID: types.UID("search-community"), Name: "search-community", Namespace: "default"}, Spec: searchv1.MongoDBSearchSpec{Source: &searchv1.MongoDBSource{MongoDBResourceRef: &userv1.MongoDBResourceRef{Name: "community-db"}}}},
1316+
},
1317+
events: []DeploymentUsageSnapshotProperties{{DeploymentUID: "search-community", OperatorID: operatorUUID, Architecture: "static", IsMultiCluster: false, Type: "Search", IsRunningEnterpriseImage: false}},
1318+
sources: map[reflect.Type][]client.Object{
1319+
reflect.TypeOf(&mcov1.MongoDBCommunity{}): {community},
1320+
},
1321+
},
1322+
{
1323+
name: "Missing underlying resource (skipped)",
1324+
searchItems: []searchv1.MongoDBSearch{
1325+
{ObjectMeta: metav1.ObjectMeta{UID: types.UID("search-missing"), Name: "search-missing", Namespace: "default"}, Spec: searchv1.MongoDBSearchSpec{Source: &searchv1.MongoDBSource{MongoDBResourceRef: &userv1.MongoDBResourceRef{Name: "does-not-exist"}}}},
1326+
},
1327+
events: []DeploymentUsageSnapshotProperties{},
1328+
},
1329+
{
1330+
name: "No search resources",
1331+
searchItems: []searchv1.MongoDBSearch{},
1332+
events: []DeploymentUsageSnapshotProperties{},
13131333
},
13141334
}
13151335

@@ -1318,10 +1338,23 @@ func TestAddSearchEvents(t *testing.T) {
13181338
mc := &MockClient{
13191339
MockList: func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
13201340
if l, ok := list.(*searchv1.MongoDBSearchList); ok {
1321-
*l = tc.resources
1341+
*l = searchv1.MongoDBSearchList{Items: tc.searchItems}
13221342
}
13231343
return nil
13241344
},
1345+
MockGet: func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
1346+
objects := tc.sources[reflect.TypeOf(obj)]
1347+
for _, o := range objects {
1348+
if o.GetName() == key.Name && o.GetNamespace() == key.Namespace {
1349+
// copy the arranged object into the obj pointer like controller-runtime's pkg/client/fake does
1350+
bytes, _ := json.Marshal(o)
1351+
json.Unmarshal(bytes, obj)
1352+
return nil
1353+
}
1354+
}
1355+
1356+
return errors.New("not found")
1357+
},
13251358
}
13261359

13271360
events := addSearchEvents(context.Background(), mc, operatorUUID, now)

0 commit comments

Comments
 (0)