Skip to content

Commit 1ed2ffa

Browse files
feat: functionality to change Docker tags and apply manifests (#35)
1 parent a2adbd7 commit 1ed2ffa

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

kubernetes/Makefile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
KUBECTL ?= kubectl
2+
K8S_RESOURCES_DIR := .
3+
4+
# Arg APP is used to define applying resources only for specific app/subdir
5+
ifdef APP
6+
TARGET_DIR := $(K8S_RESOURCES_DIR)/$(APP)
7+
else
8+
TARGET_DIR := $(K8S_RESOURCES_DIR)
9+
endif
10+
11+
.PHONY: apply-namespaces apply-all apply update-image
12+
13+
.DEFAULT_GOAL := help
14+
15+
apply-namespaces: ## Apply all namespaces defined in YAML files (of specific APP if specified)
16+
@echo "Applying all manifests with kind Namespace from $(TARGET_DIR)/..."
17+
@find $(TARGET_DIR) -type f -name "*.yaml" -o -name "*.yml" \
18+
| xargs grep --files-with-matches "^kind: *Namespace" \
19+
| xargs --no-run-if-empty $(KUBECTL) apply --filename
20+
21+
apply-all: ## Apply all resources (also namespaces) defined in YAML files (of specific APP if specified)
22+
@echo "Applying all Kubernetes manifests from $(TARGET_DIR)/..."
23+
@$(KUBECTL) apply --filename $(TARGET_DIR)
24+
25+
apply: apply-namespaces apply-all ## Apply first namespaces and then all resources defined in YAML files (of specific APP if specified)
26+
27+
update-image: ## Update tag of given IMAGE to TAG of all resources defined in YAML files (of specific APP if specified)
28+
@if [ -z "$(IMAGE)" ] || [ -z "$(TAG)" ]; then \
29+
echo "Usage: make update-image IMAGE=<image> TAG=<tag> [APP=<app>]"; \
30+
exit 1; \
31+
fi
32+
@echo "Updating image '$(IMAGE)' to tag '$(TAG)' in $(TARGET_DIR)/..."
33+
@find $(TARGET_DIR) -type f -print0 -name "*.yaml" -o -name "*.yml" \
34+
| xargs -0 grep --files-with-matches "image: *$(IMAGE):" \
35+
| while read -r file; do \
36+
old_image=$$(grep --only-matching --extended-regexp "$(IMAGE):[^ \"']+" "$$file" | head -n1); \
37+
if [ -n "$$old_image" ]; then \
38+
old_tag=$$(echo "$$old_image" | sed --regexp-extended 's|^$(IMAGE):||'); \
39+
new_image="$(IMAGE):$(TAG)"; \
40+
if [ "$$old_tag" = "$(TAG)" ]; then \
41+
echo " → No change in $$file (already using $(IMAGE):\033[1;32m$(TAG)\033[0m)"; \
42+
else \
43+
highlight_old="$$(printf "%s:\033[1;31m%s\033[0m" "$(IMAGE)" "$$old_tag")"; \
44+
highlight_new="$$(printf "%s:\033[1;32m%s\033[0m" "$(IMAGE)" "$(TAG)")"; \
45+
sed --in-place --regexp-extended "s|$(IMAGE):[^ \"']+|$$new_image|g" "$$file"; \
46+
echo " → Updated $$file: $$highlight_old → $$highlight_new"; \
47+
fi; \
48+
fi; \
49+
done
50+
51+
help: ## Show this help
52+
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9._-]+:.*?## / {printf "\033[1m\033[36m%-24s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

0 commit comments

Comments
 (0)