Skip to content

Commit

Permalink
Add ability to generate .bicep file from a Kubernetes manifest (#7812)
Browse files Browse the repository at this point in the history
* Add ability to generate .bicep file from a Kubernetes manifest

* Add error handling

* Fix up build error

* Fix yaml deserialization hack
  • Loading branch information
anthony-c-martin authored Aug 5, 2022
1 parent 2b41fcf commit 36a97df
Show file tree
Hide file tree
Showing 29 changed files with 1,460 additions and 141 deletions.
8 changes: 7 additions & 1 deletion src/Bicep.Cli.IntegrationTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,11 @@
"resolved": "4.4.0",
"contentHash": "YhEdSQUsTx+C8m8Bw7ar5/VesXvCFMItyZF7G1AUY+OM0VPZUOeAVpJ4Wl6fydBGUYZxojTDR3I6Bj/+BPkJNA=="
},
"SharpYaml": {
"type": "Transitive",
"resolved": "2.0.0",
"contentHash": "KgdpJtAHf119JmlXU2P+HTo5vht8dKTrb8bP1vQeEV2jnkQNuVGg+tSea7T/eZe5xaStRZaRXyGQobUMJgb44w=="
},
"System.AppContext": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -2080,7 +2085,8 @@
"dependencies": {
"Azure.Bicep.Core": "1.0.0",
"CommandLineParser": "2.9.1",
"OmniSharp.Extensions.LanguageServer": "0.19.5"
"OmniSharp.Extensions.LanguageServer": "0.19.5",
"SharpYaml": "2.0.0"
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion src/Bicep.Core.IntegrationTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,11 @@
"resolved": "4.4.0",
"contentHash": "YhEdSQUsTx+C8m8Bw7ar5/VesXvCFMItyZF7G1AUY+OM0VPZUOeAVpJ4Wl6fydBGUYZxojTDR3I6Bj/+BPkJNA=="
},
"SharpYaml": {
"type": "Transitive",
"resolved": "2.0.0",
"contentHash": "KgdpJtAHf119JmlXU2P+HTo5vht8dKTrb8bP1vQeEV2jnkQNuVGg+tSea7T/eZe5xaStRZaRXyGQobUMJgb44w=="
},
"System.AppContext": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -2070,7 +2075,8 @@
"dependencies": {
"Azure.Bicep.Core": "1.0.0",
"CommandLineParser": "2.9.1",
"OmniSharp.Extensions.LanguageServer": "0.19.5"
"OmniSharp.Extensions.LanguageServer": "0.19.5",
"SharpYaml": "2.0.0"
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion src/Bicep.Core.Samples/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,11 @@
"resolved": "4.4.0",
"contentHash": "YhEdSQUsTx+C8m8Bw7ar5/VesXvCFMItyZF7G1AUY+OM0VPZUOeAVpJ4Wl6fydBGUYZxojTDR3I6Bj/+BPkJNA=="
},
"SharpYaml": {
"type": "Transitive",
"resolved": "2.0.0",
"contentHash": "KgdpJtAHf119JmlXU2P+HTo5vht8dKTrb8bP1vQeEV2jnkQNuVGg+tSea7T/eZe5xaStRZaRXyGQobUMJgb44w=="
},
"System.AppContext": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -2058,7 +2063,8 @@
"dependencies": {
"Azure.Bicep.Core": "1.0.0",
"CommandLineParser": "2.9.1",
"OmniSharp.Extensions.LanguageServer": "0.19.5"
"OmniSharp.Extensions.LanguageServer": "0.19.5",
"SharpYaml": "2.0.0"
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion src/Bicep.Core.UnitTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,11 @@
"resolved": "4.4.0",
"contentHash": "YhEdSQUsTx+C8m8Bw7ar5/VesXvCFMItyZF7G1AUY+OM0VPZUOeAVpJ4Wl6fydBGUYZxojTDR3I6Bj/+BPkJNA=="
},
"SharpYaml": {
"type": "Transitive",
"resolved": "2.0.0",
"contentHash": "KgdpJtAHf119JmlXU2P+HTo5vht8dKTrb8bP1vQeEV2jnkQNuVGg+tSea7T/eZe5xaStRZaRXyGQobUMJgb44w=="
},
"System.AppContext": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -2024,7 +2029,8 @@
"dependencies": {
"Azure.Bicep.Core": "1.0.0",
"CommandLineParser": "2.9.1",
"OmniSharp.Extensions.LanguageServer": "0.19.5"
"OmniSharp.Extensions.LanguageServer": "0.19.5",
"SharpYaml": "2.0.0"
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion src/Bicep.Decompiler.IntegrationTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,11 @@
"resolved": "4.4.0",
"contentHash": "YhEdSQUsTx+C8m8Bw7ar5/VesXvCFMItyZF7G1AUY+OM0VPZUOeAVpJ4Wl6fydBGUYZxojTDR3I6Bj/+BPkJNA=="
},
"SharpYaml": {
"type": "Transitive",
"resolved": "2.0.0",
"contentHash": "KgdpJtAHf119JmlXU2P+HTo5vht8dKTrb8bP1vQeEV2jnkQNuVGg+tSea7T/eZe5xaStRZaRXyGQobUMJgb44w=="
},
"System.AppContext": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -2043,7 +2048,8 @@
"dependencies": {
"Azure.Bicep.Core": "1.0.0",
"CommandLineParser": "2.9.1",
"OmniSharp.Extensions.LanguageServer": "0.19.5"
"OmniSharp.Extensions.LanguageServer": "0.19.5",
"SharpYaml": "2.0.0"
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion src/Bicep.Decompiler.UnitTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,11 @@
"resolved": "4.4.0",
"contentHash": "YhEdSQUsTx+C8m8Bw7ar5/VesXvCFMItyZF7G1AUY+OM0VPZUOeAVpJ4Wl6fydBGUYZxojTDR3I6Bj/+BPkJNA=="
},
"SharpYaml": {
"type": "Transitive",
"resolved": "2.0.0",
"contentHash": "KgdpJtAHf119JmlXU2P+HTo5vht8dKTrb8bP1vQeEV2jnkQNuVGg+tSea7T/eZe5xaStRZaRXyGQobUMJgb44w=="
},
"System.AppContext": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -2043,7 +2048,8 @@
"dependencies": {
"Azure.Bicep.Core": "1.0.0",
"CommandLineParser": "2.9.1",
"OmniSharp.Extensions.LanguageServer": "0.19.5"
"OmniSharp.Extensions.LanguageServer": "0.19.5",
"SharpYaml": "2.0.0"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using FluentAssertions;
using FluentAssertions.Primitives;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using Bicep.Core.UnitTests.Assertions;
using FluentAssertions.Collections;
using System.Collections.Generic;

namespace Bicep.LangServer.IntegrationTests.Assertions
{
Expand All @@ -11,6 +16,11 @@ public static TelemetryEventParamsAssertions Should(this TelemetryEventParams in
{
return new TelemetryEventParamsAssertions(instance);
}

public static TelemetryEventParamsCollectionAssertions Should(this IEnumerable<TelemetryEventParams> instance)
{
return new TelemetryEventParamsCollectionAssertions(instance);
}
}

public class TelemetryEventParamsAssertions : ReferenceTypeAssertions<TelemetryEventParams, TelemetryEventParamsAssertions>
Expand All @@ -21,5 +31,30 @@ public TelemetryEventParamsAssertions(TelemetryEventParams instance)
}

protected override string Identifier => "telemetry event";

public AndConstraint<TelemetryEventParamsAssertions> HaveProperties(JObject properties, string because = "", params object[] becauseArgs)
{
(Subject.ExtensionData["properties"] as JToken)!.Should().DeepEqual(properties, because, becauseArgs);

return new(this);
}
}

