From 7624eb3a0f42285f65533f37ec61ecb45c9cb8ef Mon Sep 17 00:00:00 2001 From: Mark Lopez Date: Wed, 25 Jan 2023 14:48:35 -0600 Subject: [PATCH] Improved error message when no agent connection is specified. --- .../Core/State/Appliers/AgentInjectorApplier.cs | 10 ++++++---- .../Primitives/AgentInjectorConnectionReference.cs | 2 +- .../Core/State/StateContainerExtensions.cs | 6 +++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Contrast.K8s.AgentOperator/Core/State/Appliers/AgentInjectorApplier.cs b/src/Contrast.K8s.AgentOperator/Core/State/Appliers/AgentInjectorApplier.cs index d80605ab..929ec016 100644 --- a/src/Contrast.K8s.AgentOperator/Core/State/Appliers/AgentInjectorApplier.cs +++ b/src/Contrast.K8s.AgentOperator/Core/State/Appliers/AgentInjectorApplier.cs @@ -48,13 +48,15 @@ public override async ValueTask CreateFrom(V1Beta1AgentIn ); var selector = GetSelector(spec, @namespace); - var connectionName = spec.Connection?.Name ?? _clusterDefaults.GetDefaultAgentConnectionName(@namespace); - var connectionReference = new AgentInjectorConnectionReference(@namespace, connectionName); + var namespaceDefaultConnectionName = _clusterDefaults.GetDefaultAgentConnectionName(@namespace); + var connectionName = spec.Connection?.Name ?? namespaceDefaultConnectionName; + var isConnectionNamespaceDefault = connectionName == namespaceDefaultConnectionName; + var connectionReference = new AgentInjectorConnectionReference(@namespace, connectionName, isConnectionNamespaceDefault); var namespaceDefaultConfigurationName = _clusterDefaults.GetDefaultAgentConfigurationName(@namespace); var configurationName = spec.Configuration?.Name ?? namespaceDefaultConfigurationName; - var isNamespaceDefault = namespaceDefaultConfigurationName == configurationName; - var configurationReference = new AgentConfigurationReference(@namespace, configurationName, isNamespaceDefault); + var isConfigurationNamespaceDefault = namespaceDefaultConfigurationName == configurationName; + var configurationReference = new AgentConfigurationReference(@namespace, configurationName, isConfigurationNamespaceDefault); var pullSecretName = spec.Image.PullSecretName != null ? new SecretReference(@namespace, spec.Image.PullSecretName, ".dockerconfigjson") : null; var pullPolicy = spec.Image.PullPolicy ?? "Always"; diff --git a/src/Contrast.K8s.AgentOperator/Core/State/Resources/Primitives/AgentInjectorConnectionReference.cs b/src/Contrast.K8s.AgentOperator/Core/State/Resources/Primitives/AgentInjectorConnectionReference.cs index 33b9af7e..48755d34 100644 --- a/src/Contrast.K8s.AgentOperator/Core/State/Resources/Primitives/AgentInjectorConnectionReference.cs +++ b/src/Contrast.K8s.AgentOperator/Core/State/Resources/Primitives/AgentInjectorConnectionReference.cs @@ -3,5 +3,5 @@ namespace Contrast.K8s.AgentOperator.Core.State.Resources.Primitives { - public record AgentInjectorConnectionReference(string Namespace, string Name); + public record AgentInjectorConnectionReference(string Namespace, string Name, bool IsNamespaceDefault); } diff --git a/src/Contrast.K8s.AgentOperator/Core/State/StateContainerExtensions.cs b/src/Contrast.K8s.AgentOperator/Core/State/StateContainerExtensions.cs index c978df2e..558d292c 100644 --- a/src/Contrast.K8s.AgentOperator/Core/State/StateContainerExtensions.cs +++ b/src/Contrast.K8s.AgentOperator/Core/State/StateContainerExtensions.cs @@ -35,6 +35,7 @@ public static async ValueTask> GetReadyAgentI && await state.GetReadyAgentConnectionById( connectionRef.Name, connectionRef.Namespace, + connectionRef.IsNamespaceDefault, context, cancellationToken ) != null; @@ -76,6 +77,7 @@ public static async ValueTask> GetReadyAgentI private static async ValueTask GetReadyAgentConnectionById(this IStateContainer state, string name, string @namespace, + bool isNamespaceDefault, ReadyContext readyContext, CancellationToken cancellationToken = default) { @@ -112,7 +114,9 @@ public static async ValueTask> GetReadyAgentI } else { - readyContext.AddFailureReason($"AgentConnection '{@namespace}/{name}' could not be found."); + readyContext.AddFailureReason(isNamespaceDefault + ? "AgentConnection was not specified, and no cluster default was found." + : $"AgentConnection '{@namespace}/{name}' could not be found."); } return null;