Skip to content

Commit fa9491e

Browse files
authored
Remove username (#14)
Remove username in terraform files. `namespace` parameter is require when creating kubernetes resource and deploying.
1 parent 39b5b30 commit fa9491e

File tree

14 files changed

+134
-97
lines changed

14 files changed

+134
-97
lines changed
File renamed without changes.

.env.multiusers

Lines changed: 0 additions & 8 deletions
This file was deleted.

.github/workflows/pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ jobs:
1515
disk-cache: ${{ github.workflow }}
1616
# Share repository cache between workflows.
1717
repository-cache: true
18-
- run: cp .env.singleuser env.bzl
18+
- run: cp .env.example env.bzl
1919
- run: bazel build //...

README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,13 @@ We are releasing an alpha version, which may miss some necessary features.
7878

7979
## Deploying in Google Cloud Platform (GCP)
8080
### Defining environment variables
81-
First, determine whether there are multiple developers deploying simultaneously in your environment. If yes, use the multiple users template, otherwise use the single user template.
81+
First, copy the example environment variables template to the existing directory.
8282
```
83-
# multiple users
84-
cp .env.multiusers env.bzl
85-
```
86-
```
87-
# single user
88-
cp .env.singleuser env.bzl
83+
cp .env.example env.bzl
8984
```
9085
Edit the variables in `env.bzl`. The `env.bzl` file is the one that really takes effect, the other files are just templates. The double quotes around a variable name are needed. For example:
9186
```
9287
env="dev" # the deployment environment
93-
username="mock developer name" # the developer name
9488
mysql_username="mockname" # mysql database username
9589
mysql_password="mockpwd" # mysql database password
9690
project_id="you project id" # gcp project id
@@ -102,17 +96,20 @@ zone="" # the zone that the resources created in
10296
### Preparing resources
10397
Create resources for the data clean room by terraform. Make sure you have correctly defined environment variables in the `env.bzl`.
10498

105-
`resources/gcp` directory contains the resources releated to the gcp including: clusters, cloud sql instance, database, docker repositories, and service accounts. These resource are global and only created once for all the developers in one project.
106-
107-
`resources/kubernetes` directory includes the resources releated to the kubernete cluster including: namespace, role, secret.
108-
109-
Create all the resources by:
99+
`resources/gcp` directory contains the resources releated to the gcp including: clusters, cloud sql instance, database, docker repositories, and service accounts. These resource are global and only created once for all the developers in one project. If you are the project owner, run the commands to create global resources.
110100
```
111-
pushd resources
101+
pushd resources/gcp
112102
./apply.sh
113103
popd
114104
```
115105

106+
`resources/kubernetes` directory includes the resources releated to the kubernete cluster including: namespace, role, secret. Once the global resources have been created, the developers can run the commands to create user-specific resources. The namespace is required, and make sure the namespace is distinct.
107+
```
108+
pushd resources/kubernetes
109+
./apply.sh --namespace=xxxx
110+
popd
111+
```
112+
116113
### Building and Pushing Images
117114
`app` directory contains the source codes of the data clean room which has three components:
118115

deployment/data-clean-room/deploy.sh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ fi
2222
VAR_FILE=$(realpath $VAR_FILE)
2323
source $VAR_FILE
2424

25-
tag="latest"
26-
if [ -z "$username" ]; then
27-
helm_name="data-clean-room-helm-$env"
28-
k8s_namespace="data-clean-room-$env"
29-
else
30-
helm_name="data-clean-room-helm-$username"
31-
k8s_namespace="data-clean-room-$username"
25+
if [ -z "$1" ]
26+
then
27+
echo "Error: No namespace argument supplied."
28+
exit 1
3229
fi
30+
namespace=$1
31+
32+
tag="latest"
33+
helm_name="data-clean-room-helm"
3334

3435
connection_name="${project_id}:${region}:dcr-${env}-db-instance"
3536
service_account="dcr-k8s-pod-sa"
@@ -44,7 +45,7 @@ helm upgrade --cleanup-on-fail \
4445
--set monitorImage.tag=${tag} \
4546
--set serviceAccount.name=${service_account} \
4647
--set cloudSql.connection_name=${connection_name} \
47-
--set namespace=${k8s_namespace} \
48+
--set namespace=${namespace} \
4849
--install $helm_name ./ \
49-
--namespace $k8s_namespace \
50+
--namespace $namespace \
5051
--values config.yaml

deployment/deploy.sh

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,31 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
set -e
16+
17+
for arg in "$@"
18+
do
19+
case $arg in
20+
--namespace=*)
21+
# If we find an argument --namespace=something, split the string into a name/value array.
22+
IFS='=' read -ra NAMESPACE <<< "$arg"
23+
# Assign the second element of the array (the value of the --namespace argument) to our variable.
24+
namespace="${NAMESPACE[1]}"
25+
;;
26+
esac
27+
done
28+
29+
30+
if [ -z "$namespace" ]; then
31+
echo "Error: the namespace parameter is required, run the script again like ./apply.sh --namespace="
32+
exit 1
33+
fi
34+
1635
deploy_service() {
1736
app=$1
1837
pushd $app
19-
./deploy.sh
38+
./deploy.sh $2
2039
popd
2140
}
2241

23-
if [[ $# == 0 ]]; then
24-
deploy_service data-clean-room
25-
deploy_service jupyterhub
26-
elif [[ $# == 1 ]]; then
27-
deploy_service $1
28-
else
29-
echo "ERROR: unkown parameters"
30-
fi
42+
deploy_service data-clean-room $namespace
43+
deploy_service jupyterhub $namespace

deployment/jupyterhub/deploy.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ fi
2222
VAR_FILE=$(realpath $VAR_FILE)
2323
source $VAR_FILE
2424

25-
tag="latest"
26-
if [ -z "$username" ]; then
27-
helm_name="jupyterhub-helm-$env"
28-
k8s_namespace="jupyterhub-$env"
29-
api="http://data-clean-room.data-clean-room-$env.svc.cluster.local"
30-
else
31-
helm_name="jupyterhub-helm-$username"
32-
k8s_namespace="jupyterhub-$username"
33-
api="http://data-clean-room.data-clean-room-$username.svc.cluster.local"
25+
if [ -z "$1" ]
26+
then
27+
echo "Error: No namespace argument supplied."
28+
exit 1
3429
fi
30+
namespace=$1
31+
32+
tag="latest"
33+
helm_name="jupyterhub-helm"
34+
api="http://data-clean-room.$namespace.svc.cluster.local"
3535

3636
service_account="jupyter-k8s-pod-sa"
3737
docker_repo="dcr-${env}-images"
@@ -50,9 +50,9 @@ helm upgrade --cleanup-on-fail \
5050
--set singleuser.extraEnv.KEY_LOCALTION=${region} \
5151
--set singleuser.networkPolicy.enabled=false \
5252
--install $helm_name jupyterhub/jupyterhub \
53-
--namespace ${k8s_namespace} \
53+
--namespace ${namespace} \
5454
--version=3.0.3 \
5555
--values config.yaml
5656

5757
echo "Deployment Completed."
58-
echo "Try 'kubectl --namespace=$k8s_namespace get service proxy-public' to obtain external IP"
58+
echo "Try 'kubectl --namespace=$namespace get service proxy-public' to obtain external IP"

resources/apply.sh renamed to resources/gcp/apply.sh

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
echo "You are creating the gcp resources and this should only be done once."
17+
1618
# Check if gcloud is installed
1719
if ! [ -x "$(command -v gcloud)" ]; then
1820
echo "Error: gcloud is not installed." >&2
@@ -26,7 +28,7 @@ if ! gcloud auth list | grep -q 'ACTIVE'; then
2628
fi
2729

2830
# check whether variables has been set
29-
VAR_FILE="../env.bzl"
31+
VAR_FILE="../../env.bzl"
3032
if [ ! -f "$VAR_FILE" ]; then
3133
echo "Error: Variables file does not exist."
3234
exit 1
@@ -38,8 +40,6 @@ if ! gsutil ls gs://dcr-tf-state-$env > /dev/null 2>&1; then
3840
gsutil mb -l us gs://dcr-tf-state-$env
3941
fi
4042

41-
pushd gcp
42-
4343
cat << EOF > backend.tf
4444
terraform {
4545
backend "gcs" {
@@ -52,14 +52,3 @@ EOF
5252
cp $VAR_FILE terraform.tfvars
5353
terraform init -reconfigure
5454
terraform apply
55-
popd
56-
57-
zone=$region-a
58-
# get kubernete cluster credentials
59-
gcloud container clusters get-credentials dcr-$env-cluster --zone $zone --project $project_id
60-
61-
pushd kubernetes
62-
cp $VAR_FILE terraform.tfvars
63-
terraform init -reconfigure
64-
terraform apply
65-
popd

resources/gcp/variables.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
variable "username" {
18-
type = string
19-
description = "Username to postfix resources with"
20-
default = ""
21-
}
22-
2317
variable "env" {
2418
type = string
2519
description = "Deployment environment, e.g., dev, prod, oss"

resources/kubernetes/apply.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/bash
2+
# Copyright 2024 TikTok Pte. Ltd.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
set -e
16+
17+
for arg in "$@"
18+
do
19+
case $arg in
20+
--namespace=*)
21+
# If we find an argument --namespace=something, split the string into a name/value array.
22+
IFS='=' read -ra NAMESPACE <<< "$arg"
23+
# Assign the second element of the array (the value of the --namespace argument) to our variable.
24+
namespace="${NAMESPACE[1]}"
25+
;;
26+
esac
27+
done
28+
29+
30+
if [ -z "$namespace" ]; then
31+
echo "Error: the namespace parameter is required, run the script again like ./apply.sh --namespace="
32+
exit 1
33+
fi
34+
35+
# Check if gcloud is installed
36+
if ! [ -x "$(command -v gcloud)" ]; then
37+
echo "Error: gcloud is not installed." >&2
38+
exit 1
39+
fi
40+
41+
# Check if gcloud logged in
42+
if ! gcloud auth list | grep -q 'ACTIVE'; then
43+
echo "Error: No active gcloud account found." >&2
44+
exit 1
45+
fi
46+
47+
# check whether variables has been set
48+
VAR_FILE="../../env.bzl"
49+
if [ ! -f "$VAR_FILE" ]; then
50+
echo "Error: Variables file does not exist."
51+
exit 1
52+
fi
53+
VAR_FILE=$(realpath $VAR_FILE)
54+
source $VAR_FILE
55+
56+
zone=$region-a
57+
# get kubernete cluster credentials
58+
gcloud container clusters get-credentials dcr-$env-cluster --zone $zone --project $project_id
59+
60+
cp $VAR_FILE terraform.tfvars
61+
echo -e "\nnamespace=\"$namespace\"\n" >> terraform.tfvars
62+
terraform init -reconfigure
63+
terraform apply

0 commit comments

Comments
 (0)