-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathk8s2tf.sh
executable file
·254 lines (217 loc) · 6.36 KB
/
k8s2tf.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
usage() {
echo "Usage: $0 [-p <profile>] [-c] [-v] [-r <region>] [-t <type>] [-h] [-d] [-n] <namespace name>"
echo " -p <profile> specify the AWS profile to use (Default=\"default\")"
echo " -c <yes|no> (default=no) Continue from previous run"
echo " -f <yes|no> (default=no) fast forward, use with -c"
echo " -r <region> specify the AWS region to use (Default=the aws command line setting)"
echo " -v <yes|no> (default=no) Stop after terraform validate step"
echo " -h Help - this message"
echo " -d <yes|no|st|info> (default=no) Debug - lots of output if info"
echo " -cl <cluster> EKS cluster"
echo " -n <namespace> which namespace to use"
echo " -t <type> choose a sub-type of K8s resources to get:"
echo " configmap"
echo " serviceaccount"
echo " namespace"
echo " role"
echo " rolebinding"
echo " clusterrolebinding"
echo " daemonset"
echo " clusterrole"
echo " networkpolicy"
echo " deployment"
echo " service"
echo " ingress"
echo " hpa"
echo " job"
exit 1
}
x="no"
p="default" # profile
f="no"
v="no"
r="no" # region
d="no"
n=""
while getopts ":p:r:x:f:v:t:i:c:d:h:n:cl:" o; do
case "${o}" in
h)
usage
;;
i)
i=${OPTARG}
;;
t)
t=${OPTARG}
;;
r)
r=${OPTARG}
;;
x)
x="yes"
;;
p)
p=${OPTARG}
;;
f)
f="yes"
;;
v)
v="yes"
;;
c)
c=${OPTARG}
;;
d)
d=${OPTARG}
;;
n)
n=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
trap ctrl_c INT
function ctrl_c() {
echo "Requested to stop."
exit 1
}
if [ "$d" = "info" ]; then
set -x
echo "CAUTION - lots of output, potentially including sensitive information"
fi
if [ ! -z ${AWS_PROFILE+x} ]; then
p=$(echo $AWS_PROFILE)
echo "profile $AWS_PROFILE set from env variables"
fi
export aws2tfmess="# File generated by aws2tf see https://github.com/aws-samples/aws2tf"
if [ -z ${AWS_ACCESS_KEY_ID+x} ] && [ -z ${AWS_SECRET_ACCESS_KEY+x} ]; then
mysub=$(aws sts get-caller-identity --output json --profile $p | jq .Account | tr -d '"')
else
mysub=$(aws sts get-caller-identity --output json | jq .Account | tr -d '"')
fi
if [ "$r" = "no" ]; then
if [ ! -z ${AWS_DEFAULT_REGION+x} ]; then
r=$(echo $AWS_DEFAULT_REGION)
echo "region $AWS_DEFAULT_REGION set from env variable AWS_DEFAULT_REGION"
fi
if [ ! -z ${AWS_REGION+x} ]; then
r=$(echo $AWS_REGION)
echo "region $AWS_REGION set from env variable AWS_REGION"
fi
if [ "$r" = "no" ]; then
r=$(aws configure get region)
echo "Getting region from aws cli = $r"
fi
fi
#if [ "$t" == "no" ]; then t="*"; fi
if [ "$c" == "" ]; then
echo "EKS cluster name must be supplied with -c " && exit
fi
mycluster=$(echo $c)
f="no"
mkdir -p generated/tf.$mycluster
cd generated/tf.$mycluster
if [ "$f" = "no" ]; then
echo "Cleaning generated/tf.$mycluster"
rm -f import.log resources*.txt
rm -f processed.txt
rm -f *.tf *.json
rm -f terraform.*
rm -rf .terraform
else
sort -u processed.txt >pt.txt
cp pt.txt processed.txt
fi
# write the k8s.tf file
printf "terraform {\n" >k8s.tf
printf " required_version = \">= 1.9.5\"\n" >>k8s.tf
printf " required_providers {\n" >>k8s.tf
printf " kubernetes = {\n" >>k8s.tf
printf " source = \"hashicorp/kubernetes\"\n" >>k8s.tf
printf " version = \">=2.36.0\"\n" >>k8s.tf
printf " }\n" >>k8s.tf
printf " }\n" >>k8s.tf
printf "}\n" >>k8s.tf
printf "provider \"kubernetes\" {\n" >>k8s.tf
printf "config_path = \"~/.kube/config\"\n" >>k8s.tf
printf "}\n" >>k8s.tf
cat k8s.tf
echo "terraform init -upgrade"
terraform init -upgrade -no-color 2>&1 | tee -a import.log
if [[ $? -ne 0 ]]; then
echo "Terraform INit failed - exiting ....."
exit
fi
pwd
echo "t=$t pre=$pre"
pre="4*"
if [[ "$t" == "clusterrolebinding" ]]; then pre="051*"; fi
if [[ "$t" == "clusterrole" ]]; then pre="050*"; fi
if [[ "$t" == "secret" ]]; then pre="401*"; fi
if [[ "$t" == "configmap" ]]; then pre="404*"; fi
if [[ "$t" == "serviceaccount" ]]; then pre="410*"; fi
if [[ "$t" == "namespace" ]]; then pre="402*"; fi
if [[ "$t" == "role" ]]; then pre="412*"; fi
if [[ "$t" == "rolebinding" ]]; then pre="413*"; fi
if [[ "$t" == "daemonset" ]]; then pre="420*"; fi
if [[ "$t" == "networkpolicy" ]]; then pre="430*"; fi
if [[ "$t" == "deployment" ]]; then pre="472*"; fi
if [[ "$t" == "service" ]]; then pre="473*"; fi
if [[ "$t" == "ingress" ]]; then pre="475*"; fi
if [[ "$t" == "hpa" ]]; then pre="480*"; fi
if [[ "$t" == "job" ]]; then pre="490*"; fi
if [[ "$t" == "statefulset" ]]; then pre="477*"; fi
echo "t=$t c=$c n=$n"
date
lc=0
echo "t=$t pre=$pre"
echo "loop through providers"
pwd
for com in $(ls ../../scripts/$pre-*$t*.sh | cut -d'/' -f4 | sort -g); do
#echo "$com"
docomm=". ../../scripts/$com $n"
echo "$docomm"
if [ "$f" = "no" ]; then
eval $docomm 2>&1 | tee -a import.log
else
grep "$docomm" processed.txt
if [ $? -eq 0 ]; then
echo "skipping $docomm"
else
eval $docomm 2>&1 | tee -a import.log
fi
fi
lc=$(expr $lc + 1)
file="import.log"
while IFS= read -r line; do
if [[ "${line}" == *"Error"* ]]; then
if [[ "${line}" == *"Duplicate"* ]]; then
echo "Ignoring $line"
else
echo "Found Error: $line exiting .... (pass for now)"
pass
fi
fi
done <"$file"
echo "$docomm" >>processed.txt
rm -f terraform*.backup
done
#########################################################################
date
echo "---------------------------------------------------------------------------"
echo "k8s2tff output files are in generated/tf.$mycluster"
echo "---------------------------------------------------------------------------"
echo "Terraform fmt ..."
terraform fmt
echo "Terraform validate ..."
terraform validate
if [ "$v" = "yes" ]; then
exit
fi
echo "Terraform Plan ..."
terraform plan
echo "code in generated/tf.$mycluster"