Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Won't merge - Spike: deploy Bicep using AzureResourceGroup #942

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
"CheckForbiddenWords",
"Clean",
"Compile",
"CopySashimiPackagesForConsolidation",
"CopyToLocalPackages",
"Pack",
"PackageConsolidatedCalamariZip",
Expand All @@ -153,7 +152,6 @@
"CheckForbiddenWords",
"Clean",
"Compile",
"CopySashimiPackagesForConsolidation",
"CopyToLocalPackages",
"Pack",
"PackageConsolidatedCalamariZip",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public async Task Deploy_with_template_in_package()
public async Task Deploy_with_template_inline()
{
var packagePath = TestEnvironment.GetTestPath("Packages", "AzureResourceGroup");
var template = File.ReadAllText(Path.Combine(packagePath, "azure_website_template.json"));
var paramsFileContent = File.ReadAllText(Path.Combine(packagePath, "azure_website_params.json"));
var parameters = JObject.Parse(paramsFileContent)["parameters"].ToString();

Expand All @@ -97,17 +98,61 @@ public async Task Deploy_with_template_inline()
context.Variables.Add(SpecialVariables.Action.Azure.ResourceGroupTemplate, File.ReadAllText(Path.Combine(packagePath, "azure_website_template.json")));
context.Variables.Add(SpecialVariables.Action.Azure.ResourceGroupTemplateParameters, parameters);

context.WithDataFile(template, "template.json");
context.WithDataFile(paramsFileContent, "parameters.json");
context.WithFilesToCopy(packagePath);
})
.Execute();
}

[Test]
public async Task Deploy_with_bicep_in_package()
{
var packagePath = TestEnvironment.GetTestPath("Packages", "Bicep");
await CommandTestBuilder.CreateAsync<DeployAzureResourceGroupCommand, Program>()
.WithArrange(context =>
{
AddDefaults(context);
context.Variables.Add(SpecialVariables.Action.Azure.ResourceGroupDeploymentMode, "Complete");
context.Variables.Add("Octopus.Action.Azure.TemplateSource", "Package");
context.Variables.Add("Octopus.Action.Azure.ResourceGroupTemplate", "container_app_sample.json");
context.Variables.Add("Octopus.Action.Azure.ResourceGroupTemplateParameters", "container_app_sample_params.json");
context.WithFilesToCopy(packagePath);
})
.Execute();
}

[Test]
public async Task Deploy_with_bicep_inline()
{
var packagePath = TestEnvironment.GetTestPath("Packages", "Bicep");
var template = File.ReadAllText(Path.Combine(packagePath, "container_app_sample.json"));
var paramsFileContent = File.ReadAllText(Path.Combine(packagePath, "container_app_sample_params.json"));
var parameters = JObject.Parse(paramsFileContent)["parameters"].ToString();

await CommandTestBuilder.CreateAsync<DeployAzureResourceGroupCommand, Program>()
.WithArrange(context =>
{
AddDefaults(context);
context.Variables.Add(SpecialVariables.Action.Azure.ResourceGroupDeploymentMode, "Complete");
context.Variables.Add("Octopus.Action.Azure.TemplateSource", "Inline");
context.Variables.Add(SpecialVariables.Action.Azure.ResourceGroupTemplate, File.ReadAllText(Path.Combine(packagePath, "container_app_sample.json")));
context.Variables.Add(SpecialVariables.Action.Azure.ResourceGroupTemplateParameters, parameters);

context.WithDataFile(template, "template.json");
context.WithDataFile(paramsFileContent, "parameters.json");
context.WithFilesToCopy(packagePath);
})
.Execute();
}

[Test]
[WindowsTest]
[RequiresPowerShell5OrAbove]
public async Task Deploy_Ensure_Tools_Are_Configured()
{
var packagePath = TestEnvironment.GetTestPath("Packages", "AzureResourceGroup");
var template = File.ReadAllText(Path.Combine(packagePath, "azure_website_template.json"));
var paramsFileContent = File.ReadAllText(Path.Combine(packagePath, "azure_website_params.json"));
var parameters = JObject.Parse(paramsFileContent)["parameters"].ToString();
var psScript = @"
Expand All @@ -129,6 +174,8 @@ public async Task Deploy_Ensure_Tools_Are_Configured()
context.Variables.Add(KnownVariables.Action.CustomScripts.GetCustomScriptStage(DeploymentStages.PreDeploy, ScriptSyntax.CSharp), "Console.WriteLine(\"Hello from C#\");");
context.Variables.Add(KnownVariables.Action.CustomScripts.GetCustomScriptStage(DeploymentStages.PostDeploy, ScriptSyntax.FSharp), "printfn \"Hello from F#\"");

context.WithDataFile(template, "template.json");
context.WithDataFile(paramsFileContent, "parameters.json");
context.WithFilesToCopy(packagePath);
})
.Execute();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using Calamari.Common.Plumbing.FileSystem;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

