-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into pr-checklist
- Loading branch information
Showing
15 changed files
with
814 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# ------------------------------------------------------------ | ||
# Copyright 2023 The Radius Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ------------------------------------------------------------ | ||
|
||
POSTGRES_USER=postgres | ||
POSTGRES_PASSWORD=ucprulz! | ||
POSTGRES_IMAGE=ghcr.io/radius-project/mirror/postgres:latest | ||
POSTGRES_CONTAINER_NAME=radius-postgres | ||
|
||
##@ Database | ||
|
||
.PHONY: db-init | ||
db-init: db-dependency-docker-running ## Initialize a local PostgresSQL database for testing | ||
@echo "$(ARROW) Initializing local PostgresSQL database" | ||
@if [ "$$( docker container inspect -f '{{.State.Running}}' $(POSTGRES_CONTAINER_NAME) 2> /dev/null)" = "true" ]; then \ | ||
echo "PostgresSQL container $(POSTGRES_CONTAINER_NAME) is already runnning"; \ | ||
elif [ "$$( docker container inspect -f '{{.State.Running}}' $(POSTGRES_CONTAINER_NAME) 2> /dev/null)" = "false" ]; then \ | ||
echo "PostgresSQL container $(POSTGRES_CONTAINER_NAME) is not running"; \ | ||
echo "This might have been a crash"; \ | ||
echo ""; \ | ||
docker logs $(POSTGRES_CONTAINER_NAME); \ | ||
echo ""; \ | ||
echo "Restarting PostgresSQL container $(POSTGRES_CONTAINER_NAME)" \ | ||
docker start $(POSTGRES_CONTAINER_NAME) 1> /dev/null; \ | ||
else \ | ||
docker run \ | ||
--detach \ | ||
--name $(POSTGRES_CONTAINER_NAME) \ | ||
--publish 5432:5432 \ | ||
--env POSTGRES_USER=$(POSTGRES_USER) \ | ||
--env POSTGRES_PASSWORD=$(POSTGRES_PASSWORD) \ | ||
--volume $(PWD)/deploy/init-db/:/docker-entrypoint-initdb.d/ \ | ||
$(POSTGRES_IMAGE) 1> /dev/null; \ | ||
echo "Started PostgresSQL container $(POSTGRES_CONTAINER_NAME)"; \ | ||
fi; | ||
@echo "" | ||
@echo "Use PostgreSQL in tests:" | ||
@echo "" | ||
@echo "export TEST_POSTGRES_URL=postgresql://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@localhost:5432/ucp" | ||
@echo "" | ||
@echo "Makefile cheatsheet:" | ||
@echo " - Stop the database $(ARROW) make db-stop" | ||
@echo " - Reset the database $(ARROW) make db-reset" | ||
@echo " - Logs $(ARROW) docker logs $(POSTGRES_CONTAINER_NAME)" | ||
@echo " - Connect to the database server: make db-shell" | ||
@echo " - Shell tip: Connect to UCP database $(ARROW) \\\\c ucp" | ||
@echo " - Shell tip: Connect to applications_rp database $(ARROW) \\\\c applications_rp" | ||
@echo " - Shell tip: List resources $(ARROW) select * from resources;" | ||
|
||
.PHONY: db-stop | ||
db-stop: db-dependency-docker-running ## Stop the local PostgresSQL database | ||
@echo "$(ARROW) Stopping local PostgresSQL database..." | ||
@if [ "$$( docker container inspect -f '{{.State.Running}}' $(POSTGRES_CONTAINER_NAME) 2> /dev/null)" = "true" ]; then \ | ||
docker stop $(POSTGRES_CONTAINER_NAME) 1> /dev/null; \ | ||
else \ | ||
echo "PostgresSQL container $(POSTGRES_CONTAINER_NAME) is not running"; \ | ||
fi; | ||
|
||
.PHONY: db-shell | ||
db-shell: db-postgres-running ## Open a shell to the local PostgresSQL database | ||
@echo "$(ARROW) Connecting to local PostgresSQL database..." | ||
@DOCKER_CLI_HINTS=false docker exec \ | ||
--interactive \ | ||
--tty \ | ||
$(POSTGRES_CONTAINER_NAME) \ | ||
psql \ | ||
"postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@localhost:5432" | ||
|
||
.PHONY: db-reset | ||
db-reset: db-postgres-running ## Reset the local PostgresSQL database | ||
@echo "$(ARROW) Resetting local PostgresSQL database" | ||
@echo "" | ||
@echo "Resetting ucp resources..." | ||
@DOCKER_CLI_HINTS=false docker exec \ | ||
--interactive \ | ||
--tty \ | ||
$(POSTGRES_CONTAINER_NAME) \ | ||
psql \ | ||
"postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@localhost:5432/ucp" \ | ||
--command "DELETE FROM resources;" | ||
@echo "" | ||
@echo "Resetting applications_rp resources..." | ||
@DOCKER_CLI_HINTS=false docker exec \ | ||
--interactive \ | ||
--tty \ | ||
$(POSTGRES_CONTAINER_NAME) \ | ||
psql \ | ||
"postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@localhost:5432/applications_rp" \ | ||
--command "DELETE FROM resources;" | ||
|
||
.PHONY: db-dependency-docker-running | ||
db-dependency-docker-running: | ||
@if [ ! docker info > /dev/null 2>&1 ]; then \ | ||
echo "Docker is not installed or not running. Please install docker and try again."; \ | ||
exit 1; \ | ||
fi; | ||
|
||
.PHONY: db-postgres-running | ||
db-postgres-running: db-dependency-docker-running | ||
@if [ "$$( docker container inspect -f '{{.State.Running}}' $(POSTGRES_CONTAINER_NAME) 2> /dev/null)" = "true" ]; then \ | ||
exit 0; \ | ||
else \ | ||
echo "PostgresSQL container $(POSTGRES_CONTAINER_NAME) is not running"; \ | ||
exit 1; \ | ||
fi; |
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,66 @@ | ||
-- 'resources' is used to store all of our resources. See comments below for an explanation of the columns. | ||
CREATE TABLE resources ( | ||
-- example: "/planes/radius/local/resourceGroups/rg1/providers/Applications.Core/applications/my-app" | ||
-- | ||
-- We use columns to break out the most important components of the resource id for optimal querying. | ||
-- | ||
-- Since resource ids are case-insensitive we canonicalize these columns to lowercase. | ||
-- We store the original resource id with the original casing so users can work with their preferred | ||
-- naming/casing conventions. | ||
-- | ||
-- We ensure a leading and trailing slash on the components of the resource id for ease of comparison. | ||
-- | ||
-- id -> "/planes/radius/local/resourcegroups/rg1/providers/applications.core/applications/my-app/" | ||
-- resource_type -> "/applications.core/applications/" | ||
-- root_scope -> "/planes/radius/local/resourcegroups/rg1/" | ||
-- routing_scope -> "/applications.core/applications/my-app/" | ||
|
||
-- resource id used as key. | ||
id TEXT PRIMARY KEY NOT NULL, | ||
|
||
-- original_id is used to store the original id of the resource before any normalization occurs. | ||
-- This is provided for compatability with the existing design of the store API, and can be removed | ||
-- in the future. | ||
original_id TEXT NOT NULL, | ||
|
||
-- resource type by queries to filter by type. | ||
resource_type TEXT NOT NULL, | ||
|
||
-- root_scope used by queries to list resources by their scope. | ||
root_scope TEXT NOT NULL, | ||
|
||
-- routing_scope used by queries to list resources when they are child resources. | ||
routing_scope TEXT NOT NULL, | ||
|
||
-- etag used for optimistic concurrency control. | ||
etag TEXT NOT NULL, | ||
|
||
-- timestamp is used to implement cursor-based pagination (see below). | ||
created_at TIMESTAMP (6) WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, | ||
|
||
-- resource_data stores the resource data. | ||
resource_data JSONB NOT NULL | ||
); | ||
|
||
-- idx_resource_query is an index for improving performance of queries. | ||
-- | ||
-- Queries always list resources by their: | ||
-- - resource_type, and root_scope OR | ||
-- - resource_type, root_scope, and (LIKE) routing_scope | ||
-- | ||
-- eg: "find all Applications.Core/applications resources in /planes/radius/local/resourceGroups/my-rg" | ||
-- | ||
-- > "resource_type" = "/applications.core/applications/" | ||
-- > "root_scope" = "/planes/radius/local/resourcegroups/my-rg" | ||
-- > "routing_scope" = NULL | ||
-- | ||
-- 'created_at' is used with ORDER BY to sort the output, so we can implement cursor-based pagination. | ||
-- | ||
-- 1) For the initial query, we won't specify a cursor value. | ||
-- 2) For the next query, we will specify the cursor value as the last created_at value from the previous | ||
-- query, which allows us to skip the records that were already returned. | ||
-- | ||
-- The index only contains resource_type and root_scope because these are usually specified exactly. | ||
-- We don't really benefit from routing_scope being in the index because it's always used with LIKE. | ||
-- We don't benefit from created_at being in the index because it's used for sorting. | ||
CREATE INDEX idx_resource_query ON resources (resource_type, root_scope); |
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,23 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
|
||
# Array of usernames | ||
RESOURCE_PROVIDERS=("ucp" "applications_rp") | ||
|
||
# Create databases and users | ||
for RESOURCE_PROVIDER in "${RESOURCE_PROVIDERS[@]}"; do | ||
echo "Creating database and user for $RESOURCE_PROVIDER" | ||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL | ||
CREATE USER $RESOURCE_PROVIDER WITH PASSWORD '$POSTGRES_PASSWORD'; | ||
CREATE DATABASE $RESOURCE_PROVIDER; | ||
GRANT ALL PRIVILEGES ON DATABASE $RESOURCE_PROVIDER TO $RESOURCE_PROVIDER; | ||
EOSQL | ||
done | ||
|
||
# Create tables within those databases | ||
for RESOURCE_PROVIDER in "${RESOURCE_PROVIDERS[@]}"; do | ||
echo "Creating tables in database $RESOURCE_PROVIDER" | ||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$RESOURCE_PROVIDER" < $SCRIPT_DIR/db.sql.txt | ||
done |
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.