Skip to content

Commit

Permalink
Merge branch 'main' into pr-checklist
Browse files Browse the repository at this point in the history
  • Loading branch information
kachawla authored Sep 30, 2024
2 parents 250aae7 + 223ec1e commit 31bf25d
Show file tree
Hide file tree
Showing 15 changed files with 814 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/contributor/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"golang.go",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-azuretools.rad-vscode-bicep",
"ms-azuretools.vscode-bicep",
"ms-kubernetes-tools.vscode-kubernetes-tools",
"ms-azuretools.vscode-dapr",
"ms-vscode.makefile-tools"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
ARROW := \033[34;1m=>\033[0m

# order matters for these
include build/help.mk build/version.mk build/build.mk build/util.mk build/generate.mk build/test.mk build/docker.mk build/recipes.mk build/install.mk build/debug.mk
include build/help.mk build/version.mk build/build.mk build/util.mk build/generate.mk build/test.mk build/docker.mk build/recipes.mk build/install.mk build/db.mk build/debug.mk
117 changes: 117 additions & 0 deletions build/db.mk
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;
66 changes: 66 additions & 0 deletions deploy/init-db/db.sql.txt
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);
23 changes: 23 additions & 0 deletions deploy/init-db/init-db.sh
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
16 changes: 10 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/cloudcontrol v1.20.5
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.53.5
github.com/aws/aws-sdk-go-v2/service/ec2 v1.177.0
github.com/aws/aws-sdk-go-v2/service/ecr v1.32.4
github.com/aws/aws-sdk-go-v2/service/sts v1.30.5
github.com/aws/smithy-go v1.20.4
github.com/charmbracelet/bubbles v0.19.0
Expand Down Expand Up @@ -50,6 +51,7 @@ require (
github.com/hashicorp/hc-install v0.8.0
github.com/hashicorp/terraform-config-inspect v0.0.0-20240607080351-271db412dbcb
github.com/hashicorp/terraform-exec v0.21.0
github.com/jackc/pgx/v5 v5.7.1
github.com/mattn/go-isatty v0.0.20
github.com/mitchellh/mapstructure v1.5.0
github.com/novln/docker-parser v1.0.0
Expand Down Expand Up @@ -80,7 +82,7 @@ require (
go.uber.org/mock v0.4.0
go.uber.org/zap v1.27.0
golang.org/x/sync v0.8.0
golang.org/x/text v0.17.0
golang.org/x/text v0.18.0
gopkg.in/yaml.v3 v3.0.1
helm.sh/helm/v3 v3.15.4
k8s.io/api v0.30.3
Expand All @@ -106,7 +108,6 @@ require (
github.com/Microsoft/hcsshim v0.12.4 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/aws/aws-sdk-go v1.54.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ecr v1.32.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
Expand All @@ -130,6 +131,9 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.5 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand Down Expand Up @@ -191,7 +195,7 @@ require (
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/docker/cli v26.1.4+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v27.1.1+incompatible // indirect
Expand Down Expand Up @@ -309,13 +313,13 @@ require (
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.starlark.net v0.0.0-20240520160348-046347dcd104 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20240624140628-dc46fd24d27d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d // indirect
Expand Down
Loading

0 comments on commit 31bf25d

Please sign in to comment.