diff --git a/cmd/main.go b/cmd/main.go index 1c4a52b..4697a10 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,6 +19,8 @@ package main import ( "flag" "os" + "time" + "strconv" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. @@ -55,6 +57,7 @@ func main() { var keplerPrometheusUrl string var susqlPrometheusMetricsUrl string var susqlPrometheusDatabaseUrl string + var samplingRate string flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") @@ -62,6 +65,7 @@ func main() { flag.StringVar(&keplerPrometheusUrl, "kepler-prometheus-url", "", "The URL for the Prometheus server where Kepler stores the energy data") flag.StringVar(&susqlPrometheusDatabaseUrl, "susql-prometheus-database-url", "", "The URL for the Prometheus database where SusQL stores the energy data") flag.StringVar(&susqlPrometheusMetricsUrl, "susql-prometheus-metrics-url", "", "The URL for the Prometheus metrics where SusQL exposes the energy data") + flag.StringVar(&samplingRate, "sampling-rate", "", "Sampling rate in seconds") opts := zap.Options{ Development: true, @@ -95,12 +99,18 @@ func main() { os.Exit(1) } + samplingRateInteger, err := strconv.Atoi(samplingRate) + if err != nil { + samplingRateInteger = 2 + } + if err = (&controller.LabelGroupReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), KeplerPrometheusUrl: keplerPrometheusUrl, SusQLPrometheusDatabaseUrl: susqlPrometheusDatabaseUrl, SusQLPrometheusMetricsUrl: susqlPrometheusMetricsUrl, + SamplingRate: time.Duration(samplingRateInteger) * time.Second, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "LabelGroup") os.Exit(1) diff --git a/deployment/deploy.sh b/deployment/deploy.sh index a4aaae2..1ae9c50 100644 --- a/deployment/deploy.sh +++ b/deployment/deploy.sh @@ -67,6 +67,11 @@ if [[ -z ${SUSQL_PROMETHEUS_URL} ]]; then fi fi +if [[ -z ${SUSQL_SAMPLING_RATE} ]]; then + SUSQL_SAMPLING_RATE="2" +fi + + # Check if namespace exists if [[ -z $(kubectl get namespaces --no-headers -o custom-columns=':{.metadata.name}' | grep ${SUSQL_NAMESPACE}) ]]; then echo "Namespace '${SUSQL_NAMESPACE}' doesn't exist. Creating it." @@ -98,6 +103,7 @@ echo "PROMETHEUS_DOMAIN - '${PROMETHEUS_DOMAIN}'" echo "PROMETHEUS_PORT - '${PROMETHEUS_PORT}'" echo "KEPLER_PROMETHEUS_URL - '${KEPLER_PROMETHEUS_URL}'" echo "SUSQL_PROMETHEUS_URL - '${SUSQL_PROMETHEUS_URL}'" +echo "SUSQL_SAMPLING_RATE - '${SUSQL_SAMPLING_RATE}'" echo "SUSQL_ENHANCED - '${SUSQL_ENHANCED}'" echo "SUSQL_REGISTRY - '${SUSQL_REGISTRY}'" echo "SUSQL_IMAGE_NAME - '${SUSQL_IMAGE_NAME}'" @@ -125,6 +131,7 @@ echo "export PROMETHEUS_DOMAIN=${PROMETHEUS_DOMAIN}" >> ${LOGFILE} echo "export PROMETHEUS_PORT=${PROMETHEUS_PORT}" >> ${LOGFILE} echo "export KEPLER_PROMETHEUS_URL=${KEPLER_PROMETHEUS_URL}" >> ${LOGFILE} echo "export SUSQL_PROMETHEUS_URL=${SUSQL_PROMETHEUS_URL}" >> ${LOGFILE} +echo "export SUSQL_SAMPLING_RATE=${SUSQL_SAMPLING_RATE}" >> ${LOGFILE} echo "export SUSQL_ENHANCED=${SUSQL_ENHANCED}" >> ${LOGFILE} echo "export SUSQL_REGISTRY=${SUSQL_REGISTRY}" >> ${LOGFILE} echo "export SUSQL_IMAGE_NAME=${SUSQL_IMAGE_NAME}" >> ${LOGFILE} @@ -214,6 +221,7 @@ do --set keplerPrometheusUrl="${KEPLER_PROMETHEUS_URL}" \ --set susqlPrometheusDatabaseUrl="${SUSQL_PROMETHEUS_URL}" \ --set susqlPrometheusMetricsUrl="http://0.0.0.0:8082" \ + --set samplingRate="${SUSQL_SAMPLING_RATE}" \ --set imagePullPolicy="Always" \ --set containerImage="${SUSQL_REGISTRY}/${SUSQL_IMAGE_NAME}:${SUSQL_IMAGE_TAG}" if [[ ! -z ${SUSQL_ENHANCED} ]]; then diff --git a/deployment/susql-controller/templates/deployment.yaml b/deployment/susql-controller/templates/deployment.yaml index df2f02e..7aa280a 100644 --- a/deployment/susql-controller/templates/deployment.yaml +++ b/deployment/susql-controller/templates/deployment.yaml @@ -27,6 +27,7 @@ spec: - "--kepler-prometheus-url={{ .Values.keplerPrometheusUrl }}" - "--susql-prometheus-database-url={{ .Values.susqlPrometheusDatabaseUrl }}" - "--susql-prometheus-metrics-url={{ .Values.susqlPrometheusMetricsUrl }}" + - "--sampling-rate={{ .Values.samplingRate }}" ports: - name: metrics containerPort: 8082 diff --git a/deployment/susql-controller/values.yaml b/deployment/susql-controller/values.yaml index 74dea21..5275d6c 100644 --- a/deployment/susql-controller/values.yaml +++ b/deployment/susql-controller/values.yaml @@ -22,3 +22,4 @@ requests: keplerPrometheusUrl: "http://prometheus-k8s.monitoring.svc.cluster.local:9090" susqlPrometheusDatabaseUrl: "http://prometheus-susql.openshift-kepler-operator.svc.cluster.local:9090" susqlPrometheusMetricsUrl: "http://0.0.0.0:8082" +samplingRate: "2" diff --git a/internal/controller/labelgroup_controller.go b/internal/controller/labelgroup_controller.go index 998811d..4304f7d 100644 --- a/internal/controller/labelgroup_controller.go +++ b/internal/controller/labelgroup_controller.go @@ -37,12 +37,12 @@ type LabelGroupReconciler struct { KeplerPrometheusUrl string SusQLPrometheusDatabaseUrl string SusQLPrometheusMetricsUrl string + SamplingRate time.Duration // Sampling rate for all the label groups } const ( keplerMetricName = "kepler_container_joules_total" // Kepler metric to query susqlMetricName = "susql_total_energy_joules" // SusQL metric to query - samplingRate = 2 * time.Second // Sampling rate for all the label groups fixingDelay = 15 * time.Second // Time to wait in the even the label group was badly constructed errorDelay = 1 * time.Second // Time to wait when an error happens due to network connectivity issues ) @@ -227,7 +227,7 @@ func (r *LabelGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) r.SetAggregatedEnergyForLabels(totalEnergy, labelGroup.Status.PrometheusLabels) // Requeue - return ctrl.Result{RequeueAfter: samplingRate}, nil + return ctrl.Result{RequeueAfter: r.SamplingRate}, nil default: // First time seeing this object