-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Local Environment Setup] Automatically Aggregate App CRDs Under Graf…
…ana API Server (#446) As part of the `grafana-app-sdk project local generate`, create a new script to aggregate app CRDs under a grafana API server. Additionally, add a tilt step in the new-project generated tilt file to do this aggregation automatically. Resolves #271
- Loading branch information
1 parent
d707bc6
commit 0728611
Showing
8 changed files
with
406 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
cmd/grafana-app-sdk/templates/local/generated/aggregator-access.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: aggregator-access | ||
rules:{{ range $.CRDs }} | ||
- apiGroups: ["{{.Group}}"] | ||
resources: ["*"] | ||
verbs: ["*"]{{ end }} | ||
- nonResourceURLs: | ||
- "/openapi/*"{{ range $.CRDs}} | ||
- "/apis/{{.Group}}/*"{{ end }} | ||
verbs: ["*"] | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: aggregator-unauth-access-rolebinding | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: aggregator-access | ||
subjects: | ||
- apiGroup: rbac.authorization.k8s.io | ||
kind: Group | ||
name: system:unauthenticated |
77 changes: 77 additions & 0 deletions
77
cmd/grafana-app-sdk/templates/local/generated/configure-grafana.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env bash | ||
set -eufo pipefail | ||
|
||
GRAFANA_RUN_MODE="${1}" | ||
GRAFANA_HOST="grafana.k3d.localhost:{{.Port}}" | ||
|
||
if [ $GRAFANA_RUN_MODE == "in_cluster" ]; then | ||
KUBEAPI=($(kubectl run bash-local-env -it --rm --image=bash --restart=Never --command -- "bash" "-c" 'echo "$KUBERNETES_SERVICE_HOST $KUBERNETES_SERVICE_PORT"')) | ||
elif [ $GRAFANA_RUN_MODE == "standalone" ]; then | ||
CONTROLPLANE=($(kubectl cluster-info | grep "control plane")) | ||
ENDPOINT=$(echo ${CONTROLPLANE[${#CONTROLPLANE[@]}-1]} | sed 's~http[s]*://~~g') | ||
IFS=: read -r -a KUBEAPI <<< "$ENDPOINT" | ||
read -p "Grafana API Server URL (host:port): " HOST | ||
fi | ||
HOST=${KUBEAPI[0]} | ||
PORT=${KUBEAPI[1]} | ||
|
||
# Create a service account token to use in our requests (anonymous auth isn't allowed for apiservices) | ||
sa=$(curl "http://${GRAFANA_HOST}/api/serviceaccounts/" \ | ||
--request POST \ | ||
--header "Content-Type: application/json" \ | ||
--header "Accept: application/json" \ | ||
--data '{"name":"Aggregation User","role":"Admin"}' | jq .id) | ||
echo "SA: ${sa}" | ||
token=$(curl "http://${GRAFANA_HOST}/api/serviceaccounts/${sa}/tokens" \ | ||
--request POST \ | ||
--header "Content-Type: application/json" \ | ||
--header "Accept: application/json" \ | ||
--data '{"name":"sa-1-aggregation-user-e2a6a474-bdcd-4bac-8449-0deb7274484a"}'| jq -r '.key') | ||
echo "TOKEN: ${token}" | ||
|
||
{{ range .CRDs }}{{$crd:=.}}{{range .Versions}}curl \ | ||
"http://${GRAFANA_HOST}/apis/apiregistration.k8s.io/v1/apiservices?fieldManager=kubectl-create&fieldValidation=Strict" \ | ||
--request POST \ | ||
--header "Content-Type: application/json" \ | ||
--header "Authorization: Bearer ${token}" \ | ||
--data @- << EOF | ||
{ | ||
"apiVersion": "apiregistration.k8s.io/v1", | ||
"kind": "APIService", | ||
"metadata": { | ||
"name": "{{.}}.{{$crd.Group}}" | ||
}, | ||
"spec": { | ||
"version": "{{.}}", | ||
"insecureSkipTLSVerify": true, | ||
"group": "{{$crd.Group}}", | ||
"groupPriorityMinimum": 1000, | ||
"versionPriority": 15, | ||
"service": { | ||
"name": "example-apiserver", | ||
"namespace": "default", | ||
"port": ${PORT} | ||
} | ||
} | ||
} | ||
EOF | ||
{{ end }}{{end}} | ||
|
||
curl \ | ||
"http://${GRAFANA_HOST}/apis/service.grafana.app/v0alpha1/namespaces/default/externalnames?fieldManager=kubectl-create&fieldValidation=Strict" \ | ||
--request POST \ | ||
--header "Content-Type: application/json" \ | ||
--header "Authorization: Bearer ${token}" \ | ||
--data @- << EOF | ||
{ | ||
"apiVersion": "service.grafana.app/v0alpha1", | ||
"kind": "ExternalName", | ||
"metadata": { | ||
"name": "example-apiserver", | ||
"namespace": "default" | ||
}, | ||
"spec": { | ||
"host": "${HOST}" | ||
} | ||
} | ||
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.