Skip to content

Commit

Permalink
Merge branch 'dexidp:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyma2 authored Jun 10, 2022
2 parents c435c73 + b07c8b1 commit 6b947c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
35 changes: 30 additions & 5 deletions storage/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"io"
"net"
"net/http"
"net/url"
"os"
"path"
"strconv"
Expand Down Expand Up @@ -82,7 +83,9 @@ func offlineTokenName(userID string, connID string, h func() hash.Hash) string {
return strings.TrimRight(encoding.EncodeToString(hash.Sum(nil)), "=")
}

func (cli *client) urlFor(apiVersion, namespace, resource, name string) string {
func (cli *client) urlForWithParams(
apiVersion, namespace, resource, name string, params url.Values,
) string {
basePath := "apis/"
if apiVersion == "v1" {
basePath = "api/"
Expand All @@ -94,10 +97,22 @@ func (cli *client) urlFor(apiVersion, namespace, resource, name string) string {
} else {
p = path.Join(basePath, apiVersion, resource, name)
}

encodedParams := params.Encode()
paramsSuffix := ""
if len(encodedParams) > 0 {
paramsSuffix = "?" + encodedParams
}

if strings.HasSuffix(cli.baseURL, "/") {
return cli.baseURL + p
return cli.baseURL + p + paramsSuffix
}
return cli.baseURL + "/" + p

return cli.baseURL + "/" + p + paramsSuffix
}

func (cli *client) urlFor(apiVersion, namespace, resource, name string) string {
return cli.urlForWithParams(apiVersion, namespace, resource, name, url.Values{})
}

// Define an error interface so we can get at the underlying status code if it's
Expand Down Expand Up @@ -163,8 +178,7 @@ func (cli *client) get(resource, name string, v interface{}) error {
return cli.getResource(cli.apiVersion, cli.namespace, resource, name, v)
}

func (cli *client) getResource(apiVersion, namespace, resource, name string, v interface{}) error {
url := cli.urlFor(apiVersion, namespace, resource, name)
func (cli *client) getURL(url string, v interface{}) error {
resp, err := cli.client.Get(url)
if err != nil {
return err
Expand All @@ -176,6 +190,17 @@ func (cli *client) getResource(apiVersion, namespace, resource, name string, v i
return json.NewDecoder(resp.Body).Decode(v)
}

func (cli *client) getResource(apiVersion, namespace, resource, name string, v interface{}) error {
return cli.getURL(cli.urlFor(apiVersion, namespace, resource, name), v)
}

func (cli *client) listN(resource string, v interface{}, n int) error {
params := url.Values{}
params.Add("limit", fmt.Sprintf("%d", n))
u := cli.urlForWithParams(cli.apiVersion, cli.namespace, resource, "", params)
return cli.getURL(u, v)
}

func (cli *client) list(resource string, v interface{}) error {
return cli.get(resource, "", v)
}
Expand Down
12 changes: 8 additions & 4 deletions storage/kubernetes/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const (
resourceDeviceToken = "devicetokens"
)

const (
gcResultLimit = 500
)

// Config values for the Kubernetes storage type.
type Config struct {
InCluster bool `json:"inCluster"`
Expand Down Expand Up @@ -599,7 +603,7 @@ func (cli *client) UpdateConnector(id string, updater func(a storage.Connector)

func (cli *client) GarbageCollect(now time.Time) (result storage.GCResult, err error) {
var authRequests AuthRequestList
if err := cli.list(resourceAuthRequest, &authRequests); err != nil {
if err := cli.listN(resourceAuthRequest, &authRequests, gcResultLimit); err != nil {
return result, fmt.Errorf("failed to list auth requests: %v", err)
}

Expand All @@ -618,7 +622,7 @@ func (cli *client) GarbageCollect(now time.Time) (result storage.GCResult, err e
}

var authCodes AuthCodeList
if err := cli.list(resourceAuthCode, &authCodes); err != nil {
if err := cli.listN(resourceAuthCode, &authCodes, gcResultLimit); err != nil {
return result, fmt.Errorf("failed to list auth codes: %v", err)
}

Expand All @@ -633,7 +637,7 @@ func (cli *client) GarbageCollect(now time.Time) (result storage.GCResult, err e
}

var deviceRequests DeviceRequestList
if err := cli.list(resourceDeviceRequest, &deviceRequests); err != nil {
if err := cli.listN(resourceDeviceRequest, &deviceRequests, gcResultLimit); err != nil {
return result, fmt.Errorf("failed to list device requests: %v", err)
}

Expand All @@ -648,7 +652,7 @@ func (cli *client) GarbageCollect(now time.Time) (result storage.GCResult, err e
}

var deviceTokens DeviceTokenList
if err := cli.list(resourceDeviceToken, &deviceTokens); err != nil {
if err := cli.listN(resourceDeviceToken, &deviceTokens, gcResultLimit); err != nil {
return result, fmt.Errorf("failed to list device tokens: %v", err)
}

Expand Down

0 comments on commit 6b947c3

Please sign in to comment.