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: resourceregistry is incompatible in the upgrade scenario #4171

Merged
merged 1 commit into from
Oct 25, 2023

Conversation

zhzhuang-zju
Copy link
Contributor

@zhzhuang-zju zhzhuang-zju commented Oct 25, 2023

What type of PR is this?

/kind cleanup

What this PR does / why we need it:
Referring to #4144, there is a problem in the upgrade scenario of resource resourceregistry. We need to solve it, otherwise the relevant data will be lost after the upgrade.

Which issue(s) this PR fixes:
Refer to #4141

Special notes for your reviewer:
none
Does this PR introduce a user-facing change?:

`karmada-search`: Fixed the ResourceRegistry is incompatible in the upgrade scenario.

@karmada-bot karmada-bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Oct 25, 2023
@codecov-commenter
Copy link

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (fc006ce) 52.93% compared to head (23187f6) 52.93%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4171   +/-   ##
=======================================
  Coverage   52.93%   52.93%           
=======================================
  Files         239      239           
  Lines       23522    23522           
=======================================
  Hits        12452    12452           
  Misses      10395    10395           
  Partials      675      675           
Flag Coverage Δ
unittests 52.93% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
pkg/registry/search/storage/resourceregistry.go 0.00% <0.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zhzhuang-zju
Copy link
Contributor Author

zhzhuang-zju commented Oct 25, 2023

the root cause is:

opts, err := options.RESTOptions.GetRESTOptions(e.DefaultQualifiedResource)
if err != nil {
return err
}
// ResourcePrefix must come from the underlying factory
prefix := opts.ResourcePrefix
if !strings.HasPrefix(prefix, "/") {
prefix = "/" + prefix
}
if prefix == "/" {
return fmt.Errorf("store for %s has an invalid prefix %q", e.DefaultQualifiedResource.String(), opts.ResourcePrefix)
}

Field DefaultQualifiedResource is used to construct storage paths to resource directories. So, changing it will cause the storage path to change, and eventually cause upgrate problem. I did some experiments, and the results were consistent with the code analysis.

  1. use version release-1.7.0 to build the karmada environment.
  2. Create ResourceRegistry with command kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver create -f deployment-search.yaml
### deployment-search.yaml
apiVersion: search.karmada.io/v1alpha1
kind: ResourceRegistry
metadata:
  name: deployment-search
spec:
  targetCluster:
    clusterNames:
      - member1
      - member2
  resourceSelectors:
    - apiVersion: apps/v1
      kind: Deployment
  1. check the resource path in etcd.
sh-5.1#                                                                                                                                                                                sh-5.1# etcdctl --cert="/etc/karmada/pki/etcd-client.crt" --key="/etc/karmada/pki/etcd-client.key" --cacert="/etc/karmada/pki/etcd-ca.crt" get /registry --prefix --keys-only
/registry/search.karmada.io/resourceRegistries/deployment-search
  1. Upgrade the component karmada-search with the latest image.
  2. do the step 2 and 3 again.
sh-5.1#                                                                                                                                                                                sh-5.1# etcdctl --cert="/etc/karmada/pki/etcd-client.crt" --key="/etc/karmada/pki/etcd-client.key" --cacert="/etc/karmada/pki/etcd-ca.crt" get /registry --prefix --keys-only
/registry/search.karmada.io/resourceRegistries/deployment-search
/registry/search.karmada.io/resourceregistries/deployment-search

so, field DefaultQualifiedResource should not change.
Besides, run the command kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver api-resources| grep resourceregistries before and after the upgrade. The command output is as follows:

➜  karmada git:(1.7) ✗ ka api-resources|grep resourceregistries       
resourceregistries                                      search.karmada.io/v1alpha1             false        ResourceRegistry

The plural name of the resource has always been resourceregistries and is not affected by filed DefaultQualifiedResource, so I start looking for plural name of the resource.

plural := gv.WithResource(resource.Name)
singular := gv.WithResource(resource.SingularName)
// this is for legacy resources and servers which don't list singular forms. For those we must still guess.
if len(resource.SingularName) == 0 {
_, singular = meta.UnsafeGuessKindToResource(gv.WithKind(resource.Kind))
}

Tracing the code, I found the source of the value of pluralName. The key of the map is its pluralName. That's why the plural name of the resource has always been resourceregistries.
v1alpha1search := map[string]rest.Storage{}
v1alpha1search["resourceregistries"] = resourceRegistryStorage.ResourceRegistry

As you can see from the following code, pluralNames are forced to be lowercase only.
func (s *GenericAPIServer) getAPIGroupVersion(apiGroupInfo *APIGroupInfo, groupVersion schema.GroupVersion, apiPrefix string) (*genericapi.APIGroupVersion, error) {
storage := make(map[string]rest.Storage)
for k, v := range apiGroupInfo.VersionedResourcesStorageMap[groupVersion.Version] {
if strings.ToLower(k) != k {
return nil, fmt.Errorf("resource names must be lowercase only, not %q", k)
}
storage[k] = v
}

@zhzhuang-zju
Copy link
Contributor Author

Here is my test report.

  1. use version release-1.5.0 to build the karmada environment.
  2. Create ResourceRegistry with command kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver create -f deployment-search.yaml
### deployment-search.yaml
apiVersion: search.karmada.io/v1alpha1
kind: ResourceRegistry
metadata:
  name: deployment-search
spec:
  targetCluster:
    clusterNames:
      - member1
      - member2
  resourceSelectors:
    - apiVersion: apps/v1
      kind: Deployment

3.Executing kubectl get resourceregisty and kubectl get resourceregistries.

➜  karmada git:(1.5) ✗ kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver get ResourceRegistry  
NAME                CREATED AT
deployment-search   2023-10-25T07:04:14Z
➜  karmada git:(1.5) ✗ kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver get resourceregistries
NAME                CREATED AT
deployment-search   2023-10-25T07:04:14Z

4.Upgrade the component karmada-search with the latest image.
5.Executing kubectl get resourceregisty and kubectl get resourceregistries.

➜  karmada git:(1.7) ✗ kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver get ResourceRegistry  
NAME                CREATED AT
deployment-search   2023-10-25T07:04:14Z
➜  karmada git:(1.7) ✗ kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver get resourceregistries
NAME                CREATED AT
deployment-search   2023-10-25T07:04:14Z

@XiShanYongYe-Chang
Copy link
Member

Ask @RainbowMango to help rerun the CI.
/cc @RainbowMango

Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

/approve

@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 Oct 25, 2023
@RainbowMango
Copy link
Member

/lgtm

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Oct 25, 2023
Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

/lgtm

@RainbowMango
Copy link
Member

/hold

@karmada-bot karmada-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 25, 2023
@RainbowMango
Copy link
Member

/hold cancel

@karmada-bot karmada-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 25, 2023
@karmada-bot karmada-bot merged commit f57e362 into karmada-io:master Oct 25, 2023
12 checks passed
@RainbowMango RainbowMango added this to the v1.8 milestone Nov 29, 2023
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. lgtm Indicates that a PR is ready to be merged. 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.

5 participants