github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/docs/gen_kubernetes.go (about)

     1  //go:generate go run bundler.go
     2  
     3  // Copyright 2016-2020, Pulumi Corporation.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  // Pulling out some of the repeated strings tokens into constants would harm readability, so we just ignore the
    18  // goconst linter's warning.
    19  //
    20  // nolint: lll, goconst
    21  package docs
    22  
    23  import (
    24  	"fmt"
    25  	"path"
    26  	"strings"
    27  
    28  	"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
    29  )
    30  
    31  func isKubernetesPackage(pkg *schema.Package) bool {
    32  	return pkg.Name == "kubernetes"
    33  }
    34  
    35  func (mod *modContext) isKubernetesOverlayModule() bool {
    36  	// The CustomResource overlay resource is directly under the apiextensions module
    37  	// and not under a version, so we include that. The Directory overlay resource is directly under the
    38  	// kustomize module. The resources under helm and yaml are always under a version.
    39  	return mod.mod == "apiextensions" || mod.mod == "kustomize" ||
    40  		strings.HasPrefix(mod.mod, "helm") || strings.HasPrefix(mod.mod, "yaml")
    41  }
    42  
    43  func (mod *modContext) isComponentResource() bool {
    44  	// TODO: Support this more generally. For now, only the Helm, Kustomize, and YAML overlays use ComponentResources.
    45  	return strings.HasPrefix(mod.mod, "helm") ||
    46  		strings.HasPrefix(mod.mod, "kustomize") ||
    47  		strings.HasPrefix(mod.mod, "yaml")
    48  }
    49  
    50  // getKubernetesOverlayPythonFormalParams returns the formal params to render
    51  // for a Kubernetes overlay resource. These resources do not follow convention
    52  // that other resources do, so it is best to manually set these.
    53  func getKubernetesOverlayPythonFormalParams(modName string) []formalParam {
    54  	var params []formalParam
    55  	switch modName {
    56  	case "helm.sh/v2", "helm.sh/v3":
    57  		// Chart options.
    58  		params = []formalParam{
    59  			{
    60  				Name:    "release_name",
    61  				Type:    propertyType{Name: "str"},
    62  				Comment: "Name of the Chart (e.g., nginx-ingress).",
    63  			},
    64  			{
    65  				Name:    "config",
    66  				Comment: "Configuration options for the Chart.",
    67  			},
    68  			{
    69  				Name:         "opts",
    70  				DefaultValue: "=None",
    71  				Comment:      "A bag of options that control this resource's behavior.",
    72  			},
    73  		}
    74  	case "kustomize":
    75  		params = []formalParam{
    76  			{
    77  				Name: "directory",
    78  			},
    79  			{
    80  				Name:         "opts",
    81  				DefaultValue: "=None",
    82  			},
    83  			{
    84  				Name:         "transformations",
    85  				DefaultValue: "=None",
    86  			},
    87  			{
    88  				Name:         "resource_prefix",
    89  				DefaultValue: "=None",
    90  			},
    91  		}
    92  	case "yaml":
    93  		params = []formalParam{
    94  			{
    95  				Name: "file",
    96  			},
    97  			{
    98  				Name:         "opts",
    99  				DefaultValue: "=None",
   100  			},
   101  			{
   102  				Name:         "transformations",
   103  				DefaultValue: "=None",
   104  			},
   105  			{
   106  				Name:         "resource_prefix",
   107  				DefaultValue: "=None",
   108  			},
   109  		}
   110  	case "apiextensions":
   111  		params = []formalParam{
   112  			{
   113  				Name: "api_version",
   114  			},
   115  			{
   116  				Name: "kind",
   117  			},
   118  			{
   119  				Name:         "metadata",
   120  				DefaultValue: "=None",
   121  			},
   122  			{
   123  				Name:         "opts",
   124  				DefaultValue: "=None",
   125  			},
   126  		}
   127  	default:
   128  		panic(fmt.Sprintf("Unknown kubernetes overlay module %q", modName))
   129  	}
   130  	return params
   131  }
   132  
   133  func getKubernetesMod(pkg *schema.Package, token string, modules map[string]*modContext, tool string) *modContext {
   134  	modName := pkg.TokenToModule(token)
   135  	// Kubernetes' moduleFormat in the schema will match everything
   136  	// in the token. So strip some well-known domain name parts from the module
   137  	// names.
   138  	modName = strings.TrimSuffix(modName, ".k8s.io")
   139  	modName = strings.TrimSuffix(modName, ".apiserver")
   140  	modName = strings.TrimSuffix(modName, ".authorization")
   141  
   142  	mod, ok := modules[modName]
   143  	if !ok {
   144  		mod = &modContext{
   145  			pkg:  pkg,
   146  			mod:  modName,
   147  			tool: tool,
   148  		}
   149  
   150  		if modName != "" {
   151  			parentName := path.Dir(modName)
   152  			// If the parent name is blank, it means this is the package-level.
   153  			if parentName == "." || parentName == "" {
   154  				parentName = ":index:"
   155  			} else {
   156  				parentName = ":" + parentName + ":"
   157  			}
   158  			parent := getKubernetesMod(pkg, parentName, modules, tool)
   159  			parent.children = append(parent.children, mod)
   160  		}
   161  
   162  		modules[modName] = mod
   163  	}
   164  	return mod
   165  }