forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-crds.sh
executable file
·240 lines (215 loc) · 8.6 KB
/
generate-crds.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
#!/usr/bin/env bash
#
# Copyright 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# 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 -o errexit
set -o nounset
set -o pipefail
# Usage:
# generate-crds.sh [<flags>] <group> [<group> ...]
# Generate manifests for all CRDs to the current working directory.
# Useful for development purposes.
#
# -p <file-name-prefix> File name prefix for manifest files (e.g. '10-crd-')
# -l (Optional) If this argument is given then the generated CRDs will have label gardener.cloud/deletion-protected: "true"
# -k (Optional) If this argument is given then the generated CRDs will have annotation resources.gardener.cloud/keep-object: "true"
# -r <reason> (Optional) If this argument is given then the generated CRDs will have annotation api-approved.kubernetes.io: "<reason>"
# --allow-dangerous-types (Optional) If this argument is given then the CRD generation will tolerate issues related to dangerous types.
# <group> List of groups to generate (generate all if unset)
if ! command -v controller-gen &> /dev/null ; then
>&2 echo "controller-gen not available"
exit 1
fi
output_dir="$(pwd)"
output_dir_temp="$(mktemp -d)"
add_deletion_protection_label=false
add_keep_object_annotation=false
k8s_io_api_approval_reason="unapproved, temporarily squatting"
crd_options=""
# setup virtual GOPATH
source $(dirname $0)/vgopath-setup.sh
export GO111MODULE=off
get_group_package () {
case "$1" in
"extensions.gardener.cloud")
echo "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
;;
"resources.gardener.cloud")
echo "github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
;;
"operator.gardener.cloud")
echo "github.com/gardener/gardener/pkg/apis/operator/v1alpha1"
;;
"druid.gardener.cloud")
echo "github.com/gardener/etcd-druid/api/v1alpha1"
;;
"hvpaautoscaling.k8s.io")
echo "github.com/gardener/hvpa-controller/api/v1alpha1"
;;
"fluentbit.fluent.io")
echo "github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2"
;;
"monitoring.coreos.com_v1")
echo "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
;;
"monitoring.coreos.com_v1beta1")
echo "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1beta1"
;;
"monitoring.coreos.com_v1alpha1")
echo "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
;;
"autoscaling.k8s.io")
echo "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
;;
"machine.sapcloud.io")
echo "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
;;
*)
>&2 echo "unknown group $1"
return 1
esac
}
generate_all_groups () {
generate_group extensions.gardener.cloud
generate_group resources.gardener.cloud
generate_group operator.gardener.cloud
generate_group druid.gardener.cloud
generate_group hvpaautoscaling.k8s.io
generate_group autoscaling.k8s.io
generate_group fluentbit.fluent.io
generate_group monitoring.coreos.com_v1
generate_group monitoring.coreos.com_v1beta1
generate_group monitoring.coreos.com_v1alpha1
generate_group machine.sapcloud.io
}
generate_group () {
local group="$1"
echo "Generating CRDs for $group group"
local package="$(get_group_package "$group")"
if [ -z "$package" ] ; then
exit 1
fi
local package_path="$(go list -f '{{ .Dir }}' "$package")"
if [ -z "$package_path" ] ; then
exit 1
fi
generate="controller-gen crd"$crd_options" paths="$package_path" output:crd:dir="$output_dir_temp" output:stdout"
if [[ "$group" == "druid.gardener.cloud" ]]; then
# /scale subresource is intentionally removed from this CRD, although it is specified in the original CRD from
# etcd-druid, due to adverse interaction with VPA.
# See https://github.com/gardener/gardener/pull/6850 and https://github.com/gardener/gardener/pull/8560#discussion_r1347470394
# TODO(shreyas-s-rao): Remove this workaround as soon as the scale subresource is supported properly.
etcd_druid_dir="$(go list -f '{{ .Dir }}' "github.com/gardener/etcd-druid")"
etcd_api_types_file="${etcd_druid_dir}/api/v1alpha1/types_etcd.go"
# Create a local copy outside the mod cache path in order to patch the types file via sed.
etcd_api_types_backup="$(mktemp -d)/types_etcd.go"
cp "$etcd_api_types_file" "$etcd_api_types_backup"
chmod +w "$etcd_api_types_file" "$etcd_druid_dir/api/v1alpha1/"
trap 'cp "$etcd_api_types_backup" "$etcd_api_types_file" && chmod -w "$etcd_druid_dir/api/v1alpha1/"' EXIT
sed -i '/\/\/ +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.labelSelector/d' "$etcd_api_types_file"
$generate
elif [[ "$group" == "autoscaling.k8s.io" ]]; then
# See https://github.com/kubernetes/autoscaler/blame/master/vertical-pod-autoscaler/hack/generate-crd-yaml.sh#L43-L45
generator_output="$(mktemp -d)/controller-gen.log"
# As go list does not work with symlinks we need to manually construct the package paths to correctly
# generate v1beta2 CRDs.
package_path="${package_path};${package_path}beta2;"
generate="controller-gen crd"$crd_options" paths="$package_path" output:crd:dir="$output_dir_temp" output:stdout"
$generate &> "$generator_output" ||:
grep -v -e 'map keys must be strings, not int' -e 'not all generators ran successfully' -e 'usage' "$generator_output" && { echo "Failed to generate CRD YAMLs."; exit 1; }
else
$generate
fi
local relevant_files=("$@")
sanitized_group_name="${group/hvpa/}"
sanitized_group_name="${sanitized_group_name%%_*}"
while IFS= read -r crd; do
crd_out="$output_dir/$file_name_prefix$(basename $crd)"
mv "$crd" "$crd_out"
relevant_files+=("$(basename "$crd_out")")
if $add_deletion_protection_label; then
if grep -q "clusters.extensions.gardener.cloud" "$crd_out"; then
:
else
sed -i '4 a\ labels:\n\ gardener.cloud/deletion-protected: "true"' "$crd_out"
fi
fi
if $add_keep_object_annotation; then
sed -i '/^ annotations:.*/a\ resources.gardener.cloud/keep-object: "true"' "$crd_out"
fi
# TODO(plkokanov): this is needed to add the `api-approved.kubernetes.io` annotaiton to resource from the *.k8s.io api group generated by controller-gen
# Currently there is an issue open to do that automatically: https://github.com/kubernetes-sigs/controller-tools/issues/656
if [[ ${group} =~ .*\.k8s\.io ]]; then
sed -i "/^ annotations:.*/a\ api-approved.kubernetes.io: $k8s_io_api_approval_reason" "$crd_out"
fi
done < <(ls "$output_dir_temp/$sanitized_group_name"_*.yaml)
# garbage collection - clean all generated files for this group to account for changed prefix or removed resources
local pattern=".*${group}_.*\.yaml"
if [[ "$group" == "autoscaling.k8s.io" ]]; then
pattern=".*${group}_v.*\.yaml"
fi
while IFS= read -r file; do
file_name=$(basename "$file")
delete_no_longer_needed_file=true
for relevant_file_name in "${relevant_files[@]}"; do
if [[ $file_name == "$relevant_file_name" ]] || [[ ! $file_name =~ $pattern ]]; then
delete_no_longer_needed_file=false
break
fi
done
if $delete_no_longer_needed_file; then
rm "$file"
fi
done < <(ls "$output_dir")
}
parse_flags() {
while test $# -gt 0; do
case "$1" in
-p)
file_name_prefix="$2"
shift
shift
;;
-r)
k8s_io_api_approval_reason="$2"
shift
shift
;;
-l)
add_deletion_protection_label=true
shift
;;
-k)
add_keep_object_annotation=true
shift
;;
--allow-dangerous-types)
crd_options=":allowDangerousTypes=true"
shift
;;
*)
args+=("$1")
shift
;;
esac
done
}
parse_flags "$@"
if [ -n "$args" ]; then
for group in "${args[@]}"; do
generate_group "$group"
done
else
generate_all_groups
fi