...

Source file src/sigs.k8s.io/kustomize/api/konfig/plugins.go

Documentation: sigs.k8s.io/kustomize/api/konfig

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package konfig
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"runtime"
    10  
    11  	"sigs.k8s.io/kustomize/api/types"
    12  	"sigs.k8s.io/kustomize/kyaml/filesys"
    13  )
    14  
    15  const (
    16  	// Symbol that must be used inside Go plugins.
    17  	PluginSymbol = "KustomizePlugin"
    18  
    19  	// Name of environment variable used to set AbsPluginHome.
    20  	// See that variable for an explanation.
    21  	KustomizePluginHomeEnv = "KUSTOMIZE_PLUGIN_HOME"
    22  
    23  	// Relative path below XDG_CONFIG_HOME/kustomize to find plugins.
    24  	// e.g. AbsPluginHome = XDG_CONFIG_HOME/kustomize/plugin
    25  	RelPluginHome = "plugin"
    26  
    27  	// Location of builtin plugins below AbsPluginHome.
    28  	BuiltinPluginPackage = "builtin"
    29  
    30  	// The value of kubernetes ApiVersion to use in configuration
    31  	// files for builtin plugins.
    32  	// The value for non-builtins can be anything.
    33  	BuiltinPluginApiVersion = BuiltinPluginPackage
    34  
    35  	// Domain from which kustomize code is imported, for locating
    36  	// plugin source code under $GOPATH when GOPATH is defined.
    37  	DomainName = "sigs.k8s.io"
    38  
    39  	// Injected into plugin paths when plugins are disabled.
    40  	// Provides a clue in flows that shouldn't happen.
    41  	NoPluginHomeSentinal = "/No/non-builtin/plugins!"
    42  )
    43  
    44  type NotedFunc struct {
    45  	Note string
    46  	F    func() string
    47  }
    48  
    49  // DefaultAbsPluginHome returns the absolute path in the given file
    50  // system to first directory that looks like a good candidate for
    51  // the home of kustomize plugins.
    52  func DefaultAbsPluginHome(fSys filesys.FileSystem) (string, error) {
    53  	return FirstDirThatExistsElseError(
    54  		"plugin root", fSys, []NotedFunc{
    55  			{
    56  				Note: "homed in $" + KustomizePluginHomeEnv,
    57  				F: func() string {
    58  					return os.Getenv(KustomizePluginHomeEnv)
    59  				},
    60  			},
    61  			{
    62  				Note: "homed in $" + XdgConfigHomeEnv,
    63  				F: func() string {
    64  					if root := os.Getenv(XdgConfigHomeEnv); root != "" {
    65  						return filepath.Join(root, ProgramName, RelPluginHome)
    66  					}
    67  					// do not look in "kustomize/plugin" if XdgConfigHomeEnv is unset
    68  					return ""
    69  				},
    70  			},
    71  			{
    72  				Note: "homed in default value of $" + XdgConfigHomeEnv,
    73  				F: func() string {
    74  					return filepath.Join(
    75  						HomeDir(), XdgConfigHomeEnvDefault,
    76  						ProgramName, RelPluginHome)
    77  				},
    78  			},
    79  			{
    80  				Note: "homed in home directory",
    81  				F: func() string {
    82  					return filepath.Join(
    83  						HomeDir(), ProgramName, RelPluginHome)
    84  				},
    85  			},
    86  		})
    87  }
    88  
    89  // FirstDirThatExistsElseError tests different path functions for
    90  // existence, returning the first that works, else error if all fail.
    91  func FirstDirThatExistsElseError(
    92  	what string,
    93  	fSys filesys.FileSystem,
    94  	pathFuncs []NotedFunc) (string, error) {
    95  	var nope []types.Pair
    96  	for _, dt := range pathFuncs {
    97  		if dir := dt.F(); dir != "" {
    98  			if fSys.Exists(dir) {
    99  				return dir, nil
   100  			}
   101  			nope = append(nope, types.Pair{Key: dt.Note, Value: dir})
   102  		} else {
   103  			nope = append(nope, types.Pair{Key: dt.Note, Value: "<no value>"})
   104  		}
   105  	}
   106  	return "", types.NewErrUnableToFind(what, nope)
   107  }
   108  
   109  func HomeDir() string {
   110  	home := os.Getenv(homeEnv())
   111  	if len(home) > 0 {
   112  		return home
   113  	}
   114  	return "~"
   115  }
   116  
   117  func homeEnv() string {
   118  	if runtime.GOOS == "windows" {
   119  		return "USERPROFILE"
   120  	}
   121  	return "HOME"
   122  }
   123  
   124  func CurrentWorkingDir() string {
   125  	// Try for full path first to be explicit.
   126  	pwd := os.Getenv(pwdEnv())
   127  	if len(pwd) > 0 {
   128  		return pwd
   129  	}
   130  	return filesys.SelfDir
   131  }
   132  
   133  func pwdEnv() string {
   134  	if runtime.GOOS == "windows" {
   135  		return "CD"
   136  	}
   137  	return "PWD"
   138  }
   139  

View as plain text