Parse tags as semver

This commit is contained in:
2020-11-07 14:31:50 +01:00
parent 3dd61d2f91
commit 019f0a0bf5
5 changed files with 131 additions and 24 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.15
require ( require (
github.com/Microsoft/go-winio v0.4.14 // indirect github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/coreos/go-semver v0.3.0
github.com/docker/distribution v2.7.1+incompatible // indirect github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.13.1 github.com/docker/docker v1.13.1
github.com/docker/go-connections v0.4.0 // indirect github.com/docker/go-connections v0.4.0 // indirect

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU= 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/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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/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 h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=

54
main.go
View File

@ -11,22 +11,12 @@ import (
"github.com/docker/docker/client" "github.com/docker/docker/client"
) )
const (
versioningModeLabel = "lookbuilding.mode"
)
type VersioningMode string
const (
SemVerAny VersioningMode = "SemVerAny"
)
type labeledContainer struct { type labeledContainer struct {
container types.Container container types.Container
mode VersioningMode mode VersioningMode
} }
type tag struct { type Tag struct {
Creator int `json:"creator"` Creator int `json:"creator"`
ID int `json:"id"` ID int `json:"id"`
LastUpdated string `json:"last_updated"` LastUpdated string `json:"last_updated"`
@ -39,10 +29,10 @@ type tag struct {
TagStatus string `json:"tag_status"` TagStatus string `json:"tag_status"`
TagLastPulled string `json:"tag_last_pulled"` TagLastPulled string `json:"tag_last_pulled"`
TagLastPushed string `json:"tag_last_pushed"` TagLastPushed string `json:"tag_last_pushed"`
Images []image `json:images` Images []Image `json:images`
} }
type image struct { type Image struct {
Architecture string `json:architecture` Architecture string `json:architecture`
Features string `json:features` Features string `json:features`
Digest string `json:digest` Digest string `json:digest`
@ -82,10 +72,10 @@ func getImageParts(name string) (*string, string, *string) {
return owner, repository, tag return owner, repository, tag
} }
func getDockerRepoTags(maybe_owner *string, repository string) []tag { func getDockerRepoTags(maybe_owner *string, repository string) []Tag {
type dockerPollResponse struct { type dockerPollResponse struct {
Count int `json:"count"` Count int `json:"count"`
Results []tag `json:"results"` Results []Tag `json:"results"`
} }
owner := "_" owner := "_"
@ -116,19 +106,26 @@ func getLabeledContainers(cli *client.Client) []labeledContainer {
panic(err) panic(err)
} }
fmt.Println("scanning running container labels")
for _, container := range containers { for _, container := range containers {
fmt.Printf("%s %s\n", container.ID[:10], container.Image) fmt.Printf("- %s %s\n", container.ID[:10], container.Image)
for k, v := range container.Labels { for k, v := range container.Labels {
fmt.Printf(" - \"%s\": \"%s\"\n", k, v)
if k == versioningModeLabel { if k == versioningModeLabel {
mode := ParseVersioningMode(v)
if mode == nil {
fmt.Printf("Failed to parse '%s' as a versioning mode\n", v)
continue
}
lc := labeledContainer{ lc := labeledContainer{
container, container,
SemVerAny, *mode,
} }
out = append(out, lc) out = append(out, lc)
continue; continue
} }
fmt.Printf(" - %s: %s\n", k, v)
} }
} }
@ -142,6 +139,8 @@ func main() {
} }
labeledContainers := getLabeledContainers(cli) labeledContainers := getLabeledContainers(cli)
fmt.Println()
for _, lc := range labeledContainers { for _, lc := range labeledContainers {
owner, repository, tag := getImageParts(lc.container.Image) owner, repository, tag := getImageParts(lc.container.Image)
@ -150,21 +149,26 @@ func main() {
} else { } else {
fmt.Printf("container image: _/%s\n", repository) fmt.Printf("container image: _/%s\n", repository)
} }
fmt.Printf(" versioning: %+v\n", lc.mode.Label())
fmt.Printf(" id: %s\n", lc.container.ImageID) fmt.Printf(" id: %s\n", lc.container.ImageID)
if tag != nil { if tag != nil {
fmt.Printf(" tag: %s\n", *tag) fmt.Printf(" current tag: %s\n", *tag)
} else { } else {
fmt.Printf(" no tag\n") fmt.Printf(" no current tag, skipping\n")
continue
} }
repoTags := getDockerRepoTags(owner, repository) repoTags := getDockerRepoTags(owner, repository)
fmt.Println("## tags in registry ##") fmt.Println(" tags in registry:")
for _, tag := range repoTags { for _, tag := range repoTags {
fmt.Printf(" tag: %s\n", tag.Name) fmt.Printf(" - \"%s\"\n", tag.Name)
} svt := parseTagAsSemVer(tag.Name)
fmt.Println("##") if svt != nil {
fmt.Printf(" semver: true\n")
}
}
} }
} }

45
semver.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
//"fmt"
"regexp"
"github.com/coreos/go-semver/semver"
)
var (
rxSemVerPrefix = regexp.MustCompile(`^[^\d]*`)
)
type SemVerTag struct {
prefix string
version semver.Version
}
// Return a
// Returns nil if the tag did not parse as semver
func parseTagAsSemVer(tag string) *SemVerTag {
var prefix string
loc := rxSemVerPrefix.FindStringIndex(tag)
if loc != nil {
prefix = tag[:loc[1]]
tag = tag[loc[1]:]
}
version, err := semver.NewVersion(tag)
if err != nil {
return nil
}
svt := SemVerTag {
prefix,
*version,
}
return &svt
}
func (SemVerTag) asTag() string {
return ""
}

55
versioning_mode.go Normal file
View File

@ -0,0 +1,55 @@
package main
const (
versioningModeLabel = "lookbuilding.mode"
)
type VersioningMode interface {
Label() string
ShouldUpdate(currentTag string, availableTags []Tag) *Tag
}
type SameTag struct {}
type SemVerMajor struct {}
type SemVerMinor struct {}
type SemVerPatch struct {}
var (
AllModes = [...]VersioningMode{
SameTag{},
SemVerMajor{},
SemVerMinor{},
SemVerPatch{},
}
)
func (SameTag) Label() string { return "same_tag" }
func (SameTag) ShouldUpdate(currentTag string, availableTags []Tag) *Tag {
return nil // TODO: implement me
}
func (SemVerMajor) Label() string { return "semver_major" }
func (SemVerMajor) ShouldUpdate(currentTag string, availableTags []Tag) *Tag {
return nil // TODO: implement me
}
func (SemVerMinor) Label() string { return "semver_minor" }
func (SemVerMinor) ShouldUpdate(currentTag string, availableTags []Tag) *Tag {
return nil // TODO: implement me
}
func (SemVerPatch) Label() string { return "semver_patch" }
func (SemVerPatch) ShouldUpdate(currentTag string, availableTags []Tag) *Tag {
return nil // TODO: implement me
}
func ParseVersioningMode(input string) *VersioningMode {
for _, mode := range AllModes {
if mode.Label() == input {
return &mode
}
}
return nil
}