Skip to content

Commit

Permalink
all: fix grafana datasources and tests for multi wc
Browse files Browse the repository at this point in the history
  • Loading branch information
Elias-elastisys committed Oct 29, 2024
1 parent e3fed87 commit e3f7f45
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
15 changes: 9 additions & 6 deletions helmfile.d/values/grafana/grafana-user.yaml.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ datasources:
If only one workload cluster, don't bother naming the datasource and set to default.
Otherwise set the name of the clusters so users know what they are looking at.
*/}}
{{- if eq (len .Values.global.clustersMonitoring) 1 }}
{{- with first .Values.global.clustersMonitoring }}
{{- if eq (len $.Values.global.clustersMonitoring) 1 }}
- name: "Workload Cluster"
{{- else }}
- name: "Workload Cluster {{ . }}"
{{- end }}
access: proxy
basicAuth: false
editable: false
Expand All @@ -71,11 +75,11 @@ datasources:
url: http://grafana-label-enforcer:9090
version: 1
jsonData:
customQueryParameters: "tenant_id={{ first .Values.global.clustersMonitoring }}"
customQueryParameters: "tenant_id={{ . }}"
manageAlerts: false
{{- else }}
{{- range .Values.global.clustersMonitoring }}
- name: "{{ . }}"
{{- end }}
{{- range rest .Values.global.clustersMonitoring }}
- name: "Workload Cluster {{ . }}"
access: proxy
basicAuth: false
editable: false
Expand All @@ -89,7 +93,6 @@ datasources:
manageAlerts: false
{{- end }}
{{- end }}
{{- end }}

{{ $trailingDot := "" }}
{{- if .Values.grafana.user.trailingDots }}
Expand Down
30 changes: 27 additions & 3 deletions migration/v0.42/apply/10-upgrade-grafana.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,36 @@ run() {
log_info "Deleting Grafana release secrets."
kubectl_do sc delete secrets -n monitoring -l name=ops-grafana,owner=helm
kubectl_do sc delete secrets -n monitoring -l name=user-grafana,owner=helm

helmfile_apply sc app=grafana
else
log_info "Grafana releases are up to date, skipping"
fi

helmfile_apply sc app=grafana
kubectl_do sc wait pod -n monitoring --for=condition=ready -l app.kubernetes.io/instance=user-grafana --timeout=120s

if [[ $(yq4 '.global.clustersMonitoring | length' "${CK8S_CONFIG_PATH}/sc-config.yaml") -gt 1 ]]; then
clusters_monitoring=$(yq4 '[.global.clustersMonitoring[] | {"name": .}]' "${CK8S_CONFIG_PATH}/sc-config.yaml" -ojson)
export clusters_monitoring

user_grafana_cm=$(kubectl_do sc get cm user-grafana -n monitoring -o=jsonpath='{.data.datasources\.yaml}' \
| yq4 '.deleteDatasources = env(clusters_monitoring)' -o json)

PATCH="[
{
'op': 'replace',
'path': '/data/datasources.yaml',
'value': '${user_grafana_cm}'
}
]"
kubectl_do sc patch cm user-grafana -n monitoring --type json --patch "${PATCH}"
kubectl_do sc rollout restart deployment user-grafana -n monitoring
kubectl_do sc rollout status deployment user-grafana -n monitoring

kubectl_do sc delete configmap user-grafana -n monitoring
helmfile_do sc -l name=user-grafana sync

kubectl_do sc rollout restart deployment user-grafana -n monitoring
kubectl_do sc rollout status deployment user-grafana -n monitoring
fi
fi
;;
rollback)
Expand Down
2 changes: 1 addition & 1 deletion tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Container to run unit tests
FROM ubuntu:rolling AS unit
FROM ubuntu:24.04 AS unit
LABEL org.opencontainers.image.source=https://github.com/elastisys/compliantkubernetes-apps

ARG DEBIAN_FRONTEND="noninteractive"
Expand Down
22 changes: 15 additions & 7 deletions tests/end-to-end/grafana/datasources.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("grafana admin datasources", function() {

// Cypress does not like trailing dots
cy.yqDig("sc", ".grafana.ops.trailingDots")
.should("not.equal", "true")
.should((value) => assert(value !== "true", ".grafana.ops.trailingDots in sc config must not be 'true'"))
})

beforeEach(function() {
Expand Down Expand Up @@ -74,7 +74,7 @@ describe("grafana dev datasources", function() {

// Cypress does not like trailing dots
cy.yqDig("sc", ".grafana.user.trailingDots")
.should("not.equal", "true")
.should((value) => assert(value !== "true", ".grafana.ops.trailingDots in sc config must not be 'true'"))
})

beforeEach(function() {
Expand Down Expand Up @@ -109,10 +109,18 @@ describe("grafana dev datasources", function() {
})

it("has workload cluster", function() {
cy.contains("Workload Cluster")
.should("exist")
.siblings()
.contains("default")
.should("exist")
cy.yqDigParse("sc", ".global.clustersMonitoring")
.then(([first_cluster, ...rest_clusters]) => {
cy.contains(`Workload Cluster${rest_clusters.length === 0 ? "" : " " + first_cluster}`)
.should("exist")
.siblings()
.contains("default")
.should("exist")

for (const cluster of rest_clusters) {
cy.contains(`Workload Cluster ${cluster}`)
.should("exist")
}
})
})
})

0 comments on commit e3f7f45

Please sign in to comment.