diff --git a/Makefile b/Makefile index db87323df..8b0c91e3f 100644 --- a/Makefile +++ b/Makefile @@ -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 ubuntu@simon-baeumer-089.workstations.build.10gen.cc:/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 diff --git a/helm_chart/templates/operator-roles.yaml b/helm_chart/templates/operator-roles.yaml index dace37467..a1102a46c 100644 --- a/helm_chart/templates/operator-roles.yaml +++ b/helm_chart/templates/operator-roles.yaml @@ -18,6 +18,13 @@ metadata: namespace: {{ include "mongodb-kubernetes-operator.namespace" . }} {{- end }} rules: + - apiGroups: + - apps + resources: + - deployments + verbs: + - get + - list - apiGroups: - '' resources: diff --git a/helm_chart/templates/operator.yaml b/helm_chart/templates/operator.yaml index 8d523fbd1..934ae1e10 100644 --- a/helm_chart/templates/operator.yaml +++ b/helm_chart/templates/operator.yaml @@ -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 }} @@ -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 @@ -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 diff --git a/pkg/telemetry/collector.go b/pkg/telemetry/collector.go index 72ae8996d..b85ee738f 100644 --- a/pkg/telemetry/collector.go +++ b/pkg/telemetry/collector.go @@ -2,6 +2,7 @@ package telemetry import ( "context" + "github.com/pkg/errors" "slices" "strings" "time" @@ -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) @@ -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 { @@ -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() diff --git a/pkg/telemetry/types.go b/pkg/telemetry/types.go index b57e4d260..7058cee33 100644 --- a/pkg/telemetry/types.go +++ b/pkg/telemetry/types.go @@ -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. @@ -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 ( diff --git a/pkg/util/constants.go b/pkg/util/constants.go index 86219ee05..4c9befc1a 100644 --- a/pkg/util/constants.go +++ b/pkg/util/constants.go @@ -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" diff --git a/scripts/dev/prepare_local_e2e_run.sh b/scripts/dev/prepare_local_e2e_run.sh index 3f2d7d15c..42044f36c 100755 --- a/scripts/dev/prepare_local_e2e_run.sh +++ b/scripts/dev/prepare_local_e2e_run.sh @@ -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 diff --git a/scripts/dev/setup_kind_cluster.sh b/scripts/dev/setup_kind_cluster.sh index c1325a249..51590c867 100755 --- a/scripts/dev/setup_kind_cluster.sh +++ b/scripts/dev/setup_kind_cluster.sh @@ -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 <