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

Add missing binaryData field to the ConfigMap Hash #61146

Conversation

dims
Copy link
Member

@dims dims commented Mar 14, 2018

What this PR does / why we need it:

In 7e158fb, we added a BinaryData
to ConfigMap, but totally forgot to add it to the hash method.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

Release note:

The value of of BinaryData is now used in calculating the hash for the configmap

@k8s-ci-robot k8s-ci-robot added do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Mar 14, 2018
@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Mar 14, 2018
@dims
Copy link
Member Author

dims commented Mar 14, 2018

/assign @mtaufen
/assign @liggitt

@dims
Copy link
Member Author

dims commented Mar 14, 2018

/kind bug
/sig api-machinery
/priority important-soon

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. labels Mar 14, 2018
@dims
Copy link
Member Author

dims commented Mar 14, 2018

/milestone v1.10

data, err := json.Marshal(map[string]interface{}{"kind": "ConfigMap", "name": cm.Name, "data": cm.Data})
m := map[string]interface{}{"kind": "ConfigMap", "name": cm.Name, "data": cm.Data}
if len(cm.BinaryData) > 0 {
m["binaryData"] = cm.BinaryData
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the stdlib marshal []byte in unstructured maps correctly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, see TestEncodeConfigMap method "three keys" variation. uses base64 encode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://golang.org/pkg/encoding/json/#Marshal

Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string

// three keys with binary data (tests sorting order)
{"three keys with binary data", &v1.ConfigMap{BinaryData: map[string][]byte{"two": []byte("2"), "one": []byte(""), "three": []byte("3")}}, "t458mc6db2", ""},
// two keys, one with string and another with binary data
{"two keys with one each", &v1.ConfigMap{BinaryData: map[string][]byte{"one": []byte("")}, Data: map[string]string{"two": ""}}, "b8b92gc26c", ""},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personal nit: keep field order the same as the struct definition; swap BinaryData and Data so Data comes first

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixing

// three keys with binary data (tests sorting order)
{"three keys", &v1.ConfigMap{BinaryData: map[string][]byte{"two": []byte("2"), "one": []byte(""), "three": []byte("3")}}, `{"binaryData":{"one":"","three":"Mw==","two":"Mg=="},"data":null,"kind":"ConfigMap","name":""}`, ""},
// two keys, one string and one binary values
{"two keys", &v1.ConfigMap{BinaryData: map[string][]byte{"one": []byte("")}, Data: map[string]string{"two": ""}}, `{"binaryData":{"one":""},"data":{"two":""},"kind":"ConfigMap","name":""}`, ""},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixing

data, err := json.Marshal(map[string]interface{}{"kind": "ConfigMap", "name": cm.Name, "data": cm.Data})
m := map[string]interface{}{"kind": "ConfigMap", "name": cm.Name, "data": cm.Data}
if len(cm.BinaryData) > 0 {
m["binaryData"] = cm.BinaryData
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://golang.org/pkg/encoding/json/#Marshal

Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string

@mtaufen
Copy link
Contributor

mtaufen commented Mar 14, 2018

/status approved-for-milestone

@k8s-github-robot
Copy link

[MILESTONENOTIFIER] Milestone Pull Request: Up-to-date for process

@dims @liggitt @mtaufen

Pull Request Labels
  • sig/api-machinery: Pull Request will be escalated to these SIGs if needed.
  • priority/important-soon: Escalate to the pull request owners and SIG owner; move out of milestone after several unsuccessful escalation attempts.
  • kind/bug: Fixes a bug discovered during the current release.
Help

In 7e158fb, we added a BinaryData
to ConfigMap, but totally forgot to add it to the hash method.
@dims dims force-pushed the add-missing-binary-field-to-configmap-hash branch from 9c6c7d8 to 4cbdbae Compare March 14, 2018 02:08
// one key
{"one key", &v1.ConfigMap{Data: map[string]string{"one": ""}}, "9g67k2htb6", ""},
// three keys (tests sorting order)
{"three keys", &v1.ConfigMap{Data: map[string]string{"two": "2", "one": "", "three": "3"}}, "f5h7t85m9b", ""},
// empty binary data map
{"empty binary data", &v1.ConfigMap{BinaryData: map[string][]byte{}}, "dk855m5d49", ""},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have a different hash from "empty data"? Oh... I bet json.Marshal encodes null for Data in this case, and an empty object in the other one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think so

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you print the serialization to double check?
Maybe we should also consider nilling-out empty maps before hashing to make this consistent, since we don't semantically treat nil and empty as different in the API. Making this change might break backwards compatibility of the hash... or maybe not since Data was omitempty anyway?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtaufen compare line 95 and 101, i have the encoded strings there. the difference between empty and null Data is existing behavior. Are we allowed to change that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, --append-hash is only available as an option to the kubectl create configmap subcommand, and it looks like we always allocate the map there: https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/configmap.go#L131.

We might get away with encoding nil maps as {} (treating them the same as empty maps) in our encoding function. We should look at secrets too, since the same problem probably exists in that encoding function.

@liggitt wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtaufen ack, i can make the change quickly if it sounds ok to @liggitt

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liggitt WDYT? (please see @mtaufen 's comment above)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't change existing treatment of data, or mess with nilling out map values. The length check on binaryData seems fine to me.

@dims
Copy link
Member Author

dims commented Mar 14, 2018

/milestone clear

per discussion on #sig-release with @jdumars and @liggitt this can wait until 1.10.1 (merge when master opens up and then cherry pick after 1.10 ships)

@k8s-ci-robot k8s-ci-robot removed this from the v1.10 milestone Mar 14, 2018
@dims
Copy link
Member Author

dims commented Mar 14, 2018

/test pull-kubernetes-e2e-gce

@mtaufen
Copy link
Contributor

mtaufen commented Mar 14, 2018

sgtm wrt milestone

@jennybuckley
Copy link

/cc @cheftako

@dims
Copy link
Member Author

dims commented Mar 27, 2018

@mtaufen now that master is open, let's please get this in.

@mtaufen
Copy link
Contributor

mtaufen commented Mar 27, 2018

I want to settle the nil/empty thing first, but otherwise this lgtm.

@liggitt
Copy link
Member

liggitt commented Mar 27, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Mar 27, 2018
@mtaufen
Copy link
Contributor

mtaufen commented Mar 28, 2018

/lgtm

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dims, liggitt, mtaufen

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-github-robot
Copy link

Automatic merge from submit-queue (batch tested with PRs 60465, 61773, 61371, 61146). If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-github-robot k8s-github-robot merged commit e00a602 into kubernetes:master Mar 28, 2018
@xcompass
Copy link

xcompass commented Jun 6, 2018

Shouldn't this be in 1.10 branch? As the binary data field in configmap is introduced in 1.10 and it seems it doesn't work without change.

@mtaufen
Copy link
Contributor

mtaufen commented Jun 6, 2018

@dims was this cherry-picked?

@dims
Copy link
Member Author

dims commented Jun 6, 2018

@mtaufen Done! filed #64859

@dims
Copy link
Member Author

dims commented Jun 6, 2018

@xcompass @mtaufen the code in 1.10 will work even without this change, was it failing for you?

@xcompass
Copy link

xcompass commented Jun 6, 2018

when I create the configmap with binary data field, the configmap created is empty. I thought it is related to the missing hash.

▶ ls -lha
total 200
drwxr-xr-x   3 compass  staff    96B Apr 30 15:33 .
drwxr-xr-x  11 compass  staff   352B May 29 00:35 ..
-rw-r--r--   1 compass  staff   100K Apr 30 15:28 saml.jks

▶ kubectl create configmap --from-file=saml.jks test-config
configmap "test-config" created

▶ kubectl get cm test-config
NAME          DATA      AGE
test-config   0         20s

▶ kubectl describe cm test-config
Name:         test-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
Events:  <none>
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.3", GitCommit:"2bba0127d85d5a46ab4b778548be28623b32d0b0", GitTreeState:"clean", BuildDate:"2018-05-21T09:17:39Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.3+coreos.0", GitCommit:"f1b890dbbf11abe58280b3ffe17d67749f5ae70e", GitTreeState:"clean", BuildDate:"2018-05-21T17:27:17Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Jun 7, 2018
@xcompass
Copy link

xcompass commented Jun 7, 2018

Just found out that the data is actually there, I have to use get cm -o yaml to see it. It is kind of misleading when nothing shows up using get cm or describe cm

k8s-github-robot pushed a commit that referenced this pull request Jul 27, 2018
…pstream-release-1.10

Automatic merge from submit-queue.

Automated cherry pick of #61146: Add missing binaryData field to the ConfigMap Hash

Cherry pick of #61146 on release-1.10.

#61146: Add missing binaryData field to the ConfigMap Hash

```release-note
The value of of BinaryData is now used in calculating the hash for the configmap
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants