Skip to content

Commit

Permalink
Schematize overlay types
Browse files Browse the repository at this point in the history
  • Loading branch information
lblackstone committed Nov 10, 2021
1 parent eabb570 commit 453c191
Show file tree
Hide file tree
Showing 28 changed files with 2,571 additions and 23 deletions.
14 changes: 14 additions & 0 deletions provider/cmd/pulumi-gen-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,15 @@ func writeGoClient(pkg *schema.Package, outdir string, templateDir string) {
}
}

// Go codegen maps package to "v3" for Helm Release. Manually rename to
// helm to avoid conflict with existing templates.
renamePackage([]string{
"kubernetes/helm/v2/pulumiTypes.go",
"kubernetes/helm/v2/init.go",
},
"package v2",
"package helm")

// Go codegen maps package to "v3" for Helm Release. Manually rename to
// helm to avoid conflict with existing templates.
renamePackage([]string{
Expand Down Expand Up @@ -450,6 +459,11 @@ func genK8sResourceTypes(pkg *schema.Package) {
contract.Assert(len(parts) == 3)

groupVersion, kind := parts[1], parts[2]

if resource.IsOverlay {
continue
}

groupVersions.Add(groupVersion)
kinds.Add(kind)
}
Expand Down
461 changes: 460 additions & 1 deletion provider/cmd/pulumi-resource-kubernetes/schema.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/imdario/mergo v0.3.12
github.com/mitchellh/mapstructure v1.4.1
github.com/pkg/errors v0.9.1
github.com/pulumi/pulumi/pkg/v3 v3.17.0
github.com/pulumi/pulumi/pkg/v3 v3.17.1-0.20211110005010-a8a08b67a68a
github.com/pulumi/pulumi/sdk/v3 v3.17.0
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
Expand All @@ -33,5 +33,4 @@ replace (
github.com/docker/distribution => github.com/docker/distribution v0.0.0-20191216044856-a8371794149d
github.com/docker/docker => github.com/moby/moby v17.12.0-ce-rc1.0.20200618181300-9dc6525e6118+incompatible
github.com/evanphx/json-patch => github.com/evanphx/json-patch v4.11.0+incompatible

)
5 changes: 3 additions & 2 deletions provider/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -946,10 +946,11 @@ github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/pulumi/pulumi/pkg/v3 v3.17.0 h1:YzBQ7kkQ1XRFhL0XZA0R5aI7onyNz4W87EqErzNe0po=
github.com/pulumi/pulumi/pkg/v3 v3.17.0/go.mod h1:tWkGLs8Xxo0I4CU5FK+FOuwvEeREbcRk46dtT6sE+rE=
github.com/pulumi/pulumi/pkg/v3 v3.17.1-0.20211110005010-a8a08b67a68a h1:HuxbuJY91cOdEZ9821lGvXAXbDBl+sFaxmJJRWL3Pdk=
github.com/pulumi/pulumi/pkg/v3 v3.17.1-0.20211110005010-a8a08b67a68a/go.mod h1:cqMzfxnyy3h/fzN6aN0LnERXEVmKECFGP94Wd6g2oqU=
github.com/pulumi/pulumi/sdk/v3 v3.17.0 h1:CxdgdmFEvo99JviXP6d332Y2AyAIPbLI6fu4K33vhAg=
github.com/pulumi/pulumi/sdk/v3 v3.17.0/go.mod h1:252ou/zAU1g6E8iTwe2Y9ht7pb5BDl2fJlOuAgZCHiA=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8=
github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down
2 changes: 1 addition & 1 deletion provider/pkg/gen/_go-templates/kustomize/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ import (
type Directory struct {
pulumi.ResourceState

Resources pulumi.Output
Resources pulumi.Output `pulumi:"resources"`
}

// NewDirectory registers a new resource with the given unique name, arguments, and options.
Expand Down
254 changes: 254 additions & 0 deletions provider/pkg/gen/examples/configFile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
ConfigFile creates a set of Kubernetes resources from a Kubernetes YAML file.

{{% examples %}}
## Example Usage
{{% example %}}
### Local File

```typescript
import * as k8s from "@pulumi/kubernetes";

const example = new k8s.yaml.ConfigFile("example", {
file: "foo.yaml",
});
```
```python
from pulumi_kubernetes.yaml import ConfigFile

example = ConfigFile(
"example",
file="foo.yaml",
)
```
```csharp
using System.Threading.Tasks;
using Pulumi;
using Pulumi.Kubernetes.Yaml;

class YamlStack : Stack
{
public YamlStack()
{
var helloWorld = new ConfigFile("example", new ConfigFileArgs
{
File = "foo.yaml",
});
}
}
```
```go
package main

import (
"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := yaml.NewConfigFile(ctx, "example",
&yaml.ConfigFileArgs{
File: "foo.yaml",
},
)
if err != nil {
return err
}

return nil
})
}
```
{{% /example %}}
{{% example %}}
### YAML with Transformations

```typescript
import * as k8s from "@pulumi/kubernetes";

const example = new k8s.yaml.ConfigFile("example", {
file: "foo.yaml",
transformations: [
// Make every service private to the cluster, i.e., turn all services into ClusterIP instead of LoadBalancer.
(obj: any, opts: pulumi.CustomResourceOptions) => {
if (obj.kind === "Service" && obj.apiVersion === "v1") {
if (obj.spec && obj.spec.type && obj.spec.type === "LoadBalancer") {
obj.spec.type = "ClusterIP";
}
}
},

// Set a resource alias for a previous name.
(obj: any, opts: pulumi.CustomResourceOptions) => {
if (obj.kind === "Deployment") {
opts.aliases = [{name: "oldName"}]
}
},

// Omit a resource from the Chart by transforming the specified resource definition to an empty List.
(obj: any, opts: pulumi.CustomResourceOptions) => {
if (obj.kind === "Pod" && obj.metadata.name === "test") {
obj.apiVersion = "v1"
obj.kind = "List"
}
},
],
});
```
```python
from pulumi_kubernetes.yaml import ConfigFile

# Make every service private to the cluster, i.e., turn all services into ClusterIP instead of LoadBalancer.
def make_service_private(obj, opts):
if obj["kind"] == "Service" and obj["apiVersion"] == "v1":
try:
t = obj["spec"]["type"]
if t == "LoadBalancer":
obj["spec"]["type"] = "ClusterIP"
except KeyError:
pass


# Set a resource alias for a previous name.
def alias(obj, opts):
if obj["kind"] == "Deployment":
opts.aliases = ["oldName"]


# Omit a resource from the Chart by transforming the specified resource definition to an empty List.
def omit_resource(obj, opts):
if obj["kind"] == "Pod" and obj["metadata"]["name"] == "test":
obj["apiVersion"] = "v1"
obj["kind"] = "List"


example = ConfigFile(
"example",
file="foo.yaml",
transformations=[make_service_private, alias, omit_resource],
)
```
```csharp
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Pulumi;
using Pulumi.Kubernetes.Yaml;

class YamlStack : Stack
{
public YamlStack()
{
var helloWorld = new ConfigFile("example", new ConfigFileArgs
{
File = "foo.yaml",
Transformations =
{
LoadBalancerToClusterIP,
ResourceAlias,
OmitTestPod,
}
});

// Make every service private to the cluster, i.e., turn all services into ClusterIP instead of LoadBalancer.
ImmutableDictionary<string, object> LoadBalancerToClusterIP(ImmutableDictionary<string, object> obj, CustomResourceOptions opts)
{
if ((string)obj["kind"] == "Service" && (string)obj["apiVersion"] == "v1")
{
var spec = (ImmutableDictionary<string, object>)obj["spec"];
if (spec != null && (string)spec["type"] == "LoadBalancer")
{
return obj.SetItem("spec", spec.SetItem("type", "ClusterIP"));
}
}

return obj;
}

// Set a resource alias for a previous name.
ImmutableDictionary<string, object> ResourceAlias(ImmutableDictionary<string, object> obj, CustomResourceOptions opts)
{
if ((string)obj["kind"] == "Deployment")
{
opts.Aliases = new List<Input<Alias>> { new Alias { Name = "oldName" } };
}

return obj;
}

// Omit a resource from the Chart by transforming the specified resource definition to an empty List.
ImmutableDictionary<string, object> OmitTestPod(ImmutableDictionary<string, object> obj, CustomResourceOptions opts)
{
var metadata = (ImmutableDictionary<string, object>)obj["metadata"];
if ((string)obj["kind"] == "Pod" && (string)metadata["name"] == "test")
{
return new Dictionary<string, object>
{
["apiVersion"] = "v1",
["kind"] = "List",
["items"] = new Dictionary<string, object>(),
}.ToImmutableDictionary();
}

return obj;
}
}
}
```
```go
package main

import (
"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := yaml.NewConfigFile(ctx, "example",
&yaml.ConfigFileArgs{
File: "foo.yaml",
Transformations: []yaml.Transformation{
// Make every service private to the cluster, i.e., turn all services into ClusterIP
// instead of LoadBalancer.
func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
if state["kind"] == "Service" {
spec := state["spec"].(map[string]interface{})
spec["type"] = "ClusterIP"
}
},

// Set a resource alias for a previous name.
func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
if state["kind"] == "Deployment" {
aliases := pulumi.Aliases([]pulumi.Alias{
{
Name: pulumi.String("oldName"),
},
})
opts = append(opts, aliases)
}
},

// Omit a resource from the Chart by transforming the specified resource definition
// to an empty List.
func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
name := state["metadata"].(map[string]interface{})["name"]
if state["kind"] == "Pod" && name == "test" {
state["apiVersion"] = "core/v1"
state["kind"] = "List"
}
},
},
},
)
if err != nil {
return err
}

return nil
})
}
```
{{% /example %}}
{% /examples %}}
Loading

0 comments on commit 453c191

Please sign in to comment.