Skip to content

Commit 118a1cc

Browse files
committed
add goos and goarch telemetry
1 parent 489c791 commit 118a1cc

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

mongodb-community-operator/pkg/kube/client/mocked_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (m *MockedManager) GetAdmissionDecoder() admission.Decoder {
7979

8080
// GetAPIReader returns the client reader
8181
func (m *MockedManager) GetAPIReader() k8sClient.Reader {
82-
return nil
82+
return m.Client
8383
}
8484

8585
// GetClient returns a client configured with the Config

pkg/telemetry/collector.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package telemetry
22

33
import (
44
"context"
5+
"runtime"
56
"slices"
67
"strings"
78
"time"
@@ -182,6 +183,8 @@ func collectOperatorSnapshot(ctx context.Context, memberClusterMap map[string]Co
182183
OperatorID: operatorUUID,
183184
OperatorVersion: versionutil.StaticContainersOperatorVersion(),
184185
OperatorType: MEKO,
186+
OperatorArchitecture: runtime.GOARCH,
187+
OperatorOS: runtime.GOOS,
185188
}
186189

187190
event := createEvent(operatorEvent, time.Now(), Operators)

pkg/telemetry/collector_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package telemetry
33
import (
44
"context"
55
"errors"
6+
"runtime"
67
"testing"
78
"time"
89

910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
12+
corev1 "k8s.io/api/core/v1"
1113
"k8s.io/apimachinery/pkg/types"
14+
"k8s.io/client-go/rest"
1215
"k8s.io/utils/ptr"
1316
"sigs.k8s.io/controller-runtime/pkg/client"
1417

@@ -23,6 +26,8 @@ import (
2326
mockClient "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/kube/client"
2427
"github.com/mongodb/mongodb-kubernetes/pkg/util"
2528
"github.com/mongodb/mongodb-kubernetes/pkg/util/architectures"
29+
30+
kubeclient "sigs.k8s.io/controller-runtime/pkg/client"
2631
)
2732

2833
const (
@@ -1331,3 +1336,93 @@ func TestAddSearchEvents(t *testing.T) {
13311336
})
13321337
}
13331338
}
1339+
1340+
func TestCollectOperatorSnapshot(t *testing.T) {
1341+
tests := map[string]struct {
1342+
memberClusterMap map[string]ConfigClient
1343+
expectedProps OperatorUsageSnapshotProperties
1344+
}{
1345+
"single cluster": {
1346+
memberClusterMap: map[string]ConfigClient{},
1347+
expectedProps: OperatorUsageSnapshotProperties{
1348+
OperatorID: testOperatorUUID,
1349+
OperatorType: MEKO,
1350+
OperatorArchitecture: runtime.GOARCH,
1351+
OperatorOS: runtime.GOOS,
1352+
},
1353+
},
1354+
"multi cluster": {
1355+
memberClusterMap: map[string]ConfigClient{
1356+
"cluster1": &mockConfigClient{clusterUUID: "cluster-uuid-1"},
1357+
"cluster2": &mockConfigClient{clusterUUID: "cluster-uuid-2"},
1358+
},
1359+
expectedProps: OperatorUsageSnapshotProperties{
1360+
OperatorID: testOperatorUUID,
1361+
OperatorType: MEKO,
1362+
OperatorArchitecture: runtime.GOARCH,
1363+
OperatorOS: runtime.GOOS,
1364+
},
1365+
},
1366+
}
1367+
1368+
for name, test := range tests {
1369+
t.Run(name, func(t *testing.T) {
1370+
t.Parallel()
1371+
1372+
kubeSystemNamespace := &corev1.Namespace{
1373+
ObjectMeta: metav1.ObjectMeta{
1374+
Name: "kube-system",
1375+
UID: types.UID("operator-cluster-uuid"),
1376+
},
1377+
}
1378+
1379+
k8sClient := mock.NewEmptyFakeClientBuilder().WithObjects(kubeSystemNamespace).Build()
1380+
mgr := mockClient.NewManagerWithClient(k8sClient)
1381+
1382+
ctx := context.Background()
1383+
1384+
events := collectOperatorSnapshot(ctx, test.memberClusterMap, mgr, testOperatorUUID, "", "")
1385+
1386+
require.Len(t, events, 1, "expected exactly one operator event")
1387+
event := events[0]
1388+
1389+
assert.Equal(t, Operators, event.Source)
1390+
require.NotNil(t, event.Timestamp, "event timestamp is nil")
1391+
1392+
assert.Equal(t, runtime.GOARCH, event.Properties["operatorArchitecture"])
1393+
assert.Equal(t, runtime.GOOS, event.Properties["operatorOS"])
1394+
assert.Equal(t, testOperatorUUID, event.Properties["operatorID"])
1395+
assert.Equal(t, string(MEKO), event.Properties["operatorType"])
1396+
1397+
assert.Contains(t, event.Properties, "kubernetesClusterID")
1398+
assert.Contains(t, event.Properties, "kubernetesClusterIDs")
1399+
assert.Contains(t, event.Properties, "operatorVersion")
1400+
})
1401+
}
1402+
}
1403+
1404+
// mockConfigClient is a simple mock implementation of ConfigClient for testing
1405+
type mockConfigClient struct {
1406+
clusterUUID string
1407+
}
1408+
1409+
func (m *mockConfigClient) GetConfig() *rest.Config {
1410+
return &rest.Config{}
1411+
}
1412+
1413+
func (m *mockConfigClient) GetAPIReader() kubeclient.Reader {
1414+
uuid := m.clusterUUID
1415+
if uuid == "" {
1416+
uuid = "default-cluster-uuid"
1417+
}
1418+
1419+
kubeSystemNamespace := &corev1.Namespace{
1420+
ObjectMeta: metav1.ObjectMeta{
1421+
Name: "kube-system",
1422+
UID: types.UID(uuid),
1423+
},
1424+
}
1425+
1426+
k8sClient := mock.NewEmptyFakeClientBuilder().WithObjects(kubeSystemNamespace).Build()
1427+
return k8sClient
1428+
}

pkg/telemetry/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type OperatorUsageSnapshotProperties struct {
2020
OperatorID string `json:"operatorID"` // Operator UUID
2121
OperatorVersion string `json:"operatorVersion"` // Version of the operator
2222
OperatorType OperatorType `json:"operatorType"` // MEKO, MCK, MCO (here meko)
23+
OperatorArchitecture string `json:"operatorArchitecture"` // Architecture of the operator binary (amd64, arm64, s390x, ppc64le)
24+
OperatorOS string `json:"operatorOS"` // Operating system of the operator binary (linux, darwin, windows)
2325
}
2426

2527
func (p OperatorUsageSnapshotProperties) ConvertToFlatMap() (map[string]any, error) {

0 commit comments

Comments
 (0)