Skip to content

Commit

Permalink
[MKP] Add initializaion action for MKP deployer (#2932)
Browse files Browse the repository at this point in the history
* update docker

* add gcloud sdk

* add gcs creation and configmap

* fix lint

* update docker

* rename/move old deploy.sh

* add permission

* typo

* fix random id

* namespace typo

* add project id

* fix typo
  • Loading branch information
Jiaxiao Zheng authored Jan 30, 2020
1 parent 77a3553 commit d9fb85d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
15 changes: 15 additions & 0 deletions manifests/gcp_marketplace/deployer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# 0.9.6~0.9.7 has bug and fixed PR447.
FROM gcr.io/cloud-marketplace-tools/k8s/deployer_helm/onbuild:0.9.10
# Install curl
RUN apt-get update -y && apt-get install curl -y
# Install gcloud sdk.
RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz
RUN mkdir -p /usr/local/gcloud
RUN tar -C /usr/local/gcloud -xf /tmp/google-cloud-sdk.tar.gz
RUN /usr/local/gcloud/google-cloud-sdk/install.sh
ENV PATH $PATH:/usr/local/gcloud/google-cloud-sdk/bin

# move the old entrypoint, because mpdev tool will call it directly, whatever entrypoint is.
RUN mv /bin/deploy.sh /bin/core_deploy.sh
ADD deployer/init_action.sh /bin/deploy.sh
RUN chmod 755 /bin/deploy.sh

ENTRYPOINT ["/bin/bash", "/bin/deploy.sh"]
68 changes: 68 additions & 0 deletions manifests/gcp_marketplace/deployer/init_action.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash
#
# Copyright 2020 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.

function set_bucket_and_configmap() {
# Helper function to deploy bucket with a unique name. The unique name is ${BASE_NAME} + random
# unique string. Also detect the current GCP project ID and populate the properties into a
# config map.
#
# Usage:
# set_bucket_and_configmap BASE_NAME NUM_RETRIES
BASE_NAME=$1
NUM_RETRIES=$2
CONFIG_NAME="gcp-default-config"

for i in $(seq 1 ${NUM_RETRIES})
do
bucket_is_set=true
bucket_name="${BASE_NAME}-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 10 | head -n 1)"
gsutil mb "gs://${bucket_name}/" || bucket_is_set=false
if [ "$bucket_is_set" = true ]; then
break
fi
done
# Detect GCP project
GCP_PROJECT_ID=$(curl -H "Metadata-Flavor: Google" -w '\n' "http://metadata.google.internal/computeMetadata/v1/project/project-id")

# Populate configmap, with name gcp-default-config
if [ "${bucket_is_set}" = true ]; then
kubectl create configmap -n "${NAMESPACE}" "${CONFIG_NAME}" \
--from-literal bucket_name="${bucket_name}" \
--from-literal has_default_bucket="true" \
--from-literal project_id="${GCP_PROJECT_ID}"
else
echo "Cannot successfully create bucket after ${NUM_RETRIES} attempts. Fall back to not specifying default bucket."
kubectl create configmap -n "${NAMESPACE}" "${CONFIG_NAME}" \
--from-literal bucket_name="<your-bucket>" \
--from-literal has_default_bucket="false" \
--from-literal project_id="${GCP_PROJECT_ID}"
fi
}

# Helper script for auto-provision bucket in KFP MKP deployment.
NAME="$(/bin/print_config.py \
--xtype NAME \
--values_mode raw)"
NAMESPACE="$(/bin/print_config.py \
--xtype NAMESPACE \
--values_mode raw)"
export NAME
export NAMESPACE

set_bucket_and_configmap "${NAME}-default" 10

# Invoke normal deployer routine.
/bin/bash /bin/core_deploy.sh

0 comments on commit d9fb85d

Please sign in to comment.