mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Loader FS and empty env fix
This commit is contained in:
@@ -13,10 +13,10 @@ import (
|
||||
. "sigs.k8s.io/kustomize/api/internal/plugins/execplugin"
|
||||
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
fLdr "sigs.k8s.io/kustomize/api/loader"
|
||||
"sigs.k8s.io/kustomize/api/provider"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
)
|
||||
|
||||
func TestExecPluginConfig(t *testing.T) {
|
||||
@@ -45,7 +45,7 @@ s/$BAR/bar baz/g
|
||||
})
|
||||
|
||||
pluginConfig.RemoveBuildAnnotations()
|
||||
loader := pLdr.NewLoader(konfig.DisabledPluginConfig(), rf)
|
||||
loader := pLdr.NewLoader(types.DisabledPluginConfig(), rf, fSys)
|
||||
pluginPath, err := loader.AbsolutePluginPath(pluginConfig.OrgId())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
|
||||
@@ -30,6 +30,7 @@ import (
|
||||
type Loader struct {
|
||||
pc *types.PluginConfig
|
||||
rf *resmap.Factory
|
||||
fs filesys.FileSystem
|
||||
|
||||
// absolutePluginHome caches the location of a valid plugin root directory.
|
||||
// It should only be set once the directory's existence has been confirmed.
|
||||
@@ -37,8 +38,8 @@ type Loader struct {
|
||||
}
|
||||
|
||||
func NewLoader(
|
||||
pc *types.PluginConfig, rf *resmap.Factory) *Loader {
|
||||
return &Loader{pc: pc, rf: rf}
|
||||
pc *types.PluginConfig, rf *resmap.Factory, fs filesys.FileSystem) *Loader {
|
||||
return &Loader{pc: pc, rf: rf, fs: fs}
|
||||
}
|
||||
|
||||
func (l *Loader) LoadGenerators(
|
||||
@@ -135,7 +136,7 @@ func (l *Loader) absPluginHome() (string, error) {
|
||||
}
|
||||
|
||||
// Check default locations for a valid plugin root, and cache it if found.
|
||||
dir, err := konfig.DefaultAbsPluginHome(filesys.MakeFsOnDisk())
|
||||
dir, err := konfig.DefaultAbsPluginHome(l.fs)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filesys"
|
||||
. "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/loader"
|
||||
"sigs.k8s.io/kustomize/api/provider"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
@@ -52,9 +51,10 @@ func TestLoader(t *testing.T) {
|
||||
defer th.Reset()
|
||||
p := provider.NewDefaultDepProvider()
|
||||
rmF := resmap.NewFactory(p.GetResourceFactory())
|
||||
fsys := filesys.MakeFsInMemory()
|
||||
fLdr, err := loader.NewLoader(
|
||||
loader.RestrictionRootOnly,
|
||||
filesys.Separator, filesys.MakeFsInMemory())
|
||||
filesys.Separator, fsys)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -66,8 +66,8 @@ func TestLoader(t *testing.T) {
|
||||
for _, behavior := range []types.BuiltinPluginLoadingOptions{
|
||||
/* types.BploUseStaticallyLinked,
|
||||
types.BploLoadFromFileSys */} {
|
||||
c := konfig.EnabledPluginConfig(behavior)
|
||||
pLdr := NewLoader(c, rmF)
|
||||
c := types.EnabledPluginConfig(behavior)
|
||||
pLdr := NewLoader(c, rmF, fsys)
|
||||
if pLdr == nil {
|
||||
t.Fatal("expect non-nil loader")
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ import (
|
||||
"sigs.k8s.io/kustomize/api/filesys"
|
||||
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||
"sigs.k8s.io/kustomize/api/internal/target"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
fLdr "sigs.k8s.io/kustomize/api/loader"
|
||||
"sigs.k8s.io/kustomize/api/provider"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
)
|
||||
|
||||
func makeAndLoadKustTarget(
|
||||
@@ -37,10 +37,10 @@ func makeKustTargetWithRf(
|
||||
t.Fatal(err)
|
||||
}
|
||||
rf := resmap.NewFactory(pvd.GetResourceFactory())
|
||||
pc := konfig.DisabledPluginConfig()
|
||||
pc := types.DisabledPluginConfig()
|
||||
return target.NewKustTarget(
|
||||
ldr,
|
||||
valtest_test.MakeFakeValidator(),
|
||||
rf,
|
||||
pLdr.NewLoader(pc, rf))
|
||||
pLdr.NewLoader(pc, rf, fSys))
|
||||
}
|
||||
|
||||
@@ -41,24 +41,6 @@ const (
|
||||
NoPluginHomeSentinal = "/No/non-builtin/plugins!"
|
||||
)
|
||||
|
||||
func EnabledPluginConfig(b types.BuiltinPluginLoadingOptions) *types.PluginConfig {
|
||||
return MakePluginConfig(types.PluginRestrictionsNone, b)
|
||||
}
|
||||
|
||||
func DisabledPluginConfig() *types.PluginConfig {
|
||||
return MakePluginConfig(
|
||||
types.PluginRestrictionsBuiltinsOnly,
|
||||
types.BploUseStaticallyLinked)
|
||||
}
|
||||
|
||||
func MakePluginConfig(pr types.PluginRestrictions,
|
||||
b types.BuiltinPluginLoadingOptions) *types.PluginConfig {
|
||||
return &types.PluginConfig{
|
||||
PluginRestrictions: pr,
|
||||
BpLoadingOptions: b,
|
||||
}
|
||||
}
|
||||
|
||||
type NotedFunc struct {
|
||||
Note string
|
||||
F func() string
|
||||
@@ -79,9 +61,11 @@ func DefaultAbsPluginHome(fSys filesys.FileSystem) (string, error) {
|
||||
{
|
||||
Note: "homed in $" + XdgConfigHomeEnv,
|
||||
F: func() string {
|
||||
return filepath.Join(
|
||||
os.Getenv(XdgConfigHomeEnv),
|
||||
ProgramName, RelPluginHome)
|
||||
if root := os.Getenv(XdgConfigHomeEnv); root != "" {
|
||||
return filepath.Join(root, ProgramName, RelPluginHome)
|
||||
}
|
||||
// do not look in "kustomize/plugin" if XdgConfigHomeEnv is unset
|
||||
return ""
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -110,11 +94,14 @@ func FirstDirThatExistsElseError(
|
||||
pathFuncs []NotedFunc) (string, error) {
|
||||
var nope []types.Pair
|
||||
for _, dt := range pathFuncs {
|
||||
dir := dt.F()
|
||||
if fSys.Exists(dir) {
|
||||
return dir, nil
|
||||
if dir := dt.F(); dir != "" {
|
||||
if fSys.Exists(dir) {
|
||||
return dir, nil
|
||||
}
|
||||
nope = append(nope, types.Pair{Key: dt.Note, Value: dir})
|
||||
} else {
|
||||
nope = append(nope, types.Pair{Key: dt.Note, Value: "<no value>"})
|
||||
}
|
||||
nope = append(nope, types.Pair{Key: dt.Note, Value: dir})
|
||||
}
|
||||
return "", types.NewErrUnableToFind(what, nope)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filesys"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
)
|
||||
@@ -28,6 +30,34 @@ func TestDefaultAbsPluginHome_NoKustomizePluginHomeEnv(t *testing.T) {
|
||||
if !types.IsErrUnableToFind(err) {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
for _, expectedMsg := range []string{
|
||||
"unable to find plugin root - tried:",
|
||||
"('<no value>'; homed in $KUSTOMIZE_PLUGIN_HOME)",
|
||||
"; homed in $XDG_CONFIG_HOME)",
|
||||
"/.config/kustomize/plugin'; homed in default value of $XDG_CONFIG_HOME)",
|
||||
"/kustomize/plugin'; homed in home directory)",
|
||||
} {
|
||||
assert.Contains(t, err.Error(), expectedMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultAbsPluginHome_EmptyKustomizePluginHomeEnv(t *testing.T) {
|
||||
keep, isSet := os.LookupEnv(KustomizePluginHomeEnv)
|
||||
os.Setenv(KustomizePluginHomeEnv, "")
|
||||
|
||||
_, err := DefaultAbsPluginHome(filesys.MakeFsInMemory())
|
||||
if !isSet {
|
||||
_ = os.Unsetenv(KustomizePluginHomeEnv)
|
||||
} else {
|
||||
_ = os.Setenv(KustomizePluginHomeEnv, keep)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatalf("expected err")
|
||||
}
|
||||
if !types.IsErrUnableToFind(err) {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
assert.Contains(t, err.Error(), "('<no value>'; homed in $KUSTOMIZE_PLUGIN_HOME)")
|
||||
}
|
||||
|
||||
func TestDefaultAbsPluginHome_WithKustomizePluginHomeEnv(t *testing.T) {
|
||||
@@ -89,6 +119,25 @@ func TestDefaultAbsPluginHomeNoConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultAbsPluginHomeEmptyXdgConfig(t *testing.T) {
|
||||
keep, isSet := os.LookupEnv(XdgConfigHomeEnv)
|
||||
os.Setenv(XdgConfigHomeEnv, "")
|
||||
if isSet {
|
||||
_ = os.Unsetenv(XdgConfigHomeEnv)
|
||||
}
|
||||
_, err := DefaultAbsPluginHome(filesys.MakeFsInMemory())
|
||||
if isSet {
|
||||
os.Setenv(XdgConfigHomeEnv, keep)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatalf("expected err")
|
||||
}
|
||||
if !types.IsErrUnableToFind(err) {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
assert.Contains(t, err.Error(), "('<no value>'; homed in $XDG_CONFIG_HOME)")
|
||||
}
|
||||
|
||||
func TestDefaultAbsPluginHomeNoXdgWithDotConfig(t *testing.T) {
|
||||
fSys := filesys.MakeFsInMemory()
|
||||
configDir := filepath.Join(
|
||||
|
||||
@@ -66,7 +66,8 @@ func (b *Kustomizer) Run(
|
||||
ldr,
|
||||
b.depProvider.GetFieldValidator(),
|
||||
resmapFactory,
|
||||
pLdr.NewLoader(b.options.PluginConfig, resmapFactory),
|
||||
// The plugin configs are always located on disk, regardless of the fSys passed in
|
||||
pLdr.NewLoader(b.options.PluginConfig, resmapFactory, filesys.MakeFsOnDisk()),
|
||||
)
|
||||
err = kt.Load()
|
||||
if err != nil {
|
||||
|
||||
@@ -5,7 +5,6 @@ package krusty
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
)
|
||||
|
||||
@@ -42,7 +41,7 @@ func MakeDefaultOptions() *Options {
|
||||
AddManagedbyLabel: false,
|
||||
LoadRestrictions: types.LoadRestrictionsRootOnly,
|
||||
DoPrune: false,
|
||||
PluginConfig: konfig.DisabledPluginConfig(),
|
||||
PluginConfig: types.DisabledPluginConfig(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ func (th Harness) MakeOptionsPluginsDisabled() krusty.Options {
|
||||
|
||||
// Enables use of non-builtin plugins.
|
||||
func (th Harness) MakeOptionsPluginsEnabled() krusty.Options {
|
||||
pc := konfig.EnabledPluginConfig(types.BploLoadFromFileSys)
|
||||
pc := types.EnabledPluginConfig(types.BploLoadFromFileSys)
|
||||
o := *krusty.MakeDefaultOptions()
|
||||
o.PluginConfig = pc
|
||||
return o
|
||||
|
||||
@@ -40,7 +40,7 @@ type HarnessEnhanced struct {
|
||||
func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced {
|
||||
pte := newPluginTestEnv(t).set()
|
||||
|
||||
pc := konfig.EnabledPluginConfig(types.BploLoadFromFileSys)
|
||||
pc := types.EnabledPluginConfig(types.BploLoadFromFileSys)
|
||||
pc.FnpLoadingOptions.EnableStar = true
|
||||
p := provider.NewDefaultDepProvider()
|
||||
resourceFactory := p.GetResourceFactory()
|
||||
@@ -50,7 +50,8 @@ func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced {
|
||||
Harness: MakeHarness(t),
|
||||
pte: pte,
|
||||
rf: resmapFactory,
|
||||
pl: pLdr.NewLoader(pc, resmapFactory)}
|
||||
// The plugin configs are always located on disk, regardless of the test harness's FS
|
||||
pl: pLdr.NewLoader(pc, resmapFactory, filesys.MakeFsOnDisk())}
|
||||
|
||||
// Point the file loader to the root ('/') of the in-memory file system.
|
||||
result.ResetLoaderRoot(filesys.Separator)
|
||||
|
||||
@@ -14,3 +14,21 @@ type PluginConfig struct {
|
||||
// FnpLoadingOptions sets the way function-based plugin behaviors.
|
||||
FnpLoadingOptions FnPluginLoadingOptions
|
||||
}
|
||||
|
||||
func EnabledPluginConfig(b BuiltinPluginLoadingOptions) *PluginConfig {
|
||||
return MakePluginConfig(PluginRestrictionsNone, b)
|
||||
}
|
||||
|
||||
func DisabledPluginConfig() *PluginConfig {
|
||||
return MakePluginConfig(
|
||||
PluginRestrictionsBuiltinsOnly,
|
||||
BploUseStaticallyLinked)
|
||||
}
|
||||
|
||||
func MakePluginConfig(pr PluginRestrictions,
|
||||
b BuiltinPluginLoadingOptions) *PluginConfig {
|
||||
return &PluginConfig{
|
||||
PluginRestrictions: pr,
|
||||
BpLoadingOptions: b,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user