Skip to content

Commit

Permalink
Refactor to allow base names
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Jul 29, 2021
1 parent a24aba2 commit 427571c
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 69 deletions.
8 changes: 4 additions & 4 deletions Ubiquitous.AutoDevOps.Crds.Traefik/TraefikIngressRoute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ namespace Ubiquitous.AutoDevOps.Crds.Traefik {
[PublicAPI]
public class TraefikIngressRoute {
public TraefikIngressRoute(
string ingressRouteName,
ResourceName ingressRouteName,
Uri environmentUri,
Service service,
Output<string> namespaceName,
string endpoint,
bool tls
) {
InputList<string> endpoints = new() { endpoint };
InputList<string> endpoints = new() {endpoint};
InputList<IngressRouteRouteMiddlewaresArgs> middlewares = new();

if (tls) {
endpoints.Add($"{endpoint}-https");
middlewares.Add(new IngressRouteRouteMiddlewaresArgs { Name = "http-to-https", Namespace = "default" });
middlewares.Add(new IngressRouteRouteMiddlewaresArgs {Name = "http-to-https", Namespace = "default"});
}

IngressRule = new IngressRoute(
ingressRouteName,
ingressRouteName.AsPulumiName(),
new IngressRouteArgs {
Metadata = Meta.GetMeta(ingressRouteName, namespaceName),
Spec = new IngressRouteSpecArgs {
Expand Down
12 changes: 6 additions & 6 deletions Ubiquitous.AutoDevOps.Stack/Addons/Prometheus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ namespace Ubiquitous.AutoDevOps.Stack.Addons {
[PublicAPI]
public static class Prometheus {
public static PodMonitor CreatePodMonitor(
DeploySettings deploySettings,
ResourceName resourceName,
Pulumi.Kubernetes.Apps.V1.Deployment deployment,
Namespace kubeNamespace,
ProviderResource? providerResource = null
)
=> new(
deploySettings.PulumiName("podMonitor"),
resourceName.AsPulumiName(),
new PodMonitorArgs {
Metadata = new ObjectMetaArgs {
Namespace = kubeNamespace.Metadata.Apply(x => x.Name),
Name = deploySettings.ResourceName,
Name = resourceName,
Labels = new InputMap<string> {{"tier", "web"}}
},
Spec = new PodMonitorSpecArgs {
Expand All @@ -44,18 +44,18 @@ public static PodMonitor CreatePodMonitor(
);

public static ServiceMonitor CreateServiceMonitor(
DeploySettings deploySettings,
ResourceName resourceName,
PrometheusSettings prometheusSettings,
Service service,
Namespace kubeNamespace,
ProviderResource? providerResource = null
)
=> new(
deploySettings.PulumiName("serviceMonitor"),
resourceName.AsPulumiName(),
new ServiceMonitorArgs {
Metadata = new ObjectMetaArgs {
Namespace = kubeNamespace.Metadata.Apply(x => x.Name),
Name = deploySettings.ResourceName,
Name = resourceName,
Labels = new InputMap<string> {{"tier", "web"}}
},
Spec = new ServiceMonitorSpecArgs {
Expand Down
18 changes: 13 additions & 5 deletions Ubiquitous.AutoDevOps.Stack/AutoDevOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Collections.Generic;
using Pulumi;
using Pulumi.Kubernetes.Core.V1;
using Pulumi.Kubernetes.Networking.V1;
using Pulumi.Kubernetes.Types.Inputs.Apps.V1;
using Pulumi.Kubernetes.Types.Inputs.Core.V1;
using Ubiquitous.AutoDevOps.Stack.Addons;
using Ubiquitous.AutoDevOps.Stack.Factories;
using Ubiquitous.AutoDevOps.Stack.Resources;
using Deployment = Pulumi.Kubernetes.Apps.V1.Deployment;
using static System.Environment;
using Ingress = Pulumi.Kubernetes.Networking.V1.Ingress;

// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable InvertIf
Expand Down Expand Up @@ -67,11 +68,15 @@ public AutoDevOps(
DeploymentResult = new Result {Namespace = @namespace};
return;
}

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

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

var deployment = KubeDeployment.Create(
@namespace,
"deployment",
settings.Application,
settings.Deploy with {Replicas = replicas},
settings.GitLab,
Expand All @@ -87,12 +92,13 @@ public AutoDevOps(
var service = settings.Service.Enabled
? KubeService.Create(
@namespace,
"service",
settings.Application,
settings.Deploy,
settings.Service,
settings.GitLab,
settings.Prometheus,
deployment,
settings.Prometheus,
serviceAnnotations,
configureService,
provider
Expand All @@ -102,6 +108,8 @@ public AutoDevOps(
var ingress = settings.Ingress.Enabled && !settings.Deploy.Url!.IsEmpty() && service != null
? KubeIngress.Create(
@namespace,
"ingress",
settings.Application,
settings.Deploy,
settings.Ingress,
service,
Expand All @@ -113,10 +121,10 @@ public AutoDevOps(

if (settings.Prometheus.Metrics && settings.Prometheus.Operator) {
if (service != null) {
Prometheus.CreateServiceMonitor(settings.Deploy, settings.Prometheus, service, @namespace, provider);
Prometheus.CreateServiceMonitor("service-monitor", settings.Prometheus, service, @namespace, provider);
}
else {
Prometheus.CreatePodMonitor(settings.Deploy, deployment, @namespace, provider);
Prometheus.CreatePodMonitor("pod-monitor", deployment, @namespace, provider);
}
}

Expand Down
27 changes: 19 additions & 8 deletions Ubiquitous.AutoDevOps.Stack/Factories/Meta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Ubiquitous.AutoDevOps.Stack.Factories {
[PublicAPI]
public static class Meta {
public static ObjectMetaArgs GetMeta(
string name,
ResourceName name,
Input<string> @namespace,
InputMap<string>? annotations = null,
InputMap<string>? labels = null
Expand All @@ -23,18 +23,29 @@ public static ObjectMetaArgs GetMeta(
/// <summary>
/// Gets the default labels, which identify the app and the release
/// </summary>
/// <param name="deploySettings">Deployment settings</param>
/// <param name="appSettings">Application settings</param>
/// <param name="resourceName">Kubernetes resource name</param>
/// <param name="release">Deployment release</param>
/// <returns>An input map with default annotations</returns>
public static InputMap<string> BaseLabels(this DeploySettings deploySettings)
public static InputMap<string> BaseLabels(AppSettings appSettings, ResourceName resourceName, string release)
=> new() {
{"app", deploySettings.ResourceName},
{"release", deploySettings.Release}
{"app", appSettings.Name},
{"component", resourceName},
{"release", release}
};

public static InputMap<string> AppLabels(AppSettings appSettings, DeploySettings deploySettings)
/// <summary>
/// Add default labels following GitLab expectations
/// </summary>
/// <param name="appSettings">Application settings, used for Track, Tier and Version</param>
/// <param name="resourceName">Kubernetes resource name</param>
/// <param name="release">Deployment release</param>
/// <returns></returns>
public static InputMap<string> AppLabels(AppSettings appSettings, string resourceName, string release)
=> new() {
{"app", deploySettings.ResourceName},
{"release", deploySettings.Release},
{"app", appSettings.Name},
{"component", resourceName},
{"release", release},
{"track", appSettings.Track},
{"tier", appSettings.Tier},
{"version", appSettings.Version ?? ""}
Expand Down
46 changes: 42 additions & 4 deletions Ubiquitous.AutoDevOps.Stack/Factories/Names.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
using static Ubiquitous.AutoDevOps.Stack.AutoDevOpsSettings;
using System;
using JetBrains.Annotations;
using Pulumi;

namespace Ubiquitous.AutoDevOps.Stack.Factories {
public static class Names {
public static string PulumiName(this DeploySettings settings, string? resource = null) =>
resource.IsEmpty() ? settings.ResourceName : $"{settings.ResourceName}-{resource}";
[PublicAPI]
public record ResourceName {
public string Value { get; }

public ResourceName(string value) {
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException(nameof(value), "Resource name cannot be empty");
Value = value;
}

public static implicit operator string(ResourceName resourceName) => resourceName.GetName();

public static implicit operator Input<string>(ResourceName resourceName) => resourceName.GetName();

public static implicit operator ResourceName(string value) => new(value);

static string _baseName = "";
public static void SetBaseName(string baseName) => _baseName = baseName;

public PulumiName AsPulumiName() => new(Value);

string GetName() => _baseName.IsEmpty() ? Value : $"{_baseName}-{Value}";
}

[PublicAPI]
public record PulumiName {
public string Value { get; }

public PulumiName(string value) {
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException(nameof(value), "Pulumi resource name cannot be empty");
Value = value;
}

public static implicit operator string(PulumiName resourceName)
=> _baseName.IsEmpty() ? resourceName.Value : $"{_baseName}-{resourceName.Value}";

static string _baseName = "";
public static void SetBaseName(string baseName) => _baseName = baseName;
}
}
6 changes: 5 additions & 1 deletion Ubiquitous.AutoDevOps.Stack/Factories/Pods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Ubiquitous.AutoDevOps.Stack.Factories {
[PublicAPI]
public static class Pods {
public static List<ContainerArgs> GetAppContainers(
ResourceName resourceName,
AppSettings appSettings,
DeploySettings deploySettings,
GitLabSettings gitLabSettings,
Expand All @@ -21,7 +22,7 @@ public static List<ContainerArgs> GetAppContainers(
bool addDefaultEnvVars = true
) {
var container = new ContainerArgs {
Name = deploySettings.ResourceName,
Name = resourceName,
Image = deploySettings.Image,
ImagePullPolicy = deploySettings.ImagePullPolicy,
};
Expand Down Expand Up @@ -129,6 +130,9 @@ public static InputList<LocalObjectReferenceArgs> ImagePullSecrets(params Output

public static EnvVarArgs EnvVar(string name, string value) => new() {Name = name, Value = value};

public static VolumeMountArgs VolumeMount(string name, string mountPath)
=> new() {Name = name, MountPath = mountPath};

public static EnvVarArgs FieldFrom(string envName, string field)
=> new() {
Name = envName,
Expand Down
13 changes: 7 additions & 6 deletions Ubiquitous.AutoDevOps.Stack/Resources/KubeCronJob.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using JetBrains.Annotations;
using Pulumi;
using Pulumi.Kubernetes.Batch.V1Beta1;
using Pulumi.Kubernetes.Core.V1;
using Pulumi.Kubernetes.Types.Inputs.Batch.V1;
Expand All @@ -16,21 +15,23 @@ namespace Ubiquitous.AutoDevOps.Stack.Resources {
public class KubeCronJob {
public static CronJob Create(
Namespace kubens,
ResourceName resourceName,
AppSettings appSettings,
DeploySettings jobSettings,
DeploySettings deploySettings,
GitLabSettings gitLabSettings,
string schedule,
Secret imagePullSecret,
Secret? appSecret,
Action<ContainerArgs>? configureContainer = null,
Action<PodSpecArgs>? configurePod = null
) {
var appLabels = Meta.AppLabels(appSettings, jobSettings);
var appLabels = Meta.AppLabels(appSettings, resourceName, deploySettings.Release);
var gitLabAnnotations = gitLabSettings.GitLabAnnotations();

var containers = Pods.GetAppContainers(
resourceName,
appSettings,
jobSettings,
deploySettings,
gitLabSettings,
appSecret,
configureContainer: cfg => {
Expand All @@ -53,9 +54,9 @@ public static CronJob Create(
);

return new CronJob(
jobSettings.PulumiName("job"),
resourceName.AsPulumiName(),
new CronJobArgs {
Metadata = Meta.GetMeta(appSettings.Name, kubens.GetName(), gitLabAnnotations, appLabels),
Metadata = Meta.GetMeta(resourceName, kubens.GetName(), gitLabAnnotations, appLabels),
Spec = new CronJobSpecArgs {
Schedule = schedule,
ConcurrencyPolicy = "Forbid",
Expand Down
8 changes: 5 additions & 3 deletions Ubiquitous.AutoDevOps.Stack/Resources/KubeDeployment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Ubiquitous.AutoDevOps.Stack.Resources {
public static class KubeDeployment {
public static Deployment Create(
Namespace kubens,
ResourceName resourceName,
AppSettings appSettings,
DeploySettings deploySettings,
GitLabSettings gitLabSettings,
Expand All @@ -24,10 +25,11 @@ public static Deployment Create(
Action<DeploymentArgs>? configureDeployment = null,
ProviderResource? providerResource = null
) {
var appLabels = Meta.AppLabels(appSettings, deploySettings);
var appLabels = Meta.AppLabels(appSettings, resourceName, deploySettings.Release);
var gitLabAnnotations = gitLabSettings.GitLabAnnotations();

var containers = Pods.GetAppContainers(
resourceName,
appSettings,
deploySettings,
gitLabSettings,
Expand All @@ -37,7 +39,7 @@ public static Deployment Create(
);

var deployment = new DeploymentArgs {
Metadata = Meta.GetMeta(appSettings.Name, kubens.GetName(), gitLabAnnotations, appLabels),
Metadata = Meta.GetMeta(resourceName, kubens.GetName(), gitLabAnnotations, appLabels),
Spec = new DeploymentSpecArgs {
Selector = new LabelSelectorArgs {MatchLabels = appLabels},
Replicas = deploySettings.Replicas,
Expand All @@ -55,7 +57,7 @@ public static Deployment Create(
configureDeployment?.Invoke(deployment);

return new Deployment(
deploySettings.PulumiName("deployment"),
resourceName.AsPulumiName(),
deployment,
new CustomResourceOptions {Provider = providerResource}
);
Expand Down
10 changes: 6 additions & 4 deletions Ubiquitous.AutoDevOps.Stack/Resources/KubeIngress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ namespace Ubiquitous.AutoDevOps.Stack.Resources {
public static class KubeIngress {
public static Ingress Create(
Namespace kubens,
ResourceName resourceName,
AppSettings appSettings,
DeploySettings deploySettings,
IngressSettings ingressSettings,
Service service,
bool metricsEnabled,
Dictionary<string, string>? annotations = null,
ProviderResource? providerResource = null
) {
var ingressLabels = deploySettings.BaseLabels();
var ingressLabels = Meta.BaseLabels(appSettings, resourceName, deploySettings.Release);
var tlsEnabled = ingressSettings.Tls?.Enabled == true;

var ingressAnnotations = (annotations ?? new Dictionary<string, string>())
Expand All @@ -35,7 +37,7 @@ public static Ingress Create(

var ingress = new IngressArgs {
Metadata = Meta.GetMeta(
deploySettings.ResourceName,
resourceName,
kubens.GetName(),
ingressAnnotations,
ingressLabels
Expand All @@ -49,7 +51,7 @@ public static Ingress Create(
Tls = tlsEnabled
? new[] {
new IngressTLSArgs {
SecretName = ingressSettings.Tls!.SecretName ?? $"{deploySettings.ResourceName}-tls",
SecretName = ingressSettings.Tls!.SecretName ?? $"{resourceName}-tls",
Hosts = new[] {uri.Host}
}
}
Expand All @@ -58,7 +60,7 @@ public static Ingress Create(
};

return new Ingress(
deploySettings.PulumiName("ingress"),
resourceName.AsPulumiName(),
ingress,
new CustomResourceOptions {Provider = providerResource}
);
Expand Down
Loading

0 comments on commit 427571c

Please sign in to comment.