Skip to content

Commit 6682d84

Browse files
authored
add docker and kind cleanup and do not install bins is they exists (#4)
Signed-off-by: Nikolay Mitrofanov <nikolay.mitrofanov@flant.com>
1 parent f7f02b5 commit 6682d84

File tree

8 files changed

+322
-14
lines changed

8 files changed

+322
-14
lines changed

.github/workflows/check_pull_request.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ jobs:
3636
- name: Run tests
3737
run: make test
3838

39+
- name: Cleanup after failed
40+
if: failure()
41+
run: clean/test
42+
3943
license_validation:
4044
needs: [run_tests]
4145
name: "Validate that license added to all files"

Makefile

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ SHELL = /usr/bin/env bash
1616

1717
GOLANGCI_VERSION = 2.7.2
1818
GOFUMPT_VERSION=0.9.2
19-
JQ_VERSION=1.7.1
19+
JQ_VERSION=1.8.1
20+
KIND_VERSION=0.31.0
2021

2122
PLATFORM_NAME := $(shell uname -m)
2223

@@ -33,48 +34,72 @@ endif
3334
ifeq ($(OS_NAME), Linux)
3435
GOFUMPT_PLATFORM = linux
3536
JQ_PLATFORM = linux
37+
KIND_PLATFORM = linux
3638
else ifeq ($(OS_NAME), Darwin)
3739
GOFUMPT_PLATFORM = darwin
3840
JQ_PLATFORM = macos
41+
KIND_PLATFORM = darwin
3942
endif
4043

4144
# Set arch for deps
4245
ifeq ($(PLATFORM_NAME), x86_64)
4346
GOFUMPT_ARCH = amd64
4447
JQ_PLATFORM_ARCH = $(JQ_PLATFORM)-amd64
48+
KIND_ARCH = amd64
4549
else ifeq ($(PLATFORM_NAME), arm64)
4650
GOFUMPT_ARCH = arm64
4751
JQ_PLATFORM_ARCH = $(JQ_PLATFORM)-arm64
52+
KIND_ARCH = arm64
4853
endif
4954

50-
.PHONY: bin/jq bin/gofumpt bin/golangci-lint clean validation/license/download
55+
.PHONY: bin/jq bin/gofumpt bin/golangci-lint clean validation/license/download curl-installed docker-installed go-installed clean/test clean/ssh clean/docker
5156

5257
bin:
5358
mkdir -p bin
5459

5560
curl-installed:
5661
command -v curl > /dev/null
5762

63+
docker-installed:
64+
command -v docker > /dev/null
65+
5866
go-installed:
59-
command -v go
67+
command -v go > /dev/null
6068
go version
6169

6270
bin/jq: curl-installed bin
63-
curl -sSfL https://github.com/jqlang/jq/releases/download/jq-$(JQ_VERSION)/jq-$(JQ_PLATFORM_ARCH) -o ./bin/jq
64-
@chmod +x "./bin/jq"
71+
if ! ./hack/check_binary.sh "jq" "--version" "$(JQ_VERSION)" ; then \
72+
echo "Install jq"; \
73+
curl -sSfL https://github.com/jqlang/jq/releases/download/jq-$(JQ_VERSION)/jq-$(JQ_PLATFORM_ARCH) -o ./bin/jq; \
74+
chmod +x "./bin/jq"; \
75+
fi
6576

6677
bin/golangci-lint: curl-installed bin
67-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | BINARY=golangci-lint bash -s -- v${GOLANGCI_VERSION}
68-
@chmod +x "./bin/golangci-lint"
78+
if ! ./hack/check_binary.sh "golangci-lint" "--version" "$(GOLANGCI_VERSION)"; then \
79+
echo "Install golangci-lint"; \
80+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | BINARY=golangci-lint bash -s -- v${GOLANGCI_VERSION}; \
81+
chmod +x "./bin/golangci-lint"; \
82+
fi
6983

7084
bin/gofumpt: curl-installed bin
71-
curl -sSfLo "bin/gofumpt" https://github.com/mvdan/gofumpt/releases/download/v$(GOFUMPT_VERSION)/gofumpt_v$(GOFUMPT_VERSION)_$(GOFUMPT_PLATFORM)_$(GOFUMPT_ARCH)
72-
@chmod +x "./bin/gofumpt"
73-
74-
deps: bin bin/jq bin/golangci-lint bin/gofumpt
75-
76-
test: go-installed
85+
if ! ./hack/check_binary.sh "gofumpt" "-version" "$(GOFUMPT_VERSION)"; then \
86+
echo "Install gofumpt"; \
87+
curl -sSfLo "bin/gofumpt" https://github.com/mvdan/gofumpt/releases/download/v$(GOFUMPT_VERSION)/gofumpt_v$(GOFUMPT_VERSION)_$(GOFUMPT_PLATFORM)_$(GOFUMPT_ARCH); \
88+
chmod +x "./bin/gofumpt"; \
89+
fi
90+
91+
bin/kind: curl-installed bin
92+
if ! ./hack/check_binary.sh "kind" "version" "$(KIND_VERSION)"; then \
93+
echo "Install kind"; \
94+
curl -sSfLo "bin/kind" https://github.com/kubernetes-sigs/kind/releases/download/v$(KIND_VERSION)/kind-$(KIND_PLATFORM)-$(KIND_ARCH); \
95+
chmod +x "./bin/kind"; \
96+
fi
97+
98+
deps: bin bin/jq bin/golangci-lint bin/gofumpt bin/kind
99+
100+
test: go-installed docker-installed bin/kind
77101
./hack/run_tests.sh
102+
$(MAKE) clean/test
78103

79104
lint: bin/golangci-lint
80105
./bin/golangci-lint run ./... -c .golangci.yaml
@@ -104,8 +129,20 @@ validation/license: go-installed validation/license/download
104129
# prevent goland ide errors
105130
rm -f ./validation/go.mod ./validation/go.sum
106131

132+
clean/ssh: clean/docker
133+
echo "Remove test dir /tmp/test-lib-connection"
134+
rm -rf /tmp/test-lib-connection
135+
136+
clean/docker: docker-installed
137+
./hack/clean_docker.sh
138+
139+
clean/kind: bin/kind
140+
./hack/clean_kind.sh
141+
142+
clean/test: clean/kind clean/ssh
143+
107144
all: bin deps validation/license fmt lint test
108145

109-
clean:
146+
clean: clean/test
110147
rm -rf ./bin
111148
rm -rf ./validation

hack/check_binary.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2026 Flant JSC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
binary="$1"
18+
version_arg="$2"
19+
version="$3"
20+
21+
function not_empty_or_exit() {
22+
if [ -z "$2" ]; then
23+
echo "$1 is empty"
24+
exit 1
25+
fi
26+
27+
return 0
28+
}
29+
30+
not_empty_or_exit "binary" "$binary"
31+
not_empty_or_exit "version_arg" "$version_arg"
32+
not_empty_or_exit "version" "$version"
33+
34+
binary_full_path="$(pwd)/bin/${binary}"
35+
36+
if [ ! -x "$binary_full_path" ]; then
37+
echo "$binary_full_path not exists or not executable"
38+
exit 1
39+
fi
40+
41+
if ! "$binary_full_path" "$version_arg" | grep -q "$version" ; then
42+
echo "$binary_full_path version not match ${version}. Version is $("$binary_full_path" "$version_arg")"
43+
exit 1
44+
fi
45+
46+
exit 0

hack/clean_docker.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2026 Flant JSC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
source "$(pwd)/hack/utils.sh"
18+
19+
check_docker
20+
21+
function get_tests_containers() {
22+
docker container ls --filter='name=test_lib_connection.*' --format='{{.ID}}'
23+
}
24+
25+
function rm_containers() {
26+
echo "Remove containers $@"
27+
docker container rm -f "$@"
28+
return $?
29+
}
30+
31+
function get_tests_networks() {
32+
docker network ls --filter='name=test_lib_connection.*' --format='{{.Name}}'
33+
}
34+
35+
function rm_networks() {
36+
echo "Remove networks $@"
37+
docker network rm "$@"
38+
return $?
39+
}
40+
41+
do_in_cycle "remove tests containers" get_tests_containers rm_containers
42+
do_in_cycle "remove tests networks" get_tests_networks rm_networks
43+
44+
echo "Docker cleanup done!"
45+
exit 0

hack/clean_kind.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2026 Flant JSC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
source "$(pwd)/hack/utils.sh"
18+
19+
check_all_deps
20+
21+
kind_bin="$(kind_bin_path)"
22+
23+
function get_kind_clusters() {
24+
local clusters="$("$kind_bin" get clusters | grep --color=never "test-connection" || true)"
25+
clusters="$(trim_spaces "$clusters")"
26+
if [[ "$clusters" == "No kind clusters found." ]]; then
27+
echo -n ""
28+
return 0
29+
fi
30+
31+
echo -n "$clusters"
32+
return 0
33+
}
34+
35+
function rm_kind_clusters() {
36+
echo "Remove kind clusters $@"
37+
"$kind_bin" delete clusters "$@"
38+
return $?
39+
}
40+
41+
do_in_cycle "remove tests kind clusters" get_kind_clusters rm_kind_clusters
42+
43+
echo "Kind clusters cleanup done!"
44+
exit 0

hack/kind/cluster-kube-proxy.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2026 Flant JSC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
kind: Cluster
16+
apiVersion: kind.x-k8s.io/v1alpha4
17+
name: test-connection-kube-proxy
18+
nodes:
19+
- role: control-plane

hack/run_tests.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
source "$(pwd)/hack/utils.sh"
18+
19+
check_all_deps
20+
check_go
21+
1722
run_tests=""
1823

1924
if [ -n "$RUN_TEST" ]; then
@@ -25,6 +30,13 @@ run_dir="$(pwd)"
2530
packages="$(go list ./... | grep -v /validation/)"
2631
prefix="$(grep -oP 'module .*$' go.mod | sed 's|module ||')"
2732

33+
if [ -z "$(trim_spaces "$packages")" ]; then
34+
echo -e '\033[1;33m!!!\033[0m'
35+
echo -e "\033[1;33mNot found packages in $run_dir with module ${prefix}. Skip go tests\033[0m"
36+
echo -e '\033[1;33m!!!\033[0m'
37+
exit 0
38+
fi
39+
2840
echo "Found packages: ${packages[@]} in $run_dir with module $prefix"
2941

3042
while IFS= read -r p; do

0 commit comments

Comments
 (0)