forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-crds.sh
executable file
·131 lines (118 loc) · 4.16 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
#!/usr/bin/env bash
#
# Copyright (c) 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 <file-name-prefix> [<group> ...]
# Generate manifests for all CRDs to the current working directory.
# Useful for development purposes.
#
# <file-name-prefix> File name prefix for manifest files (e.g. '10-crd-')
# -l (Optional) If -l argument is given then the generated CRDs will have label gardener.cloud/deletion-protected: "true"
# <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)"
file_name_prefix="$1"
add_deletion_protection_label=false
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"
;;
"autoscaling.k8s.io")
echo "github.com/gardener/hvpa-controller/api/v1alpha1"
;;
*)
>&2 echo "unknown group $1"
return 1
esac
}
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
# clean all generated files for this group to account for changed prefix or removed resources
if ls "$output_dir"/*${group}_*.yaml >/dev/null 2>&1; then
rm "$output_dir"/*${group}_*.yaml
fi
controller-gen crd paths="$package_path" output:crd:dir="$output_dir" output:stdout
while IFS= read -r crd; do
crd_out="$output_dir/$file_name_prefix$(basename $crd)"
if [ "$crd" != "$crd_out" ]; then
mv "$crd" "$crd_out"
fi
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
# 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: unapproved, temporarily squatting' "$crd_out"
fi
done < <(ls "$output_dir/${group}"_*.yaml)
}
if [ -n "${2:-}" ]; then
if [ "${2}" == "-l" ]; then
add_deletion_protection_label=true
if [ -n "${3:-}" ]; then
while [ -n "${3:-}" ] ; do
generate_group "$3"
shift
done
else
generate_group extensions.gardener.cloud
generate_group resources.gardener.cloud
generate_group operator.gardener.cloud
generate_group druid.gardener.cloud
generate_group autoscaling.k8s.io
fi
else
while [ -n "${2:-}" ] ; do
generate_group "$2"
shift
done
fi
else
generate_group extensions.gardener.cloud
generate_group resources.gardener.cloud
generate_group operator.gardener.cloud
generate_group druid.gardener.cloud
generate_group autoscaling.k8s.io
fi