ContainerImage.Pinniped/deploy_carvel/build.sh

154 lines
6.6 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425
set -e # immediately exit
set -u # error if variables undefined
set -o pipefail # prevent masking errors in a pipeline
# set -x # print all executed commands to terminal
2023-08-31 21:24:31 +00:00
#
# Helper functions
#
function log_note() {
GREEN='\033[0;32m'
NC='\033[0m'
if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then
echo -e "${GREEN}$*${NC}"
else
echo "$*"
fi
}
2023-08-31 21:24:31 +00:00
function log_error() {
RED='\033[0;31m'
NC='\033[0m'
if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then
echo -e "🙁${RED} Error: $* ${NC}"
else
echo ":( Error: $*"
fi
}
2023-08-31 21:24:31 +00:00
function check_dependency() {
if ! command -v "$1" >/dev/null; then
log_error "Missing dependency..."
log_error "$2"
exit 1
fi
}
2023-08-31 21:24:31 +00:00
log_note "Deploying kapp-controller on kind cluster..."
2023-08-28 21:26:47 +00:00
kapp deploy --app kapp-controller --file https://github.com/vmware-tanzu/carvel-kapp-controller/releases/latest/download/release.yml -y
kubectl get customresourcedefinitions
2023-08-31 21:24:31 +00:00
# Global kapp-controller-namespace:
# -packaging-global-namespace=kapp-controller-packaging-global
2023-08-31 21:24:31 +00:00
# kapp-controller resources like PackageRepository and Package are namepaced.
# However, this namespace, provided via flag to kapp-controller in the yaml above,
# defines a "global" namespace. That is, resources installed in this namespace
# can be installed in every namespace as kapp will always pay attention to its
# pseudo-global namespace.
KAPP_CONTROLLER_GLOBAL_NAMESPACE="kapp-controller-packaging-global"
2023-08-31 21:24:31 +00:00
# TODO: final resting place for these images (PackageRepository, Packge) will need to
# be in the same plate as our regular images:
# - https://github.com/vmware-tanzu/pinniped/releases/tag/v0.25.0
# namely docker.io/getpinniped/ and projects.registry.vmware.com/pinniped/
#
PACKAGE_REPO_HOST="benjaminapetersen/pinniped-package-repo"
2023-08-31 21:24:31 +00:00
# TODO: this variable is currently a little quirky as our values.yaml files do NOT pin pinniped to a specific
# hard-coded version. Rather, Pinniped's values.yaml allows for a passed-in version.
PINNIPED_PACKAGE_VERSION="0.25.0"
2023-08-31 21:24:31 +00:00
# TODO: should we copy these directories:
# - ../deploy/supervisor/config/*
# - ../deploy/concierge/config/*
# rather than duplicating the files?
# in this exercise, I have transformed the values.yaml into a "values schema" so this would have to be
# migrated up. There are some incompatibilities here, in that a values schema assesses the type of value
# by the default. currently many of the values have no actual default.
2023-08-28 21:26:47 +00:00
2023-08-31 21:24:31 +00:00
log_note "Cleaning ./package-repository to generate new..."
PACKAGE_REPOSITORY_DIR="package-repository"
rm -rf "./${PACKAGE_REPOSITORY_DIR}"
mkdir -p "./${PACKAGE_REPOSITORY_DIR}/.imgpkg"
mkdir -p "./${PACKAGE_REPOSITORY_DIR}/packages/concierge.pinniped.dev"
mkdir -p "./${PACKAGE_REPOSITORY_DIR}/packages/supervisor.pinniped.dev"
2023-08-31 21:24:31 +00:00
log_note "Generating PackageRepository and Packages for Pinniped version ${PINNIPED_PACKAGE_VERSION}"
declare -a arr=("supervisor" "concierge")
for resource_name in "${arr[@]}"
do
2023-08-31 21:24:31 +00:00
log_note "Generating for ${resource_name}..."
2023-08-31 21:24:31 +00:00
log_note "Generating ${resource_name} imgpkg lock file... ${resource_name}/.imgpkg/images.yaml"
kbld --file "./${resource_name}/config/" --imgpkg-lock-output "./${resource_name}/.imgpkg/images.yml"
# generate a schema in each package directory
2023-08-31 21:24:31 +00:00
log_note "Generating ${resource_name} OpenAPIv3 Schema... ./${resource_name}/schema-openapi.yaml"
ytt \
--file "${resource_name}/config/values.yaml" \
--data-values-schema-inspect --output openapi-v3 > "${resource_name}/schema-openapi.yml"
2023-08-31 21:24:31 +00:00
# TODO: this is not the pattern we want.
# final resting place should be with our primary Pinniped image at:
# - projects.registry.vmware.com/pinniped/pinniped-server:v0.25.0 VMware Harbor
# - docker.io/getpinniped/pinniped-server:v0.25.0 DockerHub
package_push_repo_location="${PACKAGE_REPO_HOST}-package-${resource_name}:${PINNIPED_PACKAGE_VERSION}"
2023-08-31 21:24:31 +00:00
log_note "Pushing ${resource_name} package image: ${package_push_repo_location} ..."
imgpkg push --bundle "${package_push_repo_location}" --file "./${resource_name}"
resource_package_version="${resource_name}.pinniped.dev"
2023-08-31 21:24:31 +00:00
log_note "Generating ${resource_name} PackageRepository yaml..."
log_note "generating ./${PACKAGE_REPOSITORY_DIR}/packages/${resource_package_version}/${PINNIPED_PACKAGE_VERSION}.yml"
ytt \
--file "${resource_name}/package-template.yml" \
--data-value-file openapi="$(pwd)/${resource_name}/schema-openapi.yml" \
--data-value package_version="${PINNIPED_PACKAGE_VERSION}" \
--data-value package_image_repo="${package_push_repo_location}" > "${PACKAGE_REPOSITORY_DIR}/packages/${resource_package_version}/${PINNIPED_PACKAGE_VERSION}.yml"
2023-08-31 21:24:31 +00:00
log_note "generating ./${PACKAGE_REPOSITORY_DIR}/packages/${resource_package_version}/metadata.yml"
ytt \
--file "${resource_name}/metadata.yml" \
--data-value-file openapi="$(pwd)/${resource_name}/schema-openapi.yml" \
--data-value package_version="${PINNIPED_PACKAGE_VERSION}" \
--data-value package_image_repo="${package_push_repo_location}" > "${PACKAGE_REPOSITORY_DIR}/packages/${resource_package_version}/metadata.yml"
done
2023-08-31 21:24:31 +00:00
log_note "Generating Pinniped PackageRepository..."
log_note "Generating ./${PACKAGE_REPOSITORY_DIR}/.imgpkg/images.yml"
kbld --file "./${PACKAGE_REPOSITORY_DIR}/packages/" --imgpkg-lock-output "${PACKAGE_REPOSITORY_DIR}/.imgpkg/images.yml"
package_repository_push_repo_location="${PACKAGE_REPO_HOST}:${PINNIPED_PACKAGE_VERSION}"
2023-08-31 21:24:31 +00:00
log_note "Pushing Pinniped package repository image: ${PACKAGE_REPO_HOST}:${PINNIPED_PACKAGE_VERSION}..."
imgpkg push --bundle "${package_repository_push_repo_location}" --file "./${PACKAGE_REPOSITORY_DIR}"
2023-08-31 21:24:31 +00:00
# handy for a quick debug
# log_note "Validating imgpkg package bundle contents..."
# imgpkg pull --bundle "${PACKAGE_REPO_HOST}:${PINNIPED_PACKAGE_VERSION}" --output "/tmp/${PACKAGE_REPO_HOST}:${PINNIPED_PACKAGE_VERSION}"
# ls -la "/tmp/${PACKAGE_REPO_HOST}:${PINNIPED_PACKAGE_VERSION}"
2023-08-28 21:26:47 +00:00
2023-08-31 21:24:31 +00:00
log_note "Generating PackageRepository yaml file..."
PINNIPED_PACKGE_REPOSITORY_NAME="pinniped-package-repository"
PINNIPED_PACKGE_REPOSITORY_FILE="packagerepository.${PINNIPED_PACKAGE_VERSION}.yml"
echo -n "" > "${PINNIPED_PACKGE_REPOSITORY_FILE}"
cat <<EOT >> "${PINNIPED_PACKGE_REPOSITORY_FILE}"
---
apiVersion: packaging.carvel.dev/v1alpha1
kind: PackageRepository
metadata:
name: "${PINNIPED_PACKGE_REPOSITORY_NAME}"
spec:
fetch:
imgpkgBundle:
image: "${PACKAGE_REPO_HOST}:${PINNIPED_PACKAGE_VERSION}"
EOT
2023-08-31 21:24:31 +00:00
log_note "To deploy the PackageRepository, run 'kapp deploy --app pinniped-repo --file ${PINNIPED_PACKGE_REPOSITORY_FILE}'"
log_note "Or use the sibling deploy.sh script"