diff --git a/README.md b/README.md index f3cef586..b98db0f7 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,9 @@ enrich version checking on image tags: is. In this example, the current version of `my-container` will be compared against the image versions in the `docker.io/bitnami/etcd` registry. +- `priority.version-checker.io/my-container: 3`: will set the priority label. + So you can sort/prioritize better if there are many images in the cluster. + ## Known configurations From time to time, version-checker may need some of the above options applied to determine the latest version, diff --git a/pkg/api/types.go b/pkg/api/types.go index 59bb44c4..7b1ac9c0 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -37,6 +37,9 @@ const ( // PinPatchAnnotationKey will pin the patch version to check. PinPatchAnnotationKey = "pin-patch.version-checker.io" + + // PriorityKey will set the priority label on the image metric. + PriorityKey = "priority.version-checker.io" ) // Options is used to describe what restrictions should be used for determining @@ -58,6 +61,8 @@ type Options struct { PinPatch *int64 `json:"pin-patch,omitempty"` RegexMatcher *regexp.Regexp `json:"-"` + + Priority int } // ImageTag describes a container image tag. diff --git a/pkg/controller/checker/checker.go b/pkg/controller/checker/checker.go index 8149dcce..eff5b04e 100644 --- a/pkg/controller/checker/checker.go +++ b/pkg/controller/checker/checker.go @@ -22,6 +22,7 @@ type Result struct { LatestVersion string IsLatest bool ImageURL string + Priority int } func New(search search.Searcher) *Checker { @@ -48,11 +49,18 @@ func (c *Checker) Container(ctx context.Context, log *logrus.Entry, pod *corev1. imageURL = c.overrideImageURL(log, imageURL, opts) + var result *Result + var err error if opts.UseSHA { - return c.handleSHA(ctx, imageURL, statusSHA, opts, usingTag, currentTag) + result, err = c.handleSHA(ctx, imageURL, statusSHA, opts, usingTag, currentTag) + } else { + result, err = c.handleSemver(ctx, imageURL, statusSHA, currentTag, usingSHA, opts) } - - return c.handleSemver(ctx, imageURL, statusSHA, currentTag, usingSHA, opts) + if err != nil { + return result, err + } + result.Priority = opts.Priority + return result, err } func (c *Checker) handleLatestOrEmptyTag(log *logrus.Entry, currentTag, currentSHA string, opts *api.Options) { diff --git a/pkg/controller/options/options.go b/pkg/controller/options/options.go index 9982f86d..0cf7cece 100644 --- a/pkg/controller/options/options.go +++ b/pkg/controller/options/options.go @@ -42,6 +42,7 @@ func (b *Builder) Options(name string) (*api.Options, error) { b.handlePinMinorOption, b.handlePinPatchOption, b.handleOverrideURLOption, + b.handlePriority, } // Execute each handler @@ -146,6 +147,16 @@ func (b *Builder) handleOverrideURLOption(name string, opts *api.Options, setNon return nil } +func (b *Builder) handlePriority(name string, opts *api.Options, _ *bool, _ *[]string) error { + if priority, ok := b.ans[b.index(name, api.PriorityKey)]; ok { + var err error + if opts.Priority, err = strconv.Atoi(priority); err != nil { + return err + } + } + return nil +} + // IsEnabled will return whether the container has the enabled annotation set. // Will fall back to default, if not set true/false. func (b *Builder) IsEnabled(defaultEnabled bool, name string) bool { diff --git a/pkg/controller/sync.go b/pkg/controller/sync.go index d422acc6..b8669df9 100644 --- a/pkg/controller/sync.go +++ b/pkg/controller/sync.go @@ -3,6 +3,7 @@ package controller import ( "context" "fmt" + "strconv" "strings" "github.com/sirupsen/logrus" @@ -97,6 +98,7 @@ func (c *Controller) checkContainer(ctx context.Context, log *logrus.Entry, pod container.Name, containerType, result.ImageURL, result.IsLatest, result.CurrentVersion, result.LatestVersion, + strconv.Itoa(result.Priority), ) return nil diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 090baed6..5185dfc5 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -44,7 +44,7 @@ func New(log *logrus.Entry) *Metrics { Help: "Where the container in use is using the latest upstream registry version", }, []string{ - "namespace", "pod", "container", "container_type", "image", "current_version", "latest_version", + "namespace", "pod", "container", "container_type", "image", "current_version", "latest_version", "priority", }, ) @@ -87,7 +87,7 @@ func (m *Metrics) Run(servingAddress string) error { return nil } -func (m *Metrics) AddImage(namespace, pod, container, containerType, imageURL string, isLatest bool, currentVersion, latestVersion string) { +func (m *Metrics) AddImage(namespace, pod, container, containerType, imageURL string, isLatest bool, currentVersion, latestVersion, priority string) { // Remove old image url/version if it exists m.RemoveImage(namespace, pod, container, containerType) @@ -100,7 +100,7 @@ func (m *Metrics) AddImage(namespace, pod, container, containerType, imageURL st } m.containerImageVersion.With( - m.buildLabels(namespace, pod, container, containerType, imageURL, currentVersion, latestVersion), + m.buildLabels(namespace, pod, container, containerType, imageURL, currentVersion, latestVersion, priority), ).Set(isLatestF) index := m.latestImageIndex(namespace, pod, container, containerType) @@ -131,7 +131,7 @@ func (m *Metrics) latestImageIndex(namespace, pod, container, containerType stri return strings.Join([]string{namespace, pod, container, containerType}, "") } -func (m *Metrics) buildLabels(namespace, pod, container, containerType, imageURL, currentVersion, latestVersion string) prometheus.Labels { +func (m *Metrics) buildLabels(namespace, pod, container, containerType, imageURL, currentVersion, latestVersion, priority string) prometheus.Labels { return prometheus.Labels{ "namespace": namespace, "pod": pod, @@ -140,6 +140,7 @@ func (m *Metrics) buildLabels(namespace, pod, container, containerType, imageURL "image": imageURL, "current_version": currentVersion, "latest_version": latestVersion, + "priority": priority, } } diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go index 518d1780..a7c566fb 100644 --- a/pkg/metrics/metrics_test.go +++ b/pkg/metrics/metrics_test.go @@ -8,17 +8,21 @@ import ( "github.com/sirupsen/logrus" ) +const ( + defaultPriority = "0" +) + func TestCache(t *testing.T) { m := New(logrus.NewEntry(logrus.New())) for i, typ := range []string{"init", "container"} { version := fmt.Sprintf("0.1.%d", i) - m.AddImage("namespace", "pod", "container", typ, "url", true, version, version) + m.AddImage("namespace", "pod", "container", typ, "url", true, version, version, defaultPriority) } for i, typ := range []string{"init", "container"} { version := fmt.Sprintf("0.1.%d", i) - mt, _ := m.containerImageVersion.GetMetricWith(m.buildLabels("namespace", "pod", "container", typ, "url", version, version)) + mt, _ := m.containerImageVersion.GetMetricWith(m.buildLabels("namespace", "pod", "container", typ, "url", version, version, defaultPriority)) count := testutil.ToFloat64(mt) if count != 1 { t.Error("Should have added metric") @@ -30,7 +34,7 @@ func TestCache(t *testing.T) { } for i, typ := range []string{"init", "container"} { version := fmt.Sprintf("0.1.%d", i) - mt, _ := m.containerImageVersion.GetMetricWith(m.buildLabels("namespace", "pod", "container", typ, "url", version, version)) + mt, _ := m.containerImageVersion.GetMetricWith(m.buildLabels("namespace", "pod", "container", typ, "url", version, version, defaultPriority)) count := testutil.ToFloat64(mt) if count != 0 { t.Error("Should have removed metric")