mirror of https://github.com/helm/helm
parent
09397f6b7d
commit
08fc12a8c3
@ -0,0 +1,99 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
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 postrender
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"helm.sh/helm/v3/pkg/helmpath"
|
||||
)
|
||||
|
||||
type execRender struct {
|
||||
binaryPath string
|
||||
}
|
||||
|
||||
// NewExec returns a PostRenderer implementation that calls the provided binary.
|
||||
// It returns an error if the binary cannot be found. If the provided path does
|
||||
// not contain any separators, it will search first in the plugins directory,
|
||||
// then in $PATH, otherwise it will resolve any relative paths to a fully
|
||||
// qualified path
|
||||
func NewExec(binaryPath string) (PostRenderer, error) {
|
||||
fullPath, err := getFullPath(binaryPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &execRender{fullPath}, nil
|
||||
}
|
||||
|
||||
// Run the configured binary for the post render
|
||||
func (p *execRender) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) {
|
||||
cmd := exec.Command(p.binaryPath)
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var postRendered = &bytes.Buffer{}
|
||||
var stderr = &bytes.Buffer{}
|
||||
cmd.Stdout = postRendered
|
||||
cmd.Stderr = stderr
|
||||
|
||||
go func() {
|
||||
defer stdin.Close()
|
||||
io.Copy(stdin, renderedManifests)
|
||||
}()
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while running command %s. error output:\n%s", p.binaryPath, stderr.String())
|
||||
}
|
||||
|
||||
return postRendered, nil
|
||||
}
|
||||
|
||||
// getFullPath returns the full filepath to the binary to execute. If the path
|
||||
// does not contain any separators, it will search first in the plugins
|
||||
// directory, then in $PATH, otherwise it will resolve any relative paths to a
|
||||
// fully qualified path
|
||||
func getFullPath(binaryPath string) (string, error) {
|
||||
// Manually check the plugin dir first
|
||||
if !strings.Contains(binaryPath, string(filepath.Separator)) {
|
||||
// First check the plugin dir
|
||||
pluginDir := helmpath.DataPath("plugins")
|
||||
_, err := os.Stat(filepath.Join(pluginDir, binaryPath))
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return "", err
|
||||
} else if err == nil {
|
||||
binaryPath = filepath.Join(pluginDir, binaryPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Now check for the binary using the given path or check if it exists in
|
||||
// the path and is executable
|
||||
checkedPath, err := exec.LookPath(binaryPath)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "unable to find binary at %s", binaryPath)
|
||||
}
|
||||
|
||||
return filepath.Abs(checkedPath)
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
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 postrender contains an interface that can be implemented for custom
|
||||
// post-renderers and an exec implementation that can be used for arbitrary
|
||||
// binaries and scripts
|
||||
package postrender
|
||||
|
||||
import "bytes"
|
||||
|
||||
type PostRenderer interface {
|
||||
// Run expects a single buffer filled with Helm rendered manifests. It
|
||||
// expects the modified results to be returned on a separate buffer or an
|
||||
// error if there was an issue or failure while running the post render step
|
||||
Run(renderedManifests *bytes.Buffer) (modifiedManifests *bytes.Buffer, err error)
|
||||
}
|
Loading…
Reference in new issue