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
0 commit comments