wip: oci image management

This commit is contained in:
loveuer
2025-11-09 15:19:11 +08:00
commit 8de8234372
58 changed files with 6142 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
package handlers
import (
"net/http"
"strconv"
"strings"
"gitea.loveuer.com/loveuer/cluster/internal/registry/storage"
"github.com/gin-gonic/gin"
)
// GetBlob 获取 blob
func GetBlob(store storage.Storage) gin.HandlerFunc {
return func(c *gin.Context) {
repo := strings.TrimPrefix(c.Param("name"), "/")
digest := c.Param("digest")
reader, size, err := store.GetBlob(repo, digest)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"errors": []gin.H{
{
"code": "BLOB_UNKNOWN",
"message": err.Error(),
},
},
})
return
}
defer reader.Close()
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Length", strconv.FormatInt(size, 10))
c.Header("Docker-Content-Digest", digest)
c.DataFromReader(http.StatusOK, size, "application/octet-stream", reader, nil)
}
}
// HeadBlob 检查 blob 是否存在
func HeadBlob(store storage.Storage) gin.HandlerFunc {
return func(c *gin.Context) {
repo := c.GetString("repo_name")
if repo == "" {
repo = strings.TrimPrefix(c.Param("name"), "/")
}
digest := c.Param("digest")
exists, err := store.BlobExists(repo, digest)
if err != nil || !exists {
c.Status(http.StatusNotFound)
return
}
size, err := store.GetBlobSize(repo, digest)
if err != nil {
c.Status(http.StatusNotFound)
return
}
c.Header("Content-Length", strconv.FormatInt(size, 10))
c.Header("Docker-Content-Digest", digest)
c.Status(http.StatusOK)
}
}

View File

@@ -0,0 +1,147 @@
package handlers
import (
"crypto/sha256"
"encoding/hex"
"io"
"net/http"
"strconv"
"strings"
"gitea.loveuer.com/loveuer/cluster/internal/registry/storage"
"github.com/gin-gonic/gin"
)
// GetManifest 获取 manifest
func GetManifest(store storage.Storage) gin.HandlerFunc {
return func(c *gin.Context) {
repo := c.GetString("repo_name")
if repo == "" {
repo = strings.TrimPrefix(c.Param("name"), "/")
}
reference := c.Param("reference")
data, mediaType, err := store.GetManifest(repo, reference)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"errors": []gin.H{
{
"code": "MANIFEST_UNKNOWN",
"message": err.Error(),
},
},
})
return
}
c.Header("Content-Type", mediaType)
c.Header("Content-Length", strconv.FormatInt(int64(len(data)), 10))
c.Header("Docker-Content-Digest", calculateDigest(data))
c.Data(http.StatusOK, mediaType, data)
}
}
// PutManifest 推送 manifest
func PutManifest(store storage.Storage) gin.HandlerFunc {
return func(c *gin.Context) {
repo := c.Param("name")
reference := c.Param("reference")
// 读取请求体
data, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"errors": []gin.H{
{
"code": "BAD_REQUEST",
"message": err.Error(),
},
},
})
return
}
// 获取 Content-Type
mediaType := c.GetHeader("Content-Type")
if mediaType == "" {
mediaType = "application/vnd.docker.distribution.manifest.v2+json"
}
// 存储 manifest
if err := store.PutManifest(repo, reference, data, mediaType); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"errors": []gin.H{
{
"code": "INTERNAL_ERROR",
"message": err.Error(),
},
},
})
return
}
// 返回 Location 和 Digest
digest := calculateDigest(data)
c.Header("Location", c.Request.URL.Path)
c.Header("Docker-Content-Digest", digest)
c.Status(http.StatusCreated)
}
}
// DeleteManifest 删除 manifest
func DeleteManifest(store storage.Storage) gin.HandlerFunc {
return func(c *gin.Context) {
repo := c.Param("name")
reference := c.Param("reference")
if err := store.DeleteManifest(repo, reference); err != nil {
c.JSON(http.StatusNotFound, gin.H{
"errors": []gin.H{
{
"code": "MANIFEST_UNKNOWN",
"message": err.Error(),
},
},
})
return
}
c.Status(http.StatusAccepted)
}
}
// HeadManifest 检查 manifest 是否存在
func HeadManifest(store storage.Storage) gin.HandlerFunc {
return func(c *gin.Context) {
repo := c.GetString("repo_name")
if repo == "" {
repo = strings.TrimPrefix(c.Param("name"), "/")
}
reference := c.Param("reference")
exists, err := store.ManifestExists(repo, reference)
if err != nil || !exists {
c.Status(http.StatusNotFound)
return
}
// 获取 manifest 以设置正确的 headers
data, mediaType, err := store.GetManifest(repo, reference)
if err != nil {
c.Status(http.StatusNotFound)
return
}
c.Header("Content-Type", mediaType)
c.Header("Content-Length", strconv.FormatInt(int64(len(data)), 10))
c.Header("Docker-Content-Digest", calculateDigest(data))
c.Status(http.StatusOK)
}
}
// calculateDigest 计算 SHA256 digest
func calculateDigest(data []byte) string {
hash := sha256.Sum256(data)
return "sha256:" + hex.EncodeToString(hash[:])
}

