Skip to content

Commit 309d704

Browse files
authored
Add operator-sdk and olm scripts to code-generator repo. (#153)
Issue #, if available: aws-controllers-k8s/community#863 Description of changes: Migrate operator-sdk and olm scripts out of community repo to code-gen repo. aws-controllers-k8s/community#897 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 99f620a commit 309d704

File tree

4 files changed

+379
-0
lines changed

4 files changed

+379
-0
lines changed

scripts/install-operator-sdk.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
# ./scripts/install-operator-sdk.sh
4+
#
5+
#
6+
# Installs Operator SDK if not installed. Optional parameters specifies the
7+
# version of Operator SDK to install. Defaults tot eh value of the environment
8+
# variable OPERATOR_SDK_VERSION and if that is not set, the value of the
9+
# DEFAULT_OPERATOR_SDK_VERSION variable.
10+
#
11+
# NOTE: uses `sudo mv` to relocate a downloaded binary to /usr/local/bin/operator-sdk
12+
13+
set -eo pipefail
14+
15+
SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
16+
ROOT_DIR="$SCRIPTS_DIR/.."
17+
DEFAULT_OPERATOR_SDK_VERSION="1.7.1"
18+
19+
source "${SCRIPTS_DIR}/lib/common.sh"
20+
21+
__operator_sdk_version="${1}"
22+
if [ "x${__operator_sdk_version}" == "x" ]; then
23+
__operator_sdk_version=${OPERATOR_SDK_VERSION:-$DEFAULT_OPERATOR_SDK_VERSION}
24+
fi
25+
if ! is_installed operator-sdk; then
26+
__platform=$(uname | tr '[:upper:]' '[:lower:]')
27+
__tmp_install_dir=$(mktemp -d -t install-operator-sdk-XXX)
28+
__operator_sdk_url="https://github.com/operator-framework/operator-sdk/releases/download/v${__operator_sdk_version}/operator-sdk_${__platform}_amd64"
29+
echo -n "installing operator-sdk from ${__operator_sdk_url} ... "
30+
curl -sq -L ${__operator_sdk_url} --output ${__tmp_install_dir}/operator-sdk_${__platform}_amd64
31+
chmod +x ${__tmp_install_dir}/operator-sdk_${__platform}_amd64
32+
sudo mv ${__tmp_install_dir}/operator-sdk_${__platform}_amd64 /usr/local/bin/operator-sdk
33+
echo "ok."
34+
fi

scripts/olm-build-bundle-image.sh

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6+
SCRIPTS_DIR=$DIR
7+
ROOT_DIR=$DIR/..
8+
BUILD_DATE=$(date +%Y-%m-%dT%H:%M)
9+
QUIET=${QUIET:-"false"}
10+
DEFAULT_DOCKER_REPOSITORY="amazon/aws-controllers-k8s"
11+
DOCKER_REPOSITORY=${DOCKER_REPOSITORY:-$DEFAULT_DOCKER_REPOSITORY}
12+
13+
export DOCKER_BUILDKIT=${DOCKER_BUILDKIT:-1}
14+
15+
source $SCRIPTS_DIR/lib/common.sh
16+
17+
check_is_installed docker
18+
19+
# red hat operator certification requires additional labels and metadata
20+
# in the bundle container image.
21+
22+
# details on the required bundle labels
23+
# https://redhat-connect.gitbook.io/certified-operator-guide/ocp-deployment/operator-metadata/bundle-directory
24+
DEFAULT_ADD_RH_CERTIFICATION_LABELS="false"
25+
ADD_RH_CERTIFICATION_LABELS=${ADD_RH_CERTIFICATION_LABELS:-$DEFAULT_ADD_RH_CERTIFICATION_LABELS}
26+
27+
# details on how versions are reflected by this label.
28+
# https://redhat-connect.gitbook.io/certified-operator-guide/ocp-deployment/operator-metadata/bundle-directory/managing-openshift-versions
29+
cert_label_openshift_supported_version="com.redhat.openshift.versions"
30+
DEFAULT_SUPPORTED_OPENSHIFT_VERSIONS="v4.6" # v4.6 and on
31+
SUPPORTED_OPENSHIFT_VERSIONS=${SUPPORTED_OPENSHIFT_VERSIONS:-$DEFAULT_SUPPORTED_OPENSHIFT_VERSIONS}
32+
33+
# Identifies that the delivery mechanism is the bundle format
34+
cert_label_operator_bundle_delivery="com.redhat.delivery.operator.bundle"
35+
DEFAULT_RED_HAT_DELIVERY_BUNDLE="true"
36+
RED_HAT_DELIVERY_BUNDLE=${RED_HAT_DELIVERY_BUNDLE:-$DEFAULT_RED_HAT_DELIVERY_BUNDLE}
37+
38+
# Do not backport to OpenShift versions prior to v4.5 by default.
39+
cert_label_deliver_backport="com.redhat.deliver.backport"
40+
DEFAULT_RED_HAT_DELIVER_BACKPORT="false" # do not list in openshift <v4.5
41+
RED_HAT_DELIVER_BACKPORT=${RED_HAT_DELIVER_BACKPORT:-$DEFAULT_RED_HAT_DELIVER_BACKPORT}
42+
43+
USAGE="
44+
Usage:
45+
$(basename "$0") <aws_service> <bundle_version>
46+
47+
Builds the Docker image for an ACK service controller.
48+
49+
Example: $(basename "$0") ecr
50+
51+
<aws_service> should be an AWS Service name (ecr, sns, sqs, petstore, bookstore)
52+
<bundle_version> an operator lifecycle manager bundle version in semver (0.0.1, 1.0.0)
53+
54+
55+
Environment variables:
56+
QUIET: Build bundle container image quietly (<true|false>)
57+
Default: false
58+
SERVICE_CONTROLLER_SOURCE_PATH: Path to the service controller source code
59+
repository.
60+
Default: ../{SERVICE}-controller
61+
DOCKER_REPOSITORY: Name for the Docker repository to push to
62+
Default: $DEFAULT_DOCKER_REPOSITORY
63+
BUNDLE_DOCKERFILE_DIR: Specify the directory where the bundle.Dockerfile exists.
64+
Default: {SERVICE_CONTROLLER_SOURCE_PATH}/olm
65+
BUNDLE_DOCKER_IMG_TAG: Bundle container image tag
66+
Default: \$AWS_SERVICE-bundle-\$BUNDLE_VERSION
67+
BUNDLE_DOCKER_IMG: The bundle container image (including the tag).
68+
Supercedes the use of BUNDLE_DOCKER_IMAGE_TAG
69+
and DOCKER_REPOSITORY if set.
70+
Default: $DEFAULT_DOCKER_REPOSITORY:\$AWS_SERVICE-bundle-\$BUNDLE_VERSION
71+
ADD_RH_CERTIFICATION_LABELS Adds the certification labels required by Red Hat
72+
container certification (<true|false>)
73+
Default: $DEFAULT_ADD_RH_CERTIFICATION_LABELS
74+
SUPPORTED_OPENSHIFT_VERSIONS: Indicates what versions of OpenShift are supported
75+
Only used if the ADD_RH_CERTIFICATION_LABELS is
76+
set to true.
77+
Default: $DEFAULT_SUPPORTED_OPENSHIFT_VERSIONS
78+
RED_HAT_DELIVERY_BUNDLE: Red Hat should deliver the operator as a bundle.
79+
Only used if the ADD_RH_CERTIFICATION_LABELS is
80+
set to true.
81+
Default: $DEFAULT_RED_HAT_DELIVERY_BUNDLE
82+
RED_HAT_DELIVER_BACKPORT: Red Hat should backport the operator to versions of
83+
OpenShift prior to v4.5. Only used if the
84+
ADD_RH_CERTIFICATION_LABELS is set to true.
85+
Default: $DEFAULT_RED_HAT_DELIVER_BACKPORT
86+
"
87+
88+
if [ $# -ne 2 ]; then
89+
echo "AWS_SERVICE or BUNDLE_VERSION is not defined. Script accepts two parameters, the <AWS_SERVICE> and the <BUNDLE_VERSION> to build." 1>&2
90+
echo "${USAGE}"
91+
exit 1
92+
fi
93+
94+
AWS_SERVICE=$(echo "$1" | tr '[:upper:]' '[:lower:]')
95+
BUNDLE_VERSION="$2"
96+
97+
DEFAULT_SERVICE_CONTROLLER_SOURCE_PATH="$ROOT_DIR/../$AWS_SERVICE-controller"
98+
SERVICE_CONTROLLER_SOURCE_PATH=${SERVICE_CONTROLLER_SOURCE_PATH:-$DEFAULT_SERVICE_CONTROLLER_SOURCE_PATH}
99+
DEFAULT_BUNDLE_DOCKERFILE_DIR="${SERVICE_CONTROLLER_SOURCE_PATH}/olm"
100+
BUNDLE_DOCKERFILE_DIR=${BUNDLE_DOCKERFILE_DIR:-$DEFAULT_BUNDLE_DOCKERFILE_DIR}
101+
BUNDLE_DOCKERFILE="$BUNDLE_DOCKERFILE_DIR/bundle.Dockerfile"
102+
103+
# stop if the dockerfile was not found
104+
if [ ! -f $BUNDLE_DOCKERFILE ]; then
105+
echo "The bundle.Dockerfile was not found at expected path $BUNDLE_DOCKERFILE."
106+
exit 1
107+
fi
108+
109+
DEFAULT_BUNDLE_DOCKER_IMG_TAG="$AWS_SERVICE-bundle-$BUNDLE_VERSION"
110+
BUNDLE_DOCKER_IMG_TAG=${BUNDLE_DOCKER_IMG_TAG:-$DEFAULT_BUNDLE_DOCKER_IMG_TAG}
111+
BUNDLE_DOCKER_IMG=${BUNDLE_DOCKER_IMAGE:-$DOCKER_REPOSITORY:$BUNDLE_DOCKER_IMG_TAG}
112+
113+
build_args="--quiet=${QUIET} -t ${BUNDLE_DOCKER_IMG} -f ${BUNDLE_DOCKERFILE} --build-arg build_date=\"$BUILD_DATE\""
114+
115+
if [[ $ADD_RH_CERTIFICATION_LABELS = "true" ]]; then
116+
# add additional labels with values for certification purposes.
117+
build_args="$build_args --label=$cert_label_openshift_supported_version=$SUPPORTED_OPENSHIFT_VERSIONS --label=$cert_label_deliver_backport=$RED_HAT_DELIVER_BACKPORT --label=$cert_label_operator_bundle_delivery=$RED_HAT_DELIVERY_BUNDLE"
118+
fi
119+
120+
if [[ $QUIET = "false" ]]; then
121+
echo "building '$AWS_SERVICE' OLM bundle container image with tag: ${BUNDLE_DOCKER_IMG}"
122+
fi
123+
124+
pushd $BUNDLE_DOCKERFILE_DIR 1>/dev/null
125+
docker build $build_args .
126+
127+
if [ $? -ne 0 ]; then
128+
exit 2
129+
fi
130+
131+
popd 1>/dev/null

scripts/olm-create-bundle.sh

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/usr/bin/env bash
2+
3+
# A script that creates the Operator Lifecycle Manager bundle for a
4+
# specific ACK service controller
5+
6+
set -eo pipefail
7+
8+
SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
9+
ROOT_DIR="$SCRIPTS_DIR/.."
10+
BUILD_DIR="$ROOT_DIR/build"
11+
DEFAULT_BUNDLE_CHANNEL="alpha"
12+
DEFAULT_SERVICE_CONTROLLER_CONTAINER_REPOSITORY="public.ecr.aws/aws-controllers-k8s/controller"
13+
PRESERVE=${PRESERVE:-"false"}
14+
15+
export DOCKER_BUILDKIT=${DOCKER_BUILDKIT:-1}
16+
17+
source "$SCRIPTS_DIR/lib/common.sh"
18+
19+
check_is_installed uuidgen
20+
check_is_installed kustomize "You can install kustomize with the helper scripts/install-kustomize.sh"
21+
check_is_installed operator-sdk "You can install Operator SDK with the helmer scripts/install-operator-sdk.sh"
22+
23+
function clean_up {
24+
if [[ "$PRESERVE" == false ]]; then
25+
rm -r "$TMP_DIR" || :
26+
return
27+
fi
28+
echo "--"
29+
echo "To regenerate bundle with the same kustomize configs, re-run with:
30+
\"TMP_DIR=$TMP_DIR\""
31+
}
32+
33+
USAGE="
34+
Usage:
35+
$(basename "$0") <service> <version>
36+
37+
<service> should be an AWS service API aliases that you wish to build -- e.g.
38+
's3' 'sns' or 'sqs'
39+
40+
Environment variables:
41+
BUNDLE_DEFAULT_CHANNEL The default channel to publish the OLM bundle to.
42+
Default: $DEFAULT_BUNDLE_CHANNEL
43+
BUNDLE_VERSION The semantic version of the bundle should it need
44+
to differ from the version of the controller which is
45+
passed into the script. Optional. If unset, this
46+
value will match that passed in by the user as the
47+
<version> parameter (i.e. the controller version)
48+
BUNDLE_CHANNELS A comma-separated list of channels the bundle belongs
49+
to (e.g. \"alpha,beta\").
50+
Default: $DEFAULT_BUNDLE_CHANNEL
51+
BUNDLE_OUTPUT_PATH: Specify a path for the OLM bundle to output to.
52+
Default: {SERVICE_CONTROLLER_SOURCE_PATH}/olm
53+
AWS_SERVICE_DOCKER_IMG: Specify the docker image for the AWS service.
54+
This should include the registry, namespace,
55+
image name, and tag. Takes precedence over
56+
SERVICE_CONTROLLER_CONTAINER_REPOSITORY
57+
SERVICE_CONTROLLER_SOURCE_PATH: Path to the service controller source code
58+
repository.
59+
Default: ../{SERVICE}-controller
60+
SERVICE_CONTROLLER_CONTAINER_REPOSITORY The container repository where the controller exists.
61+
This should include the registry, namespace, and
62+
image name.
63+
Default: $DEFAULT_SERVICE_CONTROLLER_CONTAINER_REPOSITORY
64+
TMP_DIR Directory where kustomize assets will be temporarily
65+
copied before they are modified and passed to bundle
66+
generation logic.
67+
Default: $BUILD_DIR/tmp-olm-{RANDOMSTRING}
68+
PRESERVE: Preserves modified kustomize configs for
69+
inspection. (<true|false>)
70+
Default: false
71+
BUNDLE_GENERATE_EXTRA_ARGS Extra arguments to pass into the command
72+
'operator-sdk generate bundle'.
73+
"
74+
75+
if [ $# -ne 2 ]; then
76+
echo "ERROR: $(basename "$0") accepts two parameters, the SERVICE and VERSION" 1>&2
77+
echo "$USAGE"
78+
exit 1
79+
fi
80+
81+
SERVICE=$(echo "$1" | tr '[:upper:]' '[:lower:]')
82+
VERSION=$2
83+
BUNDLE_VERSION=${BUNDLE_VERSION:-$VERSION}
84+
85+
DEFAULT_SERVICE_CONTROLLER_SOURCE_PATH="$ROOT_DIR/../$SERVICE-controller"
86+
SERVICE_CONTROLLER_SOURCE_PATH=${SERVICE_CONTROLLER_SOURCE_PATH:-$DEFAULT_SERVICE_CONTROLLER_SOURCE_PATH}
87+
88+
BUNDLE_OUTPUT_PATH=${BUNDLE_OUTPUT_PATH:-$SERVICE_CONTROLLER_SOURCE_PATH/olm}
89+
90+
if [ -z "$TMP_DIR" ]; then
91+
TMP_ID=$(uuidgen | cut -d'-' -f1 | tr '[:upper:]' '[:lower:]')
92+
TMP_DIR=$BUILD_DIR/tmp-olm-$TMP_ID
93+
fi
94+
# if TMP_DIR is provided but doesn't exist, we still use
95+
# it and create it later.
96+
tmp_kustomize_config_dir="$TMP_DIR/config"
97+
98+
if [[ ! -d $SERVICE_CONTROLLER_SOURCE_PATH ]]; then
99+
echo "Error evaluating SERVICE_CONTROLLER_SOURCE_PATH environment variable:" 1>&2
100+
echo "$SERVICE_CONTROLLER_SOURCE_PATH is not a directory." 1>&2
101+
echo "${USAGE}"
102+
exit 1
103+
fi
104+
105+
# Set controller image.
106+
if [ -n "$AWS_SERVICE_DOCKER_IMG" ] && [ -n "$SERVICE_CONTROLLER_CONTAINER_REPOSITORY" ] ; then
107+
# It's possible to set the repository (i.e. everything except the tag) as well as the
108+
# entire path including the tag using AWS_SERVIC_DOCKER_IMG. If the latter is set, it
109+
# will take precedence, so inform the user that this will happen in case the usage of
110+
# each configurable is unclear before runtime.
111+
echo "both AWS_SERVICE_DOCKER_IMG and SERVICE_CONTROLLER_CONTAINER REPOSITORY are set."
112+
echo "AWS_SERVICE_DOCKER_IMG is expected to be more complete and will take precedence."
113+
echo "Ignoring SERVICE_CONTROLLER_CONTAINER_REPOSITORY."
114+
fi
115+
116+
SERVICE_CONTROLLER_CONTAINER_REPOSITORY=${SERVICE_CONTROLLER_CONTAINER_REPOSITORY:-$DEFAULT_SERVICE_CONTROLLER_CONTAINER_REPOSITORY}
117+
DEFAULT_AWS_SERVICE_DOCKER_IMAGE="$SERVICE_CONTROLLER_CONTAINER_REPOSITORY:$SERVICE-$VERSION"
118+
AWS_SERVICE_DOCKER_IMG=${AWS_SERVICE_DOCKER_IMG:-$DEFAULT_AWS_SERVICE_DOCKER_IMAGE}
119+
120+
trap "clean_up" EXIT
121+
122+
# prepare the temporary config dir
123+
mkdir -p $TMP_DIR
124+
cp -a $SERVICE_CONTROLLER_SOURCE_PATH/config $TMP_DIR
125+
pushd $tmp_kustomize_config_dir/controller 1>/dev/null
126+
kustomize edit set image controller="$AWS_SERVICE_DOCKER_IMG"
127+
popd 1>/dev/null
128+
129+
# prepare bundle generate arguments
130+
opsdk_gen_bundle_args="--version $BUNDLE_VERSION --package ack-$SERVICE-controller --kustomize-dir $SERVICE_CONTROLLER_SOURCE_PATH/config/manifests --overwrite "
131+
132+
# specify default channel and all channels if not specified by user
133+
BUNDLE_DEFAULT_CHANNEL=${BUNDLE_DEFAULT_CHANNEL:-$DEFAULT_BUNDLE_CHANNEL}
134+
BUNDLE_CHANNELS=${BUNDLE_CHANNELS:-$DEFAULT_BUNDLE_CHANNEL}
135+
136+
opsdk_gen_bundle_args="$opsdk_gen_bundle_args --default-channel $DEFAULT_BUNDLE_CHANNEL --channels $BUNDLE_CHANNELS"
137+
if [ -n "$BUNDLE_GENERATE_EXTRA_ARGS" ]; then
138+
opsdk_gen_bundle_args="$opsdk_gen_bundle_args $BUNDLE_GENERATE_EXTRA_ARGS"
139+
fi
140+
141+
# operator-sdk generate bundle creates a bundle.Dockerfile relative
142+
# to where it's called and it cannot be changed as of right now.
143+
# For the time being, keep the bundle.Dockerfile local to the actual
144+
# bundle assets.
145+
# TODO(): determine if it makes sense to keep the bundle.Dockerfiles
146+
# in the controller-specific repositories.
147+
mkdir -p $BUNDLE_OUTPUT_PATH
148+
pushd $BUNDLE_OUTPUT_PATH 1> /dev/null
149+
kustomize build $tmp_kustomize_config_dir/manifests | operator-sdk generate bundle $opsdk_gen_bundle_args
150+
popd 1> /dev/null
151+
152+
operator-sdk bundle validate $BUNDLE_OUTPUT_PATH/bundle

scripts/olm-publish-bundle-image.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6+
SCRIPTS_DIR=$DIR
7+
DEFAULT_DOCKER_REPOSITORY="amazon/aws-controllers-k8s"
8+
DOCKER_REPOSITORY=${DOCKER_REPOSITORY:-$DEFAULT_DOCKER_REPOSITORY}
9+
10+
source $SCRIPTS_DIR/lib/common.sh
11+
12+
check_is_installed docker
13+
14+
USAGE="
15+
Usage:
16+
$(basename "$0") <AWS_SERVICE> <BUNDLE_VERSION>
17+
18+
Publishes the Docker image for an ACK service OLM bundle. By default, the
19+
repository will be $DEFAULT_DOCKER_REPOSITORY and the image tag for the
20+
specific ACK service controller will be ":\$SERVICE-bundle-\$VERSION".
21+
22+
<AWS_SERVICE> AWS Service name (ecr, sns, sqs)
23+
<BUNDLE_VERSION> OLM bundle version in SemVer (0.0.1, 1.0.0)
24+
25+
Example:
26+
export DOCKER_REPOSITORY=aws-controllers-k8s
27+
$(basename "$0") ecr 0.0.1
28+
29+
Environment variables:
30+
DOCKER_REPOSITORY: Name for the Docker repository to push to
31+
Default: $DEFAULT_DOCKER_REPOSITORY
32+
BUNDLE_DOCKER_IMG_TAG: Bundle container image tag
33+
Default: \$AWS_SERVICE-bundle-\$BUNDLE_VERSION
34+
BUNDLE_DOCKER_IMG: The bundle container image (including the tag).
35+
Supercedes the use of BUNDLE_DOCKER_IMAGE_TAG
36+
and DOCKER_REPOSITORY if set.
37+
Default: $DEFAULT_DOCKER_REPOSITORY:\$AWS_SERVICE-bundle-\$BUNDLE_VERSION
38+
"
39+
40+
if [ $# -ne 2 ]; then
41+
echo "AWS_SERVICE or BUNDLE_VERSION is not defined. Script accepts two parameters, the <AWS_SERVICE> and the <BUNDLE_VERSION> to build." 1>&2
42+
echo "${USAGE}"
43+
exit 1
44+
fi
45+
46+
AWS_SERVICE=$(echo "$1" | tr '[:upper:]' '[:lower:]')
47+
BUNDLE_VERSION="$2"
48+
49+
DEFAULT_BUNDLE_DOCKER_IMG_TAG="$AWS_SERVICE-bundle-$BUNDLE_VERSION"
50+
BUNDLE_DOCKER_IMG_TAG=${BUNDLE_DOCKER_IMG_TAG:-$DEFAULT_BUNDLE_DOCKER_IMG_TAG}
51+
BUNDLE_DOCKER_IMG=${BUNDLE_DOCKER_IMAGE:-$DOCKER_REPOSITORY:$BUNDLE_DOCKER_IMG_TAG}
52+
53+
export BUNDLE_DOCKER_IMG
54+
${SCRIPTS_DIR}/olm-build-bundle-image.sh ${AWS_SERVICE} ${BUNDLE_VERSION}
55+
56+
echo "Pushing '$AWS_SERVICE' operator lifecycle manager bundle image with tag: ${BUNDLE_DOCKER_IMG_TAG}"
57+
58+
docker push ${BUNDLE_DOCKER_IMG}
59+
60+
if [ $? -ne 0 ]; then
61+
exit 2
62+
fi

0 commit comments

Comments
 (0)