Skip to content

Commit a87e416

Browse files
authored
Merge pull request #2 from anapsix/allow-writes-to-temp-file
Fixes #1 * making temp kubeconfig writable * updating build process.. + upgrading alpine `3.15 -> 3.17` + improving Dockerfile + improving Makefile
2 parents 49fa4bb + f14ac84 commit a87e416

File tree

7 files changed

+65
-31
lines changed

7 files changed

+65
-31
lines changed

Dockerfile

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
1+
# syntax=docker/dockerfile:1.4
2+
13
## this stage installs everything required to build the project
2-
FROM alpine:3.15 as build
3-
RUN apk add --no-cache musl-dev yaml-static upx && \
4-
apk add --repository=http://dl-cdn.alpinelinux.org/alpine/edge/main \
5-
llvm11-libs && \
6-
apk add --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community \
7-
crystal shards
4+
FROM alpine:3.17 as build
5+
ARG TARGETPLATFORM
6+
ARG TARGETARCH
7+
ARG UPX_PACK=1
8+
RUN \
9+
echo >&2 "## TARGETPLATFORM: ${TARGETPLATFORM}" && \
10+
echo >&2 "## TARGETARCH: ${TARGETARCH}" && \
11+
echo >&2 "## arch: $(arch)" && \
12+
echo >&2 "## UPX_PACK: ${UPX_PACK}" && \
13+
PACKAGES="musl-dev yaml-static crystal shards" && \
14+
if [ "${UPX_PACK}" == "1" ]; then \
15+
PACKAGES="${PACKAGES} upx"; \
16+
fi && \
17+
echo >&2 "### installing OS packages: ${PACKAGES}" && \
18+
apk add --no-cache $PACKAGES
819
WORKDIR /tmp
9-
COPY VERSION .
10-
COPY shard.yml .
11-
COPY k8s-vault_example.yaml .
12-
COPY k8s-vault-completion.bash .
13-
COPY ./src ./src
20+
COPY --link VERSION .
21+
COPY --link shard.yml .
22+
COPY --link k8s-vault_example.yaml .
23+
COPY --link k8s-vault-completion.bash .
24+
COPY --link ./src ./src
1425
RUN \
26+
echo >&2 "### installing dependencies.." && \
1527
shards install && \
28+
echo >&2 "### building.." && \
1629
crystal build --progress --release --static src/cli.cr -o /tmp/k8s-vault && \
17-
upx /tmp/k8s-vault && \
30+
if [ "${UPX_PACK}" == "1" ]; then \
31+
echo >&2 "### upx packing.." && \
32+
upx /tmp/k8s-vault; \
33+
fi && \
1834
echo >&2 "## Version check: $(/tmp/k8s-vault -v)" && \
1935
echo >&2 "## Help Check" && \
2036
/tmp/k8s-vault --help
2137

2238

23-
## this stage created final docker image
39+
## this stage creates final docker image
2440
FROM busybox as release
2541
COPY --from=build /tmp/k8s-vault /bin/k8s-vault
2642
USER nobody

Makefile

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ ifeq ($(UNAME_S),Linux)
66
OS:= linux
77
endif
88
UNAME_M:= $(shell uname -m)
9-
ifeq ($(UNAME_M),x86_64)
10-
ARCH:= amd64
9+
ifeq ($(UNAME_M), x86_64)
10+
ARCH ?= amd64
11+
else ifeq ($(UNAME_M), arm64)
12+
ARCH ?= arm64
13+
else ifeq ($(UNAME_M), aarch64)
14+
ARCH ?= arm64
15+
else
16+
$(error the "$(UNAME_M)" arch is not supported)
1117
endif
1218

1319
BINARY:= k8s-vault
@@ -16,33 +22,45 @@ TARGET:= src/cli
1622
RELEASE_DIR:= releases
1723
OUTPUT:= ./$(RELEASE_DIR)/$(BINARY)-$(VERSION)-$(OS)-$(ARCH)
1824

19-
.PHONY: all clean version prepare
25+
.PHONY: all clean version prepare help
2026

21-
all: clean prepare releases
27+
all: clean prepare releases ## Builds everything
2228

