Skip to content

Commit

Permalink
YAML and Helm support in .NET SDK (#980)
Browse files Browse the repository at this point in the history
YAML and Helm support in .NET SDK
  • Loading branch information
mikhailshilkov authored Feb 26, 2020
1 parent eb6da54 commit 415978f
Show file tree
Hide file tree
Showing 250 changed files with 5,785 additions and 2,853 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
- Fix .NET resources with empty arguments. (https://github.com/pulumi/pulumi-kubernetes/pull/983).
- Fix panic condition in Pod await logic. (https://github.com/pulumi/pulumi-kubernetes/pull/998).

### Improvements

- .NET SDK supports resources to work with YAML Kubernetes files and Helm charts.
(https://github.com/pulumi/pulumi-kubernetes/pull/980).

## 1.5.3 (February 11, 2020)

### Bug fixes
Expand Down
7 changes: 6 additions & 1 deletion cmd/pulumi-gen-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func writePythonClient(data map[string]interface{}, outdir, templateDir string)

func writeDotnetClient(data map[string]interface{}, outdir, templateDir string) {

inputAPIcs, ouputAPIcs, kindsCs, err := gen.DotnetClient(data, templateDir)
inputAPIcs, ouputAPIcs, yamlcs, kindsCs, err := gen.DotnetClient(data, templateDir)
if err != nil {
panic(err)
}
Expand All @@ -247,6 +247,11 @@ func writeDotnetClient(data map[string]interface{}, outdir, templateDir string)
panic(err)
}

err = ioutil.WriteFile(fmt.Sprintf("%s/Yaml/Yaml.cs", outdir), []byte(yamlcs), 0777)
if err != nil {
panic(err)
}

for path, contents := range kindsCs {
filename := filepath.Join(outdir, path)
err := os.MkdirAll(filepath.Dir(filename), 0700)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/dotnet-templates/Pulumi.Kubernetes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<ItemGroup>
<PackageReference Include="Pulumi" Version="1.11.0-preview" />
<PackageReference Include="System.Collections.Immutable" Version="1.6.0" />
<PackageReference Include="Glob" Version="1.1.5" />
</ItemGroup>

<ItemGroup>
Expand Down
135 changes: 127 additions & 8 deletions pkg/gen/dotnet-templates/Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,145 @@
// *** WARNING: this file was generated by the Pulumi Terraform Bridge (tfgen) Tool. ***
// Copyright 2016-2020, Pulumi Corporation
// *** WARNING: this file was generated by the Pulumi Kubernetes codegen tool. ***
// *** Do not edit by hand unless you're certain you know what you are doing! ***

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using Pulumi;
using System.Text;

namespace Pulumi.Kubernetes
{
static class Utilities
internal static class Utilities
{
private readonly static string version;
public static string Version => version;
public static string Version { get; }

static Utilities()
{
var assembly = typeof(Utilities).GetTypeInfo().Assembly;
using (var stream = assembly.GetManifestResourceStream("Pulumi.Kubernetes.version.txt"))
using (var reader = new StreamReader(stream))
using var stream = assembly.GetManifestResourceStream("Pulumi.Kubernetes.version.txt");
if (stream == null)
throw new Exception("Manifest file 'version.txt' not found");
using var reader = new StreamReader(stream);
Version = reader.ReadToEnd().Trim();
}

public static InvokeOptions WithVersion(this InvokeOptions? options)
{
if (options?.Version != null)
{
return options;
}
return new InvokeOptions
{
Parent = options?.Parent,
Provider = options?.Provider,
Version = Version,
};
}

public static string ExecuteCommand(string command, string[] flags)
{
using var process = new Process
{
StartInfo =
{
FileName = command,
Arguments = EscapeArguments(flags),
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (process.ExitCode > 0)
{
string error = process.StandardError.ReadToEnd();
throw new Exception(error);
}
return output;
}

/// <summary>
/// Convert an argument array to an argument string for using with Process.StartInfo.Arguments.
/// </summary>
private static string EscapeArguments(params string[] args)
=> string.Join(" ", args.Select(EscapeArguments));

/// <summary>
/// Convert an argument array to an argument string for using with Process.StartInfo.Arguments.
/// </summary>
private static string EscapeArguments(string argument)
{
var escapedArgument = new StringBuilder();
var backslashCount = 0;
var needsQuotes = false;

foreach (var character in argument)
{
switch (character)
{
case '\\':
// Backslashes are simply passed through, except when they need
// to be escaped when followed by a \", e.g. the argument string
// \", which would be encoded to \\\"
backslashCount++;
escapedArgument.Append('\\');
break;

case '\"':
// Escape any preceding backslashes
escapedArgument.Append(new string('\\', backslashCount));

// Append an escaped double quote.
escapedArgument.Append("\\\"");

// Reset the backslash counter.
backslashCount = 0;
break;

case ' ':
case '\t':
// White spaces are escaped by surrounding the entire string with
// double quotes, which should be done at the end to prevent
// multiple wrappings.
needsQuotes = true;

// Append the whitespace
escapedArgument.Append(character);

// Reset the backslash counter.
backslashCount = 0;
break;

default:
// Reset the backslash counter.
backslashCount = 0;

// Append the current character
escapedArgument.Append(character);
break;
}
}

// No need to wrap in quotes
if (!needsQuotes)
{
version = reader.ReadToEnd().Trim();
return escapedArgument.ToString();
}

// Prepend the "
escapedArgument.Insert(0, '"');

// Escape any preceding backslashes before appending the "
escapedArgument.Append(new string('\\', backslashCount));

// Append the final "
escapedArgument.Append('\"');

return escapedArgument.ToString();
}
}
}
Loading

0 comments on commit 415978f

Please sign in to comment.