Skip to content

Commit

Permalink
Added extra vars for the app secret (optional)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Oct 22, 2021
1 parent 878de95 commit aefe5fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
12 changes: 7 additions & 5 deletions Ubiquitous.AutoDevOps.Stack/AutoDevOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable InvertIf

namespace Ubiquitous.AutoDevOps.Stack;
namespace Ubiquitous.AutoDevOps.Stack;

/// <summary>
/// A set of Kubernetes resources, which closely replicate what
Expand All @@ -38,6 +38,7 @@ public class AutoDevOps {
/// <param name="namespaceAnnotations">Optional: namespace annotations</param>
/// <param name="deployExtras">Optional: use this to deploy other resources to the environment namespace,
/// before the application is deployed.</param>
/// <param name="extraAppVars">Additional data for the application secret</param>
/// <param name="provider">Optional: custom Kubernetes resource provider</param>
public AutoDevOps(
AutoDevOpsSettings settings,
Expand All @@ -50,6 +51,7 @@ public AutoDevOps(
Dictionary<string, string>? ingressAnnotations = null,
Dictionary<string, string>? namespaceAnnotations = null,
Action<Namespace>? deployExtras = null,
Dictionary<string, string>? extraAppVars = null,
ProviderResource? provider = null
) {
var @namespace = KubeNamespace.Create(settings.Deploy.Namespace, namespaceAnnotations, provider);
Expand All @@ -64,20 +66,20 @@ public AutoDevOps(
: settings.Deploy.Replicas;

if (replicas == 0) {
DeploymentResult = new Result {Namespace = @namespace};
DeploymentResult = new Result { Namespace = @namespace };
return;
}

ResourceName.SetBaseName(settings.Application.Name);
PulumiName.SetBaseName(settings.Application.Name);

var appSecret = KubeSecret.CreateAppSecret(@namespace, "appsecret", settings, provider);
var appSecret = KubeSecret.CreateAppSecret(@namespace, "appsecret", settings, extraAppVars, provider);

var deployment = KubeDeployment.Create(
@namespace,
"deployment",
settings.Application,
settings.Deploy with {Replicas = replicas},
settings.Deploy with { Replicas = replicas },
settings.GitLab,
imagePullSecret,
appSecret,
Expand Down
26 changes: 17 additions & 9 deletions Ubiquitous.AutoDevOps.Stack/Resources/KubeSecret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public static class KubeSecret {
/// <param name="namespace">Namespace, where the secret should be created</param>
/// <param name="resourceName">Resource name</param>
/// <param name="settings">AutoDevOps settings</param>
/// <param name="extraData">Additional variables to add to the secret</param>
/// <param name="providerResource">Optional: customer Kubernetes provider</param>
/// <returns></returns>
public static Secret? CreateAppSecret(
Namespace @namespace,
ResourceName resourceName,
AutoDevOpsSettings settings,
ProviderResource? providerResource = null
Namespace @namespace,
ResourceName resourceName,
AutoDevOpsSettings settings,
Dictionary<string, string>? extraData = null,
ProviderResource? providerResource = null
) {
var env = GetEnvironmentVariables();

Expand All @@ -31,10 +33,10 @@ public static class KubeSecret {
#pragma warning disable 8605
foreach (DictionaryEntry entry in env) {
#pragma warning restore 8605
var key = (string) entry.Key;
var key = (string)entry.Key;

if (key.StartsWith("K8S_SECRET_") && entry.Value != null)
vars[key.Remove(0, 11)] = (string) entry.Value;
vars[key.Remove(0, 11)] = (string)entry.Value;
}

if (settings.Env != null) {
Expand All @@ -43,6 +45,12 @@ public static class KubeSecret {
}
}

if (extraData != null) {
foreach (var (key, value) in extraData) {
vars.Add(key, value);
}
}

if (vars.Count == 0) return null;

return new Secret(
Expand All @@ -52,7 +60,7 @@ public static class KubeSecret {
Type = "opaque",
StringData = vars
},
new CustomResourceOptions {Provider = providerResource}
new CustomResourceOptions { Provider = providerResource }
);
}

Expand All @@ -77,7 +85,7 @@ public static Secret CreateAppSecret(
Type = "opaque",
StringData = variables
},
new CustomResourceOptions {Provider = providerResource}
new CustomResourceOptions { Provider = providerResource }
);

/// <summary>
Expand Down Expand Up @@ -105,7 +113,7 @@ public static Secret CreateRegistrySecret(
new SecretArgs {
Metadata = Meta.GetMeta(secretName, @namespace.GetName()),
Type = "kubernetes.io/dockerconfigjson",
Data = new InputMap<string> {{".dockerconfigjson", content}}
Data = new InputMap<string> { { ".dockerconfigjson", content } }
},
new CustomResourceOptions {
Provider = providerResource
Expand Down

0 comments on commit aefe5fe

Please sign in to comment.