Skip to content

Commit

Permalink
Fix prometheus rewrite metrics from counting every rule invocation, a…
Browse files Browse the repository at this point in the history
…dd helm usage docs to README.md
  • Loading branch information
cnmcavoy committed Jan 26, 2024
1 parent 680132d commit e98e27c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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!)
Expand Down
19 changes: 17 additions & 2 deletions README.md
Expand Up @@ -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
===
Expand Down
4 changes: 2 additions & 2 deletions deploy/charts/harbor-container-webhook/Chart.yaml
Expand Up @@ -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:
Expand Down
17 changes: 8 additions & 9 deletions internal/webhook/transformer.go
Expand Up @@ -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 {
Expand Down

0 comments on commit e98e27c

Please sign in to comment.