diff --git a/api/kuik/v1alpha1/cachedimage_utils.go b/api/kuik/v1alpha1/cachedimage_utils.go index 1945841d..6475ead6 100644 --- a/api/kuik/v1alpha1/cachedimage_utils.go +++ b/api/kuik/v1alpha1/cachedimage_utils.go @@ -21,6 +21,15 @@ func (r *CachedImage) Repository() (reference.Named, error) { return named, nil } +func (r *CachedImage) Upstream() (string, error) { + named, err := r.Repository() + if err != nil { + return "", err + } + + return reference.Domain(named), nil +} + func (r *CachedImage) GetPullSecrets(apiReader client.Reader) ([]corev1.Secret, error) { named, err := r.Repository() if err != nil { diff --git a/internal/controller/collector.go b/internal/controller/collector.go index dc9a3b60..f1e83748 100644 --- a/internal/controller/collector.go +++ b/internal/controller/collector.go @@ -19,6 +19,15 @@ import ( const subsystem = "controller" var ( + ImageCachingRequest = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: kuikMetrics.Namespace, + Subsystem: subsystem, + Name: "image_caching_request", + Help: "Number of request to cache an image", + }, + []string{"successful", "upstream_registry"}, + ) ImagePutInCache = prometheus.NewCounter( prometheus.CounterOpts{ Namespace: kuikMetrics.Namespace, diff --git a/internal/controller/kuik/cachedimage_controller.go b/internal/controller/kuik/cachedimage_controller.go index 8aa44153..6e8b3898 100644 --- a/internal/controller/kuik/cachedimage_controller.go +++ b/internal/controller/kuik/cachedimage_controller.go @@ -4,6 +4,7 @@ import ( "context" "crypto/x509" "net/http" + "strconv" "strings" "time" @@ -239,8 +240,14 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request) putImageInCache = false } if putImageInCache { + upstream, err := cachedImage.Upstream() + if err != nil { + return ctrl.Result{}, err + } + r.Recorder.Eventf(&cachedImage, "Normal", "Caching", "Start caching image %s", cachedImage.Spec.SourceImage) err = r.cacheImage(&cachedImage) + kuikController.ImageCachingRequest.WithLabelValues(strconv.FormatBool(err == nil), upstream).Inc() if err != nil { log.Error(err, "failed to cache image") r.Recorder.Eventf(&cachedImage, "Warning", "CacheFailed", "Failed to cache image %s, reason: %s", cachedImage.Spec.SourceImage, err) @@ -401,9 +408,6 @@ func (r *CachedImageReconciler) SetupWithManager(mgr ctrl.Manager, maxConcurrent &corev1.Pod{}, handler.EnqueueRequestsFromMapFunc(r.cachedImagesRequestFromPod), builder.WithPredicates(predicate.Funcs{ - // GenericFunc: func(e event.GenericEvent) bool { - // return true - // }, DeleteFunc: func(e event.DeleteEvent) bool { pod := e.Object.(*corev1.Pod) var currentPod corev1.Pod