github.com/pulumi/pulumi-kubernetes/sdk/v3@v3.30.2/go/kubernetes/kustomize/directory.go (about)

     1  // Copyright 2016-2022, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // *** WARNING: this file was generated by pulumigen. ***
    16  // *** Do not edit by hand unless you're certain you know what you are doing! ***
    17  
    18  package kustomize
    19  
    20  import (
    21  	"github.com/pkg/errors"
    22  	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
    23  	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    24  )
    25  
    26  // Directory is a component representing a collection of resources described by a kustomize directory (kustomization).
    27  //
    28  // ## Example Usage
    29  // ### Local Kustomize Directory
    30  //
    31  // ```go
    32  // package main
    33  //
    34  // import (
    35  //
    36  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/kustomize"
    37  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    38  //
    39  // )
    40  //
    41  //	func main() {
    42  //	    pulumi.Run(func(ctx *pulumi.Context) error {
    43  //	        _, err := kustomize.NewDirectory(ctx, "helloWorldLocal",
    44  //	            kustomize.DirectoryArgs{
    45  //	                Directory: pulumi.String("./helloWorld"),
    46  //	            },
    47  //	        )
    48  //	        if err != nil {
    49  //	            return err
    50  //	        }
    51  //
    52  //	        return nil
    53  //	    })
    54  //	}
    55  //
    56  // ```
    57  // ### Kustomize Directory from a Git Repo
    58  //
    59  // ```go
    60  // package main
    61  //
    62  // import (
    63  //
    64  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/kustomize"
    65  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    66  //
    67  // )
    68  //
    69  //	func main() {
    70  //	    pulumi.Run(func(ctx *pulumi.Context) error {
    71  //	        _, err := kustomize.NewDirectory(ctx, "helloWorldRemote",
    72  //	            kustomize.DirectoryArgs{
    73  //	                Directory: pulumi.String("https://github.com/kubernetes-sigs/kustomize/tree/v3.3.1/examples/helloWorld"),
    74  //	            },
    75  //	        )
    76  //	        if err != nil {
    77  //	            return err
    78  //	        }
    79  //
    80  //	        return nil
    81  //	    })
    82  //	}
    83  //
    84  // ```
    85  // ### Kustomize Directory with Transformations
    86  //
    87  // ```go
    88  // package main
    89  //
    90  // import (
    91  //
    92  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/kustomize"
    93  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
    94  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    95  //
    96  // )
    97  //
    98  //	func main() {
    99  //	    pulumi.Run(func(ctx *pulumi.Context) error {
   100  //	        _, err := kustomize.NewDirectory(ctx, "helloWorldRemote",
   101  //	            kustomize.DirectoryArgs{
   102  //	                Directory: pulumi.String("https://github.com/kubernetes-sigs/kustomize/tree/v3.3.1/examples/helloWorld"),
   103  //	                Transformations: []yaml.Transformation{
   104  //	                    // Make every service private to the cluster, i.e., turn all services into ClusterIP
   105  //	                    // instead of LoadBalancer.
   106  //	                    func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
   107  //	                        if state["kind"] == "Service" {
   108  //	                            spec := state["spec"].(map[string]interface{})
   109  //	                            spec["type"] = "ClusterIP"
   110  //	                        }
   111  //	                    },
   112  //
   113  //	                    // Set a resource alias for a previous name.
   114  //	                    func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
   115  //	                        if state["kind"] == "Deployment" {
   116  //	                            aliases := pulumi.Aliases([]pulumi.Alias{
   117  //	                                {
   118  //	                                    Name: pulumi.String("oldName"),
   119  //	                                },
   120  //	                            })
   121  //	                            opts = append(opts, aliases)
   122  //	                        }
   123  //	                    },
   124  //
   125  //	                    // Omit a resource from the Chart by transforming the specified resource definition
   126  //	                    // to an empty List.
   127  //	                    func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
   128  //	                        name := state["metadata"].(map[string]interface{})["name"]
   129  //	                        if state["kind"] == "Pod" && name == "test" {
   130  //	                            state["apiVersion"] = "core/v1"
   131  //	                            state["kind"] = "List"
   132  //	                        }
   133  //	                    },
   134  //	                },
   135  //	            },
   136  //	        )
   137  //	        if err != nil {
   138  //	            return err
   139  //	        }
   140  //
   141  //	        return nil
   142  //	    })
   143  //	}
   144  //
   145  // ```
   146  type Directory struct {
   147  	pulumi.ResourceState
   148  
   149  	Resources pulumi.Output
   150  }
   151  
   152  // NewDirectory registers a new resource with the given unique name, arguments, and options.
   153  func NewDirectory(ctx *pulumi.Context,
   154  	name string, args DirectoryArgs, opts ...pulumi.ResourceOption) (*Directory, error) {
   155  
   156  	// Register the resulting resource state.
   157  	chart := &Directory{}
   158  	err := ctx.RegisterComponentResource("kubernetes:kustomize:Directory", name, chart, opts...)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	// Honor the resource name prefix if specified.
   164  	if args.ResourcePrefix != "" {
   165  		name = args.ResourcePrefix + "-" + name
   166  	}
   167  
   168  	parseOpts := append(opts, pulumi.Parent(chart))
   169  	resources := args.ToDirectoryArgsOutput().ApplyT(func(args directoryArgs) (map[string]pulumi.Resource, error) {
   170  		return parseDirectory(ctx, name, args, parseOpts...)
   171  	})
   172  	chart.Resources = resources
   173  
   174  	// Finally, register all of the resources found.
   175  	// Note: Go requires that we "pull" on our futures in order to get them scheduled for execution. Here, we use
   176  	// the engine's RegisterResourceOutputs to wait for the resolution of all resources that this Helm chart created.
   177  	err = ctx.RegisterResourceOutputs(chart, pulumi.Map{"resources": resources})
   178  	if err != nil {
   179  		return nil, errors.Wrap(err, "registering child resources")
   180  	}
   181  
   182  	return chart, nil
   183  }
   184  
   185  func parseDirectory(ctx *pulumi.Context, name string, args directoryArgs, opts ...pulumi.ResourceOption,
   186  ) (map[string]pulumi.Resource, error) {
   187  	invokeArgs := struct {
   188  		Directory string `pulumi:"directory"`
   189  	}{Directory: args.Directory}
   190  	var ret struct {
   191  		Result []map[string]interface{} `pulumi:"result"`
   192  	}
   193  
   194  	// Find options which are also Invoke options, and prepare them to pass to Invoke functions
   195  	var invokeOpts []pulumi.InvokeOption
   196  	for _, opt := range opts {
   197  		if invokeOpt, ok := opt.(pulumi.InvokeOption); ok {
   198  			invokeOpts = append(invokeOpts, invokeOpt)
   199  		}
   200  	}
   201  
   202  	if err := ctx.Invoke("kubernetes:kustomize:directory", &invokeArgs, &ret, invokeOpts...); err != nil {
   203  		return nil, errors.Wrap(err, "kustomize invoke failed")
   204  	}
   205  
   206  	resources, err := yaml.ParseYamlObjects(ctx, ret.Result, args.Transformations, args.ResourcePrefix, opts...)
   207  	if err != nil {
   208  		return nil, err
   209  	}
   210  	return resources, nil
   211  }