grim/tagpull

Parents c79b1b4a64c9
Children 76c18febd8ac
It works! Needs some cleanup yet but I wanted to commit what I have now.
  • +33 -8
    operator.go
  • +2 -7
    registry.go
  • --- a/operator.go Thu Jun 18 03:02:03 2020 -0500
    +++ b/operator.go Thu Jun 18 06:24:52 2020 -0500
    @@ -198,21 +198,46 @@
    return nil
    }
    -func (o *Operator) checkContainer(container corev1.Container, imageID string, images map[string]string) (bool, error) {
    +func (o *Operator) checkContainer(container corev1.Container, imageID string, digests map[string]string) (bool, error) {
    // now that we know that the container has a ImagePullPolicy of
    - // Always, we can check it's image.
    - regImageID, found := images[container.Image]
    + // Always, we can check it's digest against the tags digest.
    +
    + // look up the digest for the local version in k8s
    + if strings.HasPrefix(imageID, "docker-pullable://") {
    + parts := strings.SplitN(imageID, "@", 2)
    + if len(parts) > 1 {
    + imageID = parts[1]
    + } else {
    + imageID = parts[0]
    + }
    + }
    +
    + localDigest, found := digests[imageID]
    if !found {
    - id, err := registryGetImageID(container.Image)
    + digest, err := registryGetDigest(container.Image, imageID)
    if err != nil {
    return false, err
    }
    - images[container.Image] = id
    - regImageID = id
    + digests[imageID] = digest
    + localDigest = digest
    }
    - if !strings.HasSuffix(imageID, regImageID) {
    - log.Infof("image id's differ, cycling pod: %s, %s", imageID, regImageID)
    + // now figure out the digest for what's on the registry
    + remoteDigest, found := digests[container.Image]
    + if !found {
    + repo, tag := parseRepositoryAndTag(container.Image)
    +
    + digest, err := registryGetDigest(repo, tag)
    + if err != nil {
    + return false, err
    + }
    +
    + digests[container.Image] = digest
    + remoteDigest = digest
    + }
    +
    + if localDigest != remoteDigest {
    + log.Infof("image digests differ, cycling pod: %s, %s", localDigest, remoteDigest)
    return true, nil
    }
    --- a/registry.go Thu Jun 18 03:02:03 2020 -0500
    +++ b/registry.go Thu Jun 18 06:24:52 2020 -0500
    @@ -1,21 +1,16 @@
    package main
    import (
    - "fmt"
    -
    "github.com/heroku/docker-registry-client/registry"
    )
    -func registryGetImageID(repository string) (string, error) {
    +func registryGetDigest(repository, tag string) (string, error) {
    reg, err := registry.New("https://index.docker.io", "", "")
    if err != nil {
    return "", err
    }
    - repo, tag := parseRepositoryAndTag(repository)
    - manifest, err := reg.ManifestV2(repo, tag)
    -
    - fmt.Printf("%#v\n", manifest)
    + manifest, err := reg.ManifestV2(repository, tag)
    return manifest.Config.Digest.String(), err
    }