mirror of https://github.com/helm/helm
Merge pull request #31217 from helm/plugin-postrender-type
[HIP-0026] Move Postrenderer to a plugin typepull/31218/head
commit
bd29e5edc2
@ -0,0 +1,8 @@
|
||||
name: "postrenderer-v1"
|
||||
version: "1.2.3"
|
||||
type: postrenderer/v1
|
||||
apiVersion: v1
|
||||
runtime: subprocess
|
||||
runtimeConfig:
|
||||
platformCommand:
|
||||
- command: "${HELM_PLUGIN_DIR}/sed-test.sh"
|
@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
if [ $# -eq 0 ]; then
|
||||
sed s/FOOTEST/BARTEST/g <&0
|
||||
else
|
||||
sed s/FOOTEST/"$*"/g <&0
|
||||
fi
|
@ -0,0 +1,8 @@
|
||||
name: "postrenderer-v1"
|
||||
version: "1.2.3"
|
||||
type: postrenderer/v1
|
||||
apiVersion: v1
|
||||
runtime: subprocess
|
||||
runtimeConfig:
|
||||
platformCommand:
|
||||
- command: "${HELM_PLUGIN_DIR}/sed-test.sh"
|
@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
if [ $# -eq 0 ]; then
|
||||
sed s/FOOTEST/BARTEST/g <&0
|
||||
else
|
||||
sed s/FOOTEST/"$*"/g <&0
|
||||
fi
|
@ -1,114 +0,0 @@
|
||||
/*
|
||||
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"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type execRender struct {
|
||||
binaryPath string
|
||||
args []string
|
||||
}
|
||||
|
||||
// NewExec returns a PostRenderer implementation that calls the provided binary.
|
||||
// It returns an error if the binary cannot be found. If the path does not
|
||||
// contain any separators, it will search in $PATH, otherwise it will resolve
|
||||
// any relative paths to a fully qualified path
|
||||
func NewExec(binaryPath string, args ...string) (PostRenderer, error) {
|
||||
fullPath, err := getFullPath(binaryPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &execRender{fullPath, args}, nil
|
||||
}
|
||||
|
||||
// Run the configured binary for the post render
|
||||
func (p *execRender) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) {
|
||||
cmd := exec.Command(p.binaryPath, p.args...)
|
||||
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, fmt.Errorf("error while running command %s. error output:\n%s: %w", p.binaryPath, stderr.String(), err)
|
||||
}
|
||||
|
||||
// If the binary returned almost nothing, it's likely that it didn't
|
||||
// successfully render anything
|
||||
if len(bytes.TrimSpace(postRendered.Bytes())) == 0 {
|
||||
return nil, fmt.Errorf("post-renderer %q produced empty output", p.binaryPath)
|
||||
}
|
||||
|
||||
return postRendered, nil
|
||||
}
|
||||
|
||||
// getFullPath returns the full filepath to the binary to execute. If the path
|
||||
// does not contain any separators, it will search in $PATH, otherwise it will
|
||||
// resolve any relative paths to a fully qualified path
|
||||
func getFullPath(binaryPath string) (string, error) {
|
||||
// NOTE(thomastaylor312): I am leaving this code commented out here. During
|
||||
// the implementation of post-render, it was brought up that if we are
|
||||
// relying on plugins, we should actually use the plugin system so it can
|
||||
// properly handle multiple OSs. This will be a feature add in the future,
|
||||
// so I left this code for reference. It can be deleted or reused once the
|
||||
// feature is implemented
|
||||
|
||||
// Manually check the plugin dir first
|
||||
// if !strings.Contains(binaryPath, string(filepath.Separator)) {
|
||||
// // First check the plugin dir
|
||||
// pluginDir := helmpath.DataPath("plugins") // Default location
|
||||
// // If location for plugins is explicitly set, check there
|
||||
// if v, ok := os.LookupEnv("HELM_PLUGINS"); ok {
|
||||
// pluginDir = v
|
||||
// }
|
||||
// // The plugins variable can actually contain multiple paths, so loop through those
|
||||
// for _, p := range filepath.SplitList(pluginDir) {
|
||||
// _, err := os.Stat(filepath.Join(p, binaryPath))
|
||||
// if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
// return "", err
|
||||
// } else if err == nil {
|
||||
// binaryPath = filepath.Join(p, binaryPath)
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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 "", fmt.Errorf("unable to find binary at %s: %w", binaryPath, err)
|
||||
}
|
||||
|
||||
return filepath.Abs(checkedPath)
|
||||
}
|
@ -1,193 +0,0 @@
|
||||
/*
|
||||
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"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const testingScript = `#!/bin/sh
|
||||
if [ $# -eq 0 ]; then
|
||||
sed s/FOOTEST/BARTEST/g <&0
|
||||
else
|
||||
sed s/FOOTEST/"$*"/g <&0
|
||||
fi
|
||||
`
|
||||
|
||||
func TestGetFullPath(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
t.Run("full path resolves correctly", func(t *testing.T) {
|
||||
testpath := setupTestingScript(t)
|
||||
|
||||
fullPath, err := getFullPath(testpath)
|
||||
is.NoError(err)
|
||||
is.Equal(testpath, fullPath)
|
||||
})
|
||||
|
||||
t.Run("relative path resolves correctly", func(t *testing.T) {
|
||||
testpath := setupTestingScript(t)
|
||||
|
||||
currentDir, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
relative, err := filepath.Rel(currentDir, testpath)
|
||||
require.NoError(t, err)
|
||||
fullPath, err := getFullPath(relative)
|
||||
is.NoError(err)
|
||||
is.Equal(testpath, fullPath)
|
||||
})
|
||||
|
||||
t.Run("binary in PATH resolves correctly", func(t *testing.T) {
|
||||
testpath := setupTestingScript(t)
|
||||
|
||||
t.Setenv("PATH", filepath.Dir(testpath))
|
||||
|
||||
fullPath, err := getFullPath(filepath.Base(testpath))
|
||||
is.NoError(err)
|
||||
is.Equal(testpath, fullPath)
|
||||
})
|
||||
|
||||
// NOTE(thomastaylor312): See note in getFullPath for more details why this
|
||||
// is here
|
||||
|
||||
// t.Run("binary in plugin path resolves correctly", func(t *testing.T) {
|
||||
// testpath, cleanup := setupTestingScript(t)
|
||||
// defer cleanup()
|
||||
|
||||
// realPath := os.Getenv("HELM_PLUGINS")
|
||||
// os.Setenv("HELM_PLUGINS", filepath.Dir(testpath))
|
||||
// defer func() {
|
||||
// os.Setenv("HELM_PLUGINS", realPath)
|
||||
// }()
|
||||
|
||||
// fullPath, err := getFullPath(filepath.Base(testpath))
|
||||
// is.NoError(err)
|
||||
// is.Equal(testpath, fullPath)
|
||||
// })
|
||||
|
||||
// t.Run("binary in multiple plugin paths resolves correctly", func(t *testing.T) {
|
||||
// testpath, cleanup := setupTestingScript(t)
|
||||
// defer cleanup()
|
||||
|
||||
// realPath := os.Getenv("HELM_PLUGINS")
|
||||
// os.Setenv("HELM_PLUGINS", filepath.Dir(testpath)+string(os.PathListSeparator)+"/another/dir")
|
||||
// defer func() {
|
||||
// os.Setenv("HELM_PLUGINS", realPath)
|
||||
// }()
|
||||
|
||||
// fullPath, err := getFullPath(filepath.Base(testpath))
|
||||
// is.NoError(err)
|
||||
// is.Equal(testpath, fullPath)
|
||||
// })
|
||||
}
|
||||
|
||||
func TestExecRun(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
// the actual Run test uses a basic sed example, so skip this test on windows
|
||||
t.Skip("skipping on windows")
|
||||
}
|
||||
is := assert.New(t)
|
||||
testpath := setupTestingScript(t)
|
||||
|
||||
renderer, err := NewExec(testpath)
|
||||
require.NoError(t, err)
|
||||
|
||||
output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
|
||||
is.NoError(err)
|
||||
is.Contains(output.String(), "BARTEST")
|
||||
}
|
||||
|
||||
func TestExecRunWithNoOutput(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
// the actual Run test uses a basic sed example, so skip this test on windows
|
||||
t.Skip("skipping on windows")
|
||||
}
|
||||
is := assert.New(t)
|
||||
testpath := setupTestingScript(t)
|
||||
|
||||
renderer, err := NewExec(testpath)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = renderer.Run(bytes.NewBufferString(""))
|
||||
is.Error(err)
|
||||
}
|
||||
|
||||
func TestNewExecWithOneArgsRun(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
// the actual Run test uses a basic sed example, so skip this test on windows
|
||||
t.Skip("skipping on windows")
|
||||
}
|
||||
is := assert.New(t)
|
||||
testpath := setupTestingScript(t)
|
||||
|
||||
renderer, err := NewExec(testpath, "ARG1")
|
||||
require.NoError(t, err)
|
||||
|
||||
output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
|
||||
is.NoError(err)
|
||||
is.Contains(output.String(), "ARG1")
|
||||
}
|
||||
|
||||
func TestNewExecWithTwoArgsRun(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
// the actual Run test uses a basic sed example, so skip this test on windows
|
||||
t.Skip("skipping on windows")
|
||||
}
|
||||
is := assert.New(t)
|
||||
testpath := setupTestingScript(t)
|
||||
|
||||
renderer, err := NewExec(testpath, "ARG1", "ARG2")
|
||||
require.NoError(t, err)
|
||||
|
||||
output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
|
||||
is.NoError(err)
|
||||
is.Contains(output.String(), "ARG1 ARG2")
|
||||
}
|
||||
|
||||
func setupTestingScript(t *testing.T) (filepath string) {
|
||||
t.Helper()
|
||||
|
||||
tempdir := t.TempDir()
|
||||
|
||||
f, err := os.CreateTemp(tempdir, "post-render-test.sh")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create tempfile for testing: %s", err)
|
||||
}
|
||||
|
||||
_, err = f.WriteString(testingScript)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to write tempfile for testing: %s", err)
|
||||
}
|
||||
|
||||
err = f.Chmod(0o755)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to make tempfile executable for testing: %s", err)
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("unable to close tempfile after writing: %s", err)
|
||||
}
|
||||
|
||||
return f.Name()
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
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 postrenderer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"helm.sh/helm/v4/internal/plugin/schema"
|
||||
|
||||
"helm.sh/helm/v4/internal/plugin"
|
||||
"helm.sh/helm/v4/pkg/cli"
|
||||
)
|
||||
|
||||
// PostRenderer is an interface different plugin runtimes
|
||||
// it may be also be used without the factory for custom post-renderers
|
||||
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)
|
||||
}
|
||||
|
||||
// NewPostRendererPlugin creates a PostRenderer that uses the plugin's Runtime
|
||||
func NewPostRendererPlugin(settings *cli.EnvSettings, pluginName string, args ...string) (PostRenderer, error) {
|
||||
descriptor := plugin.Descriptor{
|
||||
Name: pluginName,
|
||||
Type: "postrenderer/v1",
|
||||
}
|
||||
p, err := plugin.FindPlugin(filepath.SplitList(settings.PluginsDirectory), descriptor)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &postRendererPlugin{
|
||||
plugin: p,
|
||||
args: args,
|
||||
settings: settings,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// postRendererPlugin implements PostRenderer by delegating to the plugin's Runtime
|
||||
type postRendererPlugin struct {
|
||||
plugin plugin.Plugin
|
||||
args []string
|
||||
settings *cli.EnvSettings
|
||||
}
|
||||
|
||||
// Run implements PostRenderer by using the plugin's Runtime
|
||||
func (r *postRendererPlugin) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) {
|
||||
input := &plugin.Input{
|
||||
Message: schema.InputMessagePostRendererV1{
|
||||
ExtraArgs: r.args,
|
||||
Manifests: renderedManifests,
|
||||
Settings: r.settings,
|
||||
},
|
||||
}
|
||||
output, err := r.plugin.Invoke(context.Background(), input)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to invoke post-renderer plugin %q: %w", r.plugin.Metadata().Name, err)
|
||||
}
|
||||
|
||||
outputMessage := output.Message.(*schema.OutputMessagePostRendererV1)
|
||||
|
||||
// If the binary returned almost nothing, it's likely that it didn't
|
||||
// successfully render anything
|
||||
if len(bytes.TrimSpace(outputMessage.Manifests.Bytes())) == 0 {
|
||||
return nil, fmt.Errorf("post-renderer %q produced empty output", r.plugin.Metadata().Name)
|
||||
}
|
||||
|
||||
return outputMessage.Manifests, nil
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
/*
|
||||
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 postrenderer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"helm.sh/helm/v4/internal/plugin"
|
||||
"helm.sh/helm/v4/pkg/cli"
|
||||
)
|
||||
|
||||
func TestNewPostRenderPluginRunWithNoOutput(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
// the actual Run test uses a basic sed example, so skip this test on windows
|
||||
t.Skip("skipping on windows")
|
||||
}
|
||||
is := assert.New(t)
|
||||
s := cli.New()
|
||||
s.PluginsDirectory = "testdata/plugins"
|
||||
name := "postrenderer-v1"
|
||||
base := filepath.Join(s.PluginsDirectory, name)
|
||||
plugin.SetupPluginEnv(s, name, base)
|
||||
|
||||
renderer, err := NewPostRendererPlugin(s, name, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = renderer.Run(bytes.NewBufferString(""))
|
||||
is.Error(err)
|
||||
}
|
||||
|
||||
func TestNewPostRenderPluginWithOneArgsRun(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
// the actual Run test uses a basic sed example, so skip this test on windows
|
||||
t.Skip("skipping on windows")
|
||||
}
|
||||
is := assert.New(t)
|
||||
s := cli.New()
|
||||
s.PluginsDirectory = "testdata/plugins"
|
||||
name := "postrenderer-v1"
|
||||
base := filepath.Join(s.PluginsDirectory, name)
|
||||
plugin.SetupPluginEnv(s, name, base)
|
||||
|
||||
renderer, err := NewPostRendererPlugin(s, name, "ARG1")
|
||||
require.NoError(t, err)
|
||||
|
||||
output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
|
||||
is.NoError(err)
|
||||
is.Contains(output.String(), "ARG1")
|
||||
}
|
||||
|
||||
func TestNewPostRenderPluginWithTwoArgsRun(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
// the actual Run test uses a basic sed example, so skip this test on windows
|
||||
t.Skip("skipping on windows")
|
||||
}
|
||||
is := assert.New(t)
|
||||
s := cli.New()
|
||||
s.PluginsDirectory = "testdata/plugins"
|
||||
name := "postrenderer-v1"
|
||||
base := filepath.Join(s.PluginsDirectory, name)
|
||||
plugin.SetupPluginEnv(s, name, base)
|
||||
|
||||
renderer, err := NewPostRendererPlugin(s, name, "ARG1", "ARG2")
|
||||
require.NoError(t, err)
|
||||
|
||||
output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
|
||||
is.NoError(err)
|
||||
is.Contains(output.String(), "ARG1 ARG2")
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
name: "postrenderer-v1"
|
||||
version: "1.2.3"
|
||||
type: postrenderer/v1
|
||||
apiVersion: v1
|
||||
runtime: subprocess
|
||||
runtimeConfig:
|
||||
platformCommand:
|
||||
- command: "${HELM_PLUGIN_DIR}/sed-test.sh"
|
@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
if [ $# -eq 0 ]; then
|
||||
sed s/FOOTEST/BARTEST/g <&0
|
||||
else
|
||||
sed s/FOOTEST/"$*"/g <&0
|
||||
fi
|
Loading…
Reference in new issue