Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ public class PodPatcher : IPodPatcher
private readonly IClusterIdState _clusterIdState;
private readonly OperatorOptions _operatorOptions;
private readonly InitContainerOptions _initOptions;
private readonly TelemetryOptions _telemetryOptions;
private readonly IAgentInjectionTypeConverter _agentTypeConverter;

public PodPatcher(Func<IEnumerable<IAgentPatcher>> patchersFactory, IGlobMatcher globMatcher,
IClusterIdState clusterIdState, OperatorOptions operatorOptions, InitContainerOptions initOptions,
IAgentInjectionTypeConverter agentTypeConverter)
TelemetryOptions telemetryOptions, IAgentInjectionTypeConverter agentTypeConverter)
{
_patchersFactory = patchersFactory;
_globMatcher = globMatcher;
_clusterIdState = clusterIdState;
_operatorOptions = operatorOptions;
_initOptions = initOptions;
_telemetryOptions = telemetryOptions;
_agentTypeConverter = agentTypeConverter;
}

Expand Down Expand Up @@ -316,6 +318,12 @@ private IEnumerable<V1EnvVar> GenerateEnvVars(PatchingContext context, V1Pod pod
yield return new V1EnvVar { Name = "CONTRAST_MOUNT_AGENT_PATH", Value = agentMountPath };
yield return new V1EnvVar { Name = "CONTRAST_MOUNT_WRITABLE_PATH", Value = writableMountPath };

//If opt-out is set on the operator we should opt-out the agents
if (_telemetryOptions.OptOut)
{
yield return new V1EnvVar { Name = "CONTRAST_AGENT_TELEMETRY_OPTOUT", Value = "1" };
}

if (connection.TeamServerUri != null)
{
yield return new V1EnvVar { Name = "CONTRAST__API__URL", Value = connection.TeamServerUri };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,20 @@ public class ClusterIdHandler : INotificationHandler<EntityReconciled<V1Secret>>

private readonly IClusterIdWriter _clusterIdWriter;
private readonly IClusterIdState _state;
private readonly ITelemetryOptOut _optOut;
private readonly TelemetryOptions _options;
private readonly TelemetryOptions _telemetryOptions;

public ClusterIdHandler(IClusterIdWriter clusterIdWriter, IClusterIdState state, ITelemetryOptOut optOut, TelemetryOptions options)
public ClusterIdHandler(IClusterIdWriter clusterIdWriter, IClusterIdState state, TelemetryOptions options)
{
_clusterIdWriter = clusterIdWriter;
_state = state;
_optOut = optOut;
_options = options;
_telemetryOptions = options;
}

public Task Handle(EntityReconciled<V1Secret> notification, CancellationToken cancellationToken)
{
if (!_optOut.IsOptOutActive()
&& string.Equals(notification.Entity.Name(), _options.ClusterIdSecretName, StringComparison.OrdinalIgnoreCase)
&& string.Equals(notification.Entity.Namespace(), _options.ClusterIdSecretNamespace, StringComparison.OrdinalIgnoreCase))
if (!_telemetryOptions.OptOut
&& string.Equals(notification.Entity.Name(), _telemetryOptions.ClusterIdSecretName, StringComparison.OrdinalIgnoreCase)
&& string.Equals(notification.Entity.Namespace(), _telemetryOptions.ClusterIdSecretNamespace, StringComparison.OrdinalIgnoreCase))
{
var clusterId = _clusterIdWriter.ParseClusterId(notification.Entity);
if (clusterId != null)
Expand All @@ -54,7 +52,7 @@ public Task Handle(EntityReconciled<V1Secret> notification, CancellationToken ca

public async Task Handle(LeaderStateChanged notification, CancellationToken cancellationToken)
{
if (!_optOut.IsOptOutActive()
if (!_telemetryOptions.OptOut
&& notification.IsLeader)
{
var stopwatch = Stopwatch.StartNew();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Contrast.K8s.AgentOperator.Core.Telemetry.Cluster;
using Contrast.K8s.AgentOperator.Core.Telemetry.Models;
using Contrast.K8s.AgentOperator.Options;
using JetBrains.Annotations;
using Microsoft.Extensions.Hosting;
using NLog;
Expand All @@ -16,20 +17,20 @@ namespace Contrast.K8s.AgentOperator.Core.Telemetry.Services.Exceptions;
public class TelemetryExceptionWorker : BackgroundService
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ITelemetryOptOut _optOut;
private readonly TelemetryOptions _options;
private readonly TelemetryService _telemetryService;
private readonly IClusterIdState _clusterIdState;

public TelemetryExceptionWorker(ITelemetryOptOut optOut, TelemetryService telemetryService, IClusterIdState clusterIdState)
public TelemetryExceptionWorker(TelemetryOptions options, TelemetryService telemetryService, IClusterIdState clusterIdState)
{
_optOut = optOut;
_options = options;
_telemetryService = telemetryService;
_clusterIdState = clusterIdState;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (_optOut.IsOptOutActive())
if (_options.OptOut)
{
await Task.Delay(Timeout.Infinite, stoppingToken);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Contrast.K8s.AgentOperator.Core.Telemetry.Cluster;
using Contrast.K8s.AgentOperator.Core.Telemetry.Getters;
using Contrast.K8s.AgentOperator.Core.Telemetry.Models;
using Contrast.K8s.AgentOperator.Options;
using JetBrains.Annotations;
using Microsoft.Extensions.Hosting;
using NLog;
Expand All @@ -17,19 +18,19 @@ namespace Contrast.K8s.AgentOperator.Core.Telemetry.Services.Metrics;
public class TelemetryMetricsWorker : BackgroundService
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ITelemetryOptOut _optOut;
private readonly TelemetryOptions _options;
private readonly IClusterIdState _clusterIdState;
private readonly TelemetryService _telemetryService;
private readonly StatusReportGenerator _statusReportGenerator;
private readonly IsPublicTelemetryBuildGetter _isPublicTelemetryBuildGetter;

public TelemetryMetricsWorker(ITelemetryOptOut optOut,
public TelemetryMetricsWorker(TelemetryOptions options,
IClusterIdState clusterIdState,
TelemetryService telemetryService,
StatusReportGenerator statusReportGenerator,
IsPublicTelemetryBuildGetter isPublicTelemetryBuildGetter)
{
_optOut = optOut;
_options = options;
_clusterIdState = clusterIdState;
_telemetryService = telemetryService;
_statusReportGenerator = statusReportGenerator;
Expand All @@ -43,7 +44,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Logger.Warn("This instance is not running a public build.");
}

if (_optOut.IsOptOutActive())
if (_options.OptOut)
{
await Task.Delay(Timeout.Infinite, stoppingToken);
return;
Expand Down

This file was deleted.

5 changes: 4 additions & 1 deletion src/Contrast.K8s.AgentOperator/Modules/OptionsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,11 @@ protected override void Load(ContainerBuilder builder)
logger.LogOptionValue("install-source", installSource, installSourceStr);
installSource = installSourceStr;
}
var optOutOld = GetEnvironmentOptionFlag(logger, "CONTRAST_DOTNET_TELEMETRY_OPTOUT", "telemetry-opt-out", false);
var optOutNew = GetEnvironmentOptionFlag(logger, "CONTRAST_AGENT_TELEMETRY_OPTOUT", "telemetry-opt-out", false);
var telemetryOptOut = optOutOld || optOutNew;

return new TelemetryOptions("contrast-cluster-id", @namespace, installSource);
return new TelemetryOptions(telemetryOptOut, "contrast-cluster-id", @namespace, installSource);
}).SingleInstance();

builder.Register(context =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Contrast.K8s.AgentOperator.Options;

public record TelemetryOptions(string ClusterIdSecretName,
public record TelemetryOptions(bool OptOut,
string ClusterIdSecretName,
string ClusterIdSecretNamespace,
string InstallSource);
Loading