-
Notifications
You must be signed in to change notification settings - Fork 39.3k
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
add kubectl apply diff-last-applied
subcommand
#49174
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/kubectl/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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is going on with this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mengqiy wrote this, my understanding is usage in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just don't understand why it's necessary to make it a separate variable if it's only used once as part of another string. @mengqiy can you help me understand? Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote that abut one year ago. It may be not good now. Feel free to refactor it or just remove it if it is not helpful. :) |
||
|
||
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" | ||
utilfile "k8s.io/kubernetes/pkg/util/file" | ||
) | ||
|
||
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 = "./git-diff.result" | ||
fileNameNormalDiffResutl = "./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": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just use if req.URL.Path == pathRC && req.Method == "GET" {
...
}
t.Fatalf(...)
return nil, nil There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha, good justification. I'm a little surprised that's our existing style. |
||
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, _ := utilfile.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, _ = utilfile.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.
It should have flag
--diff-viewer
as described in the proposal.It should take either
KUBECTL_EXTERNAL_DIFF
or--diff-viewer
. If they are both specified, it should check if they match. If not, error out.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.
I already add that flag, still need to add some tests and update help information, if there's some scenario I haven't consider in or code could be simplify, please comment on it, thanks