mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 02:20:53 +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"
|
. "sigs.k8s.io/kustomize/api/internal/plugins/execplugin"
|
||||||
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
|
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
|
||||||
fLdr "sigs.k8s.io/kustomize/api/loader"
|
fLdr "sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExecPluginConfig(t *testing.T) {
|
func TestExecPluginConfig(t *testing.T) {
|
||||||
@@ -45,7 +45,7 @@ s/$BAR/bar baz/g
|
|||||||
})
|
})
|
||||||
|
|
||||||
pluginConfig.RemoveBuildAnnotations()
|
pluginConfig.RemoveBuildAnnotations()
|
||||||
loader := pLdr.NewLoader(konfig.DisabledPluginConfig(), rf)
|
loader := pLdr.NewLoader(types.DisabledPluginConfig(), rf, fSys)
|
||||||
pluginPath, err := loader.AbsolutePluginPath(pluginConfig.OrgId())
|
pluginPath, err := loader.AbsolutePluginPath(pluginConfig.OrgId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected err: %v", err)
|
t.Fatalf("unexpected err: %v", err)
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import (
|
|||||||
type Loader struct {
|
type Loader struct {
|
||||||
pc *types.PluginConfig
|
pc *types.PluginConfig
|
||||||
rf *resmap.Factory
|
rf *resmap.Factory
|
||||||
|
fs filesys.FileSystem
|
||||||
|
|
||||||
// absolutePluginHome caches the location of a valid plugin root directory.
|
// absolutePluginHome caches the location of a valid plugin root directory.
|
||||||
// It should only be set once the directory's existence has been confirmed.
|
// It should only be set once the directory's existence has been confirmed.
|
||||||
@@ -37,8 +38,8 @@ type Loader struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewLoader(
|
func NewLoader(
|
||||||
pc *types.PluginConfig, rf *resmap.Factory) *Loader {
|
pc *types.PluginConfig, rf *resmap.Factory, fs filesys.FileSystem) *Loader {
|
||||||
return &Loader{pc: pc, rf: rf}
|
return &Loader{pc: pc, rf: rf, fs: fs}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Loader) LoadGenerators(
|
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.
|
// 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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
. "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
. "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/loader"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
@@ -52,9 +51,10 @@ func TestLoader(t *testing.T) {
|
|||||||
defer th.Reset()
|
defer th.Reset()
|
||||||
p := provider.NewDefaultDepProvider()
|
p := provider.NewDefaultDepProvider()
|
||||||
rmF := resmap.NewFactory(p.GetResourceFactory())
|
rmF := resmap.NewFactory(p.GetResourceFactory())
|
||||||
|
fsys := filesys.MakeFsInMemory()
|
||||||
fLdr, err := loader.NewLoader(
|
fLdr, err := loader.NewLoader(
|
||||||
loader.RestrictionRootOnly,
|
loader.RestrictionRootOnly,
|
||||||
filesys.Separator, filesys.MakeFsInMemory())
|
filesys.Separator, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -66,8 +66,8 @@ func TestLoader(t *testing.T) {
|
|||||||
for _, behavior := range []types.BuiltinPluginLoadingOptions{
|
for _, behavior := range []types.BuiltinPluginLoadingOptions{
|
||||||
/* types.BploUseStaticallyLinked,
|
/* types.BploUseStaticallyLinked,
|
||||||
types.BploLoadFromFileSys */} {
|
types.BploLoadFromFileSys */} {
|
||||||
c := konfig.EnabledPluginConfig(behavior)
|
c := types.EnabledPluginConfig(behavior)
|
||||||
pLdr := NewLoader(c, rmF)
|
pLdr := NewLoader(c, rmF, fsys)
|
||||||
if pLdr == nil {
|
if pLdr == nil {
|
||||||
t.Fatal("expect non-nil loader")
|
t.Fatal("expect non-nil loader")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||||
"sigs.k8s.io/kustomize/api/internal/target"
|
"sigs.k8s.io/kustomize/api/internal/target"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
|
||||||
fLdr "sigs.k8s.io/kustomize/api/loader"
|
fLdr "sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
||||||
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeAndLoadKustTarget(
|
func makeAndLoadKustTarget(
|
||||||
@@ -37,10 +37,10 @@ func makeKustTargetWithRf(
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
rf := resmap.NewFactory(pvd.GetResourceFactory())
|
rf := resmap.NewFactory(pvd.GetResourceFactory())
|
||||||
pc := konfig.DisabledPluginConfig()
|
pc := types.DisabledPluginConfig()
|
||||||
return target.NewKustTarget(
|
return target.NewKustTarget(
|
||||||
ldr,
|
ldr,
|
||||||
valtest_test.MakeFakeValidator(),
|
valtest_test.MakeFakeValidator(),
|
||||||
rf,
|
rf,
|
||||||
pLdr.NewLoader(pc, rf))
|
pLdr.NewLoader(pc, rf, fSys))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,24 +41,6 @@ const (
|
|||||||
NoPluginHomeSentinal = "/No/non-builtin/plugins!"
|
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 {
|
type NotedFunc struct {
|
||||||
Note string
|
Note string
|
||||||
F func() string
|
F func() string
|
||||||
@@ -79,9 +61,11 @@ func DefaultAbsPluginHome(fSys filesys.FileSystem) (string, error) {
|
|||||||
{
|
{
|
||||||
Note: "homed in $" + XdgConfigHomeEnv,
|
Note: "homed in $" + XdgConfigHomeEnv,
|
||||||
F: func() string {
|
F: func() string {
|
||||||
return filepath.Join(
|
if root := os.Getenv(XdgConfigHomeEnv); root != "" {
|
||||||
os.Getenv(XdgConfigHomeEnv),
|
return filepath.Join(root, ProgramName, RelPluginHome)
|
||||||
ProgramName, RelPluginHome)
|
}
|
||||||
|
// do not look in "kustomize/plugin" if XdgConfigHomeEnv is unset
|
||||||
|
return ""
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -110,11 +94,14 @@ func FirstDirThatExistsElseError(
|
|||||||
pathFuncs []NotedFunc) (string, error) {
|
pathFuncs []NotedFunc) (string, error) {
|
||||||
var nope []types.Pair
|
var nope []types.Pair
|
||||||
for _, dt := range pathFuncs {
|
for _, dt := range pathFuncs {
|
||||||
dir := dt.F()
|
if dir := dt.F(); dir != "" {
|
||||||
if fSys.Exists(dir) {
|
if fSys.Exists(dir) {
|
||||||
return dir, nil
|
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)
|
return "", types.NewErrUnableToFind(what, nope)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filesys"
|
"sigs.k8s.io/kustomize/api/filesys"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
@@ -28,6 +30,34 @@ func TestDefaultAbsPluginHome_NoKustomizePluginHomeEnv(t *testing.T) {
|
|||||||
if !types.IsErrUnableToFind(err) {
|
if !types.IsErrUnableToFind(err) {
|
||||||
t.Fatalf("unexpected err: %v", 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) {
|
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) {
|
func TestDefaultAbsPluginHomeNoXdgWithDotConfig(t *testing.T) {
|
||||||
fSys := filesys.MakeFsInMemory()
|
fSys := filesys.MakeFsInMemory()
|
||||||
configDir := filepath.Join(
|
configDir := filepath.Join(
|
||||||
|
|||||||
@@ -66,7 +66,8 @@ func (b *Kustomizer) Run(
|
|||||||
ldr,
|
ldr,
|
||||||
b.depProvider.GetFieldValidator(),
|
b.depProvider.GetFieldValidator(),
|
||||||
resmapFactory,
|
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()
|
err = kt.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ package krusty
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
|
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,7 +41,7 @@ func MakeDefaultOptions() *Options {
|
|||||||
AddManagedbyLabel: false,
|
AddManagedbyLabel: false,
|
||||||
LoadRestrictions: types.LoadRestrictionsRootOnly,
|
LoadRestrictions: types.LoadRestrictionsRootOnly,
|
||||||
DoPrune: false,
|
DoPrune: false,
|
||||||
PluginConfig: konfig.DisabledPluginConfig(),
|
PluginConfig: types.DisabledPluginConfig(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ func (th Harness) MakeOptionsPluginsDisabled() krusty.Options {
|
|||||||
|
|
||||||
// Enables use of non-builtin plugins.
|
// Enables use of non-builtin plugins.
|
||||||
func (th Harness) MakeOptionsPluginsEnabled() krusty.Options {
|
func (th Harness) MakeOptionsPluginsEnabled() krusty.Options {
|
||||||
pc := konfig.EnabledPluginConfig(types.BploLoadFromFileSys)
|
pc := types.EnabledPluginConfig(types.BploLoadFromFileSys)
|
||||||
o := *krusty.MakeDefaultOptions()
|
o := *krusty.MakeDefaultOptions()
|
||||||
o.PluginConfig = pc
|
o.PluginConfig = pc
|
||||||
return o
|
return o
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ type HarnessEnhanced struct {
|
|||||||
func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced {
|
func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced {
|
||||||
pte := newPluginTestEnv(t).set()
|
pte := newPluginTestEnv(t).set()
|
||||||
|
|
||||||
pc := konfig.EnabledPluginConfig(types.BploLoadFromFileSys)
|
pc := types.EnabledPluginConfig(types.BploLoadFromFileSys)
|
||||||
pc.FnpLoadingOptions.EnableStar = true
|
pc.FnpLoadingOptions.EnableStar = true
|
||||||
p := provider.NewDefaultDepProvider()
|
p := provider.NewDefaultDepProvider()
|
||||||
resourceFactory := p.GetResourceFactory()
|
resourceFactory := p.GetResourceFactory()
|
||||||
@@ -50,7 +50,8 @@ func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced {
|
|||||||
Harness: MakeHarness(t),
|
Harness: MakeHarness(t),
|
||||||
pte: pte,
|
pte: pte,
|
||||||
rf: resmapFactory,
|
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.
|
// Point the file loader to the root ('/') of the in-memory file system.
|
||||||
result.ResetLoaderRoot(filesys.Separator)
|
result.ResetLoaderRoot(filesys.Separator)
|
||||||
|
|||||||
@@ -14,3 +14,21 @@ type PluginConfig struct {
|
|||||||
// FnpLoadingOptions sets the way function-based plugin behaviors.
|
// FnpLoadingOptions sets the way function-based plugin behaviors.
|
||||||
FnpLoadingOptions FnPluginLoadingOptions
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ func HonorKustomizeFlags(kOpts *krusty.Options) *krusty.Options {
|
|||||||
kOpts.DoLegacyResourceSort = getFlagReorderOutput() == legacy
|
kOpts.DoLegacyResourceSort = getFlagReorderOutput() == legacy
|
||||||
kOpts.LoadRestrictions = getFlagLoadRestrictorValue()
|
kOpts.LoadRestrictions = getFlagLoadRestrictorValue()
|
||||||
if theFlags.enable.plugins {
|
if theFlags.enable.plugins {
|
||||||
c := konfig.EnabledPluginConfig(types.BploUseStaticallyLinked)
|
c := types.EnabledPluginConfig(types.BploUseStaticallyLinked)
|
||||||
c.FnpLoadingOptions = theFlags.fnOptions
|
c.FnpLoadingOptions = theFlags.fnOptions
|
||||||
kOpts.PluginConfig = c
|
kOpts.PluginConfig = c
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user