23-
releases: prepare version $(TARGET) pack docker
24-
docker run -it --rm -v ${PWD}/$(RELEASE_DIR):/app --entrypoint "sh" $(BINARY):$(VERSION) -c "cp /bin/$(BINARY) /app/$(BINARY)-$(VERSION)-linux-amd64"
29+
help: ## Show this help
30+
@echo
31+
@printf '\033[34mtargets:\033[0m\n'
32+
@grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) |\
33+
sort |\
34+
awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}'
2535

26-
docker:
27-
docker build -t $(BINARY):$(VERSION) .
28-
docker tag $(BINARY):$(VERSION) $(BINARY):latest
36+
releases: prepare version $(TARGET) pack docker docker_arm64 ## Builds releases
37+
docker run -it --rm --platform linux/amd64 -v ${PWD}/$(RELEASE_DIR):/app --entrypoint "sh" $(BINARY):$(VERSION)-amd64 -c "cp /bin/$(BINARY) /app/$(BINARY)-$(VERSION)-linux-amd64"
38+
docker run -it --rm --platform linux/arm64 -v ${PWD}/$(RELEASE_DIR):/app --entrypoint "sh" $(BINARY):$(VERSION)-arm64 -c "cp /bin/$(BINARY) /app/$(BINARY)-$(VERSION)-linux-arm64"
39+
40+
docker: ## Builds docker image amd64
41+
docker build --platform linux/amd64 -t $(BINARY):$(VERSION)-amd64 .
42+
docker tag $(BINARY):$(VERSION)-amd64 $(BINARY):latest-amd64
43+
44+
docker_arm64: ## Builds docker image for arm64
45+
docker build --platform linux/arm64 -t $(BINARY):$(VERSION)-arm64 .
46+
docker tag $(BINARY):$(VERSION)-arm64 $(BINARY):latest-arm64
2947

3048
prepare:
3149
@if [ ! -d ./$(RELEASE_DIR) ]; then mkdir ./$(RELEASE_DIR); fi
3250

33-
clean:
51+
clean: ## Removes release directory
3452
@rm -f ./$(RELEASE_DIR)/*
3553
@echo >&2 "cleaned up"
3654

37-
version:
55+
version: ## Updates the version
3856
@sed -i "" 's/^version:.*/version: "$(VERSION)"/g' k8s-vault_example.yaml
3957
@sed -i "" 's/^version:.*/version: $(VERSION)/g' shard.yml
4058
@echo "shard.yml updated with version $(VERSION)"
4159

4260
$(TARGET): % : prepare $(filter-out $(TEMPS), $(OBJ)) %.cr
4361
@crystal build src/cli.cr -o $(OUTPUT) --progress --release
4462
@rm ./$(RELEASE_DIR)/*.dwarf
45-
@echo "compiled binaries places to \"./$(RELEASE_DIR)\" directory"
63+
@echo "compiled binaries placed to \"./$(RELEASE_DIR)\" directory"
4664

47-
pack:
65+
pack: ## Runs UPX on locally built binary
4866
@find ./$(RELEASE_DIR) -type f -name "$(BINARY)-$(VERSION)-$(OS)-$(ARCH)" | xargs upx

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Or build from source:
2424
```sh
2525
git clone https://github.com/anapsix/k8s-vault.cr.git
2626
cd k8s-vault.cr
27-
shards build
27+
shards build # or "make src/cli"
2828
# copy ./bin/k8s-vault to some directory in your PATH
2929
```
3030

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.4.2
1+
0.4.3

k8s-vault_example.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "0.4.2"
1+
version: "0.4.3"
22
k8s_api_timeout: 5 # in seconds
33
ssh_forwarding_port:
44
random: true

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: k8s-vault
2-
version: 0.4.2
2+
version: 0.4.3
33

44
description: |
55
k8s-vault makes it easy to reach K8s API via jumphost, using SSH port forwarding.

src/k8s-vault.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ module K8sVault
271271
begin
272272
config = K8sVault.config(kubecontext: kubecontext)
273273
# write temp KUBECONFIG
274-
File.write(K8sVault::KUBECONFIG_TEMP, config.kubeconfig, perm = 0o0400)
274+
File.write(K8sVault::KUBECONFIG_TEMP, config.kubeconfig, perm = 0o0600)
275275
rescue K8sVault::UnconfiguredContextError
276276
K8sVault::Log.error "\"#{kubecontext}\" context is not found in #{K8sVault::K8SVAULT_CONFIG}"
277277
cleanup

0 commit comments

Comments
 (0)