github.com/pulumi/pulumi-kubernetes/sdk/v3@v3.30.2/go/kubernetes/yaml/configGroup.go (about)

     1  // Copyright 2016-2021, 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  // ConfigGroup creates a set of Kubernetes resources from Kubernetes YAML. The YAML text may be supplied using
    28  // any of the following formats:
    29  //
    30  // 1. Using a filename or a list of filenames:
    31  // 2. Using a file pattern or a list of file patterns:
    32  // 3. Using a literal string containing YAML, or a list of such strings:
    33  // 4. Any combination of files, patterns, or YAML strings:
    34  //
    35  // ## Example Usage
    36  // ### Local File
    37  //
    38  // ```go
    39  // package main
    40  //
    41  // import (
    42  //
    43  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
    44  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    45  //
    46  // )
    47  //
    48  //	func main() {
    49  //	    pulumi.Run(func(ctx *pulumi.Context) error {
    50  //	        _, err := yaml.NewConfigGroup(ctx, "example",
    51  //	            &yaml.ConfigGroupArgs{
    52  //	                Files: []string{"foo.yaml"},
    53  //	            },
    54  //	        )
    55  //	        if err != nil {
    56  //	            return err
    57  //	        }
    58  //
    59  //	        return nil
    60  //	    })
    61  //	}
    62  //
    63  // ```
    64  // ### Multiple Local Files
    65  //
    66  // ```go
    67  // package main
    68  //
    69  // import (
    70  //
    71  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
    72  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    73  //
    74  // )
    75  //
    76  //	func main() {
    77  //	    pulumi.Run(func(ctx *pulumi.Context) error {
    78  //	        _, err := yaml.NewConfigGroup(ctx, "example",
    79  //	            &yaml.ConfigGroupArgs{
    80  //	                Files: []string{"foo.yaml", "bar.yaml"},
    81  //	            },
    82  //	        )
    83  //	        if err != nil {
    84  //	            return err
    85  //	        }
    86  //
    87  //	        return nil
    88  //	    })
    89  //	}
    90  //
    91  // ```
    92  // ### Local File Pattern
    93  //
    94  // ```go
    95  // package main
    96  //
    97  // import (
    98  //
    99  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
   100  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
   101  //
   102  // )
   103  //
   104  //	func main() {
   105  //	    pulumi.Run(func(ctx *pulumi.Context) error {
   106  //	        _, err := yaml.NewConfigGroup(ctx, "example",
   107  //	            &yaml.ConfigGroupArgs{
   108  //	                Files: []string{"yaml/*.yaml"},
   109  //	            },
   110  //	        )
   111  //	        if err != nil {
   112  //	            return err
   113  //	        }
   114  //
   115  //	        return nil
   116  //	    })
   117  //	}
   118  //
   119  // ```
   120  // ### Multiple Local File Patterns
   121  //
   122  // ```go
   123  // package main
   124  //
   125  // import (
   126  //
   127  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
   128  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
   129  //
   130  // )
   131  //
   132  //	func main() {
   133  //	    pulumi.Run(func(ctx *pulumi.Context) error {
   134  //	        _, err := yaml.NewConfigGroup(ctx, "example",
   135  //	            &yaml.ConfigGroupArgs{
   136  //	                Files: []string{"yaml/*.yaml", "bar/*.yaml"},
   137  //	            },
   138  //	        )
   139  //	        if err != nil {
   140  //	            return err
   141  //	        }
   142  //
   143  //	        return nil
   144  //	    })
   145  //	}
   146  //
   147  // ```
   148  // ### Literal YAML String
   149  //
   150  // ```go
   151  // package main
   152  //
   153  // import (
   154  //
   155  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
   156  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
   157  //
   158  // )
   159  //
   160  //	func main() {
   161  //	    pulumi.Run(func(ctx *pulumi.Context) error {
   162  //	        _, err := yaml.NewConfigGroup(ctx, "example",
   163  //	            &yaml.ConfigGroupArgs{
   164  //	                YAML: []string{
   165  //	                    `
   166  //
   167  // apiVersion: v1
   168  // kind: Namespace
   169  // metadata:
   170  //
   171  //	name: foo
   172  //
   173  // `,
   174  //
   175  //	                },
   176  //	            })
   177  //	        if err != nil {
   178  //	            return err
   179  //	        }
   180  //
   181  //	        return nil
   182  //	    })
   183  //	}
   184  //
   185  // ```
   186  // ### YAML with Transformations
   187  //
   188  // ```go
   189  // package main
   190  //
   191  // import (
   192  //
   193  //	"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
   194  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
   195  //
   196  // )
   197  //
   198  //	func main() {
   199  //	    pulumi.Run(func(ctx *pulumi.Context) error {
   200  //	        _, err := yaml.NewConfigGroup(ctx, "example",
   201  //	            &yaml.ConfigGroupArgs{
   202  //	                Files: []string{"foo.yaml"},
   203  //	                Transformations: []yaml.Transformation{
   204  //	                    // Make every service private to the cluster, i.e., turn all services into ClusterIP
   205  //	                    // instead of LoadBalancer.
   206  //	                    func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
   207  //	                        if state["kind"] == "Service" {
   208  //	                            spec := state["spec"].(map[string]interface{})
   209  //	                            spec["type"] = "ClusterIP"
   210  //	                        }
   211  //	                    },
   212  //
   213  //	                    // Set a resource alias for a previous name.
   214  //	                    func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
   215  //	                        if state["kind"] == "Deployment" {
   216  //	                            aliases := pulumi.Aliases([]pulumi.Alias{
   217  //	                                {
   218  //	                                    Name: pulumi.String("oldName"),
   219  //	                                },
   220  //	                            })
   221  //	                            opts = append(opts, aliases)
   222  //	                        }
   223  //	                    },
   224  //
   225  //	                    // Omit a resource from the Chart by transforming the specified resource definition
   226  //	                    // to an empty List.
   227  //	                    func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
   228  //	                        name := state["metadata"].(map[string]interface{})["name"]
   229  //	                        if state["kind"] == "Pod" && name == "test" {
   230  //	                            state["apiVersion"] = "core/v1"
   231  //	                            state["kind"] = "List"
   232  //	                        }
   233  //	                    },
   234  //	                },
   235  //	            },
   236  //	        )
   237  //	        if err != nil {
   238  //	            return err
   239  //	        }
   240  //
   241  //	        return nil
   242  //	    })
   243  //	}
   244  //
   245  // ```
   246  type ConfigGroup struct {
   247  	pulumi.ResourceState
   248  
   249  	Resources map[string]pulumi.Resource
   250  }
   251  
   252  // The set of arguments for constructing a ConfigGroup resource.
   253  type ConfigGroupArgs struct {
   254  	// Files is a set of paths, globs, or URLs that uniquely identify files.
   255  	Files []string
   256  	// YAML is list of strings containing Kubernetes resource definitions in YAML.
   257  	YAML []string
   258  	// Objs is a collection of object maps representing Kubernetes resources.
   259  	Objs []map[string]interface{}
   260  	// Transformations is an optional list of transformations to apply to Kubernetes resource definitions
   261  	// before registering with the engine.
   262  	Transformations []Transformation
   263  	// ResourcePrefix is an optional prefix for the auto-generated resource names. For example, a resource named `bar`
   264  	// created with resource prefix of `"foo"` would produce a resource named `"foo-bar"`.
   265  	ResourcePrefix string
   266  	// Skip await logic for all resources in this YAML. Resources will be marked ready as soon as they are created.
   267  	// Warning: This option should not be used if you have resources depending on Outputs from the YAML.
   268  	SkipAwait bool
   269  }
   270  
   271  // NewConfigGroup registers a new resource with the given unique name, arguments, and options.
   272  func NewConfigGroup(ctx *pulumi.Context,
   273  	name string, args *ConfigGroupArgs, opts ...pulumi.ResourceOption) (*ConfigGroup, error) {
   274  
   275  	// Register the resulting resource state.
   276  	configGroup := &ConfigGroup{
   277  		Resources: map[string]pulumi.Resource{},
   278  	}
   279  	err := ctx.RegisterComponentResource("kubernetes:yaml:ConfigGroup", name, configGroup, opts...)
   280  	if err != nil {
   281  		return nil, err
   282  	}
   283  
   284  	// Now provision all child resources by parsing the YAML files.
   285  	if args != nil {
   286  		// Honor the resource name prefix if specified.
   287  		if args.ResourcePrefix != "" {
   288  			name = args.ResourcePrefix + "-" + name
   289  		}
   290  
   291  		// Parse and decode the YAML files.
   292  		parseOpts := append(opts, pulumi.Parent(configGroup))
   293  		rs, err := parseDecodeYamlFiles(ctx, args, true, parseOpts...)
   294  		if err != nil {
   295  			return nil, err
   296  		}
   297  		configGroup.Resources = rs
   298  	}
   299  
   300  	// Finally, register all of the resources found.
   301  	err = ctx.RegisterResourceOutputs(configGroup, pulumi.Map{})
   302  	if err != nil {
   303  		return nil, errors.Wrapf(err, "registering child resources")
   304  	}
   305  
   306  	return configGroup, nil
   307  }
   308  
   309  // GetResource returns a resource defined by a built-in Kubernetes group/version/kind, name and namespace.
   310  // For example, GetResource("v1/Pod", "foo", "") would return a Pod called "foo" from the "default" namespace.
   311  func (cf *ConfigGroup) GetResource(gvk, name, namespace string) pulumi.Resource {
   312  	id := name
   313  	if len(namespace) > 0 && namespace != "default" {
   314  		id = fmt.Sprintf("%s/%s", namespace, name)
   315  	}
   316  	key := fmt.Sprintf("%s::%s", gvk, id)
   317  	return cf.Resources[key]
   318  }