From 2a98dc1c09927cdaa489ac0fff13695bba48bf0e Mon Sep 17 00:00:00 2001 From: bilalcaliskan Date: Sun, 5 Dec 2021 01:39:23 +0300 Subject: [PATCH] fix code smells, closes #29 --- internal/k8s/informers.go | 10 +++++----- internal/metrics/metrics.go | 5 +---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/internal/k8s/informers.go b/internal/k8s/informers.go index 6dbd633..3fd93f4 100644 --- a/internal/k8s/informers.go +++ b/internal/k8s/informers.go @@ -16,6 +16,8 @@ import ( "k8s.io/client-go/tools/cache" ) +const PodUrl = "http://%s:%d" + var ( restConfig *rest.Config clientSet *kubernetes.Clientset @@ -67,7 +69,7 @@ func RunPodInformer() { for key, value := range labels { if key == varnishLabelKey && value == varnishLabelValue && pod.Namespace == opts.VarnishNamespace { if pod.Status.PodIP != "" { - podUrl := fmt.Sprintf("http://%s:%d", pod.Status.PodIP, pod.Spec.Containers[0].Ports[0].ContainerPort) + podUrl := fmt.Sprintf(PodUrl, pod.Status.PodIP, pod.Spec.Containers[0].Ports[0].ContainerPort) logger.Info("Adding pod url to the varnishPods slice", zap.String("podUrl", podUrl)) addVarnishPod(&options.VarnishInstances, &podUrl) } else { @@ -82,15 +84,13 @@ func RunPodInformer() { newPod := newObj.(*v1.Pod) labels := oldPod.GetLabels() - // TODO: Handle all the cases - for key, value := range labels { if key == varnishLabelKey && value == varnishLabelValue && oldPod.ResourceVersion != newPod.ResourceVersion && oldPod.Namespace == opts.VarnishNamespace { if oldPod.Status.PodIP == "" && newPod.Status.PodIP != "" { logger.Info("Assigned an ip address to the pod, adding to varnishPods slice", zap.String("pod", newPod.Name), zap.String("namespace", newPod.Namespace), zap.String("ipAddress", newPod.Status.PodIP)) - podUrl := fmt.Sprintf("http://%s:%d", newPod.Status.PodIP, newPod.Spec.Containers[0].Ports[0].ContainerPort) + podUrl := fmt.Sprintf(PodUrl, newPod.Status.PodIP, newPod.Spec.Containers[0].Ports[0].ContainerPort) logger.Info("Adding pod url to the varnishPods slice", zap.String("podUrl", podUrl)) addVarnishPod(&options.VarnishInstances, &podUrl) } @@ -104,7 +104,7 @@ func RunPodInformer() { if key == varnishLabelKey && value == varnishLabelValue && pod.Namespace == opts.VarnishNamespace { logger.Info("Varnish pod is deleted, removing from varnishPods slice", zap.String("pod", pod.Name), zap.String("namespace", pod.Namespace)) - podUrl := fmt.Sprintf("http://%s:%d", pod.Status.PodIP, pod.Spec.Containers[0].Ports[0].ContainerPort) + podUrl := fmt.Sprintf(PodUrl, pod.Status.PodIP, pod.Spec.Containers[0].Ports[0].ContainerPort) index, found := findVarnishPod(options.VarnishInstances, podUrl) if found { removeVarnishPod(&options.VarnishInstances, index) diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 986a3d7..48f72ea 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -22,10 +22,6 @@ func init() { opts = options.GetVarnishCacheInvalidatorOptions() } -// TODO: Generate custom metrics, check below: -// https://prometheus.io/docs/guides/go-application/ -// https://www.robustperception.io/prometheus-middleware-for-gorilla-mux - // RunMetricsServer provides an endpoint, exports prometheus metrics using prometheus client golang func RunMetricsServer(router *mux.Router) { defer func() { @@ -41,6 +37,7 @@ func RunMetricsServer(router *mux.Router) { WriteTimeout: time.Duration(int32(opts.WriteTimeoutSeconds)) * time.Second, ReadTimeout: time.Duration(int32(opts.ReadTimeoutSeconds)) * time.Second, } + router.Handle("/metrics", promhttp.Handler()) logger.Info("starting metrics server", zap.Int("port", opts.MetricsPort)) panic(metricServer.ListenAndServe())