-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Add kubectl apply diff-last-applied
subcommand
#46560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
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 ( | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"k8s.io/kubernetes/pkg/kubectl" | ||
"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/diff" | ||
"k8s.io/kubernetes/pkg/util/i18n" | ||
) | ||
|
||
var ( | ||
applyDiffLastAppliedLong = templates.LongDesc(i18n.T(` | ||
Opens up a 2-way diff in the default diff viewer. This should follow the same semantics as 'git diff'. | ||
It should accept an environment variable KUBECTL_EXTERNAL_DIFF=meld to specify a custom diff tool. | ||
If not specified, the 'git diff' command should be used, if 'git diff' not found, it will degenerate to use diff. | ||
`)) | ||
|
||
applyDiffLastAppliedExample = templates.Examples(i18n.T(` | ||
# Diff the last-applied-configuration annotations with input file | ||
kubectl apply diff-last-applied -f deploy.yaml | ||
`)) | ||
) | ||
|
||
func NewCmdApplyDiffLastApplied(f cmdutil.Factory, out, err io.Writer) *cobra.Command { | ||
options := &diff.Options{Out: out, ErrOut: err} | ||
cmd := &cobra.Command{ | ||
Use: "diff-last-applied -f FILENAME", | ||
Short: i18n.T("Diff the last-applied-configuration annotation on a live object to match the contents of a file."), | ||
Long: applyDiffLastAppliedLong, | ||
Example: applyDiffLastAppliedExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmdutil.CheckErr(options.Complete(f, cmd)) | ||
cmdutil.CheckErr(options.Validate(f, cmd)) | ||
cmdutil.CheckErr(options.RunDiffLastApplied(f, cmd)) | ||
}, | ||
} | ||
cmdutil.AddOutputVarFlagsForJsonYaml(cmd, &options.Output, "yaml") | ||
usage := "that contains the last-applied-configuration annotations" | ||
kubectl.AddJsonFilenameFlag(cmd, &options.FilenameOptions.Filenames, "Filename, directory, or URL to files "+usage) | ||
|
||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ import ( | |
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing" | ||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
"k8s.io/kubernetes/pkg/printers" | ||
"k8s.io/kubernetes/pkg/util" | ||
) | ||
|
||
func TestApplyExtraArgsFail(t *testing.T) { | ||
|
@@ -62,18 +63,24 @@ func validateApplyArgs(cmd *cobra.Command, args []string) error { | |
} | ||
|
||
const ( | ||
filenameRC = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc.yaml" | ||
filenameRCNoAnnotation = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-no-annotation.yaml" | ||
filenameRCLASTAPPLIED = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-lastapplied.yaml" | ||
filenameSVC = "../../../test/fixtures/pkg/kubectl/cmd/apply/service.yaml" | ||
filenameRCSVC = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-service.yaml" | ||
filenameNoExistRC = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-noexist.yaml" | ||
filenameRCPatchTest = "../../../test/fixtures/pkg/kubectl/cmd/apply/patch.json" | ||
dirName = "../../../test/fixtures/pkg/kubectl/cmd/apply/testdir" | ||
filenameRCJSON = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc.json" | ||
filenameRC = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc.yaml" | ||
filenameRCNoAnnotation = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-no-annotation.yaml" | ||
filenameRCLASTAPPLIED = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-lastapplied.yaml" | ||
filenameSVC = "../../../test/fixtures/pkg/kubectl/cmd/apply/service.yaml" | ||
filenameRCSVC = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-service.yaml" | ||
filenameNoExistRC = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-noexist.yaml" | ||
filenameLocalChangedRC = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-changed.yaml" | ||
filenameApplyDiffRESP = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-resp.json" | ||
filenameApplyDiffChangedRESP = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc-changed-resp.json" | ||
filenameRCPatchTest = "../../../test/fixtures/pkg/kubectl/cmd/apply/patch.json" | ||
dirName = "../../../test/fixtures/pkg/kubectl/cmd/apply/testdir" | ||
filenameRCJSON = "../../../test/fixtures/pkg/kubectl/cmd/apply/rc.json" | ||
|
||
filenameWidgetClientside = "../../../test/fixtures/pkg/kubectl/cmd/apply/widget-clientside.yaml" | ||
filenameWidgetServerside = "../../../test/fixtures/pkg/kubectl/cmd/apply/widget-serverside.yaml" | ||
|
||
fileNameGitDiffResult = "/tmp/git-diff.result" | ||
fileNameNormalDiffResutl = "/tmp/diff.result" | ||
) | ||
|
||
func readBytesFromFile(t *testing.T, filename string) []byte { | ||
|
@@ -991,6 +998,97 @@ func TestRunApplySetLastApplied(t *testing.T) { | |
cmdutil.BehaviorOnFatal(func(str string, code int) {}) | ||
} | ||
|
||
func TestRunApplyDiffLastApplied(t *testing.T) { | ||
|
||
initTestErrorHandler(t) | ||
rcByte := readBytesFromFile(t, filenameApplyDiffRESP) | ||
pathRC := "/namespaces/test/replicationcontrollers/" + "test-rc" | ||
|
||
rcChangedByte := readBytesFromFile(t, filenameApplyDiffChangedRESP) | ||
|
||
tests := []struct { | ||
name, nameRC, pathRC, filePath, expectedGitDiffOut, expectedDiffOut, expectedErr, output string | ||
resp []byte | ||
}{ | ||
{ | ||
name: "git-diff objects with no different", | ||
filePath: filenameRC, | ||
expectedErr: "", | ||
expectedGitDiffOut: "", | ||
expectedDiffOut: "", | ||
resp: rcByte, | ||
output: "yaml", | ||
}, | ||
{ | ||
name: "git-diff after local file changed with json", | ||
filePath: filenameLocalChangedRC, | ||
expectedErr: "", | ||
expectedGitDiffOut: "- \"containerPort\": 80\n+ \"containerPort\": 123\n }\n", | ||
expectedDiffOut: "< \"containerPort\": 80\n> \"containerPort\": 123\n", | ||
resp: rcByte, | ||
output: "json", | ||
}, | ||
{ | ||
name: "git-diff after annotations changed", | ||
filePath: filenameRC, | ||
expectedErr: "", | ||
expectedGitDiffOut: "- - containerPort: 123\n+ - containerPort: 80\n", | ||
expectedDiffOut: "< - containerPort: 123\n> - containerPort: 80\n", | ||
resp: rcChangedByte, | ||
output: "yaml", | ||
}, | ||
} | ||
for _, test := range tests { | ||
f, tf, _, _ := cmdtesting.NewAPIFactory() | ||
tf.Printer = &testPrinter{} | ||
tf.UnstructuredClient = &fake.RESTClient{ | ||
APIRegistry: api.Registry, | ||
NegotiatedSerializer: unstructuredSerializer, | ||
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { | ||
switch p, m := req.URL.Path, req.Method; { | ||
case p == pathRC && m == "GET": | ||
bodyRC := ioutil.NopCloser(bytes.NewReader(test.resp)) | ||
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: bodyRC}, nil | ||
default: | ||
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) | ||
return nil, nil | ||
} | ||
}), | ||
} | ||
tf.Namespace = "test" | ||
tf.ClientConfig = defaultClientConfig() | ||
buf, errBuf := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{}) | ||
|
||
|
||
cmd := NewCmdApplyDiffLastApplied(f, buf, errBuf) | ||
os.Setenv("KUBECTL_EXTERNAL_DIFF", "testdata/diff/test_diff.sh") | ||
|
||
cmd.Flags().Set("filename", test.filePath) | ||
cmd.Flags().Set("output", test.output) | ||
cmd.Run(cmd, []string{}) | ||
|
||
pathExist, _ := util.FileExists(fileNameGitDiffResult) | ||
if pathExist { | ||
out := string(readBytesFromFile(t, fileNameGitDiffResult)) | ||
if out != test.expectedGitDiffOut { | ||
t.Fatalf("%s: unexpected output: %q\nexpected: %q", test.name, out, test.expectedGitDiffOut) | ||
} | ||
os.Remove(fileNameGitDiffResult) | ||
continue | ||
} | ||
|
||
pathExist, _ = util.FileExists(fileNameNormalDiffResutl) | ||
if pathExist { | ||
out := string(readBytesFromFile(t, fileNameNormalDiffResutl)) | ||
if out != test.expectedDiffOut { | ||
t.Fatalf("%s: unexpected output: %q\nexpected: %q", test.name, out, test.expectedDiffOut) | ||
} | ||
os.Remove(fileNameNormalDiffResutl) | ||
continue | ||
} | ||
|
||
t.Fatalf(`External test script failed. Check that either "diff" or "git diff" is installed.`) | ||
} | ||
} | ||
|
||
func checkPatchString(t *testing.T, req *http.Request) { | ||
checkString := string(readBytesFromFile(t, filenameRCPatchTest)) | ||
patch, err := ioutil.ReadAll(req.Body) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we start to print it? Is this for debug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, this value should pass to PrintSuccess, if we execute
./kubectl apply set-last-applied -f ~/nginx.yaml -o yaml
it will print outThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I finally understood.
FormatConvert
no longer does the printing.