From e98e27c94b2a90fc94d187efa4911d4fbd4cd4b7 Mon Sep 17 00:00:00 2001 From: Cameron McAvoy Date: Fri, 26 Jan 2024 11:20:23 -0600 Subject: [PATCH] Fix prometheus rewrite metrics from counting every rule invocation, add helm usage docs to README.md --- CHANGELOG.md | 6 ++++++ README.md | 19 +++++++++++++++++-- .../harbor-container-webhook/Chart.yaml | 4 ++-- internal/webhook/transformer.go | 17 ++++++++--------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5f613e..c613507 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.6.3] - 2024-01-26 +### Fixed +- Fixed rewrite_success prometheus metric counting every rule invocation, instead of only rewrites +### Changed +- Added helm usage to the README.md + ## [0.6.2] - 2023-12-06 ### Fixed - Fixed ServiceMonitor templates in helm chart not rendering correctly (thanks @z0rc for the fix!) diff --git a/README.md b/README.md index e06916c..d01f356 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,24 @@ Prerequisites === Requires kubernetes 1.17+ and can either be installed via helm or bare manifests. -Installing +Installing with helm === -See the helm chart available in the /deploy directory. + +Option 1: Install from chart repository +```shell +helm repo add harbor-container-webhook https://indeedeng.github.io/harbor-container-webhook/ + +helm install harbor-container-webhook harbor-container-webhook/harbor-container-webhook -n harbor-container-webhook --create-namespace +``` + +Option 2: Install chart from local build + +Build and install the Helm chart locally after cloning the repository. +```shell +make helm.build + +helm install harbor-container-webhook ./bin/chart/harbor-container-webhook.tgz -n harbor-container-webhook --create-namespace +``` Usage === diff --git a/deploy/charts/harbor-container-webhook/Chart.yaml b/deploy/charts/harbor-container-webhook/Chart.yaml index 5364b5b..d64ff31 100644 --- a/deploy/charts/harbor-container-webhook/Chart.yaml +++ b/deploy/charts/harbor-container-webhook/Chart.yaml @@ -2,8 +2,8 @@ apiVersion: v2 name: harbor-container-webhook description: Webhook to configure pods with harbor proxy cache projects type: application -version: 0.6.2 -appVersion: "0.6.2" +version: 0.6.3 +appVersion: "0.6.3" kubeVersion: ">= 1.16.0-0" home: https://github.com/IndeedEng-alpha/harbor-container-webhook maintainers: diff --git a/internal/webhook/transformer.go b/internal/webhook/transformer.go index 9f575a6..0f4d207 100644 --- a/internal/webhook/transformer.go +++ b/internal/webhook/transformer.go @@ -184,35 +184,34 @@ func (t *ruleTransformer) auth(ctx context.Context) (authn.Authenticator, error) func (t *ruleTransformer) RewriteImage(imageRef string) (string, error) { start := time.Now() - updatedRef, err := t.rewriteImage(imageRef) + rewritten, updatedRef, err := t.doRewriteImage(imageRef) duration := time.Since(start) if err != nil { rewriteErrors.WithLabelValues(t.metricName).Inc() - } else { + } else if rewritten { rewrite.WithLabelValues(t.metricName).Inc() rewriteTime.WithLabelValues(t.metricName).Observe(duration.Seconds()) } return updatedRef, err } -func (t *ruleTransformer) rewriteImage(imageRef string) (string, error) { +func (t *ruleTransformer) doRewriteImage(imageRef string) (rewritten bool, updatedRef string, err error) { registry, err := RegistryFromImageRef(imageRef) if err != nil { - rewriteErrors.WithLabelValues(t.metricName).Inc() - return "", err + return false, "", err } // shenanigans to get a fully normalized ref, e.g 'ubuntu' -> 'docker.io/library/ubuntu:latest' normalizedRef, err := ReplaceRegistryInImageRef(imageRef, registry) if err != nil { - rewriteErrors.WithLabelValues(t.metricName).Inc() - return "", err + return false, "", err } if t.findMatch(normalizedRef) && !t.anyExclusion(normalizedRef) { - return ReplaceRegistryInImageRef(imageRef, t.rule.Replace) + updatedRef, err = ReplaceRegistryInImageRef(imageRef, t.rule.Replace) + return true, updatedRef, err } - return imageRef, nil + return false, imageRef, nil } func (t *ruleTransformer) findMatch(imageRef string) bool {