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: fix GenerateBindingReferenceKey bug #1003

Merged
merged 1 commit into from
Nov 24, 2021

Conversation

JarHMJ
Copy link
Contributor

@JarHMJ JarHMJ commented Nov 22, 2021

Signed-off-by: huangminjie minjie.huang@daocloud.io

What type of PR is this?
/kind bug

What this PR does / why we need it:

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

karmada-controller-manager: fixed the issue of generating binding reference key.

@karmada-bot karmada-bot added the kind/bug Categorizes issue or PR as related to a bug. label Nov 22, 2021
@karmada-bot karmada-bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Nov 22, 2021
@RainbowMango
Copy link
Member

cc @XiShanYongYe-Chang
/priority important-soon

We should check if this bugfix would introduce in-compatible effects. That is more important than the bug.

@karmada-bot karmada-bot added the priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. label Nov 22, 2021
@XiShanYongYe-Chang
Copy link
Member

Ok, I will check it.

@XiShanYongYe-Chang
Copy link
Member

I'm sorry, It's my fault.

To prevent version incompatibility when users upgrade Karmada from v0.10.0 to a later version (for example, v.0.11.0), the current fix maybe not be enough. We need to introduce a new group of labels:

ResourceBindingReferenceHashKey = "resourcebinding.karmada.io/hash-key"
ClusterResourceBindingReferenceHashKey = "clusterresourcebinding.karmada.io/hash-key"

to replace the current group of labels:

ResourceBindingReferenceKey = "resourcebinding.karmada.io/key"
ClusterResourceBindingReferenceKey = "clusterresourcebinding.karmada.io/key"

The current group of labels can be removed in the next version.

The two groups of labels both need to be processed, this may be complex. For example, a user creates the nginx deployment in the test namespace with v0.10.0 version. In v0.11.0 version, user wants to delete it, so we need to process both ResourceBindingReferenceKey and ResourceBindingReferenceHashKey labels.

@XiShanYongYe-Chang
Copy link
Member

@JarHMJ , Would you like to fix it in this way?

@JarHMJ
Copy link
Contributor Author

JarHMJ commented Nov 22, 2021

@XiShanYongYe-Chang Ok,I see.

You means that the label ResourceBindingReferenceKey is not precise,so you want use ResourceBindingReferenceHashKey to replace the old label. Right?

I would like to do this feature.

I think this pr should be merged as hotfix, because this leads to some error when deployment has same name in diffrenet namespce.

@XiShanYongYe-Chang
Copy link
Member

You means that the label ResourceBindingReferenceKey is not precise

I don't mean that the label isn't precise enough. I mean we need another group of labels to indicate correct key.

I think this pr should be merged as hotfix, because this leads to some error when deployment has same name in diffrenet namespce.

It's really a bug. I want the old one to stay the same, use a new method to generate the correct key, and then use the new label to replace the old one. For example:

