Skip to content

Commit 8b9ab61

Browse files
committed
feat: support grafana v10 and v11
1 parent 6027059 commit 8b9ab61

7 files changed

Lines changed: 80 additions & 44 deletions

File tree

stackgres-k8s/e2e/spec/dbops-restart-after-upgrade

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ e2e_test_install() {
6161
PREVIOUS_VERSION_AS_NUMBER="$(get_version_as_number "$STACKGRES_PREVIOUS_VERSION")"
6262
VERSION_AS_NUMBER_0_9_5="$(get_version_as_number 0.9.5)"
6363
VERSION_AS_NUMBER_1_3_3="$(get_version_as_number 1.3.3)"
64-
if [ "$PREVIOUS_VERSION_AS_NUMBER" -gt "$VERSION_AS_NUMBER_1_3_3" ]
64+
VERSION_AS_NUMBER_1_15_0="$(get_version_as_number 1.15.0-SNAPSHOT)"
65+
if [ "$PREVIOUS_VERSION_AS_NUMBER" -gt "$VERSION_AS_NUMBER_1_15_0" ]
6566
then
6667
install_prometheus_operator
68+
elif [ "$PREVIOUS_VERSION_AS_NUMBER" -gt "$VERSION_AS_NUMBER_1_3_3" ]
69+
then
70+
E2E_GRAFANA_VERSION=9.5.21 install_prometheus_operator
6771
elif [ "$PREVIOUS_VERSION_AS_NUMBER" -gt "$VERSION_AS_NUMBER_0_9_5" ]
6872
then
6973
E2E_GRAFANA_VERSION=8.5.13 install_prometheus_operator

stackgres-k8s/e2e/spec/dbops-security-upgrade

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ e2e_test_install() {
8787

8888
e2e_load_images
8989

90-
if [ "$PREVIOUS_VERSION_AS_NUMBER" -gt "$VERSION_AS_NUMBER_1_3_3" ]
90+
if [ "$PREVIOUS_VERSION_AS_NUMBER" -gt "$VERSION_AS_NUMBER_1_15_0" ]
9191
then
9292
install_prometheus_operator
93+
elif [ "$PREVIOUS_VERSION_AS_NUMBER" -gt "$VERSION_AS_NUMBER_1_3_3" ]
94+
then
95+
E2E_GRAFANA_VERSION=9.5.21 install_prometheus_operator
9396
elif [ "$PREVIOUS_VERSION_AS_NUMBER" -gt "$VERSION_AS_NUMBER_0_9_5" ]
9497
then
9598
E2E_GRAFANA_VERSION=8.5.13 install_prometheus_operator

stackgres-k8s/e2e/utils/prometheus-operator

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ install_prometheus_operator_for_version() {
5454
--set coreDns.enabled=false \
5555
--set kubeControllerManager.enabled=false \
5656
--set kubeEtcd.enabled=false \
57-
--set-string grafana.image.tag="${E2E_GRAFANA_VERSION:-9.1.6}" \
57+
--set-string grafana.image.tag="${E2E_GRAFANA_VERSION:-11.5.2}" \
5858
--set-string prometheus.prometheusSpec.podMonitorNamespaceSelector.matchLabels.monitoring=true \
5959
"$@"
6060

stackgres-k8s/src/common/src/main/java/io/stackgres/common/WebUtil.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.security.cert.X509Certificate;
1414
import java.util.Map;
1515
import java.util.Optional;
16+
import java.util.function.Function;
17+
import java.util.function.Predicate;
1618

1719
import javax.net.ssl.HostnameVerifier;
1820
import javax.net.ssl.SSLContext;
@@ -28,8 +30,6 @@
2830

2931
public interface WebUtil {
3032

31-
int BAD_REQUEST_STATUS_CODE = Integer.valueOf(Response.Status.BAD_REQUEST.getStatusCode()).intValue();
32-
3333
static boolean checkUri(String uri, Map<String, Object> headers) {
3434
try {
3535
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
@@ -48,7 +48,11 @@ static boolean checkUri(String uri, Map<String, Object> headers) {
4848
}
4949
}
5050

51-
static Optional<Exception> checkUnsecureUri(String uri, Map<String, Object> headers) {
51+
static Optional<Exception> checkUnsecureUri(
52+
String uri,
53+
Map<String, Object> headers,
54+
Predicate<Response> predicate,
55+
Function<Response, Exception> errorMapper) {
5256
try {
5357
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
5458
try (Closer closer = Closer.create()) {
@@ -61,9 +65,9 @@ static Optional<Exception> checkUnsecureUri(String uri, Map<String, Object> head
6165
.headers(new MultivaluedHashMap<>(headers))
6266
.buildGet()
6367
.invoke();
64-
return Optional.of(response.getStatus())
65-
.filter(statusCode -> statusCode.intValue() >= BAD_REQUEST_STATUS_CODE)
66-
.map(status -> new Exception("Invalid status code " + status));
68+
return Optional.of(response)
69+
.filter(predicate)
70+
.map(errorMapper);
6771
}
6872
} catch (IOException | IllegalArgumentException | ProcessingException
6973
| KeyManagementException | NoSuchAlgorithmException ex) {

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/config/context/ConfigGrafanaIntegrationChecker.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
import io.stackgres.common.crd.sgconfig.StackGresConfigStatus;
1616
import io.stackgres.common.crd.sgconfig.StackGresConfigStatusGrafana;
1717
import jakarta.enterprise.context.ApplicationScoped;
18+
import jakarta.ws.rs.core.Response;
1819
import org.slf4j.Logger;
1920
import org.slf4j.LoggerFactory;
2021

2122
@ApplicationScoped
2223
public class ConfigGrafanaIntegrationChecker {
2324

25+
private static final int MULTIPLE_CHOICES_STATUS_CODE =
26+
Integer.valueOf(Response.Status.MULTIPLE_CHOICES.getStatusCode()).intValue();
27+
2428
protected static final Logger LOGGER = LoggerFactory
2529
.getLogger(ConfigGrafanaIntegrationChecker.class);
2630

@@ -61,8 +65,11 @@ public boolean isGrafanaIntegrated(StackGresConfig config) {
6165
}
6266

6367
protected Optional<Exception> checkUnsecureUri(StackGresConfigStatusGrafana grafana, String url) {
64-
return WebUtil.checkUnsecureUri(url.substring(url.indexOf(":") + 1),
65-
Map.of("Authorization", "Bearer " + grafana.getToken()));
68+
return WebUtil.checkUnsecureUri(
69+
url.substring(url.indexOf(":") + 1),
70+
Map.of("Authorization", "Bearer " + grafana.getToken()),
71+
response -> response.getStatus() >= MULTIPLE_CHOICES_STATUS_CODE,
72+
response -> new Exception("Invalid status code " + response.getStatus()));
6673
}
6774

6875
}

stackgres-k8s/src/operator/src/main/resources/webconsole/integrate-grafana.sh

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,44 @@ do
3030
}
3131
EOF
3232
GRAFANA_DASHBOARD_URL="$(curl_grafana_api -d "@/tmp/grafana-dashboard-import.json" "$GRAFANA_HOST/api/dashboards/db" | jq -M -r .url)"
33-
[ -n "$GRAFANA_DASHBOARD_URL" ]
34-
[ "$GRAFANA_DASHBOARD_URL" != null ]
33+
if [ "x$GRAFANA_DASHBOARD_URL" = x ] \
34+
|| [ "x$GRAFANA_DASHBOARD_URL" = xnull ]
35+
then
36+
echo "Can not retrieve imported grafana dashboard URL for dasboard $DASHBOARD"
37+
exit 1
38+
fi
3539
GRAFANA_DASHBOARD_URL="${DASHBOARD%.json}:$GRAFANA_HOST/${GRAFANA_DASHBOARD_URL#*/}"
36-
if [ -z "$GRAFANA_DASHBOARD_URLS" ]
40+
if [ "x$GRAFANA_DASHBOARD_URLS" = x ]
3741
then
3842
GRAFANA_DASHBOARD_URLS="$GRAFANA_DASHBOARD_URL"
3943
else
4044
GRAFANA_DASHBOARD_URLS="$GRAFANA_DASHBOARD_URLS $GRAFANA_DASHBOARD_URL"
4145
fi
4246
done
43-
GRAFANA_API_KEY_ID="$(curl_grafana_api "$GRAFANA_HOST/api/auth/keys" | jq -r '.[]|select(.name == "stackgres")|.id|select(.!=null)')"
44-
[ -z "$GRAFANA_API_KEY_ID" ] || curl_grafana_api -X DELETE "$GRAFANA_HOST/api/auth/keys/$GRAFANA_API_KEY_ID" > /dev/null
45-
GRAFANA_API_KEY_TOKEN="$(curl_grafana_api -d '{"name":"stackgres", "role": "Viewer"}' "$GRAFANA_HOST/api/auth/keys" | jq -r .key)"
46-
[ -n "$GRAFANA_API_KEY_TOKEN" ]
47+
if curl_grafana_api "$GRAFANA_HOST/api/serviceaccounts/search?query=stackgres" > /dev/null 2>&1
48+
then
49+
GRAFANA_API_SA_ID="$(curl_grafana_api "$GRAFANA_HOST/api/serviceaccounts/search?query=stackgres")"
50+
GRAFANA_API_SA_ID="$(printf %s "$GRAFANA_API_SA_ID" | jq -r '.serviceAccounts[]|select(.name == "stackgres")|.id|select(.!=null)')"
51+
[ "x$GRAFANA_API_SA_ID" = x ] || curl_grafana_api -X DELETE "$GRAFANA_HOST/api/serviceaccounts/$GRAFANA_API_SA_ID"
52+
GRAFANA_API_SA_ID="$(curl_grafana_api -d '{"name":"stackgres", "role": "Viewer", "isDisable": false}' "$GRAFANA_HOST/api/serviceaccounts" | jq -r .id)"
53+
GRAFANA_TOKEN="$(curl_grafana_api -d '{"name":"stackgres"}' "$GRAFANA_HOST/api/serviceaccounts/$GRAFANA_API_SA_ID/tokens")"
54+
GRAFANA_TOKEN="$(printf %s "$GRAFANA_TOKEN" | jq -r .key)"
55+
else
56+
GRAFANA_API_KEY_ID="$(curl_grafana_api "$GRAFANA_HOST/api/auth/keys")"
57+
GRAFANA_API_KEY_ID="$(printf %s "$GRAFANA_API_KEY_ID" | jq -r '.[]|select(.name == "stackgres")|.id|select(.!=null)')"
58+
[ "x$GRAFANA_API_KEY_ID" = x ] || curl_grafana_api -X DELETE "$GRAFANA_HOST/api/auth/keys/$GRAFANA_API_KEY_ID"
59+
GRAFANA_TOKEN="$(curl_grafana_api -d '{"name":"stackgres", "role": "Viewer"}' "$GRAFANA_HOST/api/auth/keys")"
60+
GRAFANA_TOKEN="$(printf %s "$GRAFANA_TOKEN" | jq -r .key)"
61+
fi
62+
if [ "x$GRAFANA_TOKEN" = x ]
63+
then
64+
echo "Can not retrieve grafana token"
65+
exit 1
66+
fi
4767
until kubectl get sgconfig -n "$SGCONFIG_NAMESPACE" "$OPERATOR_NAME" -o json \
4868
| jq ".
4969
| .status.grafana.urls = $(printf %s "$GRAFANA_DASHBOARD_URLS" | tr ' ' '\n' | jq -R . | jq -s .)
50-
| .status.grafana.token = \"$GRAFANA_API_KEY_TOKEN\"
70+
| .status.grafana.token = \"$GRAFANA_TOKEN\"
5171
| .status.grafana.configHash = \"$GRAFANA_CONFIG_HASH\"" \
5272
| kubectl replace --raw /apis/stackgres.io/v1/namespaces/"$SGCONFIG_NAMESPACE"/sgconfigs/"$OPERATOR_NAME"/status -f -
5373
do

stackgres-k8s/src/operator/src/main/resources/webconsole/stackgres-restapi.template

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,31 @@ server {
1616
root /opt/app-root/src;
1717
index index.html;
1818

19-
location /admin/ {
19+
location ~ ^(/|/admin|/admin/.*)\$ {
2020
try_files \$uri \$uri/index.html /admin/index.html;
2121
}
2222

23-
location ~ ^(/|/admin|/admin/)\$ {
23+
location ~ ^(/|/stackgres|/stackgres/)\$ {
2424
return 302 "\$scheme://\$http_host/admin/index.html";
2525
}
2626

27-
location /grafana-list {
27+
location ~ ^/stackgres {
28+
proxy_redirect off;
29+
proxy_http_version 1.1;
30+
proxy_set_header Host \$host;
31+
proxy_set_header X-Real-IP \$remote_addr;
32+
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
33+
proxy_set_header X-Forwarded-Proto \$scheme;
34+
proxy_set_header X-Forwarded-Host \$host;
35+
proxy_set_header X-Forwarded-Port \$my_forwarded_port;
36+
proxy_set_header X-Forwarded-Server \$host;
37+
proxy_buffers 4 256k;
38+
proxy_buffer_size 128k;
39+
proxy_busy_buffers_size 256k;
40+
proxy_pass http://localhost:8080;
41+
}
42+
43+
location ~ ^/grafana-list\$ {
2844
if (\$grafana_embedded != true) {
2945
return 404;
3046
}
@@ -45,7 +61,7 @@ server {
4561
)]";
4662
}
4763

48-
location /grafana {
64+
location ~ ^/grafana\$ {
4965
if (\$grafana_embedded != true) {
5066
return 404;
5167
}
@@ -63,30 +79,12 @@ server {
6379
)";
6480
}
6581

66-
location ~ ^(/|/stackgres|/stackgres/)\$ {
67-
return 302 "\$scheme://\$http_host/admin/index.html";
68-
}
69-
70-
location ~ ^/stackgres {
71-
proxy_redirect off;
72-
proxy_http_version 1.1;
73-
proxy_set_header Host \$host;
74-
proxy_set_header X-Real-IP \$remote_addr;
75-
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
76-
proxy_set_header X-Forwarded-Proto \$scheme;
77-
proxy_set_header X-Forwarded-Host \$host;
78-
proxy_set_header X-Forwarded-Port \$my_forwarded_port;
79-
proxy_set_header X-Forwarded-Server \$host;
80-
proxy_buffers 4 256k;
81-
proxy_buffer_size 128k;
82-
proxy_busy_buffers_size 256k;
83-
proxy_pass http://localhost:8080;
84-
}
85-
86-
location / {
82+
location ~ ^(/.*|/grafana/.*)\$ {
8783
if (\$grafana_embedded != true) {
8884
return 404;
8985
}
86+
rewrite ^/grafana/(.*) /\$1 break;
87+
proxy_redirect / /grafana/;
9088
proxy_set_header Host \$host;
9189
proxy_set_header X-Real-IP \$remote_addr;
9290
proxy_set_header X-Forwarded-Host \$host;

0 commit comments

Comments
 (0)