github.com/pulumi/pulumi-kubernetes/sdk/v3@v3.30.2/go/kubernetes/yaml/configFile.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 yaml
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/pkg/errors"
    24  	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    25  )
    26  
    27  // ConfigFile creates a set of Kubernetes resources from a Kubernetes YAML file.
    28  //
    29  // ## Example Usage
    30  // ### Local File
    31  //
    32  // ```go
    33  // package main
    34  //
    35  // import (
    36  //
    37  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
    38  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    39  //
    40  // )
    41  //
    42  //	func main() {
    43  //	    pulumi.Run(func(ctx *pulumi.Context) error {
    44  //	        _, err := yaml.NewConfigFile(ctx, "example",
    45  //	            &yaml.ConfigFileArgs{
    46  //	                File: "foo.yaml",
    47  //	            },
    48  //	        )
    49  //	        if err != nil {
    50  //	            return err
    51  //	        }
    52  //
    53  //	        return nil
    54  //	    })
    55  //	}
    56  //
    57  // ```
    58  // ### YAML with Transformations
    59  //
    60  // ```go
    61  // package main
    62  //
    63  // import (
    64  //
    65  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
    66  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    67  //
    68  // )
    69  //
    70  //	func main() {
    71  //	    pulumi.Run(func(ctx *pulumi.Context) error {
    72  //	        _, err := yaml.NewConfigFile(ctx, "example",
    73  //	            &yaml.ConfigFileArgs{
    74  //	                File: "foo.yaml",
    75  //	                Transformations: []yaml.Transformation{
    76  //	                    // Make every service private to the cluster, i.e., turn all services into ClusterIP
    77  //	                    // instead of LoadBalancer.
    78  //	                    func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
    79  //	                        if state["kind"] == "Service" {
    80  //	                            spec := state["spec"].(map[string]interface{})
    81  //	                            spec["type"] = "ClusterIP"
    82  //	                        }
    83  //	                    },
    84  //
    85  //	                    // Set a resource alias for a previous name.
    86  //	                    func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
    87  //	                        if state["kind"] == "Deployment" {
    88  //	                            aliases := pulumi.Aliases([]pulumi.Alias{
    89  //	                                {
    90  //	                                    Name: pulumi.String("oldName"),
    91  //	                                },
    92  //	                            })
    93  //	                            opts = append(opts, aliases)
    94  //	                        }
    95  //	                    },
    96  //
    97  //	                    // Omit a resource from the Chart by transforming the specified resource definition
    98  //	                    // to an empty List.
    99  //	                    func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
   100  //	                        name := state["metadata"].(map[string]interface{})["name"]
   101  //	                        if state["kind"] == "Pod" && name == "test" {
   102  //	                            state["apiVersion"] = "core/v1"
   103  //	                            state["kind"] = "List"
   104  //	                        }
   105  //	                    },
   106  //	                },
   107  //	            },
   108  //	        )
   109  //	        if err != nil {
   110  //	            return err
   111  //	        }
   112  //
   113  //	        return nil
   114  //	    })
   115  //	}
   116  //
   117  // ```
   118  type ConfigFile struct {
   119  	pulumi.ResourceState
   120  
   121  	Resources map[string]pulumi.Resource
   122  }
   123  
   124  // The set of arguments for constructing a ConfigFile resource.
   125  type ConfigFileArgs struct {
   126  	// File is a path or URL that uniquely identifies a file.
   127  	File string
   128  	// Transformations is an optional list of transformations to apply to Kubernetes resource definitions
   129  	// before registering with the engine.
   130  	Transformations []Transformation
   131  	// ResourcePrefix is an optional prefix for the auto-generated resource names. For example, a resource named `bar`
   132  	// created with resource prefix of `"foo"` would produce a resource named `"foo-bar"`.
   133  	ResourcePrefix string
   134  	// Skip await logic for all resources in this YAML. Resources will be marked ready as soon as they are created.
   135  	// Warning: This option should not be used if you have resources depending on Outputs from the YAML.
   136  	SkipAwait bool
   137  }
   138  
   139  // NewConfigFile registers a new resource with the given unique name, arguments, and options.
   140  func NewConfigFile(ctx *pulumi.Context,
   141  	name string, args *ConfigFileArgs, opts ...pulumi.ResourceOption) (*ConfigFile, error) {
   142  
   143  	// Register the resulting resource state.
   144  	configFile := &ConfigFile{
   145  		Resources: map[string]pulumi.Resource{},
   146  	}
   147  	err := ctx.RegisterComponentResource("kubernetes:yaml:ConfigFile", name, configFile, opts...)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  
   152  	// Now provision all child resources by parsing the YAML file.
   153  	if args != nil {
   154  		// Honor the resource name prefix if specified.
   155  		if args.ResourcePrefix != "" {
   156  			name = args.ResourcePrefix + "-" + name
   157  		}
   158  
   159  		transformations := args.Transformations
   160  		if args.SkipAwait {
   161  			transformations = AddSkipAwaitTransformation(transformations)
   162  		}
   163  
   164  		// Parse and decode the YAML files.
   165  		parseOpts := append(opts, pulumi.Parent(configFile))
   166  		rs, err := parseDecodeYamlFiles(ctx, &ConfigGroupArgs{
   167  			Files:           []string{args.File},
   168  			Transformations: args.Transformations,
   169  			ResourcePrefix:  args.ResourcePrefix,
   170  		}, true, parseOpts...)
   171  		if err != nil {
   172  			return nil, err
   173  		}
   174  		configFile.Resources = rs
   175  	}
   176  
   177  	// Finally, register all of the resources found.
   178  	err = ctx.RegisterResourceOutputs(configFile, pulumi.Map{})
   179  	if err != nil {
   180  		return nil, errors.Wrapf(err, "registering child resources")
   181  	}
   182  
   183  	return configFile, nil
   184  }
   185  
   186  // GetResource returns a resource defined by a built-in Kubernetes group/version/kind, name and namespace.
   187  // For example, GetResource("v1/Pod", "foo", "") would return a Pod called "foo" from the "default" namespace.
   188  func (cf *ConfigFile) GetResource(gvk, name, namespace string) pulumi.Resource {
   189  	id := name
   190  	if len(namespace) > 0 && namespace != "default" {
   191  		id = fmt.Sprintf("%s/%s", namespace, name)
   192  	}
   193  	key := fmt.Sprintf("%s::%s", gvk, id)
   194  	return cf.Resources[key]
   195  }