Skip to content
New issue

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

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

Already on GitHub? # to your account

Added fallback method select container by name #347

Merged
merged 2 commits into from
Mar 30, 2023
Merged
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
45 changes: 33 additions & 12 deletions src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using k8s.Models;
using Microsoft.ApplicationInsights.Kubernetes.Debugging;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -44,24 +45,44 @@ public bool TryBackFillContainerId(V1Pod pod, out V1ContainerStatus? containerSt
}
containerStatus = null;

// If there's no container id provided providers, check to see if there's only 1 container inside the pod
// If there's no container id provided providers
IList<V1ContainerStatus>? containerStatuses = pod.Status?.ContainerStatuses;
if (containerStatuses is not null && containerStatuses.Count == 1)
if (containerStatuses is not null)
{
containerStatus = containerStatuses[0];
_logger.LogInformation(FormattableString.Invariant($"Use the only container inside the pod for container id: {containerStatus.ContainerID}"));

using (IServiceScope scope = _serviceScopeFactory.CreateScope())
// check to see if there's only 1 container inside the pod
// else select container by environment variable.
if (containerStatuses.Count == 1)
{
IContainerIdNormalizer normalizer = scope.ServiceProvider.GetRequiredService<IContainerIdNormalizer>();
if (normalizer.TryNormalize(containerStatus.ContainerID, out string? normalizedContainerId))
{
_containerId = normalizedContainerId;
return true;
containerStatus = containerStatuses[0];
_logger.LogDebug(FormattableString.Invariant($"Use the only container inside the pod for container id: {containerStatus.ContainerID}"));
}
else
{
string? containerName = Environment.GetEnvironmentVariable("ContainerName");
_logger.LogDebug(FormattableString.Invariant($"Select container by environment variable containerName: {containerName}"));
containerStatus = containerStatuses.FirstOrDefault(c => string.Equals(c.Name, containerName, StringComparison.Ordinal));
if (containerStatus is not null)
{
_logger.LogDebug(FormattableString.Invariant($"Selected container by container.name property container id: {containerStatus.ContainerID}"));
}

}
if (containerStatus is not null)
{
_logger.LogInformation(FormattableString.Invariant($"Selected container {containerStatus.Name} container id: {containerStatus.ContainerID}"));

_logger.LogError(FormattableString.Invariant($"Normalization failed for container id: {containerStatus.ContainerID}"));
using (IServiceScope scope = _serviceScopeFactory.CreateScope())
{
IContainerIdNormalizer normalizer = scope.ServiceProvider.GetRequiredService<IContainerIdNormalizer>();
if (normalizer.TryNormalize(containerStatus.ContainerID, out string? normalizedContainerId))
{
_containerId = normalizedContainerId;
return true;
}
}
_logger.LogError(FormattableString.Invariant($"Normalization failed for container id: {containerStatus.ContainerID}"));
}
_logger.LogError(FormattableString.Invariant($"Try back fill ContainerId failed"));
}
return false;
}
Expand Down