Skip to content

CLOUDP-306286: add telemetry installation methods #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ all-tests: test python-tests
manager: generate fmt vet
GOOS=linux GOARCH=amd64 go build -o docker/mongodb-kubernetes-operator/content/mongodb-kubernetes-operator main.go

remote-build: manager
kubectl scale deployment mongodb-enterprise-operator --replicas=0
scp -P 22 ./docker/mongodb-enterprise-operator/content/mongodb-enterprise-operator [email protected]:/home/ubuntu/ops-manager-kubernetes/bin
kubectl scale deployment mongodb-enterprise-operator --replicas=1

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet manifests
go run ./main.go
Expand Down
7 changes: 7 additions & 0 deletions helm_chart/templates/operator-roles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ metadata:
namespace: {{ include "mongodb-kubernetes-operator.namespace" . }}
{{- end }}
rules:
- apiGroups:
- apps
resources:
- deployments
verbs:
- get
- list
- apiGroups:
- ''
resources:
Expand Down
12 changes: 10 additions & 2 deletions helm_chart/templates/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ spec:
command:
- /usr/local/bin/mongodb-kubernetes-operator
{{- end }}
{{- if .Values.multiCluster.clusters }}
volumeMounts:
- mountPath: /usr/local/bin/mongodb-enterprise-operator
name: binary
{{- if .Values.multiCluster.clusters }}
- mountPath: /etc/config/kubeconfig
name: kube-config-volume
{{- end }}
Expand Down Expand Up @@ -98,6 +100,8 @@ spec:
- name: MANAGED_SECURITY_CONTEXT
value: 'true'
{{- end }}
- name: MDB_OPERATOR_NAME
value: { { .Values.operator.name } }
{{- $telemetry := default dict .Values.operator.telemetry }}
{{- if eq $telemetry.enabled false }}
- name: MDB_OPERATOR_TELEMETRY_ENABLED
Expand Down Expand Up @@ -268,8 +272,12 @@ spec:
value: '{{ (split "=" .)._1 }}'
{{- end }}
{{- end }}
{{- if .Values.multiCluster.clusters }}
volumes:
- name: binary
hostPath:
path: /usr/bin/mongodb-enterprise-operator
type: "File"
{{- if .Values.multiCluster.clusters }}
- name: kube-config-volume
secret:
defaultMode: 420
Expand Down
45 changes: 40 additions & 5 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"
"github.com/pkg/errors"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -163,6 +164,13 @@ func collectOperatorSnapshot(ctx context.Context, memberClusterMap map[string]Co
memberClusterMap["single"] = operatorClusterMgr
}

// TODO: Read own deployment object or pass annotation/read env var here to distinguish installation method.
installationMethod, err := getOperatorInstallationMethod(ctx, uncachedClient)
if err != nil {
Logger.Error(err)
}
Logger.Infof("INSTALLATION METHOD DETECTED: %s", installationMethod)

