From 8626b91b16fcfa97e85d80b79a04ddf9a3e19e60 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Tue, 20 Feb 2024 16:14:15 +0100 Subject: [PATCH] refactor to dry --- src/main/cljc/dda/c4k_common/monitoring.cljc | 36 ++++--------------- .../monitoring/monitoring_internal.cljc | 10 ++---- .../monitoring_regex_test.clj | 4 +-- .../should_filter_metrik.edn | 0 4 files changed, 10 insertions(+), 40 deletions(-) rename src/test/clj/dda/c4k_common/{ => monitoring}/monitoring_regex_test.clj (88%) rename src/test/resources/dda/c4k_common/{ => monitoring}/monitoring_regex_test/should_filter_metrik.edn (100%) diff --git a/src/main/cljc/dda/c4k_common/monitoring.cljc b/src/main/cljc/dda/c4k_common/monitoring.cljc index 1536443..a3c70a4 100644 --- a/src/main/cljc/dda/c4k_common/monitoring.cljc +++ b/src/main/cljc/dda/c4k_common/monitoring.cljc @@ -3,46 +3,22 @@ [clojure.spec.alpha :as s] #?(:clj [orchestra.core :refer [defn-spec]] :cljs [orchestra.core :refer-macros [defn-spec]]) - [clojure.string :as str] [dda.c4k-common.yaml :as yaml] - [dda.c4k-common.predicate :as cp] [dda.c4k-common.monitoring.monitoring-internal :as int])) -(s/def ::grafana-cloud-user cp/bash-env-string?) -(s/def ::grafana-cloud-password cp/bash-env-string?) -(s/def ::grafana-cloud-url string?) -(s/def ::cluster-name string?) -(s/def ::cluster-stage cp/stage?) -(s/def ::node-regex string?) -(s/def ::traefik-regex string?) -(s/def ::kube-state-regex string?) +(s/def ::grafana-cloud-user ::int/grafana-cloud-user) +(s/def ::grafana-cloud-password ::int/grafana-cloud-password) +(s/def ::grafana-cloud-url ::int/grafana-cloud-url) +(s/def ::cluster-name ::int/cluster-name) +(s/def ::cluster-stage ::int/cluster-stage) (s/def ::mon-cfg (s/keys :req-un [::grafana-cloud-url ::cluster-name ::cluster-stage])) (s/def ::mon-auth (s/keys :req-un [::grafana-cloud-user ::grafana-cloud-password])) -(s/def ::filter-regex (s/keys :req-un [::node-regex - ::traefik-regex - ::kube-state-regex])) -(def metric-regex {:node-regex - (str "node_cpu_sec.+|node_load[0-9]+|node_memory_Buf.*|node_memory_Mem.*|" - "node_memory_Cached.*|node_disk_[r,w,i].*|node_filesystem_[s,a].*|" - "node_network_receive_bytes_total|node_network_transmit_bytes_total") - :traefik-regex (str "traefik_entrypoint_.*_total|" - "traefik_entrypoint_.*_seconds_count|" - "traefik_router_.*_total|" - "traefik_router_.*_seconds_count|" - "traefik_service_.*_total|" - "traefik_service_.*_seconds_count|" - "traefik_tls_certs_not_after") - :kube-state-regex (str "kube_pod_container_status_restarts_total|" - "kube_pod_status_reason|kube_node_status_capacity|kube_node_status_allocatable|" - "kube_cronjob_status_active|kube_job_status_failed")}) - -(def filter-regex-string - (str/join "|" (vals metric-regex))) +(def filter-regex-string int/filter-regex-string) (defn-spec generate seq? diff --git a/src/main/cljc/dda/c4k_common/monitoring/monitoring_internal.cljc b/src/main/cljc/dda/c4k_common/monitoring/monitoring_internal.cljc index 6dfc3a3..023e027 100644 --- a/src/main/cljc/dda/c4k_common/monitoring/monitoring_internal.cljc +++ b/src/main/cljc/dda/c4k_common/monitoring/monitoring_internal.cljc @@ -14,17 +14,11 @@ (s/def ::grafana-cloud-url string?) (s/def ::cluster-name string?) (s/def ::cluster-stage cp/stage?) -(s/def ::node-regex string?) -(s/def ::traefik-regex string?) -(s/def ::kube-state-regex string?) (s/def ::mon-cfg (s/keys :req-un [::grafana-cloud-url ::cluster-name ::cluster-stage])) (s/def ::mon-auth (s/keys :req-un [::grafana-cloud-user ::grafana-cloud-password])) -(s/def ::filter-regex (s/keys :req-un [::node-regex - ::traefik-regex - ::kube-state-regex])) (def metric-regex {:node-regex (str "node_cpu_sec.+|node_load[0-9]+|node_memory_Buf.*|node_memory_Mem.*|" @@ -44,7 +38,7 @@ (def filter-regex-string (str/join "|" (vals metric-regex))) -(defn-spec generate-prometheus-config cp/map-or-seq? +(defn-spec generate-prometheus-config map? [config ::mon-cfg auth ::mon-auth] (let [{:keys [grafana-cloud-url cluster-name cluster-stage]} config @@ -63,7 +57,7 @@ grafana-cloud-password) (cm/replace-all-matching-values-by-new-value "FILTER_REGEX" filter-regex-string)))) -(defn-spec generate-config cp/map-or-seq? +(defn-spec generate-config map? [config ::mon-cfg auth ::mon-auth] (-> diff --git a/src/test/clj/dda/c4k_common/monitoring_regex_test.clj b/src/test/clj/dda/c4k_common/monitoring/monitoring_regex_test.clj similarity index 88% rename from src/test/clj/dda/c4k_common/monitoring_regex_test.clj rename to src/test/clj/dda/c4k_common/monitoring/monitoring_regex_test.clj index 196a911..50f03b1 100644 --- a/src/test/clj/dda/c4k_common/monitoring_regex_test.clj +++ b/src/test/clj/dda/c4k_common/monitoring/monitoring_regex_test.clj @@ -1,8 +1,8 @@ -(ns dda.c4k-common.monitoring-regex-test +(ns dda.c4k-common.monitoring.monitoring-regex-test (:require [clojure.test :refer [deftest is are testing run-tests]] [data-test :refer :all] - [dda.c4k-common.monitoring :as cut])) + [dda.c4k-common.monitoring.monitoring-internal :as cut])) (defn filter-by-regex [regex-str collection] diff --git a/src/test/resources/dda/c4k_common/monitoring_regex_test/should_filter_metrik.edn b/src/test/resources/dda/c4k_common/monitoring/monitoring_regex_test/should_filter_metrik.edn similarity index 100% rename from src/test/resources/dda/c4k_common/monitoring_regex_test/should_filter_metrik.edn rename to src/test/resources/dda/c4k_common/monitoring/monitoring_regex_test/should_filter_metrik.edn