Skip to content

Instantly share code, notes, and snippets.

@kofj
Created December 20, 2019 15:06
Show Gist options
  • Save kofj/ea5df56beb5053d40ae20853a6fc5025 to your computer and use it in GitHub Desktop.
Save kofj/ea5df56beb5053d40ae20853a6fc5025 to your computer and use it in GitHub Desktop.
Docker Registry P2P bridge demo

FileServer

go run fileserver/main.go /YOUR/Storage/path

Registry

go run github.com/kofj/distribution/cmd/registry/main.go config-dev.yml
# github.com/kofj/distribution/cmd/registry/
version: 0.1
log:
level: debug
fields:
service: registry
environment: development
storage:
delete:
enabled: true
cache:
blobdescriptor: redis
filesystem:
rootdirectory: ./devdata
maintenance:
uploadpurging:
enabled: false
http:
addr: :5000
debug:
addr: :5001
prometheus:
enabled: true
path: /metrics
headers:
X-Content-Type-Options: [nosniff]
redis:
addr: host.docker.internal:6379
pool:
maxidle: 16
maxactive: 64
idletimeout: 300s
dialtimeout: 10ms
readtimeout: 10ms
writetimeout: 10ms
middleware:
storage:
- name: fileserver
options:
baseurl: http://10.254.57.46:8001
test: abc
// fileserver/main.go
package main
import (
"os"
"github.com/gin-gonic/gin"
)
func main() {
if len(os.Args) < 2 {
os.Exit(1)
}
var relativePath = os.Args[1]
var r = gin.Default()
r.Static("/", relativePath)
r.Run(":8001")
}
// github.com/docker/distribution/registry/storage/driver/middleware/fileserver/middleware.go
package fileserver
import (
"context"
"fmt"
"net/url"
"strings"
dcontext "github.com/docker/distribution/context"
storagedriver "github.com/docker/distribution/registry/storage/driver"
storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware"
)
type fileServerStorageMiddleware struct {
storagedriver.StorageDriver
baseURL string
}
var _ storagedriver.StorageDriver = &fileServerStorageMiddleware{}
func newFileServerStorageMiddleware(storageDriver storagedriver.StorageDriver, options map[string]interface{}) (sd storagedriver.StorageDriver, err error) {
base, ok := options["baseurl"]
if !ok {
return nil, fmt.Errorf("no baseurl provided")
}
baseURL, ok := base.(string)
if !ok {
return nil, fmt.Errorf("baseurl must be a string")
}
if !strings.Contains(baseURL, "://") {
baseURL = "https://" + baseURL
}
if _, err := url.Parse(baseURL); err != nil {
return nil, fmt.Errorf("invalid baseurl: %v", err)
}
return &fileServerStorageMiddleware{
StorageDriver: storageDriver,
baseURL: baseURL,
}, nil
}
// URLFor attempts to find a url which may be used to retrieve the file at the given path.
func (fs *fileServerStorageMiddleware) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) {
var fullurl = fs.baseURL + path
method, ok := options["method"]
if !ok {
return "", fmt.Errorf("no method provided")
}
methodStr, ok := method.(string)
if !ok {
return "", fmt.Errorf("method must be a string")
}
if methodStr != "GET" && methodStr != "HEAD" {
return "", nil
}
dcontext.GetLogger(ctx).Debugf("[FileServer] name=%s\t path=%s\t method=%s\n", fs.Name(), path, methodStr)
return fullurl, nil
}
// init registers the alicdn layerHandler backend.
func init() {
storagemiddleware.Register("fileserver", storagemiddleware.InitFunc(newFileServerStorageMiddleware))
}
// github.com/kofj/distribution/cmd/registry/main.go
package main
import (
_ "net/http/pprof"
"github.com/docker/distribution/registry"
_ "github.com/docker/distribution/registry/auth/htpasswd"
_ "github.com/docker/distribution/registry/auth/silly"
_ "github.com/docker/distribution/registry/auth/token"
_ "github.com/docker/distribution/registry/proxy"
_ "github.com/docker/distribution/registry/storage/driver/azure"
_ "github.com/docker/distribution/registry/storage/driver/filesystem"
_ "github.com/docker/distribution/registry/storage/driver/gcs"
_ "github.com/docker/distribution/registry/storage/driver/inmemory"
_ "github.com/docker/distribution/registry/storage/driver/middleware/alicdn"
_ "github.com/docker/distribution/registry/storage/driver/middleware/cloudfront"
_ "github.com/docker/distribution/registry/storage/driver/middleware/redirect"
# The storage middleware use to redirect HEAD/GET request to fileserver
_ "github.com/docker/distribution/registry/storage/driver/middleware/fileserver"
_ "github.com/docker/distribution/registry/storage/driver/oss"
_ "github.com/docker/distribution/registry/storage/driver/s3-aws"
_ "github.com/docker/distribution/registry/storage/driver/swift"
)
func main() {
registry.RootCmd.Execute()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment