mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-13 13:55:41 +00:00
Merge pull request #11300 from mwielgus/kubectl_replace_tempfile
Dump stdin to a temporary file in kubectl replace --force
This commit is contained in:
commit
52282b1a36
@ -19,7 +19,9 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
@ -131,6 +133,22 @@ func forceReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i, filename := range filenames {
|
||||||
|
if filename == "-" {
|
||||||
|
tempDir, err := ioutil.TempDir("", "kubectl_replace_")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tempDir)
|
||||||
|
tempFilename := filepath.Join(tempDir, "resource.stdin")
|
||||||
|
err = cmdutil.DumpReaderToFile(os.Stdin, tempFilename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
filenames[i] = tempFilename
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mapper, typer := f.Object()
|
mapper, typer := f.Object()
|
||||||
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
|
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
|
||||||
ContinueOnError().
|
ContinueOnError().
|
||||||
|
@ -406,3 +406,28 @@ func Merge(dst runtime.Object, fragment, kind string) (runtime.Object, error) {
|
|||||||
}
|
}
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DumpReaderToFile writes all data from the given io.Reader to the specified file
|
||||||
|
// (usually for temporary use).
|
||||||
|
func DumpReaderToFile(reader io.Reader, filename string) error {
|
||||||
|
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||||
|
defer f.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
buffer := make([]byte, 1024)
|
||||||
|
for {
|
||||||
|
count, err := reader.Read(buffer)
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = f.Write(buffer[:count])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -298,3 +299,25 @@ func TestCheckInvalidErr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDumpReaderToFile(t *testing.T) {
|
||||||
|
testString := "TEST STRING"
|
||||||
|
tempFile, err := ioutil.TempFile("", "hlpers_test_dump_")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error setting up a temporary file %v", err)
|
||||||
|
}
|
||||||
|
defer syscall.Unlink(tempFile.Name())
|
||||||
|
defer tempFile.Close()
|
||||||
|
err = DumpReaderToFile(strings.NewReader(testString), tempFile.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error in DumpReaderToFile: %v", err)
|
||||||
|
}
|
||||||
|
data, err := ioutil.ReadFile(tempFile.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error when reading %s: %v", tempFile, err)
|
||||||
|
}
|
||||||
|
stringData := string(data)
|
||||||
|
if stringData != testString {
|
||||||
|
t.Fatalf("Wrong file content %s != %s", testString, stringData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -164,7 +164,7 @@ func (b *Builder) Path(paths ...string) *Builder {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
visitors, err := ExpandPathsToFileVisitors(b.mapper, p, false, []string{".json", ".yaml", ".yml"}, b.continueOnError, b.schema)
|
visitors, err := ExpandPathsToFileVisitors(b.mapper, p, false, []string{".json", ".stdin", ".yaml", ".yml"}, b.continueOnError, b.schema)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.errs = append(b.errs, fmt.Errorf("error reading %q: %v", p, err))
|
b.errs = append(b.errs, fmt.Errorf("error reading %q: %v", p, err))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user