Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: federated learning use case implementation #70

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ monitoring-dashboard-samples/
**/tfplan
*.terraform/
*.terraform-*/
terraform.tfstate*

# Test
test/log/*.log
Expand Down
2 changes: 2 additions & 0 deletions platforms/gke/base/use-cases/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# GKE Accelerated Platform Use Cases

- [Federated learning](/docs/use-cases/federated-learning/README.md)
16 changes: 16 additions & 0 deletions platforms/gke/base/use-cases/federated-learning/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# Federated learning on Google Cloud

## Deploy the Federated learning reference architecture

1. Provision the Federated Learning reference architecture:

```sh
"${ACP_PLATFORM_BASE_DIR}/use-cases/federated-learning/deploy.sh"
```

## Teardown the Federated Learning reference architecture

1. Teardown the Federated Learning reference architecture:

```sh
"${ACP_PLATFORM_BASE_DIR}/use-cases/federated-learning/teardown.sh"
```
98 changes: 98 additions & 0 deletions platforms/gke/base/use-cases/federated-learning/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env bash
#
# Copyright 2024 Google LLC
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

ACP_PLATFORM_SHARED_CONFIG_DIR="${ACP_PLATFORM_BASE_DIR}/_shared_config"

# shellcheck disable=SC2034 # Variable is used in other scripts
ACP_PLATFORM_SHARED_CONFIG_CLUSTER_AUTO_VARS_FILE="${ACP_PLATFORM_SHARED_CONFIG_DIR}/cluster.auto.tfvars"

# shellcheck disable=SC1091
source "${ACP_PLATFORM_SHARED_CONFIG_DIR}/scripts/set_environment_variables.sh" "${ACP_PLATFORM_SHARED_CONFIG_DIR}"

FEDERATED_LEARNING_USE_CASE_DIR="${ACP_PLATFORM_BASE_DIR}/use-cases/federated-learning"
FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR="${FEDERATED_LEARNING_USE_CASE_DIR}/terraform"
# shellcheck disable=SC2034 # Variable is used in other scripts
FEDERATED_LEARNING_SHARED_CONFIG_DIR="${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/_shared_config"

# shellcheck disable=SC2034 # Variable is used in other scripts
federated_learning_terraservices=(
"initialize"
"container_image_repository"
"private_google_access"
)

# shellcheck disable=SC2034 # Variable is used in other scripts
TERRAFORM_CLUSTER_CONFIGURATION=(
)

apply_or_destroy_terraservice() {
local terraservice
terraservice="${1}"

local operation_mode
operation_mode="${2:-"not set"}"

cd "${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/${terraservice}" &&
terraform init

echo "Current working directory: $(pwd)"

if [[ "${operation_mode}" == "apply" ]]; then
echo "Provisioning ${terraservice}"
terraform plan -input=false -out=tfplan &&
terraform apply -input=false tfplan
_terraform_result=$?
elif [[ "${operation_mode}" == "destroy" ]]; then
echo "Destroying ${terraservice}"
terraform destroy -auto-approve
_terraform_result=$?
else
echo "Error: operation mode not supported: ${operation_mode}"
_terraform_result=1
fi

rm -rf \
"${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/${terraservice}/.terraform" \
"${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/${terraservice}/tfplan"

if [[ ${_terraform_result} -ne 0 ]]; then
echo "Terraform ${operation_mode} command failed with code ${_terraform_result} for ${terraservice}"
exit ${_terraform_result}
fi
}

provision_terraservice() {
apply_or_destroy_terraservice "${1}" "apply"
}

destroy_terraservice() {
apply_or_destroy_terraservice "${1}" "destroy"
}

get_terraform_output() {
terraservice="${1}"
output_name="${2}"
local output
if ! output="$(cd "${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/${terraservice}" && terraform output -raw "${output_name}")"; then
echo "Error while getting ${output_name} output"
return 1
fi
echo "${output}"
}
45 changes: 45 additions & 0 deletions platforms/gke/base/use-cases/federated-learning/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
#
# Copyright 2024 Google LLC
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

# shellcheck disable=SC1091
source "${ACP_PLATFORM_BASE_DIR}/use-cases/federated-learning/common.sh"

start_timestamp_federated_learning=$(date +%s)

echo "Preparing core platform configuration files"
for configuration_variable in "${TERRAFORM_CLUSTER_CONFIGURATION[@]}"; do
configuration_variable_name="$(echo "${configuration_variable}" | awk '{ print $1 }')"
echo "Checking if ${configuration_variable_name} is in ${ACP_PLATFORM_SHARED_CONFIG_CLUSTER_AUTO_VARS_FILE}"
grep -q "${configuration_variable_name}" "${ACP_PLATFORM_SHARED_CONFIG_CLUSTER_AUTO_VARS_FILE}" || echo "${configuration_variable}" >>"${ACP_PLATFORM_SHARED_CONFIG_CLUSTER_AUTO_VARS_FILE}"
done
terraform fmt "${ACP_PLATFORM_SHARED_CONFIG_CLUSTER_AUTO_VARS_FILE}"

echo "Provisioning the core platform"
"${ACP_PLATFORM_CORE_DIR}/deploy.sh"

echo "Provisioning the use case resources"
# shellcheck disable=SC2154 # variable defined in common.sh
for terraservice in "${federated_learning_terraservices[@]}"; do
provision_terraservice "${terraservice}"
done

end_timestamp_federated_learning=$(date +%s)
total_runtime_value_federated_learning=$((end_timestamp_federated_learning - start_timestamp_federated_learning))
echo "Total runtime (Federated learning use case provisioning and configuration): $(date -d@${total_runtime_value_federated_learning} -u +%H:%M:%S)"
45 changes: 45 additions & 0 deletions platforms/gke/base/use-cases/federated-learning/teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
#
# Copyright 2024 Google LLC
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

# shellcheck disable=SC1091
source "${ACP_PLATFORM_BASE_DIR}/use-cases/federated-learning/common.sh"

start_timestamp_federated_learning=$(date +%s)

# Iterate over the terraservices array so we destroy them in reverse order, keeping the
# initialize terraservice last.
# shellcheck disable=SC2154 # variable defined in common.sh
for ((i = ${#federated_learning_terraservices[@]} - 1; i >= 0; i--)); do
terraservice=${federated_learning_terraservices[i]}
destroy_terraservice "${terraservice}"
done

echo "Destroying the core platform"
"${ACP_PLATFORM_CORE_DIR}/teardown.sh"

for configuration_variable in "${TERRAFORM_CLUSTER_CONFIGURATION[@]}"; do
configuration_variable_name="$(echo "${configuration_variable}" | awk ' { print $1 }'))"
sed -i "/${configuration_variable_name}/d" "${ACP_PLATFORM_SHARED_CONFIG_CLUSTER_AUTO_VARS_FILE}"
done
terraform fmt "${ACP_PLATFORM_SHARED_CONFIG_CLUSTER_AUTO_VARS_FILE}"

end_timestamp_federated_learning=$(date +%s)
total_runtime_value_federated_learning=$((end_timestamp_federated_learning - start_timestamp_federated_learning))
echo "Total runtime (Federated learning use case provisioning and configuration): $(date -d@${total_runtime_value_federated_learning} -u +%H:%M:%S)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Google LLC
#
# 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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Google LLC
#
# 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.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2024 Google LLC
#
# 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.

resource "google_artifact_registry_repository" "container_image_repository" {
location = var.cluster_region
repository_id = "federated-learning-container-image-repository"
description = "Federated Learning container image repository"
format = "DOCKER"
project = google_project_service.artifactregistry_googleapis_com.project

cleanup_policies {
action = "DELETE"
id = "Delete untagged images"

condition {
tag_state = "UNTAGGED"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Google LLC
#
# 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.

data "google_project" "default" {
project_id = var.cluster_project_id
}

resource "google_project_service" "artifactregistry_googleapis_com" {
disable_dependent_services = false
disable_on_destroy = false
project = data.google_project.default.project_id
service = "artifactregistry.googleapis.com"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2024 Google LLC
#
# 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.

terraform {
required_version = ">= 1.5.7"

required_providers {
google = {
source = "hashicorp/google"
version = "6.12.0"
}
}

provider_meta "google" {
module_name = "cloud-solutions/acp_fl_container_image_repository_deploy-v1"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading