2025-07-17 02:35:36 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2025-07-17 02:53:36 +02:00
|
|
|
"log"
|
2025-07-17 02:35:36 +02:00
|
|
|
"regexp"
|
2025-07-17 03:04:35 +02:00
|
|
|
"strings"
|
2025-07-17 02:35:36 +02:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/image"
|
|
|
|
"github.com/docker/docker/client"
|
2025-07-17 02:53:36 +02:00
|
|
|
"github.com/regclient/regclient"
|
|
|
|
"github.com/regclient/regclient/types/ref"
|
2025-07-17 02:35:36 +02:00
|
|
|
)
|
|
|
|
|
2025-07-17 03:04:35 +02:00
|
|
|
// Wandelt ein ImageTag in registrykompatibles Format (ggf. registry hinzufügen)
|
|
|
|
func toRegistryImage(imageTag string) (string, error) {
|
2025-07-17 02:35:36 +02:00
|
|
|
r := regexp.MustCompile(`^(?:(?P<registry>[^/]+)/)?(?P<repo>[^:]+)(?::(?P<tag>.+))?$`)
|
2025-07-17 03:04:35 +02:00
|
|
|
match := r.FindStringSubmatch(imageTag)
|
2025-07-17 02:35:36 +02:00
|
|
|
if len(match) == 0 {
|
2025-07-17 03:04:35 +02:00
|
|
|
return "", fmt.Errorf("Image-Tag nicht erkannt: %s", imageTag)
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
2025-07-17 02:53:36 +02:00
|
|
|
registry := match[r.SubexpIndex("registry")]
|
|
|
|
repo := match[r.SubexpIndex("repo")]
|
|
|
|
tag := match[r.SubexpIndex("tag")]
|
|
|
|
if registry == "" {
|
|
|
|
registry = "registry-1.docker.io"
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
2025-07-17 02:53:36 +02:00
|
|
|
if tag == "" {
|
|
|
|
tag = "latest"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/%s:%s", registry, repo, tag), nil
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
|
|
|
|
2025-07-17 03:04:35 +02:00
|
|
|
// Extrahiert nur den reinen sha256:<...>-Digest
|
|
|
|
func extractDigest(s string) string {
|
|
|
|
for _, part := range strings.Split(s, "@") {
|
|
|
|
if strings.HasPrefix(part, "sha256:") {
|
|
|
|
return part
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
|
|
|
}
|
2025-07-17 03:04:35 +02:00
|
|
|
return s // ggf. nur die ID, wenn kein Digest
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2025-07-17 02:53:36 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2025-07-17 02:35:36 +02:00
|
|
|
if err != nil {
|
2025-07-17 02:53:36 +02:00
|
|
|
log.Fatal(err)
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
2025-07-17 02:53:36 +02:00
|
|
|
defer cli.Close()
|
|
|
|
rc := regclient.New()
|
|
|
|
|
2025-07-17 03:04:35 +02:00
|
|
|
images, err := cli.ImageList(ctx, image.ListOptions{All: true})
|
2025-07-17 02:35:36 +02:00
|
|
|
if err != nil {
|
2025-07-17 02:53:36 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2025-07-17 02:35:36 +02:00
|
|
|
|
2025-07-17 03:04:35 +02:00
|
|
|
for _, img := range images {
|
|
|
|
for _, tag := range img.RepoTags {
|
|
|
|
imageRef, err := toRegistryImage(tag)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2025-07-17 02:35:36 +02:00
|
|
|
|
2025-07-17 03:04:35 +02:00
|
|
|
// Lokalen Digest extrahieren
|
|
|
|
var localDigest string
|
|
|
|
if len(img.RepoDigests) > 0 {
|
|
|
|
localDigest = extractDigest(img.RepoDigests[0])
|
|
|
|
} else {
|
|
|
|
localDigest = img.ID
|
|
|
|
}
|
2025-07-17 02:35:36 +02:00
|
|
|
|
2025-07-17 03:04:35 +02:00
|
|
|
// Remote-Digest bestimmen
|
|
|
|
refObj, err := ref.New(imageRef)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ImageRef-Fehler bei %s: %v\n", tag, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
desc, err := rc.ManifestHead(ctx, refObj)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Manifest nicht gefunden (%s): %v\n", tag, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
remoteDigest := desc.GetDigest().String()
|
2025-07-17 02:35:36 +02:00
|
|
|
|
2025-07-17 03:04:35 +02:00
|
|
|
fmt.Printf("Image: %s\n Local Digest: %s\n Remote Digest: %s\n", tag, localDigest, remoteDigest)
|
|
|
|
if localDigest == remoteDigest {
|
|
|
|
fmt.Println(" -> Kein Update verfügbar.")
|
|
|
|
} else {
|
|
|
|
fmt.Println(" -> Update verfügbar!")
|
|
|
|
}
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|