diff --git a/pkg/controllers/binding/common.go b/pkg/controllers/binding/common.go
index a7131bcd..52251ae5 100644
--- a/pkg/controllers/binding/common.go
+++ b/pkg/controllers/binding/common.go
@@ -193,8 +193,8 @@ func mergeLabel(workload *unstructured.Unstructured, workNamespace string, bindi
        util.MergeLabel(workload, workv1alpha2.WorkNamespaceLabel, workNamespace)
        util.MergeLabel(workload, workv1alpha2.WorkNameLabel, names.GenerateWorkName(workload.GetKind(), workload.GetName(), workload.GetNamespace()))
        if scope == apiextensionsv1.NamespaceScoped {
-               util.MergeLabel(workload, workv1alpha2.ResourceBindingReferenceKey, names.GenerateBindingReferenceKey(binding.GetNamespace(), binding.GetName()))
-               workLabel[workv1alpha2.ResourceBindingReferenceKey] = names.GenerateBindingReferenceKey(binding.GetNamespace(), binding.GetName())
+               util.MergeLabel(workload, workv1alpha2.ResourceBindingReferenceHashKey, names.GenerateBindingReferenceHashKey(binding.GetNamespace(), binding.GetName()))
+               workLabel[workv1alpha2.ResourceBindingReferenceHashKey] = names.GenerateBindingReferenceHashKey(binding.GetNamespace(), binding.GetName())
        } else {
                util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingReferenceKey, names.GenerateBindingReferenceKey("", binding.GetName()))
                workLabel[workv1alpha2.ClusterResourceBindingReferenceKey] = names.GenerateBindingReferenceKey("", binding.GetName())
diff --git a/pkg/util/names/names.go b/pkg/util/names/names.go
index 01d5fe13..2511e69b 100644
--- a/pkg/util/names/names.go
+++ b/pkg/util/names/names.go
@@ -71,6 +71,19 @@ func GenerateBindingReferenceKey(namespace, name string) string {
        return rand.SafeEncodeString(fmt.Sprint(hash.Sum32()))
 }

+// GenerateBindingReferenceHashKey will generate the hash key of binding object with the hash of its namespace and name.
+func GenerateBindingReferenceHashKey(namespace, name string) string {
+       var bindingName string
+       if len(namespace) != 0 {
+               bindingName = namespace + "-" + name
+       } else {
+               bindingName = name
+       }
+       hash := fnv.New32a()
+       hashutil.DeepHashObject(hash, bindingName)
+       return rand.SafeEncodeString(fmt.Sprint(hash.Sum32()))
+}

In the next version, we can delete the old logic, so the upgrade can be not affected.

@JarHMJ
Copy link
Contributor Author

JarHMJ commented Nov 22, 2021

@XiShanYongYe-Chang 👌I will try to fix in your way

@XiShanYongYe-Chang
Copy link
Member

@JarHMJ Thanks very much.

@karmada-bot karmada-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 23, 2021
@JarHMJ
Copy link
Contributor Author

JarHMJ commented Nov 23, 2021

@XiShanYongYe-Chang done.

@JarHMJ
Copy link
Contributor Author

JarHMJ commented Nov 23, 2021

@XiShanYongYe-Chang Should I clean up the old code?

	// TODO: Delete this logic in the next release to prevent incompatibility when upgrading the current release (v0.10.0).
	workList, err := GetWorksByLabelSelector(c, labels.SelectorFromSet(
		labels.Set{
			workv1alpha2.ClusterResourceBindingLabel: binding.Name,
		},
	))
	if err != nil {
		klog.Errorf("Failed to get works by ClusterResourceBinding(%s): %v", binding.Name, err)
		return err
	}
	targetWorks = append(targetWorks, workList.Items...)

But #1006 has done it。

@XiShanYongYe-Chang
Copy link
Member

@XiShanYongYe-Chang done.

Thanks, I will review it ASAP.

Should I clean up the old code?

This pr can don't care about it.

@RainbowMango
Copy link
Member

I'm thinking about cutting a new bugfix release(v0.10.1) to solve this issue.
Let's hold on for a while.

@Garrybest Garrybest mentioned this pull request Nov 23, 2021
@pigletfly
Copy link
Contributor

I think we could provide a way for users to fix current rb ReferenceKey, so it can avoid deal the backward compatibility in the controller. steps as follow:

  1. stop karmada-controller-manager
  2. run the fix script to update all rb ReferenceKey into correct value
  3. update karmada-controller-manager to the correct version, which has fixed GenerateBindingReferenceKey func

@RainbowMango
Copy link
Member

Hi @pigletfly
I suggest fixing this issue first and then cutting a new release(v0.10.1).
People who are using v0.9.0 can upgrade to v1.10.1 (skip v0.10.0).

How do you think?

@RainbowMango
Copy link
Member

I'd like to take the patch from this PR and take the unit test from #1013.

@RainbowMango
Copy link
Member

@JarHMJ Can you help revert the patch to the first version? something like:

diff --git a/pkg/util/names/names.go b/pkg/util/names/names.go
index 01d5fe13..98a0fabc 100644
--- a/pkg/util/names/names.go
+++ b/pkg/util/names/names.go
@@ -61,7 +61,7 @@ func GenerateBindingName(kind, name string) string {
 // GenerateBindingReferenceKey will generate the key of binding object with the hash of its namespace and name.
 func GenerateBindingReferenceKey(namespace, name string) string {
        var bindingName string
-       if len(namespace) == 0 {
+       if len(namespace) > 0 {
                bindingName = namespace + "-" + name
        } else {
                bindingName = name

Signed-off-by: huangminjie <minjie.huang@daocloud.io>
@karmada-bot karmada-bot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 23, 2021
@JarHMJ
Copy link
Contributor Author

JarHMJ commented Nov 23, 2021

@RainbowMango done

@RainbowMango
Copy link
Member

Thanks for your quick response.
Let's see if @pigletfly has other comments.

@pigletfly
Copy link
Contributor

Hi @pigletfly I suggest fixing this issue first and then cutting a new release(v0.10.1). People who are using v0.9.0 can upgrade to v1.10.1 (skip v0.10.0).

How do you think?

agreed

@RainbowMango
Copy link
Member

OK. Let's do it.
/lgtm
/approve

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Nov 24, 2021
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: RainbowMango

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

@karmada-bot karmada-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 24, 2021
@karmada-bot karmada-bot merged commit ff92876 into karmada-io:master Nov 24, 2021
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. kind/bug Categorizes issue or PR as related to a bug. lgtm 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. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants