11KUBECTL ?= kubectl
22K8S_RESOURCES_DIR := .
33
4+ KUBECONFIG_TEMPLATE ?= ./sa-kubeconfig.yaml.template
5+ NEW_KUBECONFIG_FILE ?= ../sa-kubeconfig.yaml
6+
47# Arg DIR is used to define applying resources only for specific app/subdir
58ifdef DIR
69 TARGET_DIR := $(K8S_RESOURCES_DIR ) /$(DIR )
@@ -24,7 +27,7 @@ define generate_skip_dirs_through_prune
2427 $(foreach dir,$(SPACE_SEPARATED_SKIP_DIRS ) ,-path $(TARGET_DIR ) /$(dir ) -o) -false
2528endef
2629
27- .PHONY : apply-namespaces apply-all apply update-image print-decoded-token help
30+ .PHONY : apply-namespaces apply-all apply update-image print-decoded-token print-ca-cert print-api-url help
2831
2932.DEFAULT_GOAL := help
3033
@@ -75,11 +78,11 @@ print-decoded-token: ## Print base64 decoded token of secret which is passed as
7578 exit 1; \
7679 fi
7780 @NAME=$(SECRET_NAME ) ; \
78- NAMESPACE=$$(kubectl get secrets --all-namespaces --output jsonpath='{range .items[?(@.metadata.name=="'"$$NAME"'" ) ]}{.metadata.namespace}{" \n" }{end}' ) ; \
81+ NAMESPACE=$$($( KUBECTL ) get secrets --all-namespaces --output jsonpath='{range .items[?(@.metadata.name=="'"$$NAME"'") ]}{.metadata.namespace}{" \n" }{end}' ) ; \
7982 if [ -z " $$ NAMESPACE" ]; then \
8083 echo " Secret $$ NAME not found in any namespace." >&2 ; exit 1 ; \
8184 fi ; \
82- TOKEN=$$(kubectl get secret $$NAME -n $$NAMESPACE -o jsonpath='{.data.token}' | base64 --decode ) ; \
85+ TOKEN=$$($( KUBECTL ) get secret $$NAME -n $$NAMESPACE -o jsonpath='{.data.token}' | base64 --decode) ; \
8386 if [ -z " $$ TOKEN" ]; then \
8487 echo " Token not found in secret $$ NAME in namespace $$ NAMESPACE." >&2 ; exit 1 ; \
8588 fi ; \
@@ -91,11 +94,11 @@ print-ca-cert: ## Print base64 encoded CA cert data of secret which is passed as
9194 exit 1; \
9295 fi
9396 @NAME=$(SECRET_NAME) ; \
94- NAMESPACE=$$(kubectl get secrets --all-namespaces --output jsonpath='{range .items[?(@.metadata.name=="'"$$NAME"'")]}{.metadata.namespace}{"\n"}{end}') ; \
97+ NAMESPACE=$$($(KUBECTL) get secrets --all-namespaces --output jsonpath='{range .items[?(@.metadata.name=="'"$$NAME"'")]}{.metadata.namespace}{"\n"}{end}') ; \
9598 if [ -z "$$NAMESPACE" ]; then \
9699 echo "Secret $$NAME not found in any namespace." >&2 ; exit 1 ; \
97100 fi ; \
98- CA_CERT_DATA=$$(kubectl get secret $$NAME -n $$NAMESPACE -o jsonpath='{.data.ca\.crt}') ; \
101+ CA_CERT_DATA=$$($(KUBECTL) get secret $$NAME -n $$NAMESPACE -o jsonpath='{.data.ca\.crt}') ; \
99102 if [ -z "$$CA_CERT_DATA" ]; then \
100103 echo "Token not found in secret $$NAME in namespace $$NAMESPACE." >&2 ; exit 1 ; \
101104 fi ; \
@@ -104,5 +107,17 @@ print-ca-cert: ## Print base64 encoded CA cert data of secret which is passed as
104107print-api-url : # # Print URL of Kubernetes API server from `kubectl config view`
105108 @echo $$($(KUBECTL ) config view --minify -o jsonpath='{.clusters[0].cluster.server}')
106109
110+ write-kubeconfig : # # Write a Kubeconfig file at location NEW_KUBECONFIG_FILE, needs args TOKEN, CA_CERT and API_URL
111+ @if [ -z " $( TOKEN) " ] || [ -z " $( CA_CERT) " ] || [ -z " $( API_URL) " ]; then \
112+ echo " Usage: make write-kubeconfig TOKEN=... CA_CERT=... API_URL=..." ; \
113+ exit 1; \
114+ fi ; \
115+ sed \
116+ -e ' s|{{TOKEN}}|$(TOKEN)|g' \
117+ -e ' s|{{CA_CERT}}|$(CA_CERT)|g' \
118+ -e ' s|{{API_SERVER}}|$(API_URL)|g' \
119+ $(KUBECONFIG_TEMPLATE ) > $(NEW_KUBECONFIG_FILE ) ; \
120+ echo " Wrote $( NEW_KUBECONFIG_FILE) "
121+
107122help : # # Show this help
108123 @awk ' BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9._-]+:.*?## / {printf "\033[1m\033[36m%-24s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST )
0 commit comments