Commit 6ba5f77d authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #53920 from apelisse/diff

Automatic merge from submit-queue (batch tested with PRs 53051, 52489, 53920). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Implement `kubectl alpha diff` to diff resources `kubectl alpha diff` lets you diff your resources against live resources, or last applied, or even preview what changes are going to be applied on the cluster. This is still quite premature, and mostly untested. **What this PR does / why we need it**: **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # **Special notes for your reviewer**: **Release note**: Clearly not ready for Release note. ```release-note NONE ``` kubernetes/community#287
parents f816e75b 14ec200e
...@@ -18,6 +18,7 @@ docs/man/man1/kube-apiserver.1 ...@@ -18,6 +18,7 @@ docs/man/man1/kube-apiserver.1
docs/man/man1/kube-controller-manager.1 docs/man/man1/kube-controller-manager.1
docs/man/man1/kube-proxy.1 docs/man/man1/kube-proxy.1
docs/man/man1/kube-scheduler.1 docs/man/man1/kube-scheduler.1
docs/man/man1/kubectl-alpha-diff.1
docs/man/man1/kubectl-alpha.1 docs/man/man1/kubectl-alpha.1
docs/man/man1/kubectl-annotate.1 docs/man/man1/kubectl-annotate.1
docs/man/man1/kubectl-api-versions.1 docs/man/man1/kubectl-api-versions.1
...@@ -115,6 +116,8 @@ docs/man/man1/kubectl-version.1 ...@@ -115,6 +116,8 @@ docs/man/man1/kubectl-version.1
docs/man/man1/kubectl.1 docs/man/man1/kubectl.1
docs/man/man1/kubelet.1 docs/man/man1/kubelet.1
docs/user-guide/kubectl/kubectl.md docs/user-guide/kubectl/kubectl.md
docs/user-guide/kubectl/kubectl_alpha.md
docs/user-guide/kubectl/kubectl_alpha_diff.md
docs/user-guide/kubectl/kubectl_annotate.md docs/user-guide/kubectl/kubectl_annotate.md
docs/user-guide/kubectl/kubectl_api-versions.md docs/user-guide/kubectl/kubectl_api-versions.md
docs/user-guide/kubectl/kubectl_apply.md docs/user-guide/kubectl/kubectl_apply.md
......
This file is autogenerated, but we've stopped checking such files into the
repository to reduce the need for rebases. Please run hack/generate-docs.sh to
populate this file.
This file is autogenerated, but we've stopped checking such files into the
repository to reduce the need for rebases. Please run hack/generate-docs.sh to
populate this file.
This file is autogenerated, but we've stopped checking such files into the
repository to reduce the need for rebases. Please run hack/generate-docs.sh to
populate this file.
...@@ -38,6 +38,7 @@ go_library( ...@@ -38,6 +38,7 @@ go_library(
"create_serviceaccount.go", "create_serviceaccount.go",
"delete.go", "delete.go",
"describe.go", "describe.go",
"diff.go",
"drain.go", "drain.go",
"edit.go", "edit.go",
"exec.go", "exec.go",
...@@ -78,6 +79,8 @@ go_library( ...@@ -78,6 +79,8 @@ go_library(
"//pkg/client/clientset_generated/internalclientset/typed/rbac/internalversion:go_default_library", "//pkg/client/clientset_generated/internalclientset/typed/rbac/internalversion:go_default_library",
"//pkg/client/unversioned:go_default_library", "//pkg/client/unversioned:go_default_library",
"//pkg/kubectl:go_default_library", "//pkg/kubectl:go_default_library",
"//pkg/kubectl/apply/parse:go_default_library",
"//pkg/kubectl/apply/strategy:go_default_library",
"//pkg/kubectl/cmd/auth:go_default_library", "//pkg/kubectl/cmd/auth:go_default_library",
"//pkg/kubectl/cmd/config:go_default_library", "//pkg/kubectl/cmd/config:go_default_library",
"//pkg/kubectl/cmd/rollout:go_default_library", "//pkg/kubectl/cmd/rollout:go_default_library",
...@@ -169,6 +172,7 @@ go_test( ...@@ -169,6 +172,7 @@ go_test(
"create_test.go", "create_test.go",
"delete_test.go", "delete_test.go",
"describe_test.go", "describe_test.go",
"diff_test.go",
"drain_test.go", "drain_test.go",
"edit_test.go", "edit_test.go",
"exec_test.go", "exec_test.go",
...@@ -244,6 +248,7 @@ go_test( ...@@ -244,6 +248,7 @@ go_test(
"//vendor/k8s.io/client-go/tools/remotecommand:go_default_library", "//vendor/k8s.io/client-go/tools/remotecommand:go_default_library",
"//vendor/k8s.io/kube-openapi/pkg/util/proto:go_default_library", "//vendor/k8s.io/kube-openapi/pkg/util/proto:go_default_library",
"//vendor/k8s.io/metrics/pkg/apis/metrics/v1alpha1:go_default_library", "//vendor/k8s.io/metrics/pkg/apis/metrics/v1alpha1:go_default_library",
"//vendor/k8s.io/utils/exec:go_default_library",
], ],
) )
......
...@@ -37,6 +37,7 @@ func NewCmdAlpha(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Com ...@@ -37,6 +37,7 @@ func NewCmdAlpha(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Com
// Alpha commands should be added here. As features graduate from alpha they should move // Alpha commands should be added here. As features graduate from alpha they should move
// from here to the CommandGroups defined by NewKubeletCommand() in cmd.go. // from here to the CommandGroups defined by NewKubeletCommand() in cmd.go.
//cmd.AddCommand(NewCmdDebug(f, in, out, err)) //cmd.AddCommand(NewCmdDebug(f, in, out, err))
cmd.AddCommand(NewCmdDiff(f, out, err))
// NewKubeletCommand() will hide the alpha command if it has no subcommands. Overriding // NewKubeletCommand() will hide the alpha command if it has no subcommands. Overriding
// the help function ensures a reasonable message if someone types the hidden command anyway. // the help function ensures a reasonable message if someone types the hidden command anyway.
......
/*
Copyright 2017 The Kubernetes 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 cmd
import (
"bytes"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
"k8s.io/utils/exec"
)
type FakeObject struct {
name string
local map[string]interface{}
merged map[string]interface{}
live map[string]interface{}
last map[string]interface{}
}
var _ Object = &FakeObject{}
func (f *FakeObject) Name() string {
return f.name
}
func (f *FakeObject) Local() (map[string]interface{}, error) {
return f.local, nil
}
func (f *FakeObject) Merged() (map[string]interface{}, error) {
return f.merged, nil
}
func (f *FakeObject) Live() (map[string]interface{}, error) {
return f.live, nil
}
func (f *FakeObject) Last() (map[string]interface{}, error) {
return f.last, nil
}
func TestArguments(t *testing.T) {
tests := []struct {
// Input
args []string
// Outputs
from string
to string
err string
}{
// Defaults
{
args: []string{},
from: "LOCAL",
to: "LIVE",
err: "",
},
// One valid argument
{
args: []string{"MERGED"},
from: "MERGED",
to: "LIVE",
err: "",
},
// One invalid argument
{
args: []string{"WRONG"},
from: "",
to: "",
err: `Invalid parameter "WRONG", must be either "LOCAL", "LIVE", "LAST" or "MERGED"`,
},
// Two valid arguments
{
args: []string{"MERGED", "LAST"},
from: "MERGED",
to: "LAST",
err: "",
},
// Two same arguments is fine
{
args: []string{"MERGED", "MERGED"},
from: "MERGED",
to: "MERGED",
err: "",
},
// Second argument is invalid
{
args: []string{"MERGED", "WRONG"},
from: "",
to: "",
err: `Invalid parameter "WRONG", must be either "LOCAL", "LIVE", "LAST" or "MERGED"`,
},
// Three arguments
{
args: []string{"MERGED", "LIVE", "LAST"},
from: "",
to: "",
err: `Invalid number of arguments: expected at most 2.`,
},
}
for _, test := range tests {
from, to, e := parseDiffArguments(test.args)
err := ""
if e != nil {
err = e.Error()
}
if from != test.from || to != test.to || err != test.err {
t.Errorf("parseDiffArguments(%v) = (%v, %v, %v), expected (%v, %v, %v)",
test.args,
from, to, err,
test.from, test.to, test.err,
)
}
}
}
func TestDiffProgram(t *testing.T) {
os.Setenv("KUBERNETES_EXTERNAL_DIFF", "echo")
stdout := bytes.Buffer{}
diff := DiffProgram{
Stdout: &stdout,
Stderr: &bytes.Buffer{},
Exec: exec.New(),
}
err := diff.Run("one", "two")
if err != nil {
t.Fatal(err)
}
if output := stdout.String(); output != "one two\n" {
t.Fatalf(`stdout = %q, expected "one two\n"`, output)
}
}
func TestPrinter(t *testing.T) {
printer := Printer{}
obj := map[string]interface{}{
"string": "string",
"list": []int{1, 2, 3},
"int": 12,
}
buf := bytes.Buffer{}
printer.Print(obj, &buf)
want := `int: 12
list:
- 1
- 2
- 3
string: string
`
if buf.String() != want {
t.Errorf("Print() = %q, want %q", buf.String(), want)
}
}
func TestDiffVersion(t *testing.T) {
diff, err := NewDiffVersion("LOCAL")
if err != nil {
t.Fatal(err)
}
defer diff.Dir.Delete()
obj := FakeObject{
name: "bla",
local: map[string]interface{}{"local": true},
last: map[string]interface{}{"last": true},
live: map[string]interface{}{"live": true},
merged: map[string]interface{}{"merged": true},
}
err = diff.Print(&obj, Printer{})
if err != nil {
t.Fatal(err)
}
fcontent, err := ioutil.ReadFile(path.Join(diff.Dir.Name, obj.Name()))
if err != nil {
t.Fatal(err)
}
econtent := "local: true\n"
if string(fcontent) != econtent {
t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
}
}
func TestDirectory(t *testing.T) {
dir, err := CreateDirectory("prefix")
defer dir.Delete()
if err != nil {
t.Fatal(err)
}
_, err = os.Stat(dir.Name)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(filepath.Base(dir.Name), "prefix") {
t.Fatalf(`Directory doesn't start with "prefix": %q`, dir.Name)
}
entries, err := ioutil.ReadDir(dir.Name)
if err != nil {
t.Fatal(err)
}
if len(entries) != 0 {
t.Fatalf("Directory should be empty, has %d elements", len(entries))
}
_, err = dir.NewFile("ONE")
if err != nil {
t.Fatal(err)
}
_, err = dir.NewFile("TWO")
if err != nil {
t.Fatal(err)
}
entries, err = ioutil.ReadDir(dir.Name)
if err != nil {
t.Fatal(err)
}
if len(entries) != 2 {
t.Fatalf("ReadDir should have two elements, has %d elements", len(entries))
}
err = dir.Delete()
if err != nil {
t.Fatal(err)
}
_, err = os.Stat(dir.Name)
if err == nil {
t.Fatal("Directory should be gone, still present.")
}
}
func TestDiffer(t *testing.T) {
diff, err := NewDiffer("LOCAL", "LIVE")
if err != nil {
t.Fatal(err)
}
defer diff.TearDown()
obj := FakeObject{
name: "bla",
local: map[string]interface{}{"local": true},
last: map[string]interface{}{"last": true},
live: map[string]interface{}{"live": true},
merged: map[string]interface{}{"merged": true},
}
err = diff.Diff(&obj, Printer{})
if err != nil {
t.Fatal(err)
}
fcontent, err := ioutil.ReadFile(path.Join(diff.From.Dir.Name, obj.Name()))
if err != nil {
t.Fatal(err)
}
econtent := "local: true\n"
if string(fcontent) != econtent {
t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
}
fcontent, err = ioutil.ReadFile(path.Join(diff.To.Dir.Name, obj.Name()))
if err != nil {
t.Fatal(err)
}
econtent = "live: true\n"
if string(fcontent) != econtent {
t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment