Repository: clusternet/clusternet
Pull request #820 (2024-08-23 03:14:10)

Fix shadow url replacement error when the resource type in the apis url contains a word starting with v
Fix shadow url replacement error when the resource type in the apis url contains a word starting with v
<!--  Thanks for sending a pull request!

-->

#### What type of PR is this?
/bug
#### What this PR does / why we need it:

Wrong replacement：
`https://my.k8s.io/apis/kubevirt.io/v1/namespaces/abc/virtualmachines/abc` => `https://my.k8s.io/apis/shadow/v1alpha1/abc`

The correct should be `https://my.k8s.io/apis/shadow/v1alpha1/namespaces/abc/virtualmachines/abc`

#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes #

#### Special notes for your reviewer:


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/clusternet/clusternet/pull/820?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 13.87%. Comparing base [(`e2ffa5f`)](https://app.codecov.io/gh/clusternet/clusternet/commit/e2ffa5fa54a7e81045fbef497d0dfd68a4bcc7f6?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) to head [(`a0b8790`)](https://app.codecov.io/gh/clusternet/clusternet/commit/a0b879013675308ab8eee7d0c85230290b8581ca?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet).
> Report is 156 commits behind head on main.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #820      +/-   ##
==========================================
+ Coverage   13.60%   13.87%   +0.27%     
==========================================
  Files          66       77      +11     
  Lines        7492     9087    +1595     
==========================================
+ Hits         1019     1261     +242     
- Misses       6391     7729    +1338     
- Partials       82       97      +15     
```

| [Flag](https://app.codecov.io/gh/clusternet/clusternet/pull/820/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/clusternet/clusternet/pull/820/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) | `13.87% <ø> (?)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet#carryforward-flags-in-the-pull-request-comment) to find out more.

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/clusternet/clusternet/pull/820?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet).

silenceper: why FOSSA ci failed? Is there anything else I need to do?  
dixudx: > why FOSSA ci failed? Is there anything else I need to do?

Currently FOSSA CI pipeline is not stable. It is not related to this PR.

Patch:
diff --git a/pkg/wrappers/clientgo/wrapper.go b/pkg/wrappers/clientgo/wrapper.go
index 5c7becc..a9c75e4 100644
--- a/pkg/wrappers/clientgo/wrapper.go
+++ b/pkg/wrappers/clientgo/wrapper.go
@@ -31,7 +31,7 @@ var (
 	// regex matches '/api/v1/{path}'
 	apiv1Regex = regexp.MustCompile(`^(/api/v1)/(.*)`)
 	// regex matches "/apis/{group}/{version}/{path}"
-	apisRegex = regexp.MustCompile(`^(/apis/.*/v\w*.)/(.*)`)
+	apisRegex = regexp.MustCompile(`^(/apis/[^/]+/v\w+)(/.*)`)
 	// regex matches "/apis/xxx.clusternet.io/{version}/{path}"
 	clusternetAPIsRegex = regexp.MustCompile(`^(/apis/\w*\.clusternet\.io/v\w*.)/(.*)`)
 


Test patch:
diff --git a/pkg/wrappers/clientgo/wrapper_test.go b/pkg/wrappers/clientgo/wrapper_test.go
index f3ec8fa..bdbf91d 100644
--- a/pkg/wrappers/clientgo/wrapper_test.go
+++ b/pkg/wrappers/clientgo/wrapper_test.go
@@ -89,6 +89,12 @@ func TestNormalizeLocation(t *testing.T) {
 			requestURL:    "https://my.k8s.io/apis/far/v1bar/namespaces/abc/foos/abc",
 			normalizedURL: "https://my.k8s.io/apis/shadow/v1alpha1/namespaces/abc/foos/abc",
 		},
+		{
+			name:          "apis - get namespace-scoped virtualmachines abc",
+			host:          "https://my.k8s.io",
+			requestURL:    "https://my.k8s.io/apis/far/v1bar/namespaces/abc/virtualmachines/abc",
+			normalizedURL: "https://my.k8s.io/apis/shadow/v1alpha1/namespaces/abc/virtualmachines/abc",
+		},
 		{
 			name:          "apis - watch namespace-scoped foos",
 			host:          "https://my.k8s.io",