Skip to content

Instantly share code, notes, and snippets.

@coryodaniel
Last active August 9, 2023 23:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coryodaniel/e91a8d298a19f38b5d70648cd6111925 to your computer and use it in GitHub Desktop.
Save coryodaniel/e91a8d298a19f38b5d70648cd6111925 to your computer and use it in GitHub Desktop.
A makefile for doing common things with kubectl
##################################################
## A makefile for doing common things with kubectl
##################################################
ifndef K8S_PROJECT_NAME
$(error K8S_PROJECT_NAME is not set. Please set this to your k8s project namein YOUR Makefile)
endif
UNAME_S := $(shell uname -s)
STAGING_NAMESPACE = staging-${K8S_PROJECT_NAME}
PROD_NAMESPACE = prod-${K8S_PROJECT_NAME}
K8S_BIN_DIR = ~/bin
KUSTOMIZE_BIN = ${K8S_BIN_DIR}/kustomize
KUBECTL_BIN = ${K8S_BIN_DIR}/kubectl
KAIL_BIN = ${K8S_BIN_DIR}/kail
STERN_BIN = ${K8S_BIN_DIR}/stern
ifeq ($(UNAME_S),Linux)
STERN_URL = "https://github.com/wercker/stern/releases/download/1.8.0/stern_linux_amd64"
KAIL_URL = "https://github.com/boz/kail/releases/download/v0.6.0/kail_0.6.0_linux_amd64.tar.gz"
KUSTOMIZE_URL = "https://github.com/kubernetes-sigs/kustomize/releases/download/v1.0.3/kustomize_1.0.3_linux_amd64"
KUBECTL_URL = "https://storage.googleapis.com/kubernetes-release/release/v1.10.1/bin/linux/amd64/kubectl"
else
STERN_URL = "https://github.com/wercker/stern/releases/download/1.8.0/stern_darwin_amd64"
KAIL_URL = "https://github.com/boz/kail/releases/download/v0.6.0/kail_0.6.0_darwin_amd64.tar.gz"
KUSTOMIZE_URL = "https://github.com/kubernetes-sigs/kustomize/releases/download/v1.0.3/kustomize_1.0.3_darwin_amd64"
KUBECTL_URL = "https://storage.googleapis.com/kubernetes-release/release/v1.10.1/bin/darwin/amd64/kubectl"
endif
k8s.help: ## Show this help.
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
# https://gist.githubusercontent.com/coryodaniel/e91a8d298a19f38b5d70648cd6111925/raw
k8s.update-make: ## Update this Makefile from https://git.io/fAItR
k8s.update-make:
curl -Lo ./k8s.Makefile https://git.io/fAItR
_bin:
@echo You may need to add ~/bin to your PATH variable
@mkdir -p ${K8S_BIN_DIR}
_go:
@echo This requires go > 1.10
@go get -u github.com/kardianos/govendor
_kustomization:
mkdir ./k8s
mkdir ./k8s/base
mkdir ./k8s/overlays
mkdir ./k8s/overlays/prod
mkdir ./k8s/overlays/staging
touch ./k8s/base/kustomization.yaml
touch ./k8s/overlays/prod/kustomization.yaml
touch ./k8s/overlays/staging/kustomization.yaml
bin/stern: ## Download the newest stern release
@echo "Downloading ${K8S_BIN_DIR}/stern for tailing k8s logs."
@curl -sSL $(STERN_URL) > ${STERN_BIN}
@chmod +x ${STERN_BIN}
bin/kustomize: ## Download the kustomize stern release
@echo "Downloading ${K8S_BIN_DIR}/kustomize for k8s deployments."
@curl -sSL $(KUSTOMIZE_URL) > ${KUSTOMIZE_BIN}
@chmod +x ${KUSTOMIZE_BIN}
bin/kubectl: ## Download the kubectl stern release
@echo "Downloading ${K8S_BIN_DIR}/kubectl for managing k8s resources."
@curl -sS $(KUBECTL_URL) > ${KUBECTL_BIN}
@chmod +x ${KUBECTL_BIN}
k8s.setup: ## Install toolset and kustomization boilerplate
k8s.setup: _bin _go bin/kubectl bin/kustomize bin/stern _kustomization
k8s.envs: ## Output your environment namespaces
k8s.envs:
@echo "Staging: ${STAGING_NAMESPACE}"
@echo "Production: ${PROD_NAMESPACE}"
k8s.use.staging: ## Switch kubectl context to staging
k8s.use.staging:
${KUBECTL_BIN} config set-context $(shell kubectl config current-context) --namespace=${STAGING_NAMESPACE}
k8s.use.prod: ## Switch kubectl context to prod
k8s.use.prod:
${KUBECTL_BIN} config set-context $(shell kubectl config current-context) --namespace=${PROD_NAMESPACE}
k8s.shell.staging: ## Run a busybox shell in your staging environment
k8s.shell.staging:
${KUBECTL_BIN} run busybox -it --rm --image=busybox -n ${STAGING_NAMESPACE} -- sh
k8s.shell.prod: ## Run a busybox shell in your prod environment
k8s.shell.prod:
${KUBECTL_BIN} run busybox -it --rm --image=busybox -n ${PROD_NAMESPACE} -- sh
k8s.dryrun.staging: ## See what kustomize will apply to staging
k8s.dryrun.staging:
${KUSTOMIZE_BIN} build ./k8s/overlays/staging
k8s.dryrun.prod: ## See what kustomize will apply to prod
k8s.dryrun.prod:
${KUSTOMIZE_BIN} build ./k8s/overlays/prod
k8s.deploy.staging: ## Deploy staging
k8s.deploy.staging:
${KUSTOMIZE_BIN} build ./k8s/overlays/staging | kubectl apply -n ${STAGING_NAMESPACE} -f -
k8s.deploy.prod: ## Deploy prod
k8s.deploy.prod:
${KUSTOMIZE_BIN} build ./k8s/overlays/prod | kubectl apply -n ${PROD_NAMESPACE} -f -
k8s.clean: ## Clean up evicted containers
k8s.clean:
${KUBECTL_BIN} get pods | grep Evicted | awk '{print $$1}' | xargs kubectl delete pod
k8s.events: ## See k8s scheduling events
k8s.events:
${KUBECTL_BIN} get events --sort-by=.metadata.creationTimestamp
.PHONY: k8s.setup k8s.deploy.staging k8s.deploy.prod k8s.dryrun.prod k8s.dryrun.staging
.PHONY: k8s.use.staging k8s.use.prod k8s.clean k8s.events
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment