From 3dd61d2f91ad6aa4679733f1d2a0b36db6eca07e Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Fri, 6 Nov 2020 15:02:18 +0100 Subject: [PATCH] Initial Commit --- go.mod | 14 +++++ go.sum | 38 +++++++++++++ main.go | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1330980 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module lookbuilding + +go 1.15 + +require ( + github.com/Microsoft/go-winio v0.4.14 // indirect + github.com/docker/distribution v2.7.1+incompatible // indirect + github.com/docker/docker v1.13.1 + github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-units v0.4.0 // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/pkg/errors v0.9.1 // indirect + golang.org/x/net v0.0.0-20201031054903-ff519b6c9102 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8671483 --- /dev/null +++ b/go.sum @@ -0,0 +1,38 @@ +github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU= +github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug= +github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= +github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo= +github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= +github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20201031054903-ff519b6c9102 h1:42cLlJJdEh+ySyeUUbEQ5bsTiq8voBeTuweGVkY6Puw= +golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..3a706b8 --- /dev/null +++ b/main.go @@ -0,0 +1,170 @@ +package main + +import ( + "context" + "encoding/json" + "net/http" + "fmt" + "strings" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/client" +) + +const ( + versioningModeLabel = "lookbuilding.mode" +) + +type VersioningMode string + +const ( + SemVerAny VersioningMode = "SemVerAny" +) + +type labeledContainer struct { + container types.Container + mode VersioningMode +} + +type tag struct { + Creator int `json:"creator"` + ID int `json:"id"` + LastUpdated string `json:"last_updated"` + LastUpdater int `json:"lastUpdater"` + LastUpdaterUsername string `json:"lastUpdaterUsername"` + Name string `json:"name"` + Repository int `json:"repository"` + FullSize int `json:"full_size"` + V2 bool `json:"v2"` + TagStatus string `json:"tag_status"` + TagLastPulled string `json:"tag_last_pulled"` + TagLastPushed string `json:"tag_last_pushed"` + Images []image `json:images` +} + +type image struct { + Architecture string `json:architecture` + Features string `json:features` + Digest string `json:digest` + OS string `json:linux` + OSFeatures string `json:os_features` + Size int `json:size` + Status string `json:status` + LastPulled string `json:last_pulled` + LastPushed string `json:last_pushed` + //"variant":null, + //"os_version":null, +} + +// Extract the repository owner (if any), repository and tag (if any) from a docker image name +func getImageParts(name string) (*string, string, *string) { + var repository string + var owner *string + var tag *string + + slashIndex := strings.Index(name, "/") + if slashIndex >= 0 { + tmp := name[:slashIndex] + owner = &tmp + name = name[slashIndex+1:] + } + + colonIndex := strings.Index(name, ":") + if colonIndex >= 0 { + tmp := name[colonIndex+1:] + tag = &tmp + + repository = name[:colonIndex] + } else { + repository = name + } + + return owner, repository, tag +} + +func getDockerRepoTags(maybe_owner *string, repository string) []tag { + type dockerPollResponse struct { + Count int `json:"count"` + Results []tag `json:"results"` + } + + owner := "_" + if maybe_owner != nil { + owner = *maybe_owner + } + + url := fmt.Sprintf("https://hub.docker.com/v2/repositories/%s/%s/tags", owner, repository) + resp, err := http.Get(url) + if err != nil { + panic(err) + } + + var data dockerPollResponse + err = json.NewDecoder(resp.Body).Decode(&data) + if err != nil { + panic(err) + } + + return data.Results +} + +func getLabeledContainers(cli *client.Client) []labeledContainer { + out := make([]labeledContainer, 0) + + containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{}) + if err != nil { + panic(err) + } + + for _, container := range containers { + fmt.Printf("%s %s\n", container.ID[:10], container.Image) + for k, v := range container.Labels { + if k == versioningModeLabel { + lc := labeledContainer{ + container, + SemVerAny, + } + + out = append(out, lc) + continue; + } + fmt.Printf(" - %s: %s\n", k, v) + } + } + + return out +} + +func main() { + cli, err := client.NewEnvClient() + if err != nil { + panic(err) + } + + labeledContainers := getLabeledContainers(cli) + for _, lc := range labeledContainers { + owner, repository, tag := getImageParts(lc.container.Image) + + if owner != nil { + fmt.Printf("container image: %s/%s\n", *owner, repository) + } else { + fmt.Printf("container image: _/%s\n", repository) + } + + fmt.Printf(" id: %s\n", lc.container.ImageID) + + if tag != nil { + fmt.Printf(" tag: %s\n", *tag) + } else { + fmt.Printf(" no tag\n") + } + + repoTags := getDockerRepoTags(owner, repository) + + fmt.Println("## tags in registry ##") + for _, tag := range repoTags { + fmt.Printf(" tag: %s\n", tag.Name) + } + fmt.Println("##") + } +}