namespace Calamari.AzureResourceGroup.Tests;

[TestFixture]
public class BicepTemplateCompilerFixture
{
private readonly IBicepTemplateCompiler compiler = new ServiceCollection()
.AddSingleton<ICalamariFileSystem>(CalamariPhysicalFileSystem.GetPhysicalFileSystem())
.AddBicep()
.BuildServiceProvider()
.GetRequiredService<IBicepTemplateCompiler>();

[Test]
public void TestCompile()
{
var template = File.ReadAllText("./Packages/Bicep/container_app_sample.bicep");
var expected = File.ReadAllText("./Packages/Bicep/container_app_sample.json");

var got = compiler.Compile(template);

Assert.AreEqual(expected, got);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
<AssemblyName>Calamari.AzureResourceGroup.Tests</AssemblyName>
<IsPackable>false</IsPackable>
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64;linux-arm;linux-arm64</RuntimeIdentifiers>
</PropertyGroup>
<PropertyGroup Condition="!$([MSBuild]::IsOSUnixLike())">
<TargetFrameworks>net461;net6.0</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSUnixLike())">
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
Expand All @@ -33,5 +28,14 @@
<None Update="Packages\AzureResourceGroup\Default.aspx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Packages\Bicep\container_app_sample.bicep">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Packages\Bicep\container_app_sample.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Packages\Bicep\container_app_sample_params.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
@description('Specifies the name of the container app.')
param containerAppName string = 'containerapp-${uniqueString(resourceGroup().id)}'

@description('Specifies the name of the container app environment.')
param containerAppEnvName string = 'containerapp-env-${uniqueString(resourceGroup().id)}'

@description('Specifies the name of the log analytics workspace.')
param containerAppLogAnalyticsName string = 'containerapp-log-${uniqueString(resourceGroup().id)}'

@description('Specifies the location for all resources.')
@allowed([
'northcentralusstage'
'eastus'
'northeurope'
'canadacentral'
])
param location string //cannot use resourceGroup().location since it's not available in most of regions

@description('Specifies the docker container image to deploy.')
param containerImage string = 'mcr.microsoft.com/azuredocs/containerapps-helloworld:latest'

@description('Specifies the container port.')
param targetPort int = 80

@description('Number of CPU cores the container can use. Can be with a maximum of two decimals.')
param cpuCore string = '0.5'

@description('Amount of memory (in gibibytes, GiB) allocated to the container up to 4GiB. Can be with a maximum of two decimals. Ratio with CPU cores must be equal to 2.')
param memorySize string = '1'

@description('Minimum number of replicas that will be deployed')
@minValue(0)
@maxValue(25)
param minReplicas int = 1

@description('Maximum number of replicas that will be deployed')
@minValue(0)
@maxValue(25)
param maxReplicas int = 3

resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2020-10-01' = {
name: containerAppLogAnalyticsName
location: location
properties: {
sku: {
name: 'PerGB2018'
}
}
}

resource containerAppEnv 'Microsoft.App/managedEnvironments@2022-01-01-preview' = {
name: containerAppEnvName
location: location
properties: {
appLogsConfiguration: {
destination: 'log-analytics'
logAnalyticsConfiguration: {
customerId: logAnalytics.properties.customerId
sharedKey: logAnalytics.listKeys().primarySharedKey
}
}
}
}

resource containerApp 'Microsoft.App/containerApps@2022-01-01-preview' = {
name: containerAppName
location: location
properties: {
managedEnvironmentId: containerAppEnv.id
configuration: {
ingress: {
external: true
targetPort: targetPort
allowInsecure: false
traffic: [
{
latestRevision: true
weight: 100
}
]
}
}
template: {
revisionSuffix: 'firstrevision'
containers: [
{
name: containerAppName
image: containerImage
resources: {
cpu: json(cpuCore)
memory: '${memorySize}Gi'
}
}
]
scale: {
minReplicas: minReplicas
maxReplicas: maxReplicas
}
}
}
}

output containerAppFQDN string = containerApp.properties.configuration.ingress.fqdn