code/autoregistry/proxy/registry.go
2024-08-31 19:58:24 +02:00

80 lines
2.3 KiB
Go

package main
import (
"fmt"
"github.com/go-resty/resty/v2"
"regexp"
)
type Manifest struct {
Config struct {
Digest string
}
}
func GetRegistryId(registry_url, image_name, tag_name string) string {
client := resty.New()
client.SetDebug(false)
resp, err := client.R().
EnableTrace().
SetResult(Manifest{}).
ForceContentType("application/json").
SetHeader("Accept", "application/vnd.docker.distribution.manifest.v2+json").
Get(registry_url + "/v2/" + image_name + "/manifests/" + tag_name)
if err != nil {
panic(err)
}
manifest := resp.Result().(*Manifest)
return manifest.Config.Digest
}
func ParseImage(path string) (image_name string, tag_name string, ok bool) {
r := regexp.MustCompile("^/?v2/(.*)/manifests/([^/]+)$")
matches := r.FindStringSubmatch(path)
if len(matches) == 3 {
return matches[1], matches[2], true
}
return "", "", false
}
func testRegistry() {
client := resty.New()
client.SetDebug(true)
resp, err := client.R().
EnableTrace().
SetResult(Manifest{}).
ForceContentType("application/json").
SetHeader("Accept", "application/vnd.docker.distribution.manifest.v2+json").
Get("http://host.docker.internal:4999/v2/alpine/manifests/latest")
fmt.Println("err ", err)
fmt.Printf("results: %v\n", resp.Result().(*Manifest))
fmt.Println("Response Info:")
fmt.Println(" Error :", err)
fmt.Println(" Status Code:", resp.StatusCode())
fmt.Println(" Status :", resp.Status())
fmt.Println(" Proto :", resp.Proto())
fmt.Println(" Time :", resp.Time())
fmt.Println(" Received At:", resp.ReceivedAt())
fmt.Println(" Body :\n", resp)
fmt.Println()
// Explore trace info
fmt.Println("Request Trace Info:")
ti := resp.Request.TraceInfo()
fmt.Println(" DNSLookup :", ti.DNSLookup)
fmt.Println(" ConnTime :", ti.ConnTime)
fmt.Println(" TCPConnTime :", ti.TCPConnTime)
fmt.Println(" TLSHandshake :", ti.TLSHandshake)
fmt.Println(" ServerTime :", ti.ServerTime)
fmt.Println(" ResponseTime :", ti.ResponseTime)
fmt.Println(" TotalTime :", ti.TotalTime)
fmt.Println(" IsConnReused :", ti.IsConnReused)
fmt.Println(" IsConnWasIdle :", ti.IsConnWasIdle)
fmt.Println(" ConnIdleTime :", ti.ConnIdleTime)
fmt.Println(" RequestAttempt:", ti.RequestAttempt)
fmt.Println(" RemoteAddr :", ti.RemoteAddr.String())
}