Skip to content

Commit 47d0e27

Browse files
feat: polish Makefile details to enable applying Kubernetes manifests (#50)
* refactor: clarify manifest application for specific DIR (not APP) * fix: allow proper application of several manifests * If several manifests are found for `apply-namespaces`, xargs will process only one file at a time. * The flag `--recursive` allows `apply-all` to recurse through dirs instead of providing it with single files. * feat: print token of given secret name * feat: skip dirs when applying Kubernetes manifests * feat: print CA cert and API server for getting necessary CI secrets The three prints of this Makefile now allow us to extract the necesaary secrets to let service accounts authenticate on CI agents. This assumes that we print thos details in a authenticated `kubectl` env.
1 parent bdae8a8 commit 47d0e27

File tree

1 file changed

+70
-14
lines changed

1 file changed

+70
-14
lines changed

kubernetes/Makefile

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,53 @@
11
KUBECTL ?= kubectl
22
K8S_RESOURCES_DIR := .
33

4-
# Arg APP is used to define applying resources only for specific app/subdir
5-
ifdef APP
6-
TARGET_DIR := $(K8S_RESOURCES_DIR)/$(APP)
4+
# Arg DIR is used to define applying resources only for specific app/subdir
5+
ifdef DIR
6+
TARGET_DIR := $(K8S_RESOURCES_DIR)/$(DIR)
77
else
88
TARGET_DIR := $(K8S_RESOURCES_DIR)
99
endif
1010

11-
.PHONY: apply-namespaces apply-all apply update-image
11+
empty :=
12+
space := $(empty) $(empty)
13+
comma := ,
14+
15+
define replace_commas_with_spaces
16+
$(subst $(comma),$(space),$(strip $(1)))
17+
endef
18+
19+
# SKIP_DIR (optional) - comma-separated list of dirs to skip under TARGET_DIR
20+
SKIP_DIRS := $(strip $(SKIP_DIR))
21+
SPACE_SEPARATED_SKIP_DIRS := $(call replace_commas_with_spaces,$(SKIP_DIRS))
22+
23+
define generate_skip_dirs_through_prune
24+
$(foreach dir,$(SPACE_SEPARATED_SKIP_DIRS),-path $(TARGET_DIR)/$(dir) -o) -false
25+
endef
26+
27+
.PHONY: apply-namespaces apply-all apply update-image print-decoded-token help
1228

1329
.DEFAULT_GOAL := help
1430

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" \
31+
apply-namespaces: ## Apply all namespaces defined in YAML files (of specific DIR if specified)
32+
@echo "Applying all manifests with kind Namespace from $(TARGET_DIR)/ (excluding SKIP_DIRS $(SKIP_DIRS))..."
33+
@find $(TARGET_DIR) \
34+
\( $(call generate_skip_dirs_through_prune) \) -prune -o \
35+
\( -type f \( -name "*.yaml" -o -name "*.yml" \) \) -print \
1836
| xargs grep --files-with-matches "^kind: *Namespace" \
19-
| xargs --no-run-if-empty $(KUBECTL) apply --filename
37+
| xargs --no-run-if-empty --max-args=1 $(KUBECTL) apply --filename
2038

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)
39+
apply-all: ## Apply all resources (also namespaces) defined in YAML files (of specific DIR if specified)
40+
@echo "Applying all Kubernetes manifests from $(TARGET_DIR)/ (excluding SKIP_DIRS $(SKIP_DIRS))..."
41+
@find $(TARGET_DIR) \
42+
\( $(call generate_skip_dirs_through_prune) \) -prune -o \
43+
\( -type f \( -name "*.yaml" -o -name "*.yml" \) \) -print \
44+
| xargs --no-run-if-empty --max-args=1 $(KUBECTL) apply --filename
2445

25-
apply: apply-namespaces apply-all ## Apply first namespaces and then all resources defined in YAML files (of specific APP if specified)
46+
apply: apply-namespaces apply-all ## Apply first namespaces and then all resources defined in YAML files (of specific DIR if specified)
2647

27-
update-image: ## Update tag of given IMAGE to TAG of all resources defined in YAML files (of specific APP if specified)
48+
update-image: ## Update tag of given IMAGE to TAG of all resources defined in YAML files (of specific DIR if specified)
2849
@if [ -z "$(IMAGE)" ] || [ -z "$(TAG)" ]; then \
29-
echo "Usage: make update-image IMAGE=<image> TAG=<tag> [APP=<app>]"; \
50+
echo "Usage: make update-image IMAGE=<image> TAG=<tag> [DIR=<subdirectory>]"; \
3051
exit 1; \
3152
fi
3253
@echo "Updating image '$(IMAGE)' to tag '$(TAG)' in $(TARGET_DIR)/..."
@@ -48,5 +69,40 @@ update-image: ## Update tag of given IMAGE to TAG of all resources defined in YA
4869
fi; \
4970
done
5071

72+
print-decoded-token: ## Print base64 decoded token of secret which is passed as SECRET_NAME
73+
@if [ -z "$(SECRET_NAME)" ]; then \
74+
echo "Usage: make print-decoded-token SECRET_NAME=<secret-name>"; \
75+
exit 1; \
76+
fi
77+
@NAME=$(SECRET_NAME) ; \
78+
NAMESPACE=$$(kubectl get secrets --all-namespaces --output jsonpath='{range .items[?(@.metadata.name=="'"$$NAME"'")]}{.metadata.namespace}{"\n"}{end}') ; \
79+
if [ -z "$$NAMESPACE" ]; then \
80+
echo "Secret $$NAME not found in any namespace." >&2 ; exit 1 ; \
81+
fi ; \
82+
TOKEN=$$(kubectl get secret $$NAME -n $$NAMESPACE -o jsonpath='{.data.token}' | base64 --decode) ; \
83+
if [ -z "$$TOKEN" ]; then \
84+
echo "Token not found in secret $$NAME in namespace $$NAMESPACE." >&2 ; exit 1 ; \
85+
fi ; \
86+
echo $$TOKEN
87+
88+
print-ca-cert: ## Print base64 encoded CA cert data of secret which is passed as SECRET_NAME
89+
@if [ -z "$(SECRET_NAME)" ]; then \
90+
echo "Usage: make print-ca-cert SECRET_NAME=<secret-name>"; \
91+
exit 1; \
92+
fi
93+
@NAME=$(SECRET_NAME) ; \
94+
NAMESPACE=$$(kubectl get secrets --all-namespaces --output jsonpath='{range .items[?(@.metadata.name=="'"$$NAME"'")]}{.metadata.namespace}{"\n"}{end}') ; \
95+
if [ -z "$$NAMESPACE" ]; then \
96+
echo "Secret $$NAME not found in any namespace." >&2 ; exit 1 ; \
97+
fi ; \
98+
CA_CERT_DATA=$$(kubectl get secret $$NAME -n $$NAMESPACE -o jsonpath='{.data.ca\.crt}') ; \
99+
if [ -z "$$CA_CERT_DATA" ]; then \
100+
echo "Token not found in secret $$NAME in namespace $$NAMESPACE." >&2 ; exit 1 ; \
101+
fi ; \
102+
echo $$CA_CERT_DATA
103+
104+
print-api-url: ## Print URL of Kubernetes API server from `kubectl config view`
105+
@echo $$($(KUBECTL) config view --minify -o jsonpath='{.clusters[0].cluster.server}')
106+
51107
help: ## Show this help
52108
@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)