forked from syndesisio/syndesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.lib.sh
310 lines (273 loc) · 10.6 KB
/
.lib.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env bash
#
# Some reusable shell functions:
#
__ARGS__=("$@")
# Outputs the value of a command line option.
# Usage: readopt <flag> <default value>
# Example: kind=$(readopt --kind MyKind)
readopt() {
local filter="$1"
local default="$2"
local next=false
for var in "${__ARGS__[@]}"; do
if $next; then
local value="${var##-}"
if [ "$value" != "$var" ]; then
# Next is already also option, so assume it's being used like a flag
echo true
return
fi
echo $var
return
fi
if [[ "$var" = "${filter}"* ]]; then
local value="${var//${filter}=/}"
if [ "$value" != "$var" ]; then
echo $value
return
fi
next=true
fi
done
if $next; then
# Ran out of options treat like a flag
echo true
fi
echo $default
}
build_operator()
{
local strategy="$1"
shift
local source_gen="$1"
shift
local hasgo=$(go_is_available)
local hasdocker=$(docker_is_available)
if [ "$strategy" == "auto" ] ; then
if [ "$hasgo" == "OK" ]; then
strategy="go"
elif [ "$hasdocker" == "OK" ]; then
# go not available so try docker
printf "WARN: Building with 'docker' since 'go' command is not available ... \n\t\t$hasgo\n"
strategy="docker"
else
echo "ERROR: Building the operator requires either 'docker' or 'go' commands to be installed."
exit 1
fi
fi
case "$strategy" in
"go")
if [ "$hasgo" != "OK" ]; then
echo "$hasgo"
exit 1
fi
echo ======================================================
echo Building executable with go tooling
echo ======================================================
export GO111MODULE=on
if [[ ( "$source_gen" == "on" ) || ( "$source_gen" == "verify-none" ) ]]; then
echo "generating sources"
go mod vendor
local hassdk=$(operatorsdk_is_available)
if [ "$hassdk" == "OK" ]; then
osdk_version=$(operator-sdk version | sed -n 's/.*version: "v\([^"]*\)".*/\1/p')
if [[ ${osdk_version} == 0.* ]]; then
echo "operator-sdk >= 1.0.0 required. Please upgrade ..."
exit 1
else
# Calls the config/Makefile which in turn uses controller-gen
# As described by the operator-sdk documentation
pushd config > /dev/null && make generate && popd > /dev/null
fi
else
# display warning message and move on
printf "$hassdk\n\n"
fi
openapi_gen
go generate ./pkg/...
go mod tidy
if [ "$source_gen" == "verify-none" ]; then
echo "verifying no sources have been generated"
for file in pkg/apis/syndesis/v1beta3/zz_generated.deepcopy.go; do
if [ "$(git diff $file)" != "" ] ; then
echo ===========================================
echo Looks like some generated source code
echo not previously checked in. See diff:
echo ===========================================
echo
git diff $file
exit 1
fi
done
fi
else
echo "skipping source generation"
fi
echo building executable
go test -test.short -mod=vendor ./cmd/... ./pkg/...
if [ -z "${GOOSLIST}" ]; then
GOOSLIST="linux darwin windows"
fi
for GOARCH in amd64 ; do
for GOOS in ${GOOSLIST} ; do
export GOARCH GOOS
echo building ./dist/${GOOS}-${GOARCH}/syndesis-operator executable
go build "$@" -o ./dist/${GOOS}-${GOARCH}/syndesis-operator \
-gcflags all=-trimpath=${GOPATH} -asmflags all=-trimpath=${GOPATH} -mod=vendor \
./cmd/manager
echo building ./dist/${GOOS}-${GOARCH}/platform-detect executable
go build -o ./dist/${GOOS}-${GOARCH}/platform-detect \
-gcflags all=-trimpath=${GOPATH} -asmflags all=-trimpath=${GOPATH} -mod=vendor \
./cmd/detect
done
done
mkdir -p ./build/_output/bin
cp ./dist/linux-amd64/syndesis-operator ./build/_output/bin/syndesis-operator
;;
"docker")
if [ "$hasdocker" != "OK" ]; then
echo "$hasdocker"
exit 1
fi
local BUILDER_IMAGE_NAME="operator-builder"
echo ======================================================
echo Building executable with Docker
echo ======================================================
rm -rf build/_output
OPTS=""
for i in "$@" ; do
OPTS="$OPTS '$i'"
done
cat > "${BUILDER_IMAGE_NAME}.tmp" <<EODockerfile
FROM golang:1.16
WORKDIR /go/src/${OPERATOR_GO_PACKAGE}
ENV GO111MODULE=on
COPY . .
RUN go generate ./pkg/...
RUN go test -test.short -mod=vendor ./cmd/... ./pkg/...
RUN GOOS=linux GOARCH=amd64 go build $OPTS -o /dist/linux-amd64/syndesis-operator -gcflags all=-trimpath=\${GOPATH} -asmflags all=-trimpath=\${GOPATH} -mod=vendor github.com/syndesisio/syndesis/install/operator/cmd/manager
RUN GOOS=darwin GOARCH=amd64 go build $OPTS -o /dist/darwin-amd64/syndesis-operator -gcflags all=-trimpath=\${GOPATH} -asmflags all=-trimpath=\${GOPATH} -mod=vendor github.com/syndesisio/syndesis/install/operator/cmd/manager
RUN GOOS=windows GOARCH=amd64 go build $OPTS -o /dist/windows-amd64/syndesis-operator -gcflags all=-trimpath=\${GOPATH} -asmflags all=-trimpath=\${GOPATH} -mod=vendor github.com/syndesisio/syndesis/install/operator/cmd/manager
EODockerfile
docker build -t "${BUILDER_IMAGE_NAME}" . -f "${BUILDER_IMAGE_NAME}.tmp"
rm -f "${BUILDER_IMAGE_NAME}.tmp"
echo ======================================================
for GOARCH in amd64 ; do
for GOOS in linux darwin windows ; do
echo extracting executable to ./dist/${GOOS}-${GOARCH}/syndesis-operator
mkdir -p ./dist/${GOOS}-${GOARCH}
docker run "${BUILDER_IMAGE_NAME}" cat /dist/${GOOS}-${GOARCH}/syndesis-operator > ./dist/${GOOS}-${GOARCH}/syndesis-operator
done
done
chmod a+x ./dist/*/syndesis-operator
mkdir -p ./build/_output/bin
cp ./dist/linux-amd64/syndesis-operator ./build/_output/bin/syndesis-operator
;;
*)
echo invalid build strategy: $strategy
exit 1
esac
}
build_image()
{
local strategy="$1"
local OPERATOR_IMAGE_NAME="$2"
local OPERATOR_IMAGE_TAG="$3"
local S2I_STREAM_NAME="$4"
local hasdocker=$(docker_is_available)
local hasoc=$(is_oc_available)
if [ "$strategy" == "auto" ] ; then
if [ "$hasoc" == "OK" -o "$hasoc" == "$SETUP_MINISHIFT" ]; then
strategy="s2i"
elif [ "$hasdocker" == "OK" ]; then
printf "\nWARN: Building image with 'docker' since 'oc' command is not available for s2i ... \n\t\t$hasoc\n"
strategy="docker"
else
echo "ERROR: Building an image requires either 'docker' or 'oc' commands to be installed."
exit 1
fi
fi
case "$strategy" in
"s2i")
if [ "$hasoc" == "$SETUP_MINISHIFT" ]; then
setup_minishift_oc > /dev/null
elif [ "$hasoc" != "OK" ]; then
echo "$hasoc"
exit 1
fi
echo ======================================================
echo Building image with S2I
echo ======================================================
if [ -z "$(oc get bc -o name | grep ${S2I_STREAM_NAME})" ]; then
echo "Creating BuildConfig ${S2I_STREAM_NAME} with tag ${OPERATOR_IMAGE_TAG}"
oc new-build --strategy=docker --binary=true --to=${OPERATOR_IMAGE_NAME}:${OPERATOR_IMAGE_TAG} --name ${S2I_STREAM_NAME}
fi
local arch="$(mktemp -t ${S2I_STREAM_NAME}-dockerXXX).tar"
echo $arch
trap "rm $arch" EXIT
tar --exclude-from=.dockerignore -cvf $arch build
if [ ! -d build ]; then
echo "No build directory. Something failed with building image"
exit 1
fi
cd build
tar uvf $arch Dockerfile
oc start-build --from-archive=$arch ${S2I_STREAM_NAME}
;;
"docker")
if [ "$hasdocker" != "OK" ]; then
echo "$hasdocker"
exit 1
fi
echo ======================================================
echo Building image with Docker
echo ======================================================
docker build -f "build/Dockerfile" -t "${OPERATOR_IMAGE_NAME}:${OPERATOR_IMAGE_TAG}" .
echo ======================================================
echo "Operator Image Built: ${OPERATOR_IMAGE_NAME}"
echo ======================================================
;;
*)
echo invalid build strategy: $1
exit 1
esac
}
openapi_gen() {
if ! command -v openapi-gen &> /dev/null
then
echo "Downloading and installing openapi-gen ..."
go install k8s.io/kube-openapi/cmd/openapi-gen@release-1.22
if [ $? != 0 ]; then
echo "Error: Failed to install openapi-gen"
exit 1
fi
else
echo "Warning: openapi-gen already installed but cannot guarantee if its version is compatible."
echo " To ensure compatibility, please set aside the current version."
fi
openapi-gen --logtostderr=true -o "" \
-i ./pkg/apis/syndesis/v1alpha1 -O zz_generated.openapi -p ./pkg/apis/syndesis/v1alpha1
if [ $? != 0 ]; then
echo "Error: openapi-gen failed to generate the API"
exit 1
fi
openapi-gen --logtostderr=true -o "" \
-i ./pkg/apis/syndesis/v1beta1 -O zz_generated.openapi -p ./pkg/apis/syndesis/v1beta1
if [ $? != 0 ]; then
echo "Error: openapi-gen failed to generate the API"
exit 1
fi
openapi-gen --logtostderr=true -o "" \
-i ./pkg/apis/syndesis/v1beta2 -O zz_generated.openapi -p ./pkg/apis/syndesis/v1beta2
if [ $? != 0 ]; then
echo "Error: openapi-gen failed to generate the API"
exit 1
fi
openapi-gen --logtostderr=true -o "" \
-i ./pkg/apis/syndesis/v1beta3 -O zz_generated.openapi -p ./pkg/apis/syndesis/v1beta3
if [ $? != 0 ]; then
echo "Error: openapi-gen failed to generate the API"
exit 1
fi
}