mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-09-15 12:18:57 +00:00
in plugin executor remove unnecessary code and improve error messages
This commit is contained in:
@@ -1,18 +1,5 @@
|
|||||||
/*
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
Copyright 2019 The Kubernetes Authors.
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package plugins
|
package plugins
|
||||||
|
|
||||||
@@ -24,6 +11,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/ifc"
|
"sigs.k8s.io/kustomize/v3/pkg/ifc"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/resid"
|
"sigs.k8s.io/kustomize/v3/pkg/resid"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/resmap"
|
"sigs.k8s.io/kustomize/v3/pkg/resmap"
|
||||||
@@ -31,7 +19,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
idAnnotation = "kustomize.config.k8s.io/id"
|
idAnnotation = "kustomize.config.k8s.io/id"
|
||||||
|
tmpConfigFilePrefix = "kust-plugin-config-"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExecPlugin record the name and args of an executable
|
// ExecPlugin record the name and args of an executable
|
||||||
@@ -102,27 +91,6 @@ func (p *ExecPlugin) processOptionalArgsFields() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ExecPlugin) writeConfig() (string, error) {
|
|
||||||
tmpFile, err := ioutil.TempFile("", "kust-pipe")
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
stdout, err := os.OpenFile(tmpFile.Name(), os.O_RDWR, 0600)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
_, err = stdout.Write(p.cfg)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
err = stdout.Close()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return tmpFile.Name(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ExecPlugin) Generate() (resmap.ResMap, error) {
|
func (p *ExecPlugin) Generate() (resmap.ResMap, error) {
|
||||||
output, err := p.invokePlugin(nil)
|
output, err := p.invokePlugin(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -154,33 +122,40 @@ func (p *ExecPlugin) Transform(rm resmap.ResMap) error {
|
|||||||
return p.updateResMapValues(output, rm)
|
return p.updateResMapValues(output, rm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// invokePlugin invokes the plugin
|
// invokePlugin writes plugin config to a temp file, then
|
||||||
|
// passes the full temp file path as the first arg to a process
|
||||||
|
// running the plugin binary. Process output is returned.
|
||||||
func (p *ExecPlugin) invokePlugin(input []byte) ([]byte, error) {
|
func (p *ExecPlugin) invokePlugin(input []byte) ([]byte, error) {
|
||||||
args, err := p.getArgs()
|
f, err := ioutil.TempFile("", tmpConfigFilePrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.Wrap(
|
||||||
|
err, "creating tmp plugin config file")
|
||||||
}
|
}
|
||||||
// Cleanup plugin config file after execution
|
_, err = f.Write(p.cfg)
|
||||||
defer os.Remove(args[0])
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(
|
||||||
cmd := exec.Command(p.path, args...)
|
err, "writing plugin config to "+f.Name())
|
||||||
|
}
|
||||||
|
err = f.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(
|
||||||
|
err, "closing plugin config file "+f.Name())
|
||||||
|
}
|
||||||
|
cmd := exec.Command(
|
||||||
|
p.path, append([]string{f.Name()}, p.args...)...)
|
||||||
cmd.Env = p.getEnv()
|
cmd.Env = p.getEnv()
|
||||||
cmd.Stdin = bytes.NewReader(input)
|
cmd.Stdin = bytes.NewReader(input)
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
if _, err := os.Stat(p.ldr.Root()); err == nil {
|
if _, err := os.Stat(p.ldr.Root()); err == nil {
|
||||||
cmd.Dir = p.ldr.Root()
|
cmd.Dir = p.ldr.Root()
|
||||||
}
|
}
|
||||||
return cmd.Output()
|
result, err := cmd.Output()
|
||||||
}
|
|
||||||
|
|
||||||
// The first arg is always the absolute path to a temporary file
|
|
||||||
// holding the YAML form of the plugin config.
|
|
||||||
func (p *ExecPlugin) getArgs() ([]string, error) {
|
|
||||||
configFileName, err := p.writeConfig()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.Wrapf(
|
||||||
|
err, "failure in plugin configured via %s; %v",
|
||||||
|
f.Name(), err.Error())
|
||||||
}
|
}
|
||||||
return append([]string{configFileName}, p.args...), nil
|
return result, os.Remove(f.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ExecPlugin) getEnv() []string {
|
func (p *ExecPlugin) getEnv() []string {
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ data:
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected an error")
|
t.Fatalf("expected an error")
|
||||||
}
|
}
|
||||||
if !strings.Contains(err.Error(), "data: Invalid type.") {
|
if !strings.Contains(err.Error(), "failure in plugin configured via") {
|
||||||
t.Fatalf("incorrect error %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user