Skip to content

Commit

Permalink
Make settings configurable, drop RandomPet
Browse files Browse the repository at this point in the history
  • Loading branch information
cnunciato committed Oct 18, 2022
1 parent eb75863 commit c7fea46
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 41 deletions.
30 changes: 17 additions & 13 deletions container-azure-csharp/Program.cs
Original file line number Diff line number Diff line change
@@ -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<double>("cpu"), 1.0);
var memory = Math.Max(config.GetObject<double>("memory"), 1.5);

var resourceGroup = new AzureNative.Resources.ResourceGroup("resourceGroup");

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand All @@ -101,8 +105,8 @@

return new Dictionary<string, object?>
{
["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}"),
};
});
6 changes: 6 additions & 0 deletions container-azure-csharp/Pulumi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion container-azure-csharp/app/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");
app.MapGet("/", () => "Hello, world! 👋");

app.Run();
6 changes: 6 additions & 0 deletions container-azure-go/Pulumi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion container-azure-go/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 25 additions & 9 deletions container-azure-go/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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),
Expand All @@ -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
})
Expand Down
6 changes: 6 additions & 0 deletions container-azure-python/Pulumi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 14 additions & 11 deletions container-azure-python/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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}"))
2 changes: 1 addition & 1 deletion container-azure-python/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

@app.route("/")
def index():
return "Hi, world!"
return "Hello, world! 👋"
6 changes: 6 additions & 0 deletions container-azure-typescript/Pulumi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 10 additions & 5 deletions container-azure-typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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}`);

0 comments on commit c7fea46

Please sign in to comment.