From 04dd2b8cea0d305b2805009073c68a323bfacfca Mon Sep 17 00:00:00 2001 From: Christian Nunciato Date: Fri, 14 Oct 2022 16:44:09 -0700 Subject: [PATCH] Make settings configurable, drop RandomPet --- container-azure-csharp/Program.cs | 30 +++++++++++++---------- container-azure-csharp/Pulumi.yaml | 6 +++++ container-azure-csharp/app/Program.cs | 2 +- container-azure-go/Pulumi.yaml | 6 +++++ container-azure-go/app/main.go | 2 +- container-azure-go/main.go | 34 +++++++++++++++++++------- container-azure-python/Pulumi.yaml | 6 +++++ container-azure-python/__main__.py | 25 ++++++++++--------- container-azure-python/app/app.py | 2 +- container-azure-typescript/Pulumi.yaml | 6 +++++ container-azure-typescript/index.ts | 15 ++++++++---- 11 files changed, 93 insertions(+), 41 deletions(-) diff --git a/container-azure-csharp/Program.cs b/container-azure-csharp/Program.cs index e24aafa0e..499c74e3a 100644 --- a/container-azure-csharp/Program.cs +++ b/container-azure-csharp/Program.cs @@ -1,15 +1,18 @@ -using Pulumi; +using System; +using System.Collections.Generic; +using Pulumi; using AzureNative = Pulumi.AzureNative; using Docker = Pulumi.Docker; using Random = Pulumi.Random; -using System.Collections.Generic; return await Pulumi.Deployment.RunAsync(() => { var config = new Config(); var appPath = config.Get("appPath") ?? "./app"; - var containerPort = config.GetInt32("containerPort") ?? 80; var imageName = config.Get("imageName") ?? "my-app"; + var containerPort = config.GetInt32("containerPort") ?? 80; + var cpu = Math.Max(config.GetObject("cpu"), 1.0); + var memory = Math.Max(config.GetObject("memory"), 1.5); var resourceGroup = new AzureNative.Resources.ResourceGroup("resourceGroup"); @@ -44,12 +47,13 @@ }, }); - var hostname = new Random.RandomPet("hostname", new() + var dnsName = new Random.RandomString("dns-name", new() { - Length = 2, - }); + Length = 8, + Special = false, + }).Result.Apply(result => $"{imageName}-{result.ToLower()}"); - var group = new AzureNative.ContainerInstance.ContainerGroup("group", new() + var containerGroup = new AzureNative.ContainerInstance.ContainerGroup("container-group", new() { ResourceGroupName = resourceGroup.Name, OsType = "linux", @@ -80,15 +84,15 @@ }, Resources = new AzureNative.ContainerInstance.Inputs.ResourceRequirementsArgs { Requests = new AzureNative.ContainerInstance.Inputs.ResourceRequestsArgs { - Cpu = 1.0, - MemoryInGB = 1.5, + Cpu = cpu, + MemoryInGB = memory, }, }, }, }, IpAddress = new AzureNative.ContainerInstance.Inputs.IpAddressArgs { Type = AzureNative.ContainerInstance.ContainerGroupIpAddressType.Public, - DnsNameLabel = hostname.Id, + DnsNameLabel = dnsName, Ports = new[] { new AzureNative.ContainerInstance.Inputs.PortArgs { @@ -101,8 +105,8 @@ return new Dictionary { - ["ipAddress"] = group.IpAddress.Apply(address => address!.Ip), - ["hostname"] = group.IpAddress.Apply(address => address!.Fqdn), - ["url"] = group.IpAddress.Apply(address => $"http://{address!.Fqdn}"), + ["ipAddress"] = containerGroup.IpAddress.Apply(addr => addr!.Ip), + ["hostname"] = containerGroup.IpAddress.Apply(addr => addr!.Fqdn), + ["url"] = containerGroup.IpAddress.Apply(addr => $"http://{addr!.Fqdn}:{containerPort}"), }; }); diff --git a/container-azure-csharp/Pulumi.yaml b/container-azure-csharp/Pulumi.yaml index daf9e7b01..80b641c0e 100644 --- a/container-azure-csharp/Pulumi.yaml +++ b/container-azure-csharp/Pulumi.yaml @@ -13,3 +13,9 @@ template: containerPort: description: The port to expose on the container default: 80 + cpu: + description: The CPU limit of the container instance + default: 1.0 + memory: + description: The memory limit, in GB, of the container instance + default: 1.5 diff --git a/container-azure-csharp/app/Program.cs b/container-azure-csharp/app/Program.cs index bc8438c3a..04ce7f50d 100644 --- a/container-azure-csharp/app/Program.cs +++ b/container-azure-csharp/app/Program.cs @@ -1,6 +1,6 @@ var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); -app.MapGet("/", () => "Hello World!"); +app.MapGet("/", () => "Hello, world! 👋"); app.Run(); diff --git a/container-azure-go/Pulumi.yaml b/container-azure-go/Pulumi.yaml index 3bb4bfd6d..22538c61f 100644 --- a/container-azure-go/Pulumi.yaml +++ b/container-azure-go/Pulumi.yaml @@ -13,3 +13,9 @@ template: containerPort: description: The port to expose on the container default: 80 + cpu: + description: The CPU limit of the container instance + default: 1.0 + memory: + description: The memory limit, in GB, of the container instance + default: 1.5 diff --git a/container-azure-go/app/main.go b/container-azure-go/app/main.go index 435fa6a3c..710b0f0d1 100644 --- a/container-azure-go/app/main.go +++ b/container-azure-go/app/main.go @@ -10,7 +10,7 @@ import ( func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hello, World!") + fmt.Fprintf(w, "Hello, world! 👋") }) if err := http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("PORT")), nil); err != nil { diff --git a/container-azure-go/main.go b/container-azure-go/main.go index 86db2a364..04c9e4e21 100644 --- a/container-azure-go/main.go +++ b/container-azure-go/main.go @@ -1,6 +1,9 @@ package main import ( + "fmt" + "strings" + "github.com/pulumi/pulumi-azure-native/sdk/go/azure/containerinstance" "github.com/pulumi/pulumi-azure-native/sdk/go/azure/containerregistry" "github.com/pulumi/pulumi-azure-native/sdk/go/azure/resources" @@ -26,6 +29,14 @@ func main() { if param := cfg.GetInt("containerPort"); param != 0 { containerPort = param } + cpu := 1.0 + if param := cfg.GetFloat64("cpu"); param != 0 { + cpu = param + } + memory := 1.0 + if param := cfg.GetFloat64("memory"); param != 0 { + memory = param + } resourceGroup, err := resources.NewResourceGroup(ctx, "resource-group", nil) if err != nil { @@ -65,14 +76,19 @@ func main() { return err } - hostname, err := random.NewRandomPet(ctx, "hostname", &random.RandomPetArgs{ - Length: pulumi.Int(2), + dnsNameSuffix, err := random.NewRandomString(ctx, "dns-name-suffix", &random.RandomStringArgs{ + Length: pulumi.Int(8), + Special: pulumi.Bool(false), }) if err != nil { return err } - group, err := containerinstance.NewContainerGroup(ctx, "group", &containerinstance.ContainerGroupArgs{ + dnsName := dnsNameSuffix.Result.ApplyT(func(result string) string { + return fmt.Sprintf("%s-%s", imageName, strings.ToLower(result)) + }).(pulumi.StringOutput) + + containerGroup, err := containerinstance.NewContainerGroup(ctx, "container-group", &containerinstance.ContainerGroupArgs{ ResourceGroupName: resourceGroup.Name, OsType: pulumi.String("linux"), RestartPolicy: pulumi.String("always"), @@ -101,15 +117,15 @@ func main() { }, Resources: containerinstance.ResourceRequirementsArgs{ Requests: containerinstance.ResourceRequestsArgs{ - Cpu: pulumi.Float64(1.0), - MemoryInGB: pulumi.Float64(1.5), + Cpu: pulumi.Float64(cpu), + MemoryInGB: pulumi.Float64(memory), }, }, }, }, IpAddress: containerinstance.IpAddressArgs{ Type: pulumi.String("public"), - DnsNameLabel: hostname.ID(), + DnsNameLabel: dnsName, Ports: containerinstance.PortArray{ containerinstance.PortArgs{ Port: pulumi.Int(containerPort), @@ -119,9 +135,9 @@ func main() { }, }) - ctx.Export("ipAddress", group.IpAddress.Elem().Ip()) - ctx.Export("hostname", group.IpAddress.Elem().Fqdn()) - ctx.Export("url", pulumi.Sprintf("http://%s:%d", group.IpAddress.Elem().Fqdn(), containerPort)) + ctx.Export("ipAddress", containerGroup.IpAddress.Elem().Ip()) + ctx.Export("hostname", containerGroup.IpAddress.Elem().Fqdn()) + ctx.Export("url", pulumi.Sprintf("http://%s:%d", containerGroup.IpAddress.Elem().Fqdn(), containerPort)) return nil }) diff --git a/container-azure-python/Pulumi.yaml b/container-azure-python/Pulumi.yaml index c8809ce15..0045be142 100644 --- a/container-azure-python/Pulumi.yaml +++ b/container-azure-python/Pulumi.yaml @@ -16,3 +16,9 @@ template: containerPort: description: The port to expose on the container default: 80 + cpu: + description: The CPU limit of the container instance + default: 1.0 + memory: + description: The memory limit, in GB, of the container instance + default: 1.5 diff --git a/container-azure-python/__main__.py b/container-azure-python/__main__.py index 6f457f91c..ff67b9bac 100644 --- a/container-azure-python/__main__.py +++ b/container-azure-python/__main__.py @@ -6,7 +6,9 @@ config = pulumi.Config() image_name = config.get("imageName", "my-app") app_path = config.get("appPath", "./app") -container_port = config.get_number("containerPort", 80) +container_port = config.get_int("containerPort", 80) +cpu = config.get_float("cpu", 1.0) +memory = config.get_float("memory", 1.5) resource_group = resources.ResourceGroup('resource_group') @@ -38,11 +40,12 @@ ), ) -hostname = random.RandomPet("hostname", random.RandomPetArgs( - length=2, -)) +dns_name = random.RandomString("dns-name", random.RandomStringArgs( + length=8, + special=False, +)).result.apply(lambda result: f"{image_name}-{result.lower()}") -group = containerinstance.ContainerGroup("group", containerinstance.ContainerGroupArgs( +container_group = containerinstance.ContainerGroup("container-group", containerinstance.ContainerGroupArgs( resource_group_name=resource_group.name, os_type="linux", restart_policy="always", @@ -75,15 +78,15 @@ ], resources=containerinstance.ResourceRequirementsArgs( requests=containerinstance.ResourceRequestsArgs( - cpu=1.0, - memory_in_gb=1.5, + cpu=cpu, + memory_in_gb=memory, ), ), ), ], ip_address=containerinstance.IpAddressArgs( type=containerinstance.ContainerGroupIpAddressType.PUBLIC, - dns_name_label=hostname, + dns_name_label=dns_name, ports=[ containerinstance.PortArgs( port=container_port, @@ -93,6 +96,6 @@ ), )) -pulumi.export("ipAddress", group.ip_address.apply(lambda addr: addr.ip)) -pulumi.export("hostname", group.ip_address.apply(lambda addr: addr.fqdn)) -pulumi.export("url", group.ip_address.apply(lambda addr: f"http://{addr.fqdn}:{container_port}")) +pulumi.export("ipAddress", container_group.ip_address.apply(lambda addr: addr.ip)) +pulumi.export("hostname", container_group.ip_address.apply(lambda addr: addr.fqdn)) +pulumi.export("url", container_group.ip_address.apply(lambda addr: f"http://{addr.fqdn}:{container_port}")) diff --git a/container-azure-python/app/app.py b/container-azure-python/app/app.py index f34ca6f98..161faf4d7 100644 --- a/container-azure-python/app/app.py +++ b/container-azure-python/app/app.py @@ -4,4 +4,4 @@ @app.route("/") def index(): - return "Hi, world!" + return "Hello, world! 👋" diff --git a/container-azure-typescript/Pulumi.yaml b/container-azure-typescript/Pulumi.yaml index 938093d9d..e6dc0eb10 100644 --- a/container-azure-typescript/Pulumi.yaml +++ b/container-azure-typescript/Pulumi.yaml @@ -14,3 +14,9 @@ template: containerPort: description: The port to expose on the container default: 80 + cpu: + description: The CPU limit of the container instance + default: 1.0 + memory: + description: The memory limit, in GB, of the container instance + default: 1.5 diff --git a/container-azure-typescript/index.ts b/container-azure-typescript/index.ts index 5b3f04762..4f910a8da 100644 --- a/container-azure-typescript/index.ts +++ b/container-azure-typescript/index.ts @@ -42,7 +42,12 @@ const image = new docker.Image("image", { }, }); -const group = new containerinstance.ContainerGroup("group", { +const dnsName = new random.RandomString("dns-name", { + length: 8, + special: false, +}).result.apply(result => `${imageName}-${result.toLowerCase()}`); + +const containerGroup = new containerinstance.ContainerGroup("container-group", { resourceGroupName: resourceGroup.name, osType: "linux", restartPolicy: "always", @@ -79,7 +84,7 @@ const group = new containerinstance.ContainerGroup("group", { ], ipAddress: { type: containerinstance.ContainerGroupIpAddressType.Public, - dnsNameLabel: new random.RandomPet("host", { length: 2 }).id, + dnsNameLabel: dnsName, ports: [ { port: containerPort, @@ -89,6 +94,6 @@ const group = new containerinstance.ContainerGroup("group", { }, }); -export const ipAddress = group.ipAddress.apply(address => address!.ip!); -export const hostname = group.ipAddress.apply(address => address!.fqdn!); -export const url = group.ipAddress.apply(address => `http://${address!.fqdn!}:${containerPort}`); +export const ipAddress = containerGroup.ipAddress.apply(addr => addr!.ip!); +export const hostname = containerGroup.ipAddress.apply(addr => addr!.fqdn!); +export const url = containerGroup.ipAddress.apply(addr => `http://${addr!.fqdn!}:${containerPort}`);