-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathinstall
executable file
·165 lines (150 loc) · 4.9 KB
/
install
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
#!/bin/bash
#
# Copyright 2018 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.
set -eo pipefail
for i in "$@"
do
case $i in
--parameters=*)
parameters="${i#*=}"
shift
;;
--deployer=*)
deployer="${i#*=}"
shift
;;
--entrypoint=*)
entrypoint="${i#*=}"
shift
;;
--version_meta_file=*)
version_meta_file="${i#*=}"
shift
;;
--image_pull_secret=*)
image_pull_secret="${i#*=}"
shift
;;
--storage_class_provisioner=*)
storage_class_provisioner="${i#*=}"
shift
;;
*)
>&2 echo "Unrecognized flag: $i"
exit 1
;;
esac
done
[[ -z "$parameters" ]] && >&2 echo "--parameters required" && exit 1
[[ -z "$deployer" && -z "$version_meta_file" ]] && >&2 echo "One of --deployer or --version_meta_file is required" && exit 1
[[ -n "$deployer" && -n "$version_meta_file" ]] && >&2 echo "Only one of --deployer or --version_meta_file should be specified" && exit 1
[[ -z "$entrypoint" ]] && entrypoint="/bin/deploy.sh"
if [[ -n "$version_meta_file" ]]; then
if [[ "$version_meta_file" == gs://* ]]; then
deployer="$(gsutil cat "$version_meta_file" | yaml2json | jq -r '.url')"
elif [[ "$version_meta_file" == http://* || "$version_meta_file" == http://* ]]; then
deployer="$(wget -O- -q "$version_meta_file" | yaml2json | jq -r '.url')"
else
>&2 echo "Unsupported --version_meta_file value: $version_meta_file" && exit 1
fi
[[ -z "$deployer" ]] && >&2 echo "Unable to retrieve deployer from $version_meta_file." && exit 1
version_repo="$(dirname "$version_meta_file")"
else
version_repo=""
fi
# Extract schema and values files.
container_id=$(docker create "${deployer}")
docker cp "$container_id:/data/schema.yaml" /data/schema.yaml
docker rm "$container_id"
echo "INFO: /data/schema.yaml:"
cat /data/schema.yaml
echo "$parameters" \
| json2yaml \
> /data/values.yaml
# Extract name, namespace, and api version.
name="$(print_config.py \
--values_mode raw \
--xtype NAME)"
namespace="$(print_config.py \
--values_mode raw \
--xtype NAMESPACE)"
app_version="$(print_app_api_version.py)"
if [[ -z "$image_pull_secret" ]]; then
# Check the namespace annotation.
image_pull_secret="$(kubectl get namespace ${namespace} --ignore-not-found -o json \
| jq '.metadata.annotations["marketplace.cloud.google.com/imagePullSecret"]' \
| jq --raw-output 'select(. != null)')"
fi
# Create Application instance.
kubectl apply --namespace="$namespace" --filename=- <<EOF
apiVersion: "app.k8s.io/${app_version}"
kind: Application
metadata:
name: "${name}"
namespace: "${namespace}"
spec:
selector:
matchLabels:
app.kubernetes.io/name: "${name}"
assemblyPhase: "Pending"
EOF
app_uid=$(kubectl get "applications.app.k8s.io/$name" \
--namespace="$namespace" \
--output=jsonpath='{.metadata.uid}')
# Create ServiceAccount instance.
deployer_service_account_name="$(make_dns1123_name.py --name="${name}")-deployer-sa"
kubectl apply --namespace="$namespace" --filename=- <<EOF
apiVersion: "v1"
kind: ServiceAccount
metadata:
name: "${deployer_service_account_name}"
namespace: "${namespace}"
EOF
deployer_uid=$(kubectl get sa ${deployer_service_account_name} \
--namespace="$namespace" \
--output=jsonpath='{.metadata.uid}')
# Provisions external resource dependencies and the deployer resources.
# We set the application as the owner for all of the namespaced resources,
# and the deployer as the owner of its dependent RBAC resources.
manifest_dir="/tmp/${namespace}_${name}_$(date +'%Y-%m-%d_%H-%M-%S')"
echo "${parameters}" \
| provision.py \
--values_mode=stdin \
--deployer_image="${deployer}" \
--deployer_entrypoint="${entrypoint}" \
--deployer_service_account_name="${deployer_service_account_name}" \
--version_repo="${version_repo}" \
--image_pull_secret="${image_pull_secret}" \
--storage_class_provisioner="${storage_class_provisioner}" \
| set_app_labels.py \
--manifests=- \
--dest=- \
--name="${name}" \
--namespace="${namespace}" \
| set_ownership.py \
--manifests=- \
--dest="${manifest_dir}" \
--noapp \
--app_name="${name}" \
--app_uid="${app_uid}" \
--app_api_version="app.k8s.io/${app_version}" \
--deployer_name="${deployer_service_account_name}" \
--deployer_uid="${deployer_uid}"
echo "INFO: Applying the following manifests:"
cat "${manifest_dir}"
kubectl apply \
--namespace="${namespace}" \
--filename="${manifest_dir}"
rm -r "${manifest_dir}"