forked from kubeflow/pipelines
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AWS SageMaker] Integration tests automation (kubeflow#3768)
* # This is a combination of 5 commits. # This is the 1st commit message: Add initial scripts # This is the commit message kubeflow#2: Add working pytest script # This is the commit message kubeflow#3: Add initial scripts # This is the commit message kubeflow#4: Add environment variable files # This is the commit message kubeflow#5: Remove old cluster script * Add initial scripts Add working pytest script Add initial scripts Add environment variable files Remove old cluster script Update pipeline credentials to OIDC Add initial scripts Add working pytest script Add initial scripts Add working pytest script * Remove debugging mark * Update example EKS cluster name * Remove quiet from Docker build * Manually pass env * Update env list vars as string * Update use array directly * Update variable array to export * Update to using read for splitting * Move to helper script * Update export from CodeBuild * Add wait for minio * Update kubectl wait timeout * Update minor changes for PR * Update integration test buildspec to quiet build * Add region to delete EKS * Add wait for pods * Updated README * Add fixed interval wait * Fix CodeBuild step order * Add file lock for experiment ID * Fix missing pytest parameter * Update run create only once * Add filelock to conda env * Update experiment name ensuring creation each time * Add try/catch with create experiment * Remove caching from KFP deployment * Remove disable KFP caching * Move .gitignore changes to inside component * Add blank line to default .gitignore
- Loading branch information
1 parent
fdffd0d
commit 0d8f761
Showing
18 changed files
with
404 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Any environment variable files | ||
**/*/.env |
16 changes: 13 additions & 3 deletions
16
components/aws/sagemaker/codebuild/integration-test.buildspec.yml
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
10 changes: 10 additions & 0 deletions
10
components/aws/sagemaker/codebuild/scripts/construct_environment_array.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,10 @@ | ||
#!/usr/bin/env bash | ||
|
||
# This script breaks up a string of environment variable names into a list of | ||
# parameters that `docker run` accepts. This needs to be made into a script | ||
# for CodeBuild because these commands do not run in dash - the default terminal | ||
# on the CodeBuild standard images. | ||
|
||
IFS=' ' read -a variable_array <<< $CONTAINER_VARIABLES | ||
printf -v CONTAINER_VARIABLE_ARGUMENTS -- "--env %s " "${variable_array[@]}" | ||
echo $CONTAINER_VARIABLE_ARGUMENTS |
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
12 changes: 12 additions & 0 deletions
12
components/aws/sagemaker/tests/integration_tests/.env.example
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,12 @@ | ||
# If you would like to override the credentials for the container | ||
# AWS_ACCESS_KEY_ID= | ||
# AWS_SECRET_ACCESS_KEY= | ||
# AWS_SESSION_TOKEN= | ||
|
||
REGION=us-east-1 | ||
|
||
SAGEMAKER_EXECUTION_ROLE_ARN=arn:aws:iam::123456789012:role/service-role/AmazonSageMaker-ExecutionRole-Example | ||
S3_DATA_BUCKET=my-data-bucket | ||
|
||
# If you hope to use an existing EKS cluster, rather than creating a new one. | ||
# EKS_EXISTING_CLUSTER=my-eks-cluster |
43 changes: 43 additions & 0 deletions
43
components/aws/sagemaker/tests/integration_tests/Dockerfile
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,43 @@ | ||
FROM continuumio/miniconda:4.7.12 | ||
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
curl \ | ||
wget \ | ||
git \ | ||
jq | ||
|
||
# Install eksctl | ||
RUN curl --location "https://github.com/weaveworks/eksctl/releases/download/0.19.0/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp \ | ||
&& mv /tmp/eksctl /usr/local/bin | ||
|
||
# Install aws-iam-authenticator | ||
RUN curl -S -o /usr/local/bin/aws-iam-authenticator https://amazon-eks.s3.us-west-2.amazonaws.com/1.16.8/2020-04-16/bin/linux/amd64/aws-iam-authenticator \ | ||
&& chmod +x /usr/local/bin/aws-iam-authenticator | ||
|
||
# Install Kubectl | ||
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl \ | ||
&& chmod +x ./kubectl \ | ||
&& mv ./kubectl /usr/local/bin/kubectl | ||
|
||
# Install Argo CLI | ||
RUN curl -sSL -o /usr/local/bin/argo https://github.com/argoproj/argo/releases/download/v2.8.0/argo-linux-amd64 \ | ||
&& chmod +x /usr/local/bin/argo | ||
|
||
# Copy conda environment early to avoid cache busting | ||
COPY ./sagemaker/tests/integration_tests/environment.yml environment.yml | ||
|
||
# Create conda environment for running tests and set as start-up environment | ||
RUN conda env create -f environment.yml | ||
RUN echo "source activate kfp_test_env" > ~/.bashrc | ||
ENV PATH "/opt/conda/envs/kfp_test_env/bin":$PATH | ||
|
||
# Environment variables to be used by tests | ||
ENV REGION="us-west-2" | ||
ENV SAGEMAKER_EXECUTION_ROLE_ARN="arn:aws:iam::1234567890:role/sagemaker-role" | ||
ENV S3_DATA_BUCKET="kfp-test-data" | ||
ENV MINIO_LOCAL_PORT=9000 | ||
ENV KFP_NAMESPACE="kubeflow" | ||
|
||
COPY ./sagemaker/ . | ||
|
||
ENTRYPOINT [ "/bin/bash", "./tests/integration_tests/scripts/run_integration_tests" ] |
51 changes: 15 additions & 36 deletions
51
components/aws/sagemaker/tests/integration_tests/README.md
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 |
---|---|---|
@@ -1,42 +1,21 @@ | ||
## Requirements | ||
1. [Conda](https://docs.conda.io/en/latest/miniconda.html) | ||
1. [Kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) | ||
1. Argo CLI: [Mac](https://github.com/argoproj/homebrew-tap), [Linux](https://eksworkshop.com/advanced/410_batch/install/) | ||
1. K8s cluster with Kubeflow pipelines > 0.4.0 installed | ||
1. [IAM Role](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html) with a SageMakerFullAccess and S3FullAccess | ||
1. IAM User credentials with SageMakerFullAccess permissions | ||
1. [Docker](https://www.docker.com/) | ||
1. [IAM Role](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html) with a SageMakerFullAccess and AmazonS3FullAccess | ||
1. IAM User credentials with SageMakerFullAccess, AWSCloudFormationFullAccess, IAMFullAccess, AmazonEC2FullAccess, AmazonS3FullAccess permissions | ||
|
||
## Creating S3 buckets with datasets | ||
|
||
Change the bucket name and run the python script `[s3_sample_data_creator.py](https://github.com/kubeflow/pipelines/tree/master/samples/contrib/aws-samples/mnist-kmeans-sagemaker#the-sample-dataset)` to create S3 buckets with mnist dataset in the region where you want to run the tests | ||
In the following Python script, change the bucket name and run the [`s3_sample_data_creator.py`](https://github.com/kubeflow/pipelines/tree/master/samples/contrib/aws-samples/mnist-kmeans-sagemaker#the-sample-dataset) to create an S3 bucket with the sample mnist dataset in the region where you want to run the tests. | ||
|
||
## Step to run integration tests | ||
1. Configure AWS credentials with access to EKS cluster | ||
1. Fetch kubeconfig to `~/.kube/config` or set `KUBECONFIG` environment variable to point to kubeconfig of the cluster | ||
1. Create a [secret](https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/) named `aws-secret` in kubeflow namespace with credentials of IAM User for SageMakerFullAccess | ||
```yaml | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: aws-secret | ||
namespace: kubeflow | ||
type: Opaque | ||
data: | ||
AWS_ACCESS_KEY_ID: YOUR_BASE64_ACCESS_KEY | ||
AWS_SECRET_ACCESS_KEY: YOUR_BASE64_SECRET_ACCESS | ||
``` | ||
> Note: To get base64 string, run `echo -n $AWS_ACCESS_KEY_ID | base64` | ||
1. Create conda environment using environment.yml for running tests. Run `conda env create -f environment.yml` | ||
1. Activate the conda environment `conda activate kfp_test_env` | ||
1. Run port-forward to minio service in background. Example: `kubectl port-forward svc/minio-service 9000:9000 -n kubeflow &` | ||
1. Provide the following arguments to pytest: | ||
1. `region`: AWS region where test will run. Default - us-west-2 | ||
1. `role-arn`: SageMaker execution IAM role ARN | ||
1. `s3-data-bucket`: Regional S3 bucket in which test data is hosted | ||
1. `minio-service-port`: Localhost port to which minio service is mapped to. Default - 9000 | ||
1. `kfp-namespace`: Cluster namespace where kubeflow pipelines is installed. Default - Kubeflow | ||
1. cd into this directory and run | ||
``` | ||
pytest --region <> --role-arn <> --s3-data-bucket <> --minio-service-port <> --kfp-namespace <> | ||
``` | ||
1. Copy the `.env.example` file to `.env` and in the following steps modify the fields of this new file: | ||
1. Configure the AWS credentials fields with those of your IAM User. | ||
1. Update the `SAGEMAKER_EXECUTION_ROLE_ARN` with that of your role created earlier. | ||
1. Update the `S3_DATA_BUCKET` parameter with the name of the bucket created earlier. | ||
1. (Optional) If you have already created an EKS cluster for testing, replace the `EKS_EXISTING_CLUSTER` field with it's name. | ||
1. Build the image by doing the following: | ||
1. Navigate to the `components/aws` directory. | ||
1. Run `docker build . -f sagemaker/tests/integration_tests/Dockerfile -t amazon/integration_test` | ||
1. Run the image, injecting your environment variable files: | ||
1. Navigate to the `components/aws` directory. | ||
1. Run `docker run --env-file sagemaker/tests/integration_tests/.env amazon/integration_test` |
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
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
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
68 changes: 68 additions & 0 deletions
68
components/aws/sagemaker/tests/integration_tests/scripts/generate_iam_role
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,68 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Helper script to generate an IAM Role needed to install role-based authentication to a KFP service account. | ||
# | ||
# Run as: | ||
# $ ./generate_iam_role ${cluster_arn/cluster_name} ${role_name} ${cluster_region} [optional: ${service_namespace} ${service_account}] | ||
# | ||
|
||
CLUSTER_ARN="${1}" | ||
ROLE_NAME="${2}" | ||
CLUSTER_REGION="${3:-us-east-1}" | ||
SERVICE_NAMESPACE="${4:-kubeflow}" | ||
SERVICE_ACCOUNT="${5:-pipeline-runner}" | ||
aws_account=$(aws sts get-caller-identity --query Account --output text) | ||
trustfile="trust.json" | ||
|
||
cwd=$(dirname $(realpath $0)) | ||
|
||
# if using an existing cluster, use the cluster arn to get the region and cluster name | ||
# example, cluster_arn=arn:aws:eks:us-east-1:12345678910:cluster/test | ||
cluster_name=$(echo ${CLUSTER_ARN} | cut -d'/' -f2) | ||
|
||
# A function to get the OIDC_ID associated with an EKS cluster | ||
function get_oidc_id { | ||
# TODO: Ideally this should be based on version compatibility instead of command failure | ||
eksctl utils associate-iam-oidc-provider --cluster ${cluster_name} --region ${CLUSTER_REGION} --approve | ||
if [[ $? -ge 1 ]]; then | ||
eksctl utils associate-iam-oidc-provider --name ${cluster_name} --region ${CLUSTER_REGION} --approve | ||
fi | ||
|
||
local oidc=$(aws eks describe-cluster --name ${cluster_name} --region ${CLUSTER_REGION} --query cluster.identity.oidc.issuer --output text) | ||
oidc_id=$(echo ${oidc} | rev | cut -d'/' -f1 | rev) | ||
} | ||
|
||
# A function that generates an IAM role for the given account, cluster, namespace, region | ||
# Parameter: | ||
# $1: Name of the trust file to generate. | ||
function create_namespaced_iam_role { | ||
local trustfile="${1}" | ||
# Check if role already exists | ||
aws iam get-role --role-name ${ROLE_NAME} | ||
if [[ $? -eq 0 ]]; then | ||
echo "A role for this cluster and namespace already exists in this account, assuming sagemaker access and proceeding." | ||
else | ||
echo "IAM Role does not exist, creating a new Role for the cluster" | ||
aws iam create-role --role-name ${ROLE_NAME} --assume-role-policy-document file://${trustfile} --output=text --query "Role.Arn" | ||
aws iam attach-role-policy --role-name ${ROLE_NAME} --policy-arn arn:aws:iam::aws:policy/AmazonSageMakerFullAccess | ||
fi | ||
} | ||
|
||
# Remove the generated trust file | ||
# Parameter: | ||
# $1: Name of the trust file to delete. | ||
function delete_generated_file { | ||
rm "${1}" | ||
} | ||
|
||
echo "Get the OIDC ID for the cluster" | ||
get_oidc_id | ||
echo "Delete the trust json file if it already exists" | ||
delete_generated_file "${trustfile}" | ||
echo "Generate a trust json" | ||
"$cwd"/generate_trust_policy ${CLUSTER_REGION} ${aws_account} ${oidc_id} ${SERVICE_NAMESPACE} ${SERVICE_ACCOUNT} > "${trustfile}" | ||
echo "Create the IAM Role using these values" | ||
create_namespaced_iam_role "${trustfile}" | ||
echo "Cleanup for the next run" | ||
delete_generated_file "${trustfile}" | ||
|
Oops, something went wrong.