Skip to content

Commit

Permalink
Add method for deduping images
Browse files Browse the repository at this point in the history
This is helpful with Nomad because some images end up used as sidecars for many jobs.
Also periodic jobs spawn many tasks with the same image.
  • Loading branch information
IamTheFij committed Nov 20, 2024
1 parent 5da5afb commit b00b097
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
35 changes: 35 additions & 0 deletions internal/model/image.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package model

import (
"encoding/json"

"github.com/crazy-max/diun/v4/pkg/registry"
"github.com/pkg/errors"
)

// Image holds image configuration
Expand All @@ -20,6 +23,38 @@ type Image struct {
Metadata map[string]string `yaml:"metadata,omitempty" json:",omitempty"`
}

func (i Image) hash() (string, error) {
// Return json serialized image to use as a hashable key
b, err := json.Marshal(i)
if err != nil {
return "", errors.Errorf("cannot hash image: %v", err)
}

return string(b), nil
}

type ImageList []Image

// Dedupe removes duplicate images from the list and returns a new list
func (il ImageList) Dedupe() []Image {
keys := make(map[string]bool)
list := []Image{}
for _, entry := range il {
hash, err := entry.hash()
if err != nil {
// If we couldn't hash the entry, we can't dedupe it so we add it anyway
list = append(list, entry)
} else {
if _, value := keys[hash]; !value {
keys[hash] = true
list = append(list, entry)
}
}
}

return list
}

// ImagePlatform holds image platform configuration
type ImagePlatform struct {
OS string `yaml:"os,omitempty" json:",omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/nomad/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Client) listTaskImages() []model.Image {
c.logger.Error().Err(err).Msg("Cannot list Nomad jobs")
}

var list []model.Image
var list model.ImageList

for _, job := range jobs {
jobInfo, _, err := client.Jobs().Info(job.ID, nil)
Expand Down Expand Up @@ -129,7 +129,7 @@ func (c *Client) listTaskImages() []model.Image {
}
}

return list
return list.Dedupe()
}

func metadata(job *nomad.JobListStub, taskGroup *nomad.TaskGroup, task *nomad.Task) map[string]string {
Expand Down

0 comments on commit b00b097

Please sign in to comment.