mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-13 11:25:19 +00:00
Merge pull request #13604 from JanetKuo/kubectl-edit
Auto commit by PR queue bot
This commit is contained in:
@@ -149,6 +149,7 @@ Find more information at https://github.com/kubernetes/kubernetes.`,
|
||||
cmds.AddCommand(NewCmdReplace(f, out))
|
||||
cmds.AddCommand(NewCmdPatch(f, out))
|
||||
cmds.AddCommand(NewCmdDelete(f, out))
|
||||
cmds.AddCommand(NewCmdEdit(f, out))
|
||||
|
||||
cmds.AddCommand(NewCmdNamespace(out))
|
||||
cmds.AddCommand(NewCmdLog(f, out))
|
||||
|
||||
392
pkg/kubectl/cmd/edit.go
Normal file
392
pkg/kubectl/cmd/edit.go
Normal file
@@ -0,0 +1,392 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
client "k8s.io/kubernetes/pkg/client/unversioned"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/editor"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/jsonmerge"
|
||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||
"k8s.io/kubernetes/pkg/util/strategicpatch"
|
||||
"k8s.io/kubernetes/pkg/util/yaml"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const (
|
||||
editLong = `Edit a resource from the default editor.
|
||||
|
||||
The edit command allows you to directly edit any API resource you can retrieve via the
|
||||
command line tools. It will open the editor defined by your KUBE_EDITOR, GIT_EDITOR,
|
||||
or EDITOR environment variables, or fall back to 'vi'. You can edit multiple objects,
|
||||
although changes are applied one at a time. The command accepts filenames as well as
|
||||
command line arguments, although the files you point to must be previously saved
|
||||
versions of resources.
|
||||
|
||||
The files to edit will be output in the default API version, or a version specified
|
||||
by --output-version. The default format is YAML - if you would like to edit in JSON
|
||||
pass -o json.
|
||||
|
||||
In the event an error occurs while updating, a temporary file will be created on disk
|
||||
that contains your unapplied changes. The most common error when updating a resource
|
||||
is another editor changing the resource on the server. When this occurs, you will have
|
||||
to apply your changes to the newer version of the resource, or update your temporary
|
||||
saved copy to include the latest resource version.`
|
||||
|
||||
editExample = ` # Edit the service named 'docker-registry':
|
||||
$ kubectl edit svc/docker-registry
|
||||
|
||||
# Use an alternative editor
|
||||
$ KUBE_EDITOR="nano" kubectl edit svc/docker-registry
|
||||
|
||||
# Edit the service 'docker-registry' in JSON using the v1 API format:
|
||||
$ kubectl edit svc/docker-registry --output-version=v1 -o json`
|
||||
)
|
||||
|
||||
var errExit = fmt.Errorf("exit directly")
|
||||
|
||||
func NewCmdEdit(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
filenames := []string{}
|
||||
cmd := &cobra.Command{
|
||||
Use: "edit (RESOURCE/NAME | -f FILENAME)",
|
||||
Short: "Edit a resource on the server",
|
||||
Long: editLong,
|
||||
Example: fmt.Sprintf(editExample),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := RunEdit(f, out, cmd, args, filenames)
|
||||
if err == errExit {
|
||||
os.Exit(1)
|
||||
}
|
||||
cmdutil.CheckErr(err)
|
||||
},
|
||||
}
|
||||
usage := "Filename, directory, or URL to file to use to edit the resource"
|
||||
kubectl.AddJsonFilenameFlag(cmd, &filenames, usage)
|
||||
cmd.Flags().StringP("output", "o", "yaml", "Output format. One of: yaml|json.")
|
||||
cmd.Flags().String("output-version", "", "Output the formatted object with the given version (default api-version).")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func RunEdit(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, filenames []string) error {
|
||||
var printer kubectl.ResourcePrinter
|
||||
var ext string
|
||||
switch format := cmdutil.GetFlagString(cmd, "output"); format {
|
||||
case "json":
|
||||
printer = &kubectl.JSONPrinter{}
|
||||
ext = ".json"
|
||||
case "yaml":
|
||||
printer = &kubectl.YAMLPrinter{}
|
||||
ext = ".yaml"
|
||||
default:
|
||||
return cmdutil.UsageError(cmd, "The flag 'output' must be one of yaml|json")
|
||||
}
|
||||
|
||||
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mapper, typer := f.Object()
|
||||
rmap := &resource.Mapper{
|
||||
ObjectTyper: typer,
|
||||
RESTMapper: mapper,
|
||||
ClientMapper: f.ClientMapperForCommand(),
|
||||
}
|
||||
|
||||
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
|
||||
NamespaceParam(cmdNamespace).DefaultNamespace().
|
||||
FilenameParam(enforceNamespace, filenames...).
|
||||
ResourceTypeOrNameArgs(true, args...).
|
||||
Latest().
|
||||
Flatten().
|
||||
Do()
|
||||
err = r.Err()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
infos, err := r.Infos()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clientConfig, err := f.ClientConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defaultVersion := cmdutil.OutputVersion(cmd, clientConfig.Version)
|
||||
results := editResults{}
|
||||
for {
|
||||
obj, err := resource.AsVersionedObject(infos, false, defaultVersion)
|
||||
if err != nil {
|
||||
return preservedFile(err, results.file, out)
|
||||
}
|
||||
|
||||
// TODO: add an annotating YAML printer that can print inline comments on each field,
|
||||
// including descriptions or validation errors
|
||||
|
||||
// generate the file to edit
|
||||
buf := &bytes.Buffer{}
|
||||
if err := results.header.writeTo(buf); err != nil {
|
||||
return preservedFile(err, results.file, out)
|
||||
}
|
||||
if err := printer.PrintObj(obj, buf); err != nil {
|
||||
return preservedFile(err, results.file, out)
|
||||
}
|
||||
original := buf.Bytes()
|
||||
|
||||
// launch the editor
|
||||
edit := editor.NewDefaultEditor()
|
||||
edited, file, err := edit.LaunchTempFile("kubectl-edit-", ext, buf)
|
||||
if err != nil {
|
||||
return preservedFile(err, results.file, out)
|
||||
}
|
||||
|
||||
// cleanup any file from the previous pass
|
||||
if len(results.file) > 0 {
|
||||
os.Remove(results.file)
|
||||
}
|
||||
|
||||
glog.V(4).Infof("User edited:\n%s", string(edited))
|
||||
fmt.Printf("User edited:\n%s", string(edited))
|
||||
lines, err := hasLines(bytes.NewBuffer(edited))
|
||||
if err != nil {
|
||||
return preservedFile(err, file, out)
|
||||
}
|
||||
if bytes.Equal(original, edited) {
|
||||
if len(results.edit) > 0 {
|
||||
preservedFile(nil, file, out)
|
||||
} else {
|
||||
os.Remove(file)
|
||||
}
|
||||
fmt.Fprintln(out, "Edit cancelled, no changes made.")
|
||||
return nil
|
||||
}
|
||||
if !lines {
|
||||
if len(results.edit) > 0 {
|
||||
preservedFile(nil, file, out)
|
||||
} else {
|
||||
os.Remove(file)
|
||||
}
|
||||
fmt.Fprintln(out, "Edit cancelled, saved file was empty.")
|
||||
return nil
|
||||
}
|
||||
|
||||
results = editResults{
|
||||
file: file,
|
||||
}
|
||||
|
||||
// parse the edited file
|
||||
updates, err := rmap.InfoForData(edited, "edited-file")
|
||||
if err != nil {
|
||||
return preservedFile(err, file, out)
|
||||
}
|
||||
|
||||
visitor := resource.NewFlattenListVisitor(updates, rmap)
|
||||
|
||||
// need to make sure the original namespace wasn't changed while editing
|
||||
if err = visitor.Visit(resource.RequireNamespace(cmdNamespace)); err != nil {
|
||||
return preservedFile(err, file, out)
|
||||
}
|
||||
|
||||
// use strategic merge to create a patch
|
||||
originalJS, err := yaml.ToJSON(original)
|
||||
if err != nil {
|
||||
return preservedFile(err, file, out)
|
||||
}
|
||||
editedJS, err := yaml.ToJSON(edited)
|
||||
if err != nil {
|
||||
return preservedFile(err, file, out)
|
||||
}
|
||||
patch, err := strategicpatch.CreateStrategicMergePatch(originalJS, editedJS, obj)
|
||||
// TODO: change all jsonmerge to strategicpatch
|
||||
// for checking preconditions
|
||||
preconditions := []jsonmerge.PreconditionFunc{}
|
||||
if err != nil {
|
||||
glog.V(4).Infof("Unable to calculate diff, no merge is possible: %v", err)
|
||||
return preservedFile(err, file, out)
|
||||
} else {
|
||||
preconditions = append(preconditions, jsonmerge.RequireKeyUnchanged("apiVersion"))
|
||||
preconditions = append(preconditions, jsonmerge.RequireKeyUnchanged("kind"))
|
||||
preconditions = append(preconditions, jsonmerge.RequireMetadataKeyUnchanged("name"))
|
||||
results.version = defaultVersion
|
||||
}
|
||||
|
||||
if hold, msg := jsonmerge.TestPreconditionsHold(patch, preconditions); !hold {
|
||||
fmt.Fprintf(out, "error: %s", msg)
|
||||
return preservedFile(nil, file, out)
|
||||
}
|
||||
|
||||
err = visitor.Visit(func(info *resource.Info, err error) error {
|
||||
patched, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, api.StrategicMergePatchType, patch)
|
||||
if err != nil {
|
||||
fmt.Fprintln(out, results.addError(err, info))
|
||||
return nil
|
||||
}
|
||||
info.Refresh(patched, true)
|
||||
cmdutil.PrintSuccess(mapper, false, out, info.Mapping.Resource, info.Name, "edited")
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return preservedFile(err, file, out)
|
||||
}
|
||||
|
||||
if results.retryable > 0 {
|
||||
fmt.Fprintf(out, "You can run `kubectl replace -f %s` to try this update again.\n", file)
|
||||
return errExit
|
||||
}
|
||||
if results.conflict > 0 {
|
||||
fmt.Fprintf(out, "You must update your local resource version and run `kubectl replace -f %s` to overwrite the remote changes.\n", file)
|
||||
return errExit
|
||||
}
|
||||
if len(results.edit) == 0 {
|
||||
if results.notfound == 0 {
|
||||
os.Remove(file)
|
||||
} else {
|
||||
fmt.Fprintf(out, "The edits you made on deleted resources have been saved to %q\n", file)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// loop again and edit the remaining items
|
||||
infos = results.edit
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// print json file (such as patch file) content for debugging
|
||||
func printJson(out io.Writer, file []byte) error {
|
||||
diff := make(map[string]interface{})
|
||||
if err := json.Unmarshal(file, &diff); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "%v\n", diff)
|
||||
return nil
|
||||
}
|
||||
|
||||
// editReason preserves a message about the reason this file must be edited again
|
||||
type editReason struct {
|
||||
head string
|
||||
other []string
|
||||
}
|
||||
|
||||
// editHeader includes a list of reasons the edit must be retried
|
||||
type editHeader struct {
|
||||
reasons []editReason
|
||||
}
|
||||
|
||||
// writeTo outputs the current header information into a stream
|
||||
func (h *editHeader) writeTo(w io.Writer) error {
|
||||
fmt.Fprint(w, `# Please edit the object below. Lines beginning with a '#' will be ignored,
|
||||
# and an empty file will abort the edit. If an error occurs while saving this file will be
|
||||
# reopened with the relevant failures.
|
||||
#
|
||||
`)
|
||||
for _, r := range h.reasons {
|
||||
if len(r.other) > 0 {
|
||||
fmt.Fprintf(w, "# %s:\n", r.head)
|
||||
} else {
|
||||
fmt.Fprintf(w, "# %s\n", r.head)
|
||||
}
|
||||
for _, o := range r.other {
|
||||
fmt.Fprintf(w, "# * %s\n", o)
|
||||
}
|
||||
fmt.Fprintln(w, "#")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// editResults capture the result of an update
|
||||
type editResults struct {
|
||||
header editHeader
|
||||
retryable int
|
||||
notfound int
|
||||
conflict int
|
||||
edit []*resource.Info
|
||||
file string
|
||||
|
||||
version string
|
||||
}
|
||||
|
||||
func (r *editResults) addError(err error, info *resource.Info) string {
|
||||
switch {
|
||||
case errors.IsInvalid(err):
|
||||
r.edit = append(r.edit, info)
|
||||
reason := editReason{
|
||||
head: fmt.Sprintf("%s %s was not valid", info.Mapping.Kind, info.Name),
|
||||
}
|
||||
if err, ok := err.(client.APIStatus); ok {
|
||||
if details := err.Status().Details; details != nil {
|
||||
for _, cause := range details.Causes {
|
||||
reason.other = append(reason.other, cause.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
r.header.reasons = append(r.header.reasons, reason)
|
||||
return fmt.Sprintf("Error: the %s %s is invalid", info.Mapping.Kind, info.Name)
|
||||
case errors.IsNotFound(err):
|
||||
r.notfound++
|
||||
return fmt.Sprintf("Error: the %s %s could not be found on the server", info.Mapping.Kind, info.Name)
|
||||
default:
|
||||
r.retryable++
|
||||
return fmt.Sprintf("Error: the %s %s could not be patched: %v", info.Mapping.Kind, info.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// preservedFile writes out a message about the provided file if it exists to the
|
||||
// provided output stream when an error happens. Used to notify the user where
|
||||
// their updates were preserved.
|
||||
func preservedFile(err error, path string, out io.Writer) error {
|
||||
if len(path) > 0 {
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
fmt.Fprintf(out, "A copy of your changes has been stored to %q\n", path)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// hasLines returns true if any line in the provided stream is non empty - has non-whitespace
|
||||
// characters, or the first non-whitespace character is a '#' indicating a comment. Returns
|
||||
// any errors encountered reading the stream.
|
||||
func hasLines(r io.Reader) (bool, error) {
|
||||
// TODO: if any files we read have > 64KB lines, we'll need to switch to bytes.ReadLine
|
||||
// TODO: probably going to be secrets
|
||||
s := bufio.NewScanner(r)
|
||||
for s.Scan() {
|
||||
if line := strings.TrimSpace(s.Text()); len(line) > 0 && line[0] != '#' {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
if err := s.Err(); err != nil && err != io.EOF {
|
||||
return false, err
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
199
pkg/kubectl/cmd/util/editor/editor.go
Normal file
199
pkg/kubectl/cmd/util/editor/editor.go
Normal file
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 editor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/pkg/term"
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
const (
|
||||
// sorry, blame Git
|
||||
defaultEditor = "vi"
|
||||
defaultShell = "/bin/bash"
|
||||
)
|
||||
|
||||
type Editor struct {
|
||||
Args []string
|
||||
Shell bool
|
||||
}
|
||||
|
||||
// NewDefaultEditor creates a struct Editor that uses the OS environment to
|
||||
// locate the editor program, looking at EDITOR environment variable to find
|
||||
// the proper command line. If the provided editor has no spaces, or no quotes,
|
||||
// it is treated as a bare command to be loaded. Otherwise, the string will
|
||||
// be passed to the user's shell for execution.
|
||||
func NewDefaultEditor() Editor {
|
||||
args, shell := defaultEnvEditor()
|
||||
return Editor{
|
||||
Args: args,
|
||||
Shell: shell,
|
||||
}
|
||||
}
|
||||
|
||||
func defaultEnvShell() []string {
|
||||
shell := os.Getenv("SHELL")
|
||||
if len(shell) == 0 {
|
||||
shell = defaultShell
|
||||
}
|
||||
return []string{shell, "-c"}
|
||||
}
|
||||
|
||||
func defaultEnvEditor() ([]string, bool) {
|
||||
editor := os.Getenv("EDITOR")
|
||||
if len(editor) == 0 {
|
||||
editor = defaultEditor
|
||||
}
|
||||
if !strings.Contains(editor, " ") {
|
||||
return []string{editor}, false
|
||||
}
|
||||
if !strings.ContainsAny(editor, "\"'\\") {
|
||||
return strings.Split(editor, " "), false
|
||||
}
|
||||
// rather than parse the shell arguments ourselves, punt to the shell
|
||||
shell := defaultEnvShell()
|
||||
return append(shell, editor), true
|
||||
}
|
||||
|
||||
func (e Editor) args(path string) []string {
|
||||
args := make([]string, len(e.Args))
|
||||
copy(args, e.Args)
|
||||
if e.Shell {
|
||||
last := args[len(args)-1]
|
||||
args[len(args)-1] = fmt.Sprintf("%s %q", last, path)
|
||||
} else {
|
||||
args = append(args, path)
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
// Launch opens the described or returns an error. The TTY will be protected, and
|
||||
// SIGQUIT, SIGTERM, and SIGINT will all be trapped.
|
||||
func (e Editor) Launch(path string) error {
|
||||
if len(e.Args) == 0 {
|
||||
return fmt.Errorf("no editor defined, can't open %s", path)
|
||||
}
|
||||
abs, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args := e.args(abs)
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdin = os.Stdin
|
||||
glog.V(5).Infof("Opening file with editor %v", args)
|
||||
if err := withSafeTTYAndInterrupts(cmd.Run); err != nil {
|
||||
if err, ok := err.(*exec.Error); ok {
|
||||
if err.Err == exec.ErrNotFound {
|
||||
return fmt.Errorf("unable to launch the editor %q", strings.Join(e.Args, " "))
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("there was a problem with the editor %q", strings.Join(e.Args, " "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LaunchTempFile reads the provided stream into a temporary file in the given directory
|
||||
// and file prefix, and then invokes Launch with the path of that file. It will return
|
||||
// the contents of the file after launch, any errors that occur, and the path of the
|
||||
// temporary file so the caller can clean it up as needed.
|
||||
func (e Editor) LaunchTempFile(prefix, suffix string, r io.Reader) ([]byte, string, error) {
|
||||
f, err := tempFile(prefix, suffix)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
defer f.Close()
|
||||
path := f.Name()
|
||||
if _, err := io.Copy(f, r); err != nil {
|
||||
os.Remove(path)
|
||||
return nil, path, err
|
||||
}
|
||||
if err := e.Launch(path); err != nil {
|
||||
return nil, path, err
|
||||
}
|
||||
bytes, err := ioutil.ReadFile(path)
|
||||
return bytes, path, err
|
||||
}
|
||||
|
||||
// withSafeTTYAndInterrupts invokes the provided function after the terminal
|
||||
// state has been stored, and then on any error or termination attempts to
|
||||
// restore the terminal state to its prior behavior. It also eats signals
|
||||
// for the duration of the function.
|
||||
func withSafeTTYAndInterrupts(fn func() error) error {
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, childSignals...)
|
||||
defer signal.Stop(ch)
|
||||
|
||||
inFd := os.Stdin.Fd()
|
||||
if !term.IsTerminal(inFd) {
|
||||
if f, err := os.Open("/dev/tty"); err == nil {
|
||||
defer f.Close()
|
||||
inFd = f.Fd()
|
||||
}
|
||||
}
|
||||
|
||||
if term.IsTerminal(inFd) {
|
||||
state, err := term.SaveState(inFd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
if _, ok := <-ch; !ok {
|
||||
return
|
||||
}
|
||||
term.RestoreTerminal(inFd, state)
|
||||
}()
|
||||
defer term.RestoreTerminal(inFd, state)
|
||||
return fn()
|
||||
}
|
||||
return fn()
|
||||
}
|
||||
|
||||
func tempFile(prefix, suffix string) (f *os.File, err error) {
|
||||
dir := os.TempDir()
|
||||
|
||||
for i := 0; i < 10000; i++ {
|
||||
name := filepath.Join(dir, prefix+randSeq(5)+suffix)
|
||||
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if os.IsExist(err) {
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
|
||||
|
||||
func randSeq(n int) string {
|
||||
b := make([]rune, n)
|
||||
for i := range b {
|
||||
b[i] = letters[rand.Intn(len(letters))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
63
pkg/kubectl/cmd/util/editor/editor_test.go
Normal file
63
pkg/kubectl/cmd/util/editor/editor_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 editor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestArgs(t *testing.T) {
|
||||
if e, a := []string{"/bin/bash", "-c \"test\""}, (Editor{Args: []string{"/bin/bash", "-c"}, Shell: true}).args("test"); !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("unexpected args: %v", a)
|
||||
}
|
||||
if e, a := []string{"/bin/bash", "-c", "test"}, (Editor{Args: []string{"/bin/bash", "-c"}, Shell: false}).args("test"); !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("unexpected args: %v", a)
|
||||
}
|
||||
if e, a := []string{"/bin/bash", "-i -c \"test\""}, (Editor{Args: []string{"/bin/bash", "-i -c"}, Shell: true}).args("test"); !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("unexpected args: %v", a)
|
||||
}
|
||||
if e, a := []string{"/test", "test"}, (Editor{Args: []string{"/test"}}).args("test"); !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("unexpected args: %v", a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEditor(t *testing.T) {
|
||||
edit := Editor{Args: []string{"cat"}}
|
||||
testStr := "test something\n"
|
||||
contents, path, err := edit.LaunchTempFile("", "someprefix", bytes.NewBufferString(testStr))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
t.Fatalf("no temp file: %s", path)
|
||||
}
|
||||
defer os.Remove(path)
|
||||
if disk, err := ioutil.ReadFile(path); err != nil || !bytes.Equal(contents, disk) {
|
||||
t.Errorf("unexpected file on disk: %v %s", err, string(disk))
|
||||
}
|
||||
if !bytes.Equal(contents, []byte(testStr)) {
|
||||
t.Errorf("unexpected contents: %s", string(contents))
|
||||
}
|
||||
if !strings.Contains(path, "someprefix") {
|
||||
t.Errorf("path not expected: %s", path)
|
||||
}
|
||||
}
|
||||
27
pkg/kubectl/cmd/util/editor/term.go
Normal file
27
pkg/kubectl/cmd/util/editor/term.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// +build !windows
|
||||
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 editor
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// childSignals are the allowed signals that can be sent to children in Unix variant OS's
|
||||
var childSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT}
|
||||
26
pkg/kubectl/cmd/util/editor/term_unsupported.go
Normal file
26
pkg/kubectl/cmd/util/editor/term_unsupported.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// +build windows
|
||||
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 editor
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// childSignals are the allowed signals that can be sent to children in Windows to terminate
|
||||
var childSignals = []os.Signal{os.Interrupt}
|
||||
227
pkg/kubectl/cmd/util/jsonmerge/jsonmerge.go
Normal file
227
pkg/kubectl/cmd/util/jsonmerge/jsonmerge.go
Normal file
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 jsonmerge
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/evanphx/json-patch"
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/kubernetes/pkg/util/yaml"
|
||||
)
|
||||
|
||||
// Delta represents a change between two JSON documents.
|
||||
type Delta struct {
|
||||
original []byte
|
||||
edit []byte
|
||||
|
||||
preconditions []PreconditionFunc
|
||||
}
|
||||
|
||||
// PreconditionFunc is a test to verify that an incompatible change
|
||||
// has occurred before an Apply can be successful.
|
||||
type PreconditionFunc func(interface{}) (hold bool, message string)
|
||||
|
||||
// AddPreconditions adds precondition checks to a change which must
|
||||
// be satisfied before an Apply is considered successful. If a
|
||||
// precondition returns false, the Apply is failed with
|
||||
// ErrPreconditionFailed.
|
||||
func (d *Delta) AddPreconditions(fns ...PreconditionFunc) {
|
||||
d.preconditions = append(d.preconditions, fns...)
|
||||
}
|
||||
|
||||
// RequireKeyUnchanged creates a precondition function that fails
|
||||
// if the provided key is present in the diff (indicating its value
|
||||
// has changed).
|
||||
func RequireKeyUnchanged(key string) PreconditionFunc {
|
||||
return func(diff interface{}) (bool, string) {
|
||||
m, ok := diff.(map[string]interface{})
|
||||
if !ok {
|
||||
return true, ""
|
||||
}
|
||||
// the presence of key in a diff means that its value has been changed, therefore
|
||||
// we should fail the precondition.
|
||||
_, ok = m[key]
|
||||
if ok {
|
||||
return false, key + " should not be changed\n"
|
||||
} else {
|
||||
return true, ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RequireKeyUnchanged creates a precondition function that fails
|
||||
// if the metadata.key is present in the diff (indicating its value
|
||||
// has changed).
|
||||
func RequireMetadataKeyUnchanged(key string) PreconditionFunc {
|
||||
return func(diff interface{}) (bool, string) {
|
||||
m, ok := diff.(map[string]interface{})
|
||||
if !ok {
|
||||
return true, ""
|
||||
}
|
||||
m1, ok := m["metadata"]
|
||||
if !ok {
|
||||
return true, ""
|
||||
}
|
||||
m2, ok := m1.(map[string]interface{})
|
||||
if !ok {
|
||||
return true, ""
|
||||
}
|
||||
_, ok = m2[key]
|
||||
if ok {
|
||||
return false, "metadata." + key + " should not be changed\n"
|
||||
} else {
|
||||
return true, ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestPreconditions test if preconditions hold given the edit
|
||||
func TestPreconditionsHold(edit []byte, preconditions []PreconditionFunc) (bool, string) {
|
||||
diff := make(map[string]interface{})
|
||||
if err := json.Unmarshal(edit, &diff); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
for _, fn := range preconditions {
|
||||
if hold, msg := fn(diff); !hold {
|
||||
return false, msg
|
||||
}
|
||||
}
|
||||
return true, ""
|
||||
}
|
||||
|
||||
// NewDelta accepts two JSON or YAML documents and calculates the difference
|
||||
// between them. It returns a Delta object which can be used to resolve
|
||||
// conflicts against a third version with a common parent, or an error
|
||||
// if either document is in error.
|
||||
func NewDelta(from, to []byte) (*Delta, error) {
|
||||
d := &Delta{}
|
||||
before, err := yaml.ToJSON(from)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
after, err := yaml.ToJSON(to)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
diff, err := jsonpatch.CreateMergePatch(before, after)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
glog.V(6).Infof("Patch created from:\n%s\n%s\n%s", string(before), string(after), string(diff))
|
||||
d.original = before
|
||||
d.edit = diff
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// Apply attempts to apply the changes described by Delta onto latest,
|
||||
// returning an error if the changes cannot be applied cleanly.
|
||||
// IsConflicting will be true if the changes overlap, otherwise a
|
||||
// generic error will be returned.
|
||||
func (d *Delta) Apply(latest []byte) ([]byte, error) {
|
||||
base, err := yaml.ToJSON(latest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
changes, err := jsonpatch.CreateMergePatch(d.original, base)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
diff1 := make(map[string]interface{})
|
||||
if err := json.Unmarshal(d.edit, &diff1); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
diff2 := make(map[string]interface{})
|
||||
if err := json.Unmarshal(changes, &diff2); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, fn := range d.preconditions {
|
||||
hold1, _ := fn(diff1)
|
||||
hold2, _ := fn(diff2)
|
||||
if !hold1 || !hold2 {
|
||||
return nil, ErrPreconditionFailed
|
||||
}
|
||||
}
|
||||
|
||||
glog.V(6).Infof("Testing for conflict between:\n%s\n%s", string(d.edit), string(changes))
|
||||
if hasConflicts(diff1, diff2) {
|
||||
return nil, ErrConflict
|
||||
}
|
||||
return jsonpatch.MergePatch(base, d.edit)
|
||||
}
|
||||
|
||||
// IsConflicting returns true if the provided error indicates a
|
||||
// conflict exists between the original changes and the applied
|
||||
// changes.
|
||||
func IsConflicting(err error) bool {
|
||||
return err == ErrConflict
|
||||
}
|
||||
|
||||
// IsPreconditionFailed returns true if the provided error indicates
|
||||
// a Delta precondition did not succeed.
|
||||
func IsPreconditionFailed(err error) bool {
|
||||
return err == ErrPreconditionFailed
|
||||
}
|
||||
|
||||
var ErrPreconditionFailed = fmt.Errorf("a precondition failed")
|
||||
var ErrConflict = fmt.Errorf("changes are in conflict")
|
||||
|
||||
// hasConflicts returns true if the left and right JSON interface objects overlap with
|
||||
// different values in any key. The code will panic if an unrecognized type is passed
|
||||
// (anything not returned by a JSON decode). All keys are required to be strings.
|
||||
func hasConflicts(left, right interface{}) bool {
|
||||
switch typedLeft := left.(type) {
|
||||
case map[string]interface{}:
|
||||
switch typedRight := right.(type) {
|
||||
case map[string]interface{}:
|
||||
for key, leftValue := range typedLeft {
|
||||
if rightValue, ok := typedRight[key]; ok && hasConflicts(leftValue, rightValue) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
case []interface{}:
|
||||
switch typedRight := right.(type) {
|
||||
case []interface{}:
|
||||
if len(typedLeft) != len(typedRight) {
|
||||
return true
|
||||
}
|
||||
for i := range typedLeft {
|
||||
if hasConflicts(typedLeft[i], typedRight[i]) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
case string, float64, bool, int, int64, nil:
|
||||
return !reflect.DeepEqual(left, right)
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown type: %v", reflect.TypeOf(left)))
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Delta) Edit() []byte {
|
||||
return d.edit
|
||||
}
|
||||
75
pkg/kubectl/cmd/util/jsonmerge/jsonmerge_test.go
Normal file
75
pkg/kubectl/cmd/util/jsonmerge/jsonmerge_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 jsonmerge
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHasConflicts(t *testing.T) {
|
||||
testCases := []struct {
|
||||
A interface{}
|
||||
B interface{}
|
||||
Ret bool
|
||||
}{
|
||||
{A: "hello", B: "hello", Ret: false}, // 0
|
||||
{A: "hello", B: "hell", Ret: true},
|
||||
{A: "hello", B: nil, Ret: true},
|
||||
{A: "hello", B: 1, Ret: true},
|
||||
{A: "hello", B: float64(1.0), Ret: true},
|
||||
{A: "hello", B: false, Ret: true},
|
||||
|
||||
{A: "hello", B: []interface{}{}, Ret: true}, // 6
|
||||
{A: []interface{}{1}, B: []interface{}{}, Ret: true},
|
||||
{A: []interface{}{}, B: []interface{}{}, Ret: false},
|
||||
{A: []interface{}{1}, B: []interface{}{1}, Ret: false},
|
||||
{A: map[string]interface{}{}, B: []interface{}{1}, Ret: true},
|
||||
|
||||
{A: map[string]interface{}{}, B: map[string]interface{}{"a": 1}, Ret: false}, // 11
|
||||
{A: map[string]interface{}{"a": 1}, B: map[string]interface{}{"a": 1}, Ret: false},
|
||||
{A: map[string]interface{}{"a": 1}, B: map[string]interface{}{"a": 2}, Ret: true},
|
||||
{A: map[string]interface{}{"a": 1}, B: map[string]interface{}{"b": 2}, Ret: false},
|
||||
|
||||
{ // 15
|
||||
A: map[string]interface{}{"a": []interface{}{1}},
|
||||
B: map[string]interface{}{"a": []interface{}{1}},
|
||||
Ret: false,
|
||||
},
|
||||
{
|
||||
A: map[string]interface{}{"a": []interface{}{1}},
|
||||
B: map[string]interface{}{"a": []interface{}{}},
|
||||
Ret: true,
|
||||
},
|
||||
{
|
||||
A: map[string]interface{}{"a": []interface{}{1}},
|
||||
B: map[string]interface{}{"a": 1},
|
||||
Ret: true,
|
||||
},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
out := hasConflicts(testCase.A, testCase.B)
|
||||
if out != testCase.Ret {
|
||||
t.Errorf("%d: expected %t got %t", i, testCase.Ret, out)
|
||||
continue
|
||||
}
|
||||
out = hasConflicts(testCase.B, testCase.A)
|
||||
if out != testCase.Ret {
|
||||
t.Errorf("%d: expected reversed %t got %t", i, testCase.Ret, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user