View File

@@ -0,0 +1,192 @@
package handlers
import (
"net/http"
"strconv"
"strings"
"gitea.loveuer.com/loveuer/cluster/internal/registry/storage"
"github.com/gin-gonic/gin"
)
// StartBlobUpload 开始 blob 上传
func StartBlobUpload(store storage.Storage) gin.HandlerFunc {
return func(c *gin.Context) {
repo := c.GetString("repo_name")
if repo == "" {
repo = strings.TrimPrefix(c.Param("name"), "/")
}
uuid, err := store.StartBlobUpload(repo)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"errors": []gin.H{
{
"code": "INTERNAL_ERROR",
"message": err.Error(),
},
},
})
return
}
// 返回上传 URL
location := c.Request.URL.Path + "/" + uuid
c.Header("Location", location)
c.Header("Docker-Upload-UUID", uuid)
c.Header("Range", "0-0")
c.Status(http.StatusAccepted)
}
}
// PatchBlobUpload 上传 blob 数据块
func PatchBlobUpload(store storage.Storage) gin.HandlerFunc {
return func(c *gin.Context) {
uuid := c.Param("uuid")
// 获取 Range header
rangeHeader := c.GetHeader("Content-Range")
if rangeHeader == "" {
c.JSON(http.StatusBadRequest, gin.H{
"errors": []gin.H{
{
"code": "BAD_REQUEST",
"message": "Content-Range header required",
},
},
})
return
}
// 读取数据
data := c.Request.Body
if err := store.PutBlobUploadChunk(uuid, data); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"errors": []gin.H{
{
"code": "INTERNAL_ERROR",
"message": err.Error(),
},
},
})
return
}
// 获取当前上传大小
size, err := store.GetBlobUpload(uuid)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"errors": []gin.H{
{
"code": "UPLOAD_UNKNOWN",
"message": err.Error(),
},
},
})
return
}
location := c.Request.URL.Path
c.Header("Location", location)
c.Header("Docker-Upload-UUID", uuid)
c.Header("Range", "0-"+strconv.FormatInt(size-1, 10))
c.Status(http.StatusNoContent)
}
}
// PutBlobUpload 完成 blob 上传
func PutBlobUpload(store storage.Storage) gin.HandlerFunc {
return func(c *gin.Context) {
repo := c.GetString("repo_name")
if repo == "" {
repo = strings.TrimPrefix(c.Param("name"), "/")
}
uuid := c.Param("uuid")
// 获取 digest
digest := c.Query("digest")
if digest == "" {
c.JSON(http.StatusBadRequest, gin.H{
"errors": []gin.H{
{
"code": "BAD_REQUEST",
"message": "digest query parameter required",
},
},
})
return
}
// 如果有请求体,先追加数据
if c.Request.ContentLength > 0 {
if err := store.PutBlobUploadChunk(uuid, c.Request.Body); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"errors": []gin.H{
{
"code": "INTERNAL_ERROR",
"message": err.Error(),
},
},
})
return
}
}
// 完成上传
if err := store.CompleteBlobUpload(repo, uuid, digest); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"errors": []gin.H{
{
"code": "BAD_REQUEST",
"message": err.Error(),
},
},
})
return
}
// 清理上传文件
store.DeleteBlobUpload(uuid)
// 返回 blob 位置
// 从 /v2/{name}/blobs/uploads/{uuid} 转换为 /v2/{name}/blobs/{digest}
pathParts := strings.Split(c.Request.URL.Path, "/")
if len(pathParts) >= 4 {
// 构建新的路径: /v2/{name}/blobs/{digest}
location := "/v2/" + pathParts[2] + "/blobs/" + digest
c.Header("Location", location)
} else {
c.Header("Location", c.Request.URL.Path)
}
c.Header("Content-Length", "0")
c.Header("Docker-Content-Digest", digest)
c.Status(http.StatusCreated)
}
}
// GetBlobUpload 获取上传状态
func GetBlobUpload(store storage.Storage) gin.HandlerFunc {
return func(c *gin.Context) {
uuid := c.Param("uuid")
size, err := store.GetBlobUpload(uuid)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"errors": []gin.H{
{
"code": "UPLOAD_UNKNOWN",
"message": err.Error(),
},
},
})
return
}
location := c.Request.URL.Path
c.Header("Location", location)
c.Header("Docker-Upload-UUID", uuid)
c.Header("Range", "0-"+strconv.FormatInt(size-1, 10))
c.Status(http.StatusNoContent)
}
}

View File

@@ -0,0 +1,13 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
)
// VersionCheck API 版本检查
func VersionCheck(c *gin.Context) {
c.Header("Docker-Distribution-API-Version", "registry/2.0")
c.Status(http.StatusOK)
}