public class TelemetryEventParamsCollectionAssertions : GenericCollectionAssertions<IEnumerable<TelemetryEventParams>, TelemetryEventParams>
{
public TelemetryEventParamsCollectionAssertions(IEnumerable<TelemetryEventParams> instance)
: base(instance)
{
}

protected override string Identifier => "telemetry";

public AndConstraint<TelemetryEventParamsCollectionAssertions> ContainEvent(string eventName, JObject properties, string because = "", params object[] becauseArgs)
{
Subject.Should().ContainSingle(x => x.ExtensionData["eventName"] as string == eventName)
.Which.Should().HaveProperties(properties);

return new(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
@secure()
param kubeConfig string

import kubernetes as k8s {
namespace: 'default'
kubeConfig: kubeConfig
}

resource appsDeployment_azureVoteBack 'apps/Deployment@v1' = {
metadata: {
name: 'azure-vote-back'
}
spec: {
replicas: 1
selector: {
matchLabels: {
app: 'azure-vote-back'
}
}
template: {
metadata: {
labels: {
app: 'azure-vote-back'
}
}
spec: {
nodeSelector: {
'beta.kubernetes.io/os': 'linux'
}
containers: [
{
name: 'azure-vote-back'
image: 'mcr.microsoft.com/oss/bitnami/redis:6.0.8'
env: [
{
name: 'ALLOW_EMPTY_PASSWORD'
value: 'yes'
}
]
ports: [
{
containerPort: 6379
name: 'redis'
}
]
}
]
}
}
}
}

resource coreService_azureVoteBack 'core/Service@v1' = {
metadata: {
name: 'azure-vote-back'
}
spec: {
ports: [
{
port: 6379
}
]
selector: {
app: 'azure-vote-back'
}
}
}

resource appsDeployment_azureVoteFront 'apps/Deployment@v1' = {
metadata: {
name: 'azure-vote-front'
}
spec: {
replicas: 1
selector: {
matchLabels: {
app: 'azure-vote-front'
}
}
strategy: {
rollingUpdate: {
maxSurge: 1
maxUnavailable: 1
}
}
minReadySeconds: 5
template: {
metadata: {
labels: {
app: 'azure-vote-front'
}
}
spec: {
nodeSelector: {
'beta.kubernetes.io/os': 'linux'
}
containers: [
{
name: 'azure-vote-front'
image: 'mcr.microsoft.com/azuredocs/azure-vote-front:v1'
ports: [
{
containerPort: 80
}
]
resources: {
requests: {
cpu: '250m'
}
limits: {
cpu: '500m'
}
}
env: [
{
name: 'REDIS'
value: 'azure-vote-back'
}
]
}
]
}
}
}
}

resource coreService_azureVoteFront 'core/Service@v1' = {
metadata: {
name: 'azure-vote-front'
}
spec: {
type: 'LoadBalancer'
ports: [
{
port: 80
}
]
selector: {
app: 'azure-vote-front'
}
}
}
Loading

0 comments on commit 36a97df

Please sign in to comment.