mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-17 09:49:13 +00:00
Refactor exec plugin load to error if plugin isn't executable
This commit is contained in:
@@ -44,8 +44,15 @@ type ExecPlugin struct {
|
|||||||
h *resmap.PluginHelpers
|
h *resmap.PluginHelpers
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExecPlugin(p string) *ExecPlugin {
|
func NewExecPlugin(p string) (*ExecPlugin, error) {
|
||||||
return &ExecPlugin{path: p}
|
f, err := os.Stat(p)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if f.Mode()&0111 == 0000 {
|
||||||
|
return nil, fmt.Errorf("unable to execute plugin on path: %s", p)
|
||||||
|
}
|
||||||
|
return &ExecPlugin{path: p}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ExecPlugin) Path() string {
|
func (p *ExecPlugin) Path() string {
|
||||||
@@ -60,15 +67,6 @@ func (p *ExecPlugin) Cfg() []byte {
|
|||||||
return p.cfg
|
return p.cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
// isAvailable checks to see if the plugin is available
|
|
||||||
func (p *ExecPlugin) IsAvailable() bool {
|
|
||||||
f, err := os.Stat(p.path)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return f.Mode()&0111 != 0000
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ExecPlugin) Config(h *resmap.PluginHelpers, config []byte) error {
|
func (p *ExecPlugin) Config(h *resmap.PluginHelpers, config []byte) error {
|
||||||
p.h = h
|
p.h = h
|
||||||
p.cfg = config
|
p.cfg = config
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package execplugin_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/plugins/loader"
|
"sigs.k8s.io/kustomize/api/plugins/loader"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
"sigs.k8s.io/kustomize/api/testutils/valtest"
|
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,10 +44,13 @@ s/$BAR/bar/g
|
|||||||
\ \ \
|
\ \ \
|
||||||
`))
|
`))
|
||||||
|
|
||||||
p := NewExecPlugin(
|
p, err := NewExecPlugin(
|
||||||
loader.AbsolutePluginPath(
|
loader.AbsolutePluginPath(
|
||||||
config.DefaultPluginConfig(),
|
config.DefaultPluginConfig(),
|
||||||
pluginConfig.OrgId()))
|
pluginConfig.OrgId()))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
yaml, err := pluginConfig.AsYAML()
|
yaml, err := pluginConfig.AsYAML()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -114,7 +118,10 @@ func strptr(s string) *string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateResourceOptions(t *testing.T) {
|
func TestUpdateResourceOptions(t *testing.T) {
|
||||||
p := NewExecPlugin("")
|
p, err := NewExecPlugin("")
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("unexpected error: %v", err.Error())
|
||||||
|
}
|
||||||
rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
||||||
in := resmap.New()
|
in := resmap.New()
|
||||||
expected := resmap.New()
|
expected := resmap.New()
|
||||||
@@ -159,7 +166,10 @@ func TestUpdateResourceOptions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateResourceOptionsWithInvalidHashAnnotationValues(t *testing.T) {
|
func TestUpdateResourceOptionsWithInvalidHashAnnotationValues(t *testing.T) {
|
||||||
p := NewExecPlugin("")
|
p, err := NewExecPlugin("")
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("unexpected error: %v", err.Error())
|
||||||
|
}
|
||||||
rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
||||||
cases := []string{
|
cases := []string{
|
||||||
"",
|
"",
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package loader
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"plugin"
|
"plugin"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -145,10 +146,13 @@ func (l *Loader) makeBuiltinPlugin(r resid.Gvk) (resmap.Configurable, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Loader) loadPlugin(resId resid.ResId) (resmap.Configurable, error) {
|
func (l *Loader) loadPlugin(resId resid.ResId) (resmap.Configurable, error) {
|
||||||
p := execplugin.NewExecPlugin(l.absolutePluginPath(resId))
|
p, err := execplugin.NewExecPlugin(l.absolutePluginPath(resId))
|
||||||
if p.IsAvailable() {
|
if err == nil {
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
c, err := l.loadGoPlugin(resId)
|
c, err := l.loadGoPlugin(resId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user