wip: oci image management
This commit is contained in:
57
controller/manifest.go
Normal file
57
controller/manifest.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
|
||||
"gitea.loveuer.com/loveuer/cluster/internal/registry/storage"
|
||||
)
|
||||
|
||||
// GetManifest 获取 manifest
|
||||
func GetManifest(store storage.Storage, repo, reference string) ([]byte, string, string, error) {
|
||||
data, mediaType, err := store.GetManifest(repo, reference)
|
||||
if err != nil {
|
||||
return nil, "", "", err
|
||||
}
|
||||
|
||||
digest := calculateDigest(data)
|
||||
return data, mediaType, digest, nil
|
||||
}
|
||||
|
||||
// PutManifest 推送 manifest
|
||||
func PutManifest(store storage.Storage, repo, reference string, data []byte, mediaType string) (string, error) {
|
||||
if err := store.PutManifest(repo, reference, data, mediaType); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
digest := calculateDigest(data)
|
||||
return digest, nil
|
||||
}
|
||||
|
||||
// DeleteManifest 删除 manifest
|
||||
func DeleteManifest(store storage.Storage, repo, reference string) error {
|
||||
return store.DeleteManifest(repo, reference)
|
||||
}
|
||||
|
||||
// HeadManifest 检查 manifest 是否存在并返回元数据
|
||||
func HeadManifest(store storage.Storage, repo, reference string) (bool, []byte, string, string, error) {
|
||||
exists, err := store.ManifestExists(repo, reference)
|
||||
if err != nil || !exists {
|
||||
return false, nil, "", "", err
|
||||
}
|
||||
|
||||
// 获取 manifest 以设置正确的 headers
|
||||
data, mediaType, err := store.GetManifest(repo, reference)
|
||||
if err != nil {
|
||||
return false, nil, "", "", err
|
||||
}
|
||||
|
||||
digest := calculateDigest(data)
|
||||
return true, data, mediaType, digest, nil
|
||||
}
|
||||
|
||||
// calculateDigest 计算 SHA256 digest
|
||||
func calculateDigest(data []byte) string {
|
||||
hash := sha256.Sum256(data)
|
||||
return "sha256:" + hex.EncodeToString(hash[:])
|
||||
}
|
||||
Reference in New Issue
Block a user