-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
add docker hub ratelimit metric #208
Merged
+179
−4
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
35ce3e3
add docker hub ratelimit metrics
gamoutatsumi bdfc827
fix lint suggestions
gamoutatsumi 2f81c3c
use repository_owner in workflow
gamoutatsumi bd8b5d2
add option for docker hub token
gamoutatsumi 53bfd72
use BASIC Auth for get Docker Hub Token
gamoutatsumi 484ae3d
add error handling for Docker Hub paid user
gamoutatsumi 239ff71
set docker hub metrics to opt-out
gamoutatsumi 7bde314
add handling for docker hub metrics mode
gamoutatsumi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package docker | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v4" | ||
"github.com/whywaita/myshoes/pkg/config" | ||
) | ||
|
||
// RateLimit is Docker Hub API rate limit | ||
type RateLimit struct { | ||
Limit int | ||
Remaining int | ||
} | ||
|
||
type tokenCache struct { | ||
expire time.Time | ||
token string | ||
} | ||
|
||
var cacheMap = make(map[int]tokenCache, 1) | ||
|
||
func getToken() (string, error) { | ||
url := "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull" | ||
req, err := http.NewRequest("GET", url, nil) | ||
if config.Config.DockerHubCredential.Password != "" && config.Config.DockerHubCredential.Username != "" { | ||
req.SetBasicAuth(config.Config.DockerHubCredential.Username, config.Config.DockerHubCredential.Password) | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return "", fmt.Errorf("request token: %w", err) | ||
} | ||
if cache, ok := cacheMap[0]; ok && cache.expire.After(time.Now()) { | ||
return cache.token, nil | ||
} | ||
defer resp.Body.Close() | ||
byteArray, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", fmt.Errorf("read body: %w", err) | ||
} | ||
jsonMap := make(map[string]interface{}) | ||
if err := json.Unmarshal(byteArray, &jsonMap); err != nil { | ||
return "", fmt.Errorf("unmarshal json: %w", err) | ||
} | ||
tokenString, ok := jsonMap["token"].(string) | ||
if !ok { | ||
return "", fmt.Errorf("tokenString is not string") | ||
} | ||
token, _, err := new(jwt.Parser).ParseUnverified(tokenString, jwt.MapClaims{}) | ||
if err != nil { | ||
return "", fmt.Errorf("parse token: %w", err) | ||
} | ||
exp, ok := token.Claims.(jwt.MapClaims)["exp"].(float64) | ||
if !ok { | ||
return "", fmt.Errorf("exp is not float64") | ||
} | ||
cacheMap[0] = tokenCache{ | ||
expire: time.Unix(int64(exp), 0), | ||
token: tokenString, | ||
} | ||
return tokenString, nil | ||
} | ||
|
||
// GetRateLimit get Docker Hub API rate limit | ||
func GetRateLimit() (RateLimit, error) { | ||
token, err := getToken() | ||
if err != nil { | ||
return RateLimit{}, fmt.Errorf("get token: %w", err) | ||
} | ||
url := "https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest" | ||
req, err := http.NewRequest("HEAD", url, nil) | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return RateLimit{}, fmt.Errorf("get rate limit: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
limitHeader := resp.Header.Get("ratelimit-limit") | ||
if limitHeader == "" { | ||
return RateLimit{}, fmt.Errorf("not found ratelimit-limit header") | ||
} | ||
limit, err := strconv.Atoi(strings.Split(limitHeader, ";")[0]) | ||
if err != nil { | ||
return RateLimit{}, fmt.Errorf("parse limit: %w", err) | ||
} | ||
remainingHeader := resp.Header.Get("ratelimit-remaining") | ||
if remainingHeader == "" { | ||
return RateLimit{}, fmt.Errorf("not found ratelimit-remaining header") | ||
|
||
} | ||
remaining, err := strconv.Atoi(strings.Split(remainingHeader, ";")[0]) | ||
if err != nil { | ||
return RateLimit{}, fmt.Errorf("parse remaining: %w", err) | ||
} | ||
|
||
return RateLimit{ | ||
Limit: limit, | ||
Remaining: remaining, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please logging and handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, reflected your reviews.