Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (m *MockedManager) GetAdmissionDecoder() admission.Decoder {

// GetAPIReader returns the client reader
func (m *MockedManager) GetAPIReader() k8sClient.Reader {
return nil
return m.Client
}

// GetClient returns a client configured with the Config
Expand Down
3 changes: 3 additions & 0 deletions pkg/telemetry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package telemetry

import (
"context"
"runtime"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -182,6 +183,8 @@ func collectOperatorSnapshot(ctx context.Context, memberClusterMap map[string]Co
OperatorID: operatorUUID,
OperatorVersion: versionutil.StaticContainersOperatorVersion(),
OperatorType: MEKO,
OperatorArchitecture: runtime.GOARCH,
OperatorOS: runtime.GOOS,
}

event := createEvent(operatorEvent, time.Now(), Operators)
Expand Down
93 changes: 93 additions & 0 deletions pkg/telemetry/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package telemetry
import (
"context"
"errors"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

mdbv1 "github.com/mongodb/mongodb-kubernetes/api/v1/mdb"
Expand Down Expand Up @@ -1331,3 +1334,93 @@ func TestAddSearchEvents(t *testing.T) {
})
}
}

func TestCollectOperatorSnapshot(t *testing.T) {
tests := map[string]struct {
memberClusterMap map[string]ConfigClient
expectedProps OperatorUsageSnapshotProperties
}{
"single cluster": {
memberClusterMap: map[string]ConfigClient{},
expectedProps: OperatorUsageSnapshotProperties{
OperatorID: testOperatorUUID,
OperatorType: MEKO,
OperatorArchitecture: runtime.GOARCH,
OperatorOS: runtime.GOOS,
},
},
"multi cluster": {
memberClusterMap: map[string]ConfigClient{
"cluster1": &mockConfigClient{clusterUUID: "cluster-uuid-1"},
"cluster2": &mockConfigClient{clusterUUID: "cluster-uuid-2"},
},
expectedProps: OperatorUsageSnapshotProperties{
OperatorID: testOperatorUUID,
OperatorType: MEKO,
OperatorArchitecture: runtime.GOARCH,
OperatorOS: runtime.GOOS,
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

kubeSystemNamespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-system",
UID: types.UID("operator-cluster-uuid"),
},
}

k8sClient := mock.NewEmptyFakeClientBuilder().WithObjects(kubeSystemNamespace).Build()
mgr := mockClient.NewManagerWithClient(k8sClient)

ctx := context.Background()

events := collectOperatorSnapshot(ctx, test.memberClusterMap, mgr, testOperatorUUID, "", "")

require.Len(t, events, 1, "expected exactly one operator event")
event := events[0]

assert.Equal(t, Operators, event.Source)
require.NotNil(t, event.Timestamp, "event timestamp is nil")

assert.Equal(t, runtime.GOARCH, event.Properties["operatorArchitecture"])
assert.Equal(t, runtime.GOOS, event.Properties["operatorOS"])
assert.Equal(t, testOperatorUUID, event.Properties["operatorID"])
assert.Equal(t, string(MEKO), event.Properties["operatorType"])

assert.Contains(t, event.Properties, "kubernetesClusterID")
assert.Contains(t, event.Properties, "kubernetesClusterIDs")
assert.Contains(t, event.Properties, "operatorVersion")
})
}
}

// mockConfigClient is a simple mock implementation of ConfigClient for testing
type mockConfigClient struct {
clusterUUID string
}

func (m *mockConfigClient) GetConfig() *rest.Config {
return &rest.Config{}
}

func (m *mockConfigClient) GetAPIReader() client.Reader {
uuid := m.clusterUUID
if uuid == "" {
uuid = "default-cluster-uuid"
}

kubeSystemNamespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-system",
UID: types.UID(uuid),
},
}

k8sClient := mock.NewEmptyFakeClientBuilder().WithObjects(kubeSystemNamespace).Build()
return k8sClient
}
2 changes: 2 additions & 0 deletions pkg/telemetry/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type OperatorUsageSnapshotProperties struct {
OperatorID string `json:"operatorID"` // Operator UUID
OperatorVersion string `json:"operatorVersion"` // Version of the operator
OperatorType OperatorType `json:"operatorType"` // MEKO, MCK, MCO (here meko)
OperatorArchitecture string `json:"operatorArchitecture"` // Architecture of the operator binary (amd64, arm64, s390x, ppc64le)
OperatorOS string `json:"operatorOS"` // Operating system of the operator binary (linux, darwin, windows)
}

func (p OperatorUsageSnapshotProperties) ConvertToFlatMap() (map[string]any, error) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/release/tests/build_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def test_load_build_info_staging(git_repo: Repo):
),
"init-ops-manager": ImageInfo(
repository="268558157000.dkr.ecr.us-east-1.amazonaws.com/staging/mongodb-kubernetes-init-ops-manager",
platforms=["linux/arm64", "linux/amd64", "linux/s390x", "linux/ppc64le"],
platforms=["linux/amd64"],
version=expected_commit_sha,
dockerfile_path="docker/mongodb-kubernetes-init-ops-manager/Dockerfile.atomic",
sign=True,
Expand Down Expand Up @@ -382,7 +382,7 @@ def test_load_build_info_release(
),
"init-ops-manager": ImageInfo(
repository="quay.io/mongodb/mongodb-kubernetes-init-ops-manager",
platforms=["linux/arm64", "linux/amd64", "linux/s390x", "linux/ppc64le"],
platforms=["linux/amd64"],
version=version,
dockerfile_path="docker/mongodb-kubernetes-init-ops-manager/Dockerfile.atomic",
sign=True,
Expand Down
2 changes: 1 addition & 1 deletion scripts/release/tests/release_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_create_release_info_json(
},
"init-ops-manager": {
"repository": "quay.io/mongodb/mongodb-kubernetes-init-ops-manager",
"platforms": ["linux/arm64", "linux/amd64", "linux/s390x", "linux/ppc64le"],
"platforms": ["linux/amd64"],
"version": "1.2.0",
},
"database": {
Expand Down
22 changes: 0 additions & 22 deletions scripts/release/tests/test_detect_ops_manager_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,6 @@ def test_new_ops_manager_version_added(self):
changed_agents = detect_ops_manager_changes()
self.assertIn(("108.0.0.8694-1", "100.10.0"), changed_agents)

def test_ops_manager_version_modified(self):
"""Test that modifying existing OM version is NOT detected (only new versions are detected)"""
modified_current = json.loads(json.dumps(self.current_release_data))
modified_current["supportedImages"]["mongodb-agent"]["opsManagerMapping"]["ops_manager"]["6.0.26"][
"agent_version"
] = "12.0.35.7911-1"

with (
patch(
"scripts.release.agent.detect_ops_manager_changes.load_current_release_json",
return_value=modified_current,
),
patch(
"scripts.release.agent.detect_ops_manager_changes.load_release_json_from_master",
return_value=self.master_release_data,
),
):

changed_agents = detect_ops_manager_changes()
# Modified existing OM versions should NOT be detected
self.assertEqual(changed_agents, [])

def test_cloud_manager_changed(self):
"""Test detection when cloud_manager is changed"""
modified_current = json.loads(json.dumps(self.current_release_data))
Expand Down