/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import * as fs from 'fs';
import { diag } from '@opentelemetry/api';
import { ATTR_CLOUD_PLATFORM, ATTR_CLOUD_PROVIDER, ATTR_K8S_CLUSTER_NAME, CLOUD_PLATFORM_VALUE_AZURE_AKS, CLOUD_PROVIDER_VALUE_AZURE, } from '../semconv';
import { AKS_CLUSTER_RESOURCE_ID, AKS_METADATA_FILE_PATH, CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE, extractClusterNameFromResourceId, } from '../types';
/**
 * The AzureAksDetector can be used to detect if a process is running in an Azure Kubernetes Service (AKS) cluster.
 * It reads cluster metadata from environment variables populated from the aks-cluster-metadata ConfigMap
 * in the kube-public namespace, or from the ConfigMap file if mounted.
 *
 * The ConfigMap contains a single key 'clusterResourceId' with the full ARM resource ID.
 * The cluster name is extracted from this resource ID.
 *
 * @returns a {@link Resource} populated with data about the AKS environment or an empty Resource if detection fails.
 */
class AzureAksDetector {
    detect() {
        let attributes = {};
        const metadata = this.getAksMetadata();
        if (metadata && (metadata.name || metadata.resourceId)) {
            attributes = {
                [ATTR_CLOUD_PROVIDER]: CLOUD_PROVIDER_VALUE_AZURE,
                [ATTR_CLOUD_PLATFORM]: CLOUD_PLATFORM_VALUE_AZURE_AKS,
            };
            if (metadata.name) {
                attributes = {
                    ...attributes,
                    [ATTR_K8S_CLUSTER_NAME]: metadata.name,
                };
            }
            if (metadata.resourceId) {
                attributes = {
                    ...attributes,
                    [CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE]: metadata.resourceId,
                };
            }
            diag.debug('AzureAksDetector: detected AKS cluster:', metadata);
        }
        return { attributes };
    }
    getAksMetadata() {
        // Try environment variable first (populated from aks-cluster-metadata ConfigMap)
        const clusterResourceId = process.env[AKS_CLUSTER_RESOURCE_ID];
        if (clusterResourceId) {
            return {
                name: extractClusterNameFromResourceId(clusterResourceId),
                resourceId: clusterResourceId,
            };
        }
        // Fall back to reading from mounted ConfigMap file
        return this.getAksMetadataFromFile();
    }
    getAksMetadataFromFile() {
        try {
            if (!fs.existsSync(AKS_METADATA_FILE_PATH)) {
                return undefined;
            }
            const content = fs.readFileSync(AKS_METADATA_FILE_PATH, 'utf8');
            const metadata = {};
            // Parse the ConfigMap file content (key=value format)
            // The native aks-cluster-metadata ConfigMap has a single key: clusterResourceId
            const lines = content.split('\n');
            for (const line of lines) {
                const trimmedLine = line.trim();
                if (!trimmedLine || trimmedLine.startsWith('#')) {
                    continue;
                }
                const [key, ...valueParts] = trimmedLine.split('=');
                const value = valueParts.join('=').trim();
                if (key === 'clusterResourceId' && value) {
                    metadata.resourceId = value;
                    metadata.name = extractClusterNameFromResourceId(value);
                }
            }
            if (metadata.resourceId) {
                return metadata;
            }
        }
        catch (err) {
            diag.debug('AzureAksDetector: failed to read AKS metadata file:', err.message);
        }
        return undefined;
    }
}
export const azureAksDetector = new AzureAksDetector();
//# sourceMappingURL=AzureAksDetector.js.map