Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -53,5 +53,11 @@ public class AppInsightsForKubernetesOptions
/// Default to false and look into totally get rid of it in the future.
/// </summary>
public bool OverwriteSDKVersion { get; set; }

/// <summary>
/// Exclude node information from cluster info by skipping calls to the nodes endpoint.
/// Default is false.
/// </summary>
public bool ExcludeNodeInformation { get; set; }
}
}
19 changes: 14 additions & 5 deletions src/ApplicationInsights.Kubernetes/K8sEnvironmentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Microsoft.ApplicationInsights.Kubernetes.Containers;
using Microsoft.ApplicationInsights.Kubernetes.Debugging;
using Microsoft.ApplicationInsights.Kubernetes.Pods;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Microsoft.ApplicationInsights.Kubernetes;

Expand All @@ -21,17 +23,20 @@ internal class K8sEnvironmentFactory : IK8sEnvironmentFactory
private readonly IPodInfoManager _podInfoManager;
private readonly IContainerStatusManager _containerStatusManager;
private readonly IK8sClientService _k8sClient;
private readonly IOptions<AppInsightsForKubernetesOptions> _options;

public K8sEnvironmentFactory(
IContainerIdHolder containerIdHolder,
IPodInfoManager podInfoManager,
IContainerStatusManager containerStatusManager,
IK8sClientService k8sClient)
IK8sClientService k8sClient,
IOptions<AppInsightsForKubernetesOptions> options)
{
_containerIdHolder = containerIdHolder ?? throw new ArgumentNullException(nameof(containerIdHolder));
_podInfoManager = podInfoManager ?? throw new ArgumentNullException(nameof(podInfoManager));
_containerStatusManager = containerStatusManager ?? throw new ArgumentNullException(nameof(containerStatusManager));
_k8sClient = k8sClient ?? throw new ArgumentNullException(nameof(k8sClient));
_options = options ?? throw new ArgumentNullException(nameof(options));
}

/// <summary>
Expand All @@ -57,10 +62,14 @@ public K8sEnvironmentFactory(
V1Deployment? deployment = replicaSet?.GetMyDeployment(allDeployment);

// Fetch node info
string nodeName = myPod.Spec.NodeName;
IEnumerable<V1Node> allNodes = await _k8sClient.GetNodesAsync(ignoreForbiddenException: true, cancellationToken).ConfigureAwait(false);
V1Node? node = allNodes.FirstOrDefault(n => string.Equals(n.Metadata.Name, nodeName, StringComparison.Ordinal));

V1Node? node = null;
if (!_options.Value.ExcludeNodeInformation)
{
string nodeName = myPod.Spec.NodeName;
IEnumerable<V1Node> allNodes = await _k8sClient.GetNodesAsync(ignoreForbiddenException: true, cancellationToken).ConfigureAwait(false);
node = allNodes.FirstOrDefault(n => string.Equals(n.Metadata.Name, nodeName, StringComparison.Ordinal));
}

K8sEnvironment k8SEnvironment = new K8sEnvironment(containerStatus, myPod, replicaSet, deployment, node);
_logger.LogDebug(JsonSerializer.Serialize<K8sEnvironment>(k8SEnvironment).EscapeForLoggingMessage());
return k8SEnvironment;
Expand Down
13 changes: 10 additions & 3 deletions tests/UnitTests/K8sEnvironemntFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using k8s.Models;
using Microsoft.ApplicationInsights.Kubernetes.Containers;
using Microsoft.ApplicationInsights.Kubernetes.Pods;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;

Expand All @@ -18,6 +20,7 @@ public async Task ShouldTimeoutWaitingPodReady()
Mock<IPodInfoManager> podInfoManagerMock = new();
Mock<IContainerStatusManager> containerStatusManagerMock = new();
Mock<IK8sClientService> k8sClientServiceMock = new();
Mock<IOptions<AppInsightsForKubernetesOptions>> appInsightsForKubernetesOptionsMock = new();

// Timeout
TimeSpan timeout = TimeSpan.FromMilliseconds(1);
Expand All @@ -34,8 +37,9 @@ public async Task ShouldTimeoutWaitingPodReady()
}
});

K8sEnvironmentFactory target = new K8sEnvironmentFactory(containerIdHolderMock.Object, podInfoManagerMock.Object, containerStatusManagerMock.Object, k8sClientServiceMock.Object);

K8sEnvironmentFactory target = new K8sEnvironmentFactory(containerIdHolderMock.Object,
podInfoManagerMock.Object, containerStatusManagerMock.Object, k8sClientServiceMock.Object,
appInsightsForKubernetesOptionsMock.Object);
IK8sEnvironment environment = null;
CancellationToken timeoutToken;
using (CancellationTokenSource timeoutSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(1)))
Expand All @@ -56,6 +60,7 @@ public async Task ShouldTimeoutWaitingContainerReady()
Mock<IPodInfoManager> podInfoManagerMock = new();
Mock<IContainerStatusManager> containerStatusManagerMock = new();
Mock<IK8sClientService> k8sClientServiceMock = new();
Mock<IOptions<AppInsightsForKubernetesOptions>> appInsightsForKubernetesOptionsMock = new();

// Timeout
TimeSpan timeout = TimeSpan.FromMilliseconds(1);
Expand All @@ -76,7 +81,9 @@ public async Task ShouldTimeoutWaitingContainerReady()
}
});

K8sEnvironmentFactory target = new K8sEnvironmentFactory(containerIdHolderMock.Object, podInfoManagerMock.Object, containerStatusManagerMock.Object, k8sClientServiceMock.Object);
K8sEnvironmentFactory target = new K8sEnvironmentFactory(containerIdHolderMock.Object,
podInfoManagerMock.Object, containerStatusManagerMock.Object, k8sClientServiceMock.Object,
appInsightsForKubernetesOptionsMock.Object);

IK8sEnvironment environment = null;
CancellationToken timeoutToken;
Expand Down