-
Notifications
You must be signed in to change notification settings - Fork 38.9k
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
Add missing binaryData field to the ConfigMap Hash #61146
Conversation
/kind bug |
/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 |
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.
does the stdlib marshal []byte in unstructured maps correctly?
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.
yes, see TestEncodeConfigMap
method "three keys"
variation. uses base64 encode
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.
https://golang.org/pkg/encoding/json/#Marshal
Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string
pkg/kubectl/util/hash/hash_test.go
Outdated
// 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", ""}, |
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.
personal nit: keep field order the same as the struct definition; swap BinaryData
and Data
so Data
comes first
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.
fixing
pkg/kubectl/util/hash/hash_test.go
Outdated
// 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":""}`, ""}, |
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.
here too
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.
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 |
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.
https://golang.org/pkg/encoding/json/#Marshal
Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string
/status approved-for-milestone |
[MILESTONENOTIFIER] Milestone Pull Request: Up-to-date for process Pull Request Labels
|
In 7e158fb, we added a BinaryData to ConfigMap, but totally forgot to add it to the hash method.
9c6c7d8
to
4cbdbae
Compare
// 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", ""}, |
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 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?
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 think so
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.
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?
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.
@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?
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.
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?
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.
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.
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 wouldn't change existing treatment of data, or mess with nilling out map values. The length check on binaryData seems fine to me.
/test pull-kubernetes-e2e-gce |
sgtm wrt milestone |
/cc @cheftako |
@mtaufen now that master is open, let's please get this in. |
I want to settle the nil/empty thing first, but otherwise this lgtm. |
/lgtm |
/lgtm |
[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 |
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. |
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. |
@dims was this cherry-picked? |
when I create the configmap with binary data field, the configmap created is empty. I thought it is related to the missing hash.
|
Just found out that the data is actually there, I have to use |
…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 ```
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: