-
Notifications
You must be signed in to change notification settings - Fork 4
feat(build): add kodo artifact store #134
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
Merged
luoliwoshang
merged 2 commits into
xgo-dev:main
from
MeteorsLiu:feat/kodo-cache-metadata-artifact
Jul 3, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,54 @@ | ||
| name: Kodo E2E | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - "**" | ||
| - "!dependabot/**" | ||
| - "!xgopilot/**" | ||
| pull_request_target: | ||
| branches: [ "**" ] | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| kodo-artifact: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Check out code | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }} | ||
| ref: ${{ github.event.pull_request.head.sha || github.sha }} | ||
| persist-credentials: false | ||
|
|
||
| - name: Set up Go | ||
| uses: actions/setup-go@v5 | ||
| with: | ||
| go-version: 1.24.x | ||
|
|
||
| - name: Download Go modules | ||
| run: go mod download | ||
|
|
||
| - name: Check Kodo secrets | ||
| env: | ||
| QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }} | ||
| QINIU_SECRET_KEY: ${{ secrets.QINIU_SECRET_KEY }} | ||
| QINIU_BUCKET: ${{ secrets.QINIU_BUCKET }} | ||
| run: | | ||
| test -n "$QINIU_ACCESS_KEY" | ||
| test -n "$QINIU_SECRET_KEY" | ||
| test -n "$QINIU_BUCKET" | ||
|
|
||
| - name: Run Kodo artifact E2E | ||
| env: | ||
| QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }} | ||
| QINIU_SECRET_KEY: ${{ secrets.QINIU_SECRET_KEY }} | ||
| QINIU_BUCKET: ${{ secrets.QINIU_BUCKET }} | ||
| QINIU_PREFIX: ${{ secrets.QINIU_PREFIX }} | ||
| run: go test -v -ldflags="-checklinkname=0" ./internal/artifact -run '^TestKodoArtifactE2E$' -count=1 |
This file contains hidden or 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 hidden or 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
File renamed without changes.
File renamed without changes.
This file contains hidden or 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 hidden or 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,128 @@ | ||
| package artifact | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| qiniuclient "github.com/qiniu/go-sdk/v7/client" | ||
| "github.com/qiniu/go-sdk/v7/storagev2/credentials" | ||
| httpclient "github.com/qiniu/go-sdk/v7/storagev2/http_client" | ||
| "github.com/qiniu/go-sdk/v7/storagev2/objects" | ||
| ) | ||
|
|
||
| const ( | ||
| kodoArtifactContentType = "application/gzip" | ||
| kodoArtifactMetadataKey = "llar-artifact" | ||
| ) | ||
|
|
||
| type KodoArtifactConfig struct { | ||
| AccessKey string | ||
| SecretKey string | ||
| Bucket string | ||
| Prefix string | ||
| } | ||
|
|
||
| type kodoArtifact struct { | ||
| bucket string | ||
| prefix string | ||
| objects *objects.ObjectsManager | ||
| } | ||
|
|
||
| func NewKodoArtifact(cfg KodoArtifactConfig) Store { | ||
| cred := credentials.NewCredentials(cfg.AccessKey, cfg.SecretKey) | ||
| return &kodoArtifact{ | ||
| bucket: cfg.Bucket, | ||
| prefix: strings.Trim(cfg.Prefix, "/"), | ||
| objects: objects.NewObjectsManager(&objects.ObjectsManagerOptions{ | ||
| Options: httpclient.Options{Credentials: cred}, | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| func (s *kodoArtifact) Get(ctx context.Context, key Key) (Artifact, bool, error) { | ||
| objectName := s.objectName(key) | ||
| object, err := s.objects.Bucket(s.bucket).Object(objectName).Stat().Call(ctx) | ||
| if err != nil { | ||
| if kodoArtifactObjectNotFound(err) { | ||
| return Artifact{}, false, nil | ||
| } | ||
| return Artifact{}, false, err | ||
| } | ||
| got, ok := kodoArtifactFromMetadata(object.Metadata) | ||
| if !ok { | ||
| return Artifact{}, false, fmt.Errorf("read kodo artifact metadata for %s", objectName) | ||
| } | ||
| return got, true, nil | ||
| } | ||
|
|
||
| func (s *kodoArtifact) Put(ctx context.Context, key Key, art Artifact) (Artifact, error) { | ||
| raw, err := encodeKodoArtifact(art) | ||
| if err != nil { | ||
| return art, err | ||
| } | ||
| err = s.objects.Bucket(s.bucket).Object(s.objectName(key)).SetMetadata(kodoArtifactContentType). | ||
| Metadata(map[string]string{kodoArtifactMetadataKey: raw}). | ||
| Call(ctx) | ||
| if err != nil { | ||
| return art, err | ||
| } | ||
| return art, nil | ||
| } | ||
|
|
||
| func (s *kodoArtifact) Delete(ctx context.Context, key Key) error { | ||
| err := s.objects.Bucket(s.bucket).Object(s.objectName(key)).Delete().Call(ctx) | ||
| if err != nil && !kodoArtifactObjectNotFound(err) { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *kodoArtifact) objectName(key Key) string { | ||
| parts := make([]string, 0, 4) | ||
| if s.prefix != "" { | ||
| parts = append(parts, s.prefix) | ||
| } | ||
| parts = append(parts, strings.Trim(key.Module, "/"), strings.Trim(key.Version, "/"), key.MatrixStr+".tar.gz") | ||
| return strings.Join(parts, "/") | ||
| } | ||
|
|
||
| func encodeKodoArtifact(art Artifact) (string, error) { | ||
| data, err := json.Marshal(art) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return base64.RawURLEncoding.EncodeToString(data), nil | ||
| } | ||
|
|
||
| func kodoArtifactFromMetadata(metadata map[string]string) (Artifact, bool) { | ||
| raw := kodoArtifactMetadataValue(metadata, kodoArtifactMetadataKey) | ||
| if raw == "" { | ||
| return Artifact{}, false | ||
| } | ||
| data, err := base64.RawURLEncoding.DecodeString(raw) | ||
| if err != nil { | ||
| return Artifact{}, false | ||
| } | ||
| var art Artifact | ||
| if err := json.Unmarshal(data, &art); err != nil { | ||
| return Artifact{}, false | ||
| } | ||
| return art, true | ||
| } | ||
|
|
||
| func kodoArtifactMetadataValue(metadata map[string]string, key string) string { | ||
| value := metadata[key] | ||
| if value == "" { | ||
| value = metadata["x-qn-meta-"+key] | ||
| } | ||
|
Comment on lines
+119
to
+121
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mark 这里link一下文档
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return value | ||
| } | ||
|
|
||
| func kodoArtifactObjectNotFound(err error) bool { | ||
| var info *qiniuclient.ErrorInfo | ||
| return errors.As(err, &info) && info.Code == 612 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
用不到后面就删除吧?经过讨论现在放Kodo了