for _, c := range memberClusterMap {
uncachedClient := c.GetAPIReader()
uid := getKubernetesClusterUUID(ctx, uncachedClient)
Expand All @@ -172,11 +180,12 @@ func collectOperatorSnapshot(ctx context.Context, memberClusterMap map[string]Co
slices.Sort(kubeClusterUUIDList)

operatorEvent := OperatorUsageSnapshotProperties{
KubernetesClusterID: kubeClusterOperatorUUID,
KubernetesClusterIDs: kubeClusterUUIDList,
OperatorID: operatorUUID,
OperatorVersion: versionutil.StaticContainersOperatorVersion(),
OperatorType: MEKO,
KubernetesClusterID: kubeClusterOperatorUUID,
KubernetesClusterIDs: kubeClusterUUIDList,
OperatorID: operatorUUID,
OperatorVersion: versionutil.StaticContainersOperatorVersion(),
OperatorType: MEKO,
OperatorInstallationMethod: installationMethod,
}
operatorProperties, err := maputil.StructToMap(operatorEvent)
if err != nil {
Expand All @@ -193,6 +202,32 @@ func collectOperatorSnapshot(ctx context.Context, memberClusterMap map[string]Co
}
}

// getOperatorInstallationMethod returns the installation of the currently installed operator
func getOperatorInstallationMethod(ctx context.Context, uncachedClient kubeclient.Reader) (OperatorInstallationMethod, error) {
currentNamespace := envvar.GetEnvOrDefault(util.CurrentNamespace, "")
if currentNamespace == "" {
return Unknown, errors.New("could not read current namespace from environment. Is NAMESPACE env var missing on deployment?")
}

deploymentName := envvar.GetEnvOrDefault(util.DeploymentName, "")
if deploymentName == "" {
return Unknown, errors.New("could not read deployment from environment. Is MDB_OPERATOR_NAME env var missing on deployment?")
}

installationMethod := Unknown
operatorDeployment := &v1.Deployment{}
err := uncachedClient.Get(ctx, kubeclient.ObjectKey{Name: deploymentName, Namespace: currentNamespace}, operatorDeployment)
if err != nil {
return Unknown, errors.Wrap(err, "failed to receive operator deployment")
}

// Check if operator was deployed by Helm
if val, _ := operatorDeployment.GetLabels()["app.kubernetes.io/managed-by"]; val == "Helm" {
installationMethod = Helm
}
return installationMethod, nil
}

func collectDeploymentsSnapshot(ctx context.Context, operatorClusterMgr manager.Manager, operatorUUID, mongodbImage, databaseNonStaticImage string) []Event {
var events []Event
operatorClusterClient := operatorClusterMgr.GetClient()
Expand Down
20 changes: 15 additions & 5 deletions pkg/telemetry/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (

// OperatorUsageSnapshotProperties represents the structure for tracking Kubernetes operator usage events.
type OperatorUsageSnapshotProperties struct {
KubernetesClusterID string `json:"kubernetesClusterID"` // Kubernetes cluster ID where the operator is running
KubernetesClusterIDs []string `json:"kubernetesClusterIDs"` // Sorted Kubernetes cluster IDs the operator is managing
OperatorID string `json:"operatorID"` // Operator UUID
OperatorVersion string `json:"operatorVersion"` // Version of the operator
OperatorType OperatorType `json:"operatorType"` // MEKO, MCK, MCO (here meko)
KubernetesClusterID string `json:"kubernetesClusterID"` // Kubernetes cluster ID where the operator is running
KubernetesClusterIDs []string `json:"kubernetesClusterIDs"` // Sorted Kubernetes cluster IDs the operator is managing
OperatorID string `json:"operatorID"` // Operator UUID
OperatorVersion string `json:"operatorVersion"` // Version of the operator
OperatorType OperatorType `json:"operatorType"` // MEKO, MCK, MCO (here meko)
OperatorInstallationMethod OperatorInstallationMethod `json:"operatorInstallationMethod"`
}

// KubernetesClusterUsageSnapshotProperties represents the structure for tracking Kubernetes cluster usage events.
Expand Down Expand Up @@ -49,6 +50,15 @@ const (
MEKO OperatorType = "MEKO"
)

type OperatorInstallationMethod string

// TODO: add which versions used for Helm and OLM?
const (
Unknown OperatorInstallationMethod = "Unknown"
OLM OperatorInstallationMethod = "OLM"
Helm OperatorInstallationMethod = "Helm"
)

type EventType string

const (
Expand Down
1 change: 1 addition & 0 deletions pkg/util/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ const (
AutomationAgentImagePullPolicy = "IMAGE_PULL_POLICY"
ImagePullSecrets = "IMAGE_PULL_SECRETS" //nolint
OmOperatorEnv = "OPERATOR_ENV"
DeploymentName = "MDB_OPERATOR_NAME"
MemberListConfigMapName = OperatorName + "-member-list"
BackupDisableWaitSecondsEnv = "BACKUP_WAIT_SEC"
BackupDisableWaitRetriesEnv = "BACKUP_WAIT_RETRIES"
Expand Down
2 changes: 2 additions & 0 deletions scripts/dev/prepare_local_e2e_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ if [[ "${DEPLOY_OPERATOR:-"false"}" == "true" ]]; then
helm_values+=" operator.replicas=0"
fi

set -x
# shellcheck disable=SC2128
helm upgrade --install mongodb-enterprise-operator helm_chart --set "$(echo "${helm_values}" | tr ' ' ',')"
set +x
fi

if [[ "${KUBE_ENVIRONMENT_NAME}" == "kind" ]]; then
Expand Down
17 changes: 17 additions & 0 deletions scripts/dev/setup_kind_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ if [[ "${RUNNING_IN_EVG:-false}" == "true" ]]; then
registry="268558157000.dkr.ecr.eu-west-1.amazonaws.com/docker-hub-mirrors"
fi

set -x
if [ "${KUBE_ENVIRONMENT_NAME}" = "performance" ]; then
echo "installing kind with more nodes with performance"
cat <<EOF | kind create cluster --name "${cluster_name}" --kubeconfig "${kubeconfig_path}" --wait 700s --config=-
Expand All @@ -114,31 +115,43 @@ nodes:
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ${HOME}/.docker/config.json
- containerPath: /usr/bin/mongodb-enterprise-operator
hostPath: ${HOME}/ops-manager-kubernetes/bin/mongodb-enterprise-operator
- role: control-plane
image: ${registry}/kindest/node:${kind_node_version}
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ${HOME}/.docker/config.json
- containerPath: /usr/bin/mongodb-enterprise-operator
hostPath: ${HOME}/ops-manager-kubernetes/bin/mongodb-enterprise-operator
- role: control-plane
image: ${registry}/kindest/node:${kind_node_version}
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ${HOME}/.docker/config.json
- containerPath: /usr/bin/mongodb-enterprise-operator
hostPath: ${HOME}/ops-manager-kubernetes/bin/mongodb-enterprise-operator
- role: worker
image: ${registry}/kindest/node:${kind_node_version}
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ${HOME}/.docker/config.json
- containerPath: /usr/bin/mongodb-enterprise-operator
hostPath: ${HOME}/ops-manager-kubernetes/bin/mongodb-enterprise-operator
- role: worker
image: ${registry}/kindest/node:${kind_node_version}
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ${HOME}/.docker/config.json
- containerPath: /usr/bin/mongodb-enterprise-operator
hostPath: ${HOME}/ops-manager-kubernetes/bin/mongodb-enterprise-operator
- role: worker
image: ${registry}/kindest/node:${kind_node_version}
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ${HOME}/.docker/config.json
- containerPath: /usr/bin/mongodb-enterprise-operator
hostPath: ${HOME}/ops-manager-kubernetes/bin/mongodb-enterprise-operator
networking:
podSubnet: "${pod_network}"
serviceSubnet: "${service_network}"
Expand All @@ -163,6 +176,8 @@ nodes:
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ${HOME}/.docker/config.json
- containerPath: /usr/bin/mongodb-enterprise-operator
hostPath: ${HOME}/ops-manager-kubernetes/bin/mongodb-enterprise-operator
networking:
podSubnet: "${pod_network}"
serviceSubnet: "${service_network}"
Expand All @@ -179,6 +194,8 @@ containerdConfigPatches:
EOF
fi

set +x

echo "finished installing kind"

# connect the registry to the cluster network if not already connected
Expand Down
4 changes: 1 addition & 3 deletions scripts/funcs/operator_deployment
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env bash

set -Eeou pipefail

get_operator_helm_values() {
# shellcheck disable=SC2153
local database_registry=${DATABASE_REGISTRY}
Expand Down Expand Up @@ -48,7 +46,7 @@ get_operator_helm_values() {
fi

# shellcheck disable=SC2154
if [[ "${KUBE_ENVIRONMENT_NAME-}" = "multi" ]]; then
if [[ "${KUBE_ENVIRONMENT_NAME-}" == "multi" ]]; then
comma_separated_list="$(echo "${MEMBER_CLUSTERS}" | tr ' ' ',')"
# shellcheck disable=SC2154
config+=("multiCluster.clusters={${comma_separated_list}}")
Expand Down