Skip to content

Commit

Permalink
client-go/rest: check if url is nil to prevent nil pointer dereference
Browse files Browse the repository at this point in the history
Signed-off-by: André Martins <[email protected]>

Kubernetes-commit: fa1d4c327716ae117120311f566d26abff9d91ad
  • Loading branch information
aanm authored and k8s-publishing-bot committed Aug 9, 2022
1 parent 1a46dfd commit c9008f3
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions rest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,17 @@ func (r Request) finalURLTemplate() url.URL {
newParams[k] = v
}
r.params = newParams
url := r.URL()
u := r.URL()
if u == nil {
return url.URL{}
}

segments := strings.Split(url.Path, "/")
segments := strings.Split(u.Path, "/")
groupIndex := 0
index := 0
trimmedBasePath := ""
if url != nil && r.c.base != nil && strings.Contains(url.Path, r.c.base.Path) {
p := strings.TrimPrefix(url.Path, r.c.base.Path)
if r.c.base != nil && strings.Contains(u.Path, r.c.base.Path) {
p := strings.TrimPrefix(u.Path, r.c.base.Path)
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
Expand All @@ -537,7 +540,7 @@ func (r Request) finalURLTemplate() url.URL {
groupIndex = 1
}
if len(segments) <= 2 {
return *url
return *u
}

const CoreGroupPrefix = "api"
Expand All @@ -555,11 +558,11 @@ func (r Request) finalURLTemplate() url.URL {
// outlet here in case more API groups are added in future if ever possible:
// https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-groups
// if a wrong API groups name is encountered, return the {prefix} for url.Path
url.Path = "/{prefix}"
url.RawQuery = ""
return *url
u.Path = "/{prefix}"
u.RawQuery = ""
return *u
}
//switch segLength := len(segments) - index; segLength {
// switch segLength := len(segments) - index; segLength {
switch {
// case len(segments) - index == 1:
// resource (with no name) do nothing
Expand All @@ -582,8 +585,8 @@ func (r Request) finalURLTemplate() url.URL {
segments[index+3] = "{name}"
}
}
url.Path = path.Join(trimmedBasePath, path.Join(segments...))
return *url
u.Path = path.Join(trimmedBasePath, path.Join(segments...))
return *u
}

func (r *Request) tryThrottleWithInfo(ctx context.Context, retryInfo string) error {
Expand Down

0 comments on commit c9008f3

Please sign in to comment.