Skip to content
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

Fix dry-run output in kubectl apply --prune #69344

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions pkg/kubectl/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,14 @@ func (o *ApplyOptions) Run() error {
return cmdutil.AddSourceToErr("creating", info.Source, err)
}
info.Refresh(obj, true)
metadata, err := meta.Accessor(info.Object)
if err != nil {
return err
}
visitedUids.Insert(string(metadata.GetUID()))
}

metadata, err := meta.Accessor(info.Object)
if err != nil {
return err
}
visitedUids.Insert(string(metadata.GetUID()))

count++

if printObject {
Expand All @@ -410,12 +411,13 @@ func (o *ApplyOptions) Run() error {
return printer.PrintObj(info.Object, o.Out)
}

if !o.DryRun {
metadata, err := meta.Accessor(info.Object)
if err != nil {
return err
}
metadata, err := meta.Accessor(info.Object)
if err != nil {
return err
}
visitedUids.Insert(string(metadata.GetUID()))

if !o.DryRun {
annotationMap := metadata.GetAnnotations()
if _, ok := annotationMap[corev1.LastAppliedConfigAnnotation]; !ok {
fmt.Fprintf(o.ErrOut, warningNoLastAppliedConfigAnnotation, o.cmdBaseName)
Expand Down Expand Up @@ -443,8 +445,6 @@ func (o *ApplyOptions) Run() error {

info.Refresh(patchedObject, true)

visitedUids.Insert(string(metadata.GetUID()))

if string(patchBytes) == "{}" && !printObject {
count++

Expand Down
48 changes: 36 additions & 12 deletions pkg/kubectl/cmd/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,24 @@ const (
filenameWidgetServerside = "../../../../test/fixtures/pkg/kubectl/cmd/apply/widget-serverside.yaml"
)

func readConfigMapList(t *testing.T, filename string) []byte {
func readConfigMapList(t *testing.T, filename string) [][]byte {
data := readBytesFromFile(t, filename)
cmList := corev1.ConfigMapList{}
if err := runtime.DecodeInto(codec, data, &cmList); err != nil {
t.Fatal(err)
}

cmListBytes, err := runtime.Encode(codec, &cmList)
if err != nil {
t.Fatal(err)
var listCmBytes [][]byte

for _, cm := range cmList.Items {
cmBytes, err := runtime.Encode(codec, &cm)
if err != nil {
t.Fatal(err)
}
listCmBytes = append(listCmBytes, cmBytes)
}

return cmListBytes
return listCmBytes
}

func readBytesFromFile(t *testing.T, filename string) []byte {
Expand Down Expand Up @@ -270,7 +275,7 @@ func walkMapPath(t *testing.T, start map[string]interface{}, path []string) map[

func TestRunApplyPrintsValidObjectList(t *testing.T) {
cmdtesting.InitTestErrorHandler(t)
cmBytes := readConfigMapList(t, filenameCM)
configMapList := readConfigMapList(t, filenameCM)
pathCM := "/namespaces/test/configmaps"

tf := cmdtesting.NewTestFactory().WithNamespace("test")
Expand All @@ -280,12 +285,21 @@ func TestRunApplyPrintsValidObjectList(t *testing.T) {
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
switch p, m := req.URL.Path, req.Method; {
case strings.HasPrefix(p, pathCM) && m != "GET":
pod := ioutil.NopCloser(bytes.NewReader(cmBytes))
return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: pod}, nil
case strings.HasPrefix(p, pathCM) && m != "PATCH":
pod := ioutil.NopCloser(bytes.NewReader(cmBytes))
return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: pod}, nil
case strings.HasPrefix(p, pathCM) && m == "GET":
fallthrough
case strings.HasPrefix(p, pathCM) && m == "PATCH":
var body io.ReadCloser

switch p {
case pathCM + "/test0":
body = ioutil.NopCloser(bytes.NewReader(configMapList[0]))
case pathCM + "/test1":
body = ioutil.NopCloser(bytes.NewReader(configMapList[1]))
default:
t.Errorf("unexpected request to %s", p)
}

return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: body}, nil
default:
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
return nil, nil
Expand All @@ -306,6 +320,16 @@ func TestRunApplyPrintsValidObjectList(t *testing.T) {
if err := runtime.DecodeInto(codec, buf.Bytes(), &cmList); err != nil {
t.Fatal(err)
}

if len(cmList.Items) != 2 {
t.Fatalf("Expected 2 items in the result; got %d", len(cmList.Items))
}
if !strings.Contains(string(cmList.Items[0].Raw), "key1") {
t.Fatalf("Did not get first ConfigMap at the first position")
}
if !strings.Contains(string(cmList.Items[1].Raw), "key2") {
t.Fatalf("Did not get second ConfigMap at the second position")
}
}

func TestRunApplyViewLastApplied(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/pkg/kubectl/cmd/apply/cm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ items:
- kind: ConfigMap
apiVersion: v1
metadata:
name: test
name: test0
data:
key1: apple
- kind: ConfigMap
apiVersion: v1
metadata:
name: test2
name: test1
data:
key2: apple
kind: ConfigMapList
Expand Down