Skip to content

Commit

Permalink
Update nullability and inference logic for environment variables (#178)
Browse files Browse the repository at this point in the history
The code has been updated to allow null values in environment variables and secrets dictionaries. It also refactors retrieval methods for both environmental and secret variables. Now, instead of directly calling methods on object instances, it utilizes a pair of the resource key and object as function parameters for greater flexibility. The dashboard injection logic has been updated to avoid duplication. The unit tests have been adapted to reflect these changes.
Otlp service name is now included and set to the resource key from the manifest (same for docker compose) which should fix the display on the dashboard.
  • Loading branch information
prom3theu5 authored May 5, 2024
1 parent 71749a1 commit d5f633f
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 46 deletions.
4 changes: 0 additions & 4 deletions src/Aspirate.Cli/Templates/kustomization.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ configMapGenerator:
{{#if isProject}}
- ASPNETCORE_URLS=http://+:8080;
{{/if}}
{{#if withDashboard}}
- OTEL_EXPORTER_OTLP_ENDPOINT=http://aspire-dashboard:18889
{{/if}}


{{/if}}
{{#if hasAnySecrets}}
Expand Down
34 changes: 8 additions & 26 deletions src/Aspirate.Processors/BaseResourceProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,45 +51,27 @@ protected BaseResourceProcessor(
/// </summary>
/// <param name="resource">The resource whose environmental variables need to be filtered.</param>
/// <param name="disableSecrets">if secrets are disabled, do not filter</param>
/// <param name="withDashboard">Should the dashboard be included.</param>
/// <returns>A dictionary representing the filtered environmental variables.</returns>
protected Dictionary<string, string> GetFilteredEnvironmentalVariables(Resource resource, bool? disableSecrets = false)
protected Dictionary<string, string?> GetFilteredEnvironmentalVariables(KeyValuePair<string, Resource> resource, bool? disableSecrets, bool? withDashboard)
{
if (resource is not IResourceWithEnvironmentalVariables resourceWithEnv)
{
return [];
}
var resourceWithEnv = resource.MapResourceToEnvVars(withDashboard);

if (disableSecrets == true)
{
return resourceWithEnv.Env;
}

var envVars = resourceWithEnv.Env;

return envVars == null ? [] : envVars.Where(e => !ProtectorType.List.Any(p => e.Key.StartsWith(p))).ToDictionary(e => e.Key, e => e.Value);
return disableSecrets == true ? resourceWithEnv : resourceWithEnv.Where(e => !ProtectorType.List.Any(p => e.Key.StartsWith(p))).ToDictionary(e => e.Key, e => e.Value);
}

/// <summary>
/// Filters the environmental variables of a given resource and returns a dictionary of the secret variables.
/// </summary>
/// <param name="resource">The resource from which to retrieve the secret environmental variables.</param>
/// <param name="disableSecrets">if secrets are disabled, do not filter</param>
/// <param name="withDashboard">Should the dashboard be included.</param>
/// <returns>A dictionary representing the secret environmental variables.</returns>
protected Dictionary<string, string> GetSecretEnvironmentalVariables(Resource resource, bool? disableSecrets = false)
protected Dictionary<string, string?> GetSecretEnvironmentalVariables(KeyValuePair<string, Resource> resource, bool? disableSecrets, bool? withDashboard)
{
if (resource is not IResourceWithEnvironmentalVariables resourceWithEnv)
{
return [];
}

if (disableSecrets == true)
{
return [];
}

var envVars = resourceWithEnv.Env;
var resourceWithEnv = resource.MapResourceToEnvVars(withDashboard);

return envVars == null ? [] : envVars.Where(e => ProtectorType.List.Any(p => e.Key.StartsWith(p))).ToDictionary(e => e.Key, e => e.Value);
return resourceWithEnv.Where(e => ProtectorType.List.Any(p => e.Key.StartsWith(p))).ToDictionary(e => e.Key, e => e.Value);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ private KubernetesDeploymentData PopulateKubernetesDeploymentData(BaseKubernetes
.SetName(options.Resource.Key)
.SetContainerImage(container.Image)
.SetImagePullPolicy(options.ImagePullPolicy)
.SetEnv(GetFilteredEnvironmentalVariables(options.Resource.Value, options.DisableSecrets))
.SetEnv(GetFilteredEnvironmentalVariables(options.Resource, options.DisableSecrets, options.WithDashboard))
.SetAnnotations(container.Annotations)
.SetVolumes(container.Volumes.KuberizeVolumeNames(options.Resource))
.SetSecrets(GetSecretEnvironmentalVariables(options.Resource.Value, options.DisableSecrets))
.SetSecrets(GetSecretEnvironmentalVariables(options.Resource, options.DisableSecrets, options.WithDashboard))
.SetSecretsFromSecretState(options.Resource, secretProvider, options.DisableSecrets)
.SetPorts(options.Resource.MapBindingsToPorts())
.SetArgs(container.Args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ private KubernetesDeploymentData PopulateKubernetesDeploymentData(BaseKubernetes
.SetContainerImage(containerImage)
.SetImagePullPolicy(options.ImagePullPolicy)
.SetArgs(dockerFile.Args)
.SetEnv(GetFilteredEnvironmentalVariables(options.Resource.Value, options.DisableSecrets))
.SetEnv(GetFilteredEnvironmentalVariables(options.Resource, options.DisableSecrets, options.WithDashboard))
.SetAnnotations(dockerFile.Annotations)
.SetSecrets(GetSecretEnvironmentalVariables(options.Resource.Value, options.DisableSecrets))
.SetSecrets(GetSecretEnvironmentalVariables(options.Resource, options.DisableSecrets, options.WithDashboard))
.SetSecretsFromSecretState(options.Resource, secretProvider, options.DisableSecrets)
.SetPorts(options.Resource.MapBindingsToPorts())
.SetManifests(_manifests)
Expand Down
4 changes: 2 additions & 2 deletions src/Aspirate.Processors/Resources/Project/ProjectProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ private KubernetesDeploymentData PopulateKubernetesDeploymentData(BaseKubernetes
.SetName(options.Resource.Key)
.SetContainerImage(containerDetails.FullContainerImage)
.SetImagePullPolicy(options.ImagePullPolicy)
.SetEnv(GetFilteredEnvironmentalVariables(options.Resource.Value, options.DisableSecrets))
.SetEnv(GetFilteredEnvironmentalVariables(options.Resource, options.DisableSecrets, options.WithDashboard))
.SetAnnotations(project.Annotations)
.SetArgs(project.Args)
.SetSecrets(GetSecretEnvironmentalVariables(options.Resource.Value, options.DisableSecrets))
.SetSecrets(GetSecretEnvironmentalVariables(options.Resource, options.DisableSecrets, options.WithDashboard))
.SetSecretsFromSecretState(options.Resource, secretProvider, options.DisableSecrets)
.SetIsProject(true)
.SetPorts(options.Resource.MapBindingsToPorts())
Expand Down
5 changes: 3 additions & 2 deletions src/Aspirate.Shared/Extensions/ResourceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public static class ResourceExtensions
environment.Add(entry.Key, entry.Value);
}

if (withDashboard.GetValueOrDefault())
if (withDashboard == true)
{
environment.Add("OTEL_EXPORTER_OTLP_ENDPOINT", "http://aspire-dashboard:18889");
environment.TryAdd("OTEL_EXPORTER_OTLP_ENDPOINT", "http://aspire-dashboard:18889");
environment.TryAdd("OTEL_SERVICE_NAME", resource.Key);
}

return environment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class KubernetesDeploymentData
{
public string? Name {get; private set;}
public string? Namespace {get; private set;}
public Dictionary<string, string>? Env {get; private set;}
public Dictionary<string, string>? Secrets {get; private set;}
public Dictionary<string, string?>? Env {get; private set;}
public Dictionary<string, string?>? Secrets {get; private set;}
public Dictionary<string, string>? Annotations {get; private set;}
public List<Volume>? Volumes {get; private set;}
public IReadOnlyCollection<string>? Manifests {get; private set;}
Expand Down Expand Up @@ -40,13 +40,13 @@ public KubernetesDeploymentData SetNamespace(string? ns)
return this;
}

public KubernetesDeploymentData SetEnv(Dictionary<string, string> env)
public KubernetesDeploymentData SetEnv(Dictionary<string, string?> env)
{
Env = env.Where(x=>!string.IsNullOrEmpty(x.Value)).ToDictionary(x => x.Key, x => x.Value);
return this;
}

public KubernetesDeploymentData SetSecrets(Dictionary<string, string> secrets)
public KubernetesDeploymentData SetSecrets(Dictionary<string, string?> secrets)
{
Secrets = secrets;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void ToKubernetesConfigMap_ShouldReturnCorrectConfigMap()
// Arrange
var data = new KubernetesDeploymentData()
.SetName("test")
.SetEnv(new Dictionary<string, string> { { "key", "value" } });
.SetEnv(new Dictionary<string, string?> { { "key", "value" } });

// Act
var result = data.ToKubernetesConfigMap();
Expand All @@ -55,7 +55,7 @@ public void ToKubernetesSecret_ShouldReturnCorrectSecret()
// Arrange
var data = new KubernetesDeploymentData()
.SetName("test")
.SetSecrets(new Dictionary<string, string> { { "key", "value" } });
.SetSecrets(new Dictionary<string, string?> { { "key", "value" } });

// Act
var result = data.ToKubernetesSecret();
Expand Down Expand Up @@ -121,8 +121,8 @@ public void ToKubernetesObjects_ShouldReturnCorrectObjects()
.SetName("test")
.SetContainerImage("test-image")
.SetPorts(new List<Ports> { new Ports { Name = "test-port", InternalPort = 8080, ExternalPort = 8080 } })
.SetEnv(new Dictionary<string, string> { { "key", "envvalue" } })
.SetSecrets(new Dictionary<string, string> { { "key", "secretvalue" } });
.SetEnv(new Dictionary<string, string?> { { "key", "envvalue" } })
.SetSecrets(new Dictionary<string, string?> { { "key", "secretvalue" } });

// Act
var result = data.ToKubernetesObjects();
Expand Down

0 comments on commit d5f633f

Please sign in to comment.