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

🐛 allow arbitrary config fields when calculating config hash #243

Merged

Conversation

haoqing0110
Copy link
Member

@openshift-ci openshift-ci bot requested review from elgnay and qiujian16 March 6, 2024 10:50
@haoqing0110 haoqing0110 force-pushed the br_empty-spec branch 2 times, most recently from 13125cf to 1c0fb7a Compare March 7, 2024 03:53
@haoqing0110
Copy link
Member Author

/assign @qiujian16

func GetSpecHash(obj *unstructured.Unstructured) (string, error) {
if obj == nil {
return "", fmt.Errorf("object is nil")
}
spec, ok := obj.Object["spec"]
if !ok {
return "", fmt.Errorf("object has no spec field")
// handle cases like secret and configmaps
spec = obj.Object["data"]
Copy link
Member

Choose a reason for hiding this comment

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

what if an API that does not have data field and spec field?

Copy link
Member Author

Choose a reason for hiding this comment

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

var spec will be nil and return a hash like case .For example, if a configmap removed data field, hash will update and trigger an upgrade.

Copy link
Member

Choose a reason for hiding this comment

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

how about we remove status and metadata part and hash?

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, sounds better. Have modified the code.

Signed-off-by: haoqing0110 <qhao@redhat.com>
@haoqing0110 haoqing0110 changed the title allow data field and empty field when calculating config hash allow arbitrary config fields when calculating config hash Mar 8, 2024
@haoqing0110 haoqing0110 changed the title allow arbitrary config fields when calculating config hash 🐛 allow arbitrary config fields when calculating config hash Mar 8, 2024
if err != nil {
return "", err
}

hash := sha256.Sum256(specBytes)
hash := sha256.Sum256(configBytes)
Copy link
Contributor

@tamalsaha tamalsaha Mar 10, 2024

Choose a reason for hiding this comment

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

May be a fast non-cryptographic is good enough here?
https://github.com/zeebo/xxh3

Copy link
Member Author

Choose a reason for hiding this comment

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

This seems to have better performance, but might not be suitable for the addon case, for example, a cluster-scoped config hash will be exposed to cma, mca and work, and might be cracked by an unexpected namespace-level user.


specBytes, err := json.Marshal(spec)
// remove non config related fields, the remaining filed should be spec, data, other use defined fields or even empty
delete(obj.Object, "apiVersion")
Copy link
Contributor

@tamalsaha tamalsaha Mar 10, 2024

Choose a reason for hiding this comment

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

This will modify the input object. Is that going to cause any unintended issue? May be a better choice is to perform a shallow copy into a new map and use that calculate hash?

cp := map[string]any{}
for k, v := range obj.Object {
  switch k: {
    case "apiVersion", "kind", "metadata", "status":
    default:
       cp[k] = v
   }
} 

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for raising this, have modified the code.

Signed-off-by: haoqing0110 <qhao@redhat.com>
@qiujian16
Copy link
Member

/approve
/lgtm

@openshift-ci openshift-ci bot added the lgtm label Mar 11, 2024
Copy link
Contributor

openshift-ci bot commented Mar 11, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: haoqing0110, qiujian16

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

@openshift-merge-bot openshift-merge-bot bot merged commit 974d337 into open-cluster-management-io:main Mar 11, 2024
12 checks passed
@haoqing0110 haoqing0110 deleted the br_empty-spec branch March 12, 2024 01:40
qiujian16 pushed a commit to qiujian16/addon-framework that referenced this pull request Apr 3, 2024
…uster-management-io#243)

* allow data field and empty field when calculating config hash

Signed-off-by: haoqing0110 <qhao@redhat.com>

* hash config fields

Signed-off-by: haoqing0110 <qhao@redhat.com>

---------

Signed-off-by: haoqing0110 <qhao@redhat.com>
qiujian16 pushed a commit to qiujian16/addon-framework that referenced this pull request Apr 3, 2024
…uster-management-io#243)

* allow data field and empty field when calculating config hash

Signed-off-by: haoqing0110 <qhao@redhat.com>

* hash config fields

Signed-off-by: haoqing0110 <qhao@redhat.com>

---------

Signed-off-by: haoqing0110 <qhao@redhat.com>
Signed-off-by: Jian Qiu <jqiu@redhat.com>
qiujian16 pushed a commit to qiujian16/addon-framework that referenced this pull request Apr 3, 2024
…uster-management-io#243)

* allow data field and empty field when calculating config hash

Signed-off-by: haoqing0110 <qhao@redhat.com>

* hash config fields

Signed-off-by: haoqing0110 <qhao@redhat.com>

---------

Signed-off-by: haoqing0110 <qhao@redhat.com>
Signed-off-by: Jian Qiu <jqiu@redhat.com>
qiujian16 pushed a commit to qiujian16/addon-framework that referenced this pull request Apr 7, 2024
…uster-management-io#243)

* allow data field and empty field when calculating config hash

Signed-off-by: haoqing0110 <qhao@redhat.com>

* hash config fields

Signed-off-by: haoqing0110 <qhao@redhat.com>

---------

Signed-off-by: haoqing0110 <qhao@redhat.com>
Signed-off-by: Jian Qiu <jqiu@redhat.com>
qiujian16 pushed a commit to qiujian16/addon-framework that referenced this pull request Apr 7, 2024
…uster-management-io#243)

* allow data field and empty field when calculating config hash

Signed-off-by: haoqing0110 <qhao@redhat.com>

* hash config fields

Signed-off-by: haoqing0110 <qhao@redhat.com>

---------

Signed-off-by: haoqing0110 <qhao@redhat.com>
Signed-off-by: Jian Qiu <jqiu@redhat.com>
qiujian16 pushed a commit to qiujian16/addon-framework that referenced this pull request Apr 7, 2024
…uster-management-io#243)

* allow data field and empty field when calculating config hash

Signed-off-by: haoqing0110 <qhao@redhat.com>

* hash config fields

Signed-off-by: haoqing0110 <qhao@redhat.com>

---------

Signed-off-by: haoqing0110 <qhao@redhat.com>
Signed-off-by: Jian Qiu <jqiu@redhat.com>
openshift-merge-bot bot pushed a commit that referenced this pull request Apr 7, 2024
…259)

* allow data field and empty field when calculating config hash



* hash config fields



---------

Signed-off-by: haoqing0110 <qhao@redhat.com>
Signed-off-by: Jian Qiu <jqiu@redhat.com>
Co-authored-by: Qing Hao <qhao@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

spechash remains empty for secrets
3 participants