From 0d92a487e98e63a4383deef95612987de3e99a3c Mon Sep 17 00:00:00 2001
From: Rob Ferguson <rob.ferguson@defenseunicorns.com>
Date: Tue, 19 Apr 2022 14:03:04 +0000
Subject: [PATCH] Loki strategy switching SKIP UPGRADE

---
 .../fluentbit/fluentbit-helmrelease.yaml      |  4 +++
 chart/templates/logging/fluentbit/values.yaml |  4 +++
 .../logging/loki/loki-helmrelease.yaml        |  7 ++++
 chart/templates/logging/loki/values.yaml      | 36 ++++++++++++++++---
 chart/templates/logging/promtail/values.yaml  |  4 +++
 chart/templates/monitoring/values.yaml        |  4 +++
 chart/values.yaml                             | 24 ++++++++++++-
 tests/rke2-test-values.yaml                   | 10 ++++++
 tests/test-values.yaml                        | 29 +++++++++++++--
 9 files changed, 115 insertions(+), 7 deletions(-)

diff --git a/chart/templates/logging/fluentbit/fluentbit-helmrelease.yaml b/chart/templates/logging/fluentbit/fluentbit-helmrelease.yaml
index cc6986a8da..7bff2ce70c 100644
--- a/chart/templates/logging/fluentbit/fluentbit-helmrelease.yaml
+++ b/chart/templates/logging/fluentbit/fluentbit-helmrelease.yaml
@@ -44,6 +44,10 @@ spec:
     - name: ek
       namespace: {{ .Release.Namespace }}
     {{- end }}
+    {{- if  .Values.loki.enabled }}
+    - name: loki
+      namespace: {{ .Release.Namespace }}
+    {{- end }}
     {{- if .Values.gatekeeper.enabled }}
     - name: gatekeeper
       namespace: {{ .Release.Namespace }}
diff --git a/chart/templates/logging/fluentbit/values.yaml b/chart/templates/logging/fluentbit/values.yaml
index f474fe106f..a3c3a22a37 100644
--- a/chart/templates/logging/fluentbit/values.yaml
+++ b/chart/templates/logging/fluentbit/values.yaml
@@ -67,7 +67,11 @@ config:
         name                   loki
         match                  kube.*
         labels                 job=fluentbit, container=$kubernetes['container_name'], pod=$kubernetes['pod_name'], namespace=$kubernetes['namespace_name'], node_name=$kubernetes['host']
+        {{- if eq .Values.loki.strategy "monolith" }}
         host                   logging-loki
+        {{- else }}
+        host                   logging-loki-write
+        {{- end }}
         port                   3100
         auto_kubernetes_labels on
         Retry_Limit            False
diff --git a/chart/templates/logging/loki/loki-helmrelease.yaml b/chart/templates/logging/loki/loki-helmrelease.yaml
index 8f7e88c70c..ce498c0944 100644
--- a/chart/templates/logging/loki/loki-helmrelease.yaml
+++ b/chart/templates/logging/loki/loki-helmrelease.yaml
@@ -10,6 +10,7 @@ metadata:
     app.kubernetes.io/component: "core"
     {{- include "commonLabels" . | nindent 4}}
 spec:
+  releaseName: {{ default "loki" .Values.loki.releaseName }}
   targetNamespace: logging
   chart:
     spec:
@@ -54,5 +55,11 @@ spec:
     - name: kyvernopolicies
       namespace: {{ .Release.Namespace }}
     {{- end }}
+    {{- with .Values.loki.objectStorage }}
+    {{- if not (and .endpoint .accessKey .accessSecret .bucketNames) }}
+    - name: minio-operator
+      namespace: {{ $.Release.Namespace }}
+    {{- end }}
+    {{- end }}
   {{- end }}
 {{- end }}
\ No newline at end of file
diff --git a/chart/templates/logging/loki/values.yaml b/chart/templates/logging/loki/values.yaml
index 914927dbde..8e70521a73 100644
--- a/chart/templates/logging/loki/values.yaml
+++ b/chart/templates/logging/loki/values.yaml
@@ -17,19 +17,47 @@ image:
   pullPolicy: {{ .Values.imagePullPolicy }}
 
 networkPolicies:
-  enabled: {{ .Values.networkPolicies.enabled }}  
-  
+  enabled: {{ .Values.networkPolicies.enabled }}
+
 monitoring:
   enabled: {{ .Values.monitoring.enabled }}
 
 istio:
   enabled: {{ .Values.istio.enabled }} 
 
-serviceMonitor:
-  enabled: {{ .Values.monitoring.enabled }}
+global:
+  createGlobalConfig: true
+  existingSecretForConfig: loki-config
+
+  objectStorage:
+    endpoint: {{ .Values.loki.objectStorage.endpoint }}
+    region: {{ .Values.loki.objectStorage.region }}
+    bucketnames: {{ .Values.loki.objectStorage.bucketNames }}
+    access_key_id: {{ .Values.loki.objectStorage.accessKey }}
+    secret_access_key: {{ .Values.loki.objectStorage.accessSecret }}
+
+{{- with .Values.loki.objectStorage }}
+{{- if not (and .endpoint .bucketNames .accessKey .accessSecret) }}
+minio:
+  enabled: true
+{{- end }}
+{{- end }}
 
 {{- if .Values.istio.enabled }}
 podAnnotations:
   {{ include "istioAnnotation" . }}
 {{- end }}
+
+loki:
+  enabled: {{ eq .Values.loki.strategy "monolith" }}
+  serviceMonitor:
+    enabled: {{ .Values.monitoring.enabled }}
+
+loki-simple-scalable:
+  enabled: {{ eq .Values.loki.strategy "scalable" }}
+  imagePullSecrets:
+    - name: private-registry
+  serviceMonitor:
+    enabled: {{ .Values.monitoring.enabled }}
+
 {{- end -}}
diff --git a/chart/templates/logging/promtail/values.yaml b/chart/templates/logging/promtail/values.yaml
index 392396cbfa..146dc2ee58 100644
--- a/chart/templates/logging/promtail/values.yaml
+++ b/chart/templates/logging/promtail/values.yaml
@@ -32,6 +32,10 @@ imagePullSecrets:
 
 {{- if .Values.loki.enabled }}
 config:
+  {{- if eq .Values.loki.strategy "monolith" }}
   lokiAddress: http://logging-loki.logging.svc.cluster.local:3100/loki/api/v1/push
+  {{- else }}
+  lokiAddress: http://logging-loki-write.logging.svc.cluster.local:3100/loki/api/v1/push
+  {{- end }}
 {{- end }}
 {{- end -}}
diff --git a/chart/templates/monitoring/values.yaml b/chart/templates/monitoring/values.yaml
index c92ae1e996..e73ac3c635 100644
--- a/chart/templates/monitoring/values.yaml
+++ b/chart/templates/monitoring/values.yaml
@@ -215,7 +215,11 @@ grafana:
   {{- if .Values.loki.enabled }}
   - name: Loki
     type: loki
+    {{- if eq .Values.loki.strategy "monolith" }}
     url: http://logging-loki.logging.svc.cluster.local:3100
+    {{- else }}
+    url: http://logging-loki-read.logging.svc.cluster.local:3100
+    {{- end }}
     access: proxy
   {{- end }}
   {{- if and .Values.loki.enabled .Values.tempo.enabled }}
diff --git a/chart/values.yaml b/chart/values.yaml
index f96e885816..995a9b2ea8 100644
--- a/chart/values.yaml
+++ b/chart/values.yaml
@@ -507,7 +507,7 @@ loki:
   git:
     repo: https://repo1.dso.mil/platform-one/big-bang/apps/sandbox/loki.git
     path: "./chart"
-    tag: "2.10.1-bb.4"
+    tag: "3.0.1-bb.0"
 
   # -- Flux reconciliation overrides specifically for the Loki Package
   flux: {}
@@ -517,6 +517,28 @@ loki:
 
   # -- Post Renderers.  See docs/postrenders.md
   postRenderers: []
+
+  # -- Loki architecture.  Options are monolith and scalable
+  strategy: monolith
+
+  objectStorage:
+    # -- S3 compatible endpoint to use for connection information.
+    # examples: "https://s3.amazonaws.com" "https://s3.us-gov-west-1.amazonaws.com" "http://minio.minio.svc.cluster.local:9000"
+    endpoint: ""
+
+    # -- S3 compatible region to use for connection information.
+    region: ""
+
+    # -- Access key for connecting to object storage endpoint.
+    accessKey: ""
+
+    # -- Secret key for connecting to object storage endpoint.
+    # Unencoded string data. This should be placed in the secret values and then encrypted
+    accessSecret: ""
+
+    # -- Bucket Names for Loki as a comma delimited list.
+    # examples: "loki-logs"
+    bucketNames: ""
 # ----------------------------------------------------------------------------------------------------------------------
 
 # ----------------------------------------------------------------------------------------------------------------------
diff --git a/tests/rke2-test-values.yaml b/tests/rke2-test-values.yaml
index 494754394b..a820342e7d 100644
--- a/tests/rke2-test-values.yaml
+++ b/tests/rke2-test-values.yaml
@@ -50,6 +50,16 @@ logging:
             cpu: .5
           limits: null
 
+loki:
+  values:
+    loki-simple-scalable:
+      write:
+        resources:
+          limits: null
+      read:
+        resources:
+          limits: null
+
 tempo:
   values:
     tempo:
diff --git a/tests/test-values.yaml b/tests/test-values.yaml
index 37dcc5b468..dd09a5b3ef 100644
--- a/tests/test-values.yaml
+++ b/tests/test-values.yaml
@@ -460,9 +460,34 @@ fluentbit:
 
 loki:
   enabled: false
+  strategy: scalable
   values:
-    persistence:
-      enabled: true
+    global:
+      createGlobalConfig: true 
+      existingSecretForConfig: "loki-config"
+    loki-simple-scalable:
+      write:
+        replicas: 1
+        persistence:
+          size: 2Gi
+        resources: 
+          limits:
+            cpu: 200m
+            memory: 400Mi 
+          requests:
+            cpu: 200m
+            memory: 400Mi
+      read:
+        replicas: 1
+        persistence:
+          size: 2Gi
+        resources: 
+          limits:
+            cpu: 200m
+            memory: 400Mi 
+          requests:
+            cpu: 200m
+            memory: 400Mi 
 
 tempo:
   enabled: false
-- 
GitLab