Repository navigation
Expand file tree
/
Copy pathldev
More file actions
executable file
·1213 lines (1149 loc) · 31.9 KB
/
Copy pathldev
File metadata and controls
executable file
·1213 lines (1149 loc) · 31.9 KB
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
6871
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Reference: https://github.com/minixxie/ldev
if [ "$(which yq)" == "" ]; then
echo >&2 "WARN: yq command is missing, please install with 'pip3 isntall yq'."
fi
if [ "$(which kubectl)" == "" ]; then
echo >&2 "WARN: kubectl command is missing, please install it accordingly. (mac: brew install kubectl)"
fi
ldevRoot="$HOME/.ldev"
k8sProj="k8s-services"
mysql="infra-mysql@8.4.9"
pgsql="infra-pgsql@15.1.0"
redis="infra-redis-cluster"
mongo="infra-mongodb@7.0.5"
ollama="ai-ollama"
apollo="infra-apollo-server"
minio="infra-minio"
golangImg=minixxie/golang:1.26.2
rustImg=rust:alpine
function help() {
cat <<EOF
Usage: $0 arg1 arg2 arg3 ...
Arguments:"
├─ help...........show this help message
│ ├─ k8s.........show help message for argument 'k8s'
│ └─ gpu.........show help message for argument 'gpu'
├─ install........download & install the 'ldev' script, upgrade if already exists
EOF
help_vm
help_k8s
help_gpu
help_mysql
help_pgsql
help_redis
help_mongo
help_apollo
help_minio
help_ollama
help_run
help_pb
help_golang
help_rust
cat <<EOF
├─ init...........initialize ldev.yaml for the application in the current folder
├─ get............get info for the application in the current folder (check for kustomization/ folder)
├─ up.............start the application in the current folder (check for kustomization/ folder)
├─ down...........stop the application in the current folder (check for kustomization/ folder)
├─ restart........rollout restart the application deployments in the current folder (check for kustomization/ folder)
├─ logs...........tail the logs of the application in the current folder (check for kustomization/ folder)
├─ desc...........describe the application pod in the current folder (check for kustomization/ folder)
├─ exec...........exec shell into the pod of the application in the current folder
├─ top............display resource (CPU/memory) usage of the pod of the application in the current folder
└─ http...........call HTTP API with a *.http file as argument (e.g. ldev http get_user.http)
EOF
}
function help_vm() {
cat <<EOF
├─ vm.............control vm (only applicable for mac os)
│ ├─ restart.....restart the lima VM
│ └─ fixtime.....fix the time lag of the VM
EOF
}
function help_k8s() {
cat <<EOF
├─ k8s............get/up/down for k8s with colima/k3s
│ ├─ get.........get info of k8s
│ ├─ up..........start k8s
│ ├─ down........stop k8s
│ ├─ restart.....restart k8s
│ └─ redo........recreate k8s
EOF
}
function help_gpu() {
cat <<EOF
├─ gpu............get/up/down for nvidia gpu operator
│ ├─ get.........get info of nvidia gpu operator
│ ├─ up..........start nvidia gpu operator
│ └─ down........stop nvidia gpu operator
EOF
}
function help_mysql() {
cat <<EOF
├─ mysql..........get/up/down for mysql
│ ├─ get.........get info of mysql
│ ├─ up..........start mysql
│ ├─ down........stop mysql
│ └─ cli.........invoke mysql cli
EOF
}
function help_pgsql() {
cat <<EOF
├─ pgsql..........get/up/down for pgsql
│ ├─ get.........get info of pgsql
│ ├─ up..........start pgsql
│ ├─ down........stop pgsql
│ └─ cli.........invoke pgsql cli
EOF
}
function help_redis() {
cat <<EOF
├─ redis..........get/up/down for redis-cluster
│ ├─ get.........get info of redis-cluster
│ ├─ up..........start redis-cluster
│ ├─ down........stop redis-cluster
│ └─ cli.........invoke redis-cluster cli
EOF
}
function help_mongo() {
cat <<EOF
├─ mongo..........get/up/down for mongodb
│ ├─ get.........get info of mongodb
│ ├─ up..........start mongodb
│ ├─ down........stop mongodb
│ └─ cli.........invoke mongodb cli
EOF
}
function help_apollo() {
cat <<EOF
├─ apollo.........get/up/down for apollo
│ ├─ get.........get info of apollo
│ ├─ up..........start apollo
│ ├─ down........stop apollo
│ ├─ web.........open web of apollo
│ └─ test........test apollo
EOF
}
function help_minio() {
cat <<EOF
├─ minio..........get/up/down for MinIO
│ ├─ get.........get info of MinIO
│ ├─ up..........start MinIO
│ ├─ down........stop MinIO
│ ├─ web.........open web of MinIO console
│ └─ test........test MinIO
EOF
}
function help_ollama() {
cat <<EOF
├─ ollama.........get/up/down for ollama
│ ├─ get.........get info of ollama
│ ├─ up..........start ollama
│ ├─ down........stop ollama
│ └─ test........test ollama
EOF
}
function help_run() {
cat <<EOF
├─ run............run a pod on-demand
│ ├─ ubuntu......run a pod with ubuntu image
│ ├─ alpine......run a pod with alpine image
│ └─ curl........run a pod with curl image
EOF
}
function help_pb() {
cat <<EOF
├─ pb.............commands for handling protobuf files
│ ├─ dep-up......"buf dep update" in a container
│ ├─ gen........."buf generate" in a container
│ └─ fmt.........run a container shell to gofmt the code
EOF
}
function help_golang() {
cat <<EOF
├─ golang.........development commands related to golang
│ ├─ sh..........run a container shell with mounted current working directory
│ ├─ buf.........run a container shell to run "buf generate"
│ ├─ buf-up......run a container shell to run "buf dep update"
│ ├─ api-linter..run a container shell to check api issue
│ └─ fmt.........run a container shell to gofmt the code
EOF
}
function help_rust() {
cat <<EOF
├─ rust...........development commands related to rust
│ └─ sh..........run a container shell with mounted current working directory
EOF
}
function install() {
sudo curl -sSL https://raw.githubusercontent.com/minixxie/ldev/refs/heads/main/ldev | \
sudo tee /usr/local/bin/ldev.new > /dev/null \
&& sudo chmod 755 /usr/local/bin/ldev.new \
&& sudo mv -f /usr/local/bin/ldev.new /usr/local/bin/ldev
}
function bootstrap() {
if ! [ -d "$ldevRoot" ]; then
mkdir -p "$ldevRoot"
chmod 700 "$ldevRoot"
fi
if ! [ -d "$ldevRoot"/"$k8sProj" ]; then
cd "$ldevRoot" && git clone --depth 1 https://github.com/minixxie/k8s-services.git > /dev/null
fi
cd "$ldevRoot"/"$k8sProj" && git pull --rebase > /dev/null
}
function vm_fixtime() {
tz=$(readlink /etc/localtime | sed 's#/var/db/timezone/zoneinfo/##g')
echo "[Before fix]"
echo "Host: "$(TZ=$tz date '+%Y-%m-%d %H:%M:%S %Z')
echo "VM: "$(TZ=$tz limactl shell ldev date '+%Y-%m-%d %H:%M:%S %Z')
limactl shell ldev sudo date -s "$(TZ=$tz date '+%Y-%m-%d %H:%M:%S')"
echo "[After fix]"
echo "Host: "$(TZ=$tz date '+%Y-%m-%d %H:%M:%S %Z')
echo "VM: "$(TZ=$tz limactl shell ldev date '+%Y-%m-%d %H:%M:%S %Z')
}
function vm_restart() {
limactl restart ldev
}
function k8s_get() {
if [ "$(uname -s)" == "Darwin" ]; then
echo "# colima list"
colima list
echo ""
fi
echo "# kubectl get node -o wide"
kubectl get node -o wide
echo ""
echo "# kubectl get ns"
kubectl get ns
}
function k8s_up() {
bootstrap
cd "$ldevRoot"/"$k8sProj" && make -s k8s-redo
}
function k8s_down() {
bootstrap
cd "$ldevRoot"/"$k8sProj" && make -s k8s-down
}
function k8s_restart() {
if [ "$(uname -s)" == "Darwin" ]; then
limactl restart ldev
else
sudo systemctl restart k3s
fi
}
function k8s_redo() {
bootstrap
cd "$ldevRoot"/"$k8sProj" && make -s k8s-redo
}
function gpu_get() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/nvidia-gpu-operator && make get
}
function gpu_up() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/nvidia-gpu-operator && make img local wait test
}
function gpu_down() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/nvidia-gpu-operator && make down
}
function mysql_get() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$mysql" && make get
}
function mysql_up() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$mysql" && make img local wait test
}
function mysql_down() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$mysql" && make down
}
function mysql_cli() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$mysql" && make cli
}
function pgsql_get() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$pgsql" && make get
}
function pgsql_up() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$pgsql" && make img local wait test
}
function pgsql_down() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$pgsql" && make down
}
function pgsql_cli() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$pgsql" && make cli
}
function redis_get() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$redis" && make get
}
function redis_up() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$redis" && make img local wait test
}
function redis_down() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$redis" && make down
}
function redis_cli() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$redis" && make cli
}
function mongo_get() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$mongo" && make get
}
function mongo_up() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$mongo" && make img local wait test
}
function mongo_down() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$mongo" && make down
}
function mongo_cli() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$mongo" && make cli
}
function apollo_get() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$apollo"-db && make get
cd "$ldevRoot"/"$k8sProj"/"$apollo" && make get
}
function apollo_up() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$apollo"-db && make build up get wait
cd "$ldevRoot"/"$k8sProj"/"$apollo" && make img local wait test
}
function apollo_down() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$apollo" && make down
cd "$ldevRoot"/"$k8sProj"/"$apollo"-db && make down
}
function apollo_web() {
bootstrap
url="http://apollo.local/"
echo "$url"
echo "# remember to add this host into /etc/hosts"
open "$url"
}
function apollo_test() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$apollo" && make test
}
function minio_get() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$minio" && make get
}
function minio_up() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$minio" && make img local wait test
}
function minio_down() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$minio" && make down
}
function minio_web() {
bootstrap
echo "http://minio.local/"
echo "user: AK_LOCAL"
echo "pass: SK_LOCAL"
echo "s3 endpoint: http://s3.minio.local/"
echo "# remember to add these hosts into /etc/hosts"
open "http://minio.local/"
}
function minio_test() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$minio" && make test
}
function ollama_get() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$ollama" && make get
}
function ollama_up() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$ollama" && make img local wait test
}
function ollama_down() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$ollama" && make down
}
function ollama_test() {
bootstrap
cd "$ldevRoot"/"$k8sProj"/"$ollama" && make test
}
function run_ubuntu() {
kubectl run ubuntu-$(date "+%s") --rm -it --restart=Never --image ubuntu --command -- bash
}
function run_alpine() {
kubectl run alpine-$(date "+%s") --rm -it --restart=Never --image alpine --command -- sh
}
function run_curl() {
kubectl run curl-$(date "+%s") --rm -it --restart=Never --image curlimages/curl --command -- sh
}
function pb_depup() {
nerdctl --namespace=k8s.io run -t --rm \
--user "$(id -u):$(id -g)" \
-e HOME=/tmp \
-v "$(pwd):/src" -w /src \
minixxie/protoc-gen-grpc:1.0.0 buf dep update
}
function pb_gen() {
rm -rf ./gen/* || true
nerdctl --namespace=k8s.io run -t --rm \
--user "$(id -u):$(id -g)" \
-e HOME=/tmp \
-v "$(pwd):/src" -w /src \
minixxie/protoc-gen-grpc:1.0.0 bash -c "api-linter.sh && buf generate"
echo "Generation completed. Remember to commit the files under ./gen/ folder."
}
function golang_sh() {
app=$(yaml_read_app)
echo "Reminded these commands are available:"
echo "install-protoc-plugins.sh # Step 1: install protoc plugin (run in the project root)"
echo "go mod download # Step 2: download all vendor files (run in the project root)"
echo "build.sh # Step 3: build the golang project (run in the project root)"
echo "buf generate # Step 4: generate the code from protobuf files (run in the project root)"
echo "api-linter.sh # Step 5: check if the API design has any problem (run in the project root)"
echo "Others:"
echo "gofmt -w . # format the source code (run in the project root)"
echo 'ls $GOPATH # check files in the GOPATH'
echo "callapi.sh < xxx.http # use curl to make an API call (load from xxx.http)"
nerdctl --namespace=k8s.io run --rm -it -v \
"$(pwd):/go/src/$app" -w "/go/src/$app" \
"$golangImg" bash
}
function rust_sh() {
#app=$(yaml_read_app)
echo "Reminded these commands are available:"
echo "cargo generate-lockfile"
nerdctl --namespace=k8s.io run --net=host --rm -it -v \
"$(pwd):/app" -w "/app" \
"$rustImg" sh
}
function golang_buf() {
app=$(yaml_read_app)
nerdctl --namespace=k8s.io run --rm -t -v \
"$(pwd):/go/src/$app" -w "/go/src/$app" \
"$golangImg" bash -c \
"install-protoc-plugins.sh && go mod download && buf generate"
}
function golang_buf_up() {
app=$(yaml_read_app)
nerdctl --namespace=k8s.io run --rm -t -v \
"$(pwd):/go/src/$app" -w "/go/src/$app" \
"$golangImg" bash -c \
"install-protoc-plugins.sh && go mod download && buf dep update"
}
function golang_api_linter() {
app=$(yaml_read_app)
nerdctl --namespace=k8s.io run --rm -t -v \
"$(pwd):/go/src/$app" -w "/go/src/$app" \
"$golangImg" bash -c \
"install-protoc-plugins.sh && go mod download && buf generate && api-linter.sh"
}
function golang_fmt() {
app=$(yaml_read_app)
nerdctl --namespace=k8s.io run --rm -t -v \
"$(pwd):/go/src/$app" -w "/go/src/$app" \
"$golangImg" gofmt -w .
}
function yaml_read_app() {
init
app=$(yq -r '.ldev.app' < ldev.yaml)
if [ "$app" == "" ]; then
echo >&2 "ERR: ldev.app is missing in ldev.yaml"
exit 1
fi
echo "$app"
}
function yaml_read_java() {
init
httpPort=$(yq -r '.ldev.java.httpPort' < ldev.yaml)
if [ x"$httpPort" == "" ]; then
httpPort=8080
fi
actuatorPort=$(yq -r '.ldev.java.actuatorPort' < ldev.yaml)
if [ x"$actuatorPort" == "" ]; then
actuatorPort=8081
fi
actuatorReadinessPath=$(yq -r '.ldev.java.actuatorReadinessPath' < ldev.yaml)
if [ x"$actuatorReadinessPath" == "" ]; then
actuatorReadinessPath=/actuator/health/readiness
fi
springProfilesActive=$(yq -r '.ldev.java.springProfilesActive' < ldev.yaml)
if [ x"$springProfilesActive" == "" ]; then
springProfilesActive=localk8s
fi
# sample: "8080\t8081\t/actuator/health/readiness\tlocalk8s"
echo "$httpPort $actuatorPort $actuatorReadinessPath $springProfilesActive"
}
function yaml_read_golang() {
init
httpPort=$(yq -r '.ldev.golang.httpPort' < ldev.yaml)
if [ x"$httpPort" == "" ]; then
httpPort=80
fi
grpcPort=$(yq -r '.ldev.golang.grpcPort' < ldev.yaml)
if [ x"$grpcPort" == "" ]; then
grpcPort=9090
fi
# sample: "80\t9090"
echo "$httpPort $grpcPort"
}
function gen_jdk_containerfile() {
if ! [ -f ./Containerfile ]; then
javaConfig=$(yaml_read_java)
httpPort=$(echo -n "$javaConfig" | cut -f 1)
actuatorPort=$(echo -n "$javaConfig" | cut -f 2)
cat <<EOF > ./Containerfile
FROM openjdk:8-jdk as jdk
ARG APP
WORKDIR /app
EOF
if [ -f build.gradle ]; then
cat <<EOF >> ./Containerfile
ADD ./gradle ./gradle
ADD ./gradlew ./
RUN ./gradlew wrapper --gradle-version=7.6.2
ADD . .
RUN ./gradlew bootJar --no-daemon -x test
EOF
fi
cat <<EOF >> ./Containerfile
FROM minixxie/eclipse-temurin:21.0.2_13-jre
ARG APP
ENV APP=\${APP}
COPY --from=jdk /app/build/libs/*.jar /app.jar
RUN /update-tz.sh
EXPOSE $httpPort
EXPOSE $actuatorPort
EXPOSE 9999
EOF
fi
}
function gen_golang_containerfile() {
if ! [ -f ./Containerfile ]; then
golangConfig=$(yaml_read_golang)
httpPort=$(echo -n "$golangConfig" | cut -f 1)
grpcPort=$(echo -n "$golangConfig" | cut -f 2)
cat <<EOF > ./Containerfile
FROM --platform=\$BUILDPLATFORM minixxie/golang:1.26.2 as golang
ARG BUILDPLATFORM
ARG APP
#ENV GOPATH=/usr/local/go
WORKDIR \$GOPATH/src/\${APP}
#ADD ./micro ./micro
COPY ./go.mod .
COPY ./go.sum .
RUN install-protoc-plugins.sh
RUN go mod download
ADD ./pb ./pb
ADD ./buf.yaml ./buf.yaml
ADD ./buf.lock ./buf.lock
ADD ./buf.gen.yaml ./buf.gen.yaml
RUN buf generate
RUN api-linter.sh
ADD . .
RUN build.sh
RUN cp ./bin/server /tmp/
RUN cp ./bin/client /tmp/
RUN cp ./gen/openapiv2/all.swagger.json /tmp/swagger.json
FROM gcr.io/distroless/base
COPY --from=golang /tmp/server /server
COPY --from=golang /tmp/client /client
# To be served with http://xxx/swagger.json (loaded by http://xxx/docs):
COPY --from=golang /tmp/swagger.json /swagger.json
# HTTP API via grpc-gateway
EXPOSE $httpPort
# gRPC
EXPOSE $grpcPort
ENTRYPOINT ["/server"]
EOF
fi
}
function gen_containerfile() {
progLang=$(detect_prog_lang)
if ! [ -f Containerfile ]; then
if ! [ -f Dockerfile ]; then
# Java/Kotlin
if [ "$progLang" == "java" ]; then
gen_jdk_containerfile
echo Containerfile
fi
if [ "$progLang" == "golang" ]; then
gen_golang_containerfile
echo Containerfile
fi
else
echo Dockerfile
fi
else
echo Containerfile
fi
}
function detect_prog_lang() {
if [ -f build.gradle ]; then
echo "java"
exit 0
fi
if [ -f pom.xml ]; then
echo "java"
exit 0
fi
if [ -f go.mod ]; then
echo "golang"
exit 0
fi
}
function init() {
if [ ! -f ldev.yaml ]; then
folderName=$(basename $(pwd))
progLang=$(detect_prog_lang)
cat <<EOF > ldev.yaml
ldev:
app: $folderName
EOF
if [ "$progLang" == "java" ]; then
cat <<EOF >> ldev.yaml
java:
httpPort: 8080
actuatorPort: 8081
actuatorReadinessPath: /actuator/health/readiness
springProfilesActive: localk8s # requires ./src/main/resources/application-localk8s.yml
EOF
fi
if [ "$progLang" == "golang" ]; then
cat <<EOF >> ldev.yaml
golang:
httpPort: 80
grpcPort: 9090
EOF
fi
fi
}
function yaml_read_ns() {
ns=
if [ -f "kustomization/base/namespace.yaml" ]; then
ns=$(grep "^ name:" kustomization/base/namespace.yaml | cut -f 2 -d: | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//')
fi
if [ "$ns" == "" ]; then
ns=$(yaml_read_app)
fi
echo $ns
}
function get() {
ns=$(yaml_read_ns)
if [ -d "kustomization" ]; then
if [ -d "kustomization/overlays/local" ]; then
kubectl get -k ./kustomization/overlays/local
else
kubectl get -k ./kustomization/base
fi
fi
echo ""
kubectl -n $ns get pod
}
function build() {
if [ -f Makefile ]; then
grep "^build:" Makefile > /dev/null
if [ $? -eq 0 ]; then
make build # assuming Makefile is there and build target is there for building docker image
return 0
fi
fi
echo >&2 "WARN: Makefile and the target 'build:' (to use nerdctl to build the container image) are not there."
echo >&2 " Auto-generate for it..."
init
containerfile=$(gen_containerfile)
if [ -f "$containerfile" ]; then
app=$(yaml_read_app)
nerdctl --namespace=k8s.io build . -f $containerfile -t local/$app:dont_push --build-arg APP=$app
nerdctl --namespace=k8s.io images | grep local/$app | grep dont_push
fi
}
function gen_java_deployment() {
app=$(yaml_read_app)
javaConfig=$(yaml_read_java)
httpPort=$(echo -n "$javaConfig" | cut -f 1)
actuatorPort=$(echo -n "$javaConfig" | cut -f 2)
actuatorReadinessPath=$(echo -n "$javaConfig" | cut -f 3)
springProfilesActive=$(echo -n "$javaConfig" | cut -f 4)
cat <<EOF > ./kustomization/base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: $app
namespace: $app
spec:
replicas: 1
selector:
matchLabels:
app: $app
template:
metadata:
labels:
app: $app
spec:
containers:
- name: java
image: local/$app:dont_push
imagePullPolicy: IfNotPresent
command:
- /bin/bash
- -c
- |
/update-tz.sh
java --add-opens java.base/java.lang=ALL-UNNAMED -jar /app.jar
ports:
- name: http
containerPort: $httpPort # spring boot HTTP port
EOF
if [ "$actuatorPort" != "$httpPort" ]; then
cat <<EOF >> ./kustomization/base/deployment.yaml
- name: http-metrics
containerPort: $actuatorPort # spring boot actuator HTTP port
EOF
fi
cat <<EOF >> ./kustomization/base/deployment.yaml
- name: xxljob
containerPort: 9999 # xxl-job HTTP port
readinessProbe:
httpGet:
path: $actuatorReadinessPath
port: $actuatorPort
initialDelaySeconds: 10
periodSeconds: 30
env:
- name: app
value: $app
- name: SPRING_PROFILES_ACTIVE
value: $springProfilesActive
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
EOF
}
function gen_golang_deployment() {
app=$(yaml_read_app)
golangConfig=$(yaml_read_golang)
httpPort=$(echo -n "$golangConfig" | cut -f 1)
grpcPort=$(echo -n "$golangConfig" | cut -f 2)
cat <<EOF > ./kustomization/base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: $app
namespace: $app
spec:
replicas: 1
selector:
matchLabels:
app: $app
template:
metadata:
labels:
app: $app
spec:
containers:
- name: golang
image: local/$app:dont_push
imagePullPolicy: IfNotPresent
command: ["/server"]
ports:
- name: http
containerPort: $httpPort # HTTP port
- name: grpc
containerPort: $grpcPort # gRPC port
env:
# just in case useful for accessing any daemonset
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
- name: MICRO_REDOC
value: "1"
- name: CORS_ORIGIN
value: "*"
EOF
}
function up() {
build
if ! [ -d "kustomization" ]; then
echo >&2 "WARN: kustomization/ folder not found."
echo >&2 " Auto-generate for it..."
mkdir -p ./kustomization
mkdir -p ./kustomization/base
mkdir -p ./kustomization/overlays
mkdir -p ./kustomization/overlays/local
app=$(yaml_read_app)
port=80
cat <<EOF > ./kustomization/base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- namespace.yaml
- deployment.yaml
- service.yaml
- ingress.yaml
EOF
cat <<EOF > ./kustomization/base/namespace.yaml
kind: Namespace
apiVersion: v1
metadata:
name: $app
EOF
progLang=$(detect_prog_lang)
if [ "$progLang" == "java" ]; then
gen_java_deployment
elif [ "$progLang" == "golang" ]; then
gen_golang_deployment
fi
cat <<EOF > ./kustomization/base/service.yaml
apiVersion: v1
kind: Service
metadata:
name: $app
namespace: $app
spec:
selector:
app: $app
type: LoadBalancer
ports:
- name: http
protocol: TCP
port: $httpPort
targetPort: $httpPort
EOF
if [ "$progLang" == "golang" ]; then
cat <<EOF >> ./kustomization/base/service.yaml
- name: grpc
protocol: TCP
port: $grpcPort
targetPort: $grpcPort
EOF
fi
cat <<EOF > ./kustomization/base/ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: $app
namespace: $app
spec:
rules:
- host: $app.local
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: $app
port:
number: $httpPort
EOF
cat <<EOF > ./kustomization/overlays/local/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
EOF
fi
if [ -d "kustomization/overlays/local" ]; then
kubectl apply -k ./kustomization/overlays/local
else
kubectl apply -k ./kustomization/base
fi
}
function down() {
if [ -d "kustomization" ]; then
if [ -d "kustomization/overlays/local" ]; then
kubectl delete -k ./kustomization/overlays/local
else
kubectl delete -k ./kustomization/base
fi
fi
}
function restart_deployment() {
if [ -d "kustomization" ]; then
if [ -d "kustomization/overlays/local" ]; then
kubectl rollout restart -k ./kustomization/overlays/local
else
kubectl rollout restart -k ./kustomization/base
fi
fi
}
function logs() {
ns=$(yaml_read_ns)
kubectl -n "$ns" logs -f $(kubectl -n "$ns" get pod | sed 1d | awk '{print $1}')
}
function desc() {
ns=$(yaml_read_ns)
kubectl -n "$ns" describe pod $(kubectl -n "$ns" get pod | sed 1d | awk '{print $1}')
}
function pod_shell() {
ns=$(yaml_read_ns)
kubectl -n "$ns" exec -it $(kubectl -n "$ns" get pod | sed 1d | awk '{print $1}') -- sh -c 'bash || sh'
}
function pod_top() {
ns=$(yaml_read_ns)
kubectl -n "$ns" top pod $(kubectl -n "$ns" get pod | sed 1d | awk '{print $1}')
}
function call_http() {
if [ -f "$1" ]; then
bodyJson=`cat "$1"`
else
bodyJson=`cat`
fi
emptyLineNum=$(echo -n "$bodyJson" | grep -E --line-number '^$' | head -n 1 | cut -f 1 -d ':')
firstLine=$(echo -n "$bodyJson" | head -n 1)
verb=$(echo -n "$firstLine" | cut -f 1 -d ' ')
url=$(echo -n "$firstLine" | cut -f 2 -d ' ')
tmpfile=$(mktemp /tmp/XXXXXX)
function cleanup {
rm -f "$tmpfile"
}
trap cleanup EXIT
headers=""
contentRemoveFirstLine=$(echo -n "$bodyJson" | tail -n +2)
if [ "$emptyLineNum" != "" ]; then
# has empty line
headerLines=$(echo -n "$bodyJson" | head -n $(expr $emptyLineNum - 1) | tail -n +2 | sed '/^\s*$/d')
headers=$(echo -n "$headerLines" | sed "s/^/-H '/" | sed "s/\$/'/" | tr '\012' ' ')
echo -n "$bodyJson" | tail -n +$(expr $emptyLineNum + 1) > "$tmpfile"
else
# has NO empty line
headerLines=$(echo -n "$contentRemoveFirstLine" | sed '/^\s*$/d')
headers=$(echo -n "$headerLines" | sed "s/^/-H '/" | sed "s/\$/'/" | tr '\012' ' ')
echo -n "" > "$tmpfile" # empty body
fi
if [ "$LOAD_TEST" == 1 ]; then
if [ "$TOTAL_REQS" == "" ]; then
TOTAL_REQS=1000
fi
if [ "$CONCURRENCY" == "" ]; then
CONCURRENCY=10
fi
if [ "$verb" == "GET" ]; then
echo "$headers $url" | xargs ab -n "$TOTAL_REQS" -c "$CONCURRENCY" -m "$verb"
else
echo "$headers $url" | xargs ab -n "$TOTAL_REQS" -c "$CONCURRENCY" -p "$tmpfile"