Skip to content

Commit

Permalink
Merge pull request #30 from hashmap-kz/doc/examples
Browse files Browse the repository at this point in the history
examples - add examples with basic usage
  • Loading branch information
hashmap-kz authored Jan 10, 2025
2 parents 0120dfa + 276ca18 commit e0172d7
Show file tree
Hide file tree
Showing 17 changed files with 304 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
integration/immutable_data/** linguist-vendored
testdata/** linguist-vendored
examples/** linguist-vendored
18 changes: 3 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,16 @@
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/
media

bin/
dist/

# Enviroment files
envs/
main
# IDE's
.idea
.vscode

# Avatars storage directory
avatars/

backups/
cover.out
coverage.txt

8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ all: build
# Build the binary (GOARCH=amd64 GOOS=linux; -o $(BINARY))
.PHONY: build
build: $(SOURCES)
CGO_ENABLED=0 go build -ldflags="-s -w" ./cmd/kubectl-envsubst
CGO_ENABLED=0 go build -ldflags="-s -w" -o bin/ ./cmd/kubectl-envsubst

# Install the binary to /usr/local/bin
.PHONY: install
install: build
@echo "Installing $(BINARY) to $(INSTALL_DIR)..."
@install -m 0755 $(BINARY) $(INSTALL_DIR)
@echo "Installing bin/$(BINARY) to $(INSTALL_DIR)..."
@install -m 0755 bin/$(BINARY) $(INSTALL_DIR)

# Run unit tests
.PHONY: test
Expand Down Expand Up @@ -68,4 +68,4 @@ format:
# Clean build artifacts
.PHONY: clean
clean:
@rm -rf $(BINARY) $(COV_REPORT)
@rm -rf bin/ $(COV_REPORT)
29 changes: 29 additions & 0 deletions examples/project-setup/00-setup-kind.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
set -euo pipefail

# prepare config for the 'kind' cluster
cat <<EOF >kind-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: "kubectl-envsubst"
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 32501
hostPort: 32501
protocol: TCP
- containerPort: 32502
hostPort: 32502
protocol: TCP
- containerPort: 32503
hostPort: 32503
protocol: TCP
EOF

# setup cluster with kind, to safely test in a sandbox
if kind get clusters | grep "kubectl-envsubst"; then
kind delete clusters "kubectl-envsubst"
fi
kind create cluster --config=kind-config.yaml
kubectl config set-context "kind-kubectl-envsubst"
rm -f kind-config.yaml
18 changes: 18 additions & 0 deletions examples/project-setup/01-deploy-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -euo pipefail

# setup envs
export PROJECT_ROOT_NAMESPACE=kubectl-envubst-examples
export PROJECT_ENV=dev
export PROJECT_NAME=nginx-gateway
export PROJECT_NAMESPACE="${PROJECT_ROOT_NAMESPACE}-${PROJECT_ENV}"
export IMAGE_NAME=nginx
export IMAGE_TAG=latest

# setup namespace and context
kubectl create ns "${PROJECT_NAMESPACE}" --dry-run=client -oyaml | kubectl apply -f -
kubectl config set-context --current --namespace="${PROJECT_NAMESPACE}"

# substitute and apply resources, according to the environment (dev, stage, prod)
export ENVSUBST_ALLOWED_PREFIXES='PROJECT_,IMAGE_'
kubectl envsubst apply -f "${PROJECT_ENV}"
18 changes: 18 additions & 0 deletions examples/project-setup/02-deploy-stage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -euo pipefail

# setup envs
export PROJECT_ROOT_NAMESPACE=kubectl-envubst-examples
export PROJECT_ENV=stage
export PROJECT_NAME=nginx-gateway
export PROJECT_NAMESPACE="${PROJECT_ROOT_NAMESPACE}-${PROJECT_ENV}"
export IMAGE_NAME=nginx
export IMAGE_TAG=latest

# setup namespace and context
kubectl create ns "${PROJECT_NAMESPACE}" --dry-run=client -oyaml | kubectl apply -f -
kubectl config set-context --current --namespace="${PROJECT_NAMESPACE}"

# substitute and apply resources, according to the environment (dev, stage, prod)
export ENVSUBST_ALLOWED_PREFIXES='PROJECT_,IMAGE_'
kubectl envsubst apply -f "${PROJECT_ENV}"
18 changes: 18 additions & 0 deletions examples/project-setup/03-deploy-prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -euo pipefail

# setup envs
export PROJECT_ROOT_NAMESPACE=kubectl-envubst-examples
export PROJECT_ENV=prod
export PROJECT_NAME=nginx-gateway
export PROJECT_NAMESPACE="${PROJECT_ROOT_NAMESPACE}-${PROJECT_ENV}"
export IMAGE_NAME=nginx
export IMAGE_TAG=latest

# setup namespace and context
kubectl create ns "${PROJECT_NAMESPACE}" --dry-run=client -oyaml | kubectl apply -f -
kubectl config set-context --current --namespace="${PROJECT_NAMESPACE}"

# substitute and apply resources, according to the environment (dev, stage, prod)
export ENVSUBST_ALLOWED_PREFIXES='PROJECT_,IMAGE_'
kubectl envsubst apply -f "${PROJECT_ENV}"
27 changes: 27 additions & 0 deletions examples/project-setup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## A brief example of usage

### Prerequisites:

You can use a Kind cluster for testing. If you haven’t installed it yet, check out the [Kind installation guide](https://kind.sigs.k8s.io/).

### Deploy:

Execute scripts one by one:
```
# prepare kind cluster
bash 00-setup-kind.sh
# deploy for each environment
bash 01-deploy-dev.sh
bash 02-deploy-stage.sh
bash 03-deploy-prod.sh
```

### Verification:

Check the result:
- dev: http://localhost:32501
- stage: http://localhost:32502
- prod: http://localhost:32503

_If your Kind cluster is running on a remote machine, replace ‘localhost’ with the machine’s IP address._
17 changes: 17 additions & 0 deletions examples/project-setup/dev/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-index
data:
index.html: |
<!DOCTYPE html>
<html>
<head>
<title>Hello from ${PROJECT_ENV}</title>
</head>
<body>
<h1>Welcome to the NGINX Test Page!</h1>
<p>Current env: ${PROJECT_ENV}</p>
</body>
</html>
29 changes: 29 additions & 0 deletions examples/project-setup/dev/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: &app ${PROJECT_NAME}
spec:
replicas: 1
selector:
matchLabels:
app: *app
template:
metadata:
labels:
app: *app
spec:
containers:
- name: *app
image: $IMAGE_NAME:$IMAGE_TAG
ports:
- containerPort: 80
volumeMounts:
- name: nginx-html
mountPath: /usr/share/nginx/html/index.html
subPath: index.html
resources: {}
volumes:
- name: nginx-html
configMap:
name: nginx-index
16 changes: 16 additions & 0 deletions examples/project-setup/dev/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
apiVersion: v1
kind: Service
metadata:
name: &app ${PROJECT_NAME}
labels:
app: *app
spec:
type: NodePort
ports:
- port: 8080
targetPort: 80
nodePort: 32501
name: *app
selector:
app: *app
17 changes: 17 additions & 0 deletions examples/project-setup/prod/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-index
data:
index.html: |
<!DOCTYPE html>
<html>
<head>
<title>Hello from ${PROJECT_ENV}</title>
</head>
<body>
<h1>Welcome to the NGINX Test Page!</h1>
<p>Current env: ${PROJECT_ENV}</p>
</body>
</html>
29 changes: 29 additions & 0 deletions examples/project-setup/prod/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: &app ${PROJECT_NAME}
spec:
replicas: 1
selector:
matchLabels:
app: *app
template:
metadata:
labels:
app: *app
spec:
containers:
- name: *app
image: $IMAGE_NAME:$IMAGE_TAG
ports:
- containerPort: 80
volumeMounts:
- name: nginx-html
mountPath: /usr/share/nginx/html/index.html
subPath: index.html
resources: {}
volumes:
- name: nginx-html
configMap:
name: nginx-index
16 changes: 16 additions & 0 deletions examples/project-setup/prod/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
apiVersion: v1
kind: Service
metadata:
name: &app ${PROJECT_NAME}
labels:
app: *app
spec:
type: NodePort
ports:
- port: 8080
targetPort: 80
nodePort: 32503
name: *app
selector:
app: *app
17 changes: 17 additions & 0 deletions examples/project-setup/stage/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-index
data:
index.html: |
<!DOCTYPE html>
<html>
<head>
<title>Hello from ${PROJECT_ENV}</title>
</head>
<body>
<h1>Welcome to the NGINX Test Page!</h1>
<p>Current env: ${PROJECT_ENV}</p>
</body>
</html>
29 changes: 29 additions & 0 deletions examples/project-setup/stage/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: &app ${PROJECT_NAME}
spec:
replicas: 1
selector:
matchLabels:
app: *app
template:
metadata:
labels:
app: *app
spec:
containers:
- name: *app
image: $IMAGE_NAME:$IMAGE_TAG
ports:
- containerPort: 80
volumeMounts:
- name: nginx-html
mountPath: /usr/share/nginx/html/index.html
subPath: index.html
resources: {}
volumes:
- name: nginx-html
configMap:
name: nginx-index
16 changes: 16 additions & 0 deletions examples/project-setup/stage/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
apiVersion: v1
kind: Service
metadata:
name: &app ${PROJECT_NAME}
labels:
app: *app
spec:
type: NodePort
ports:
- port: 8080
targetPort: 80
nodePort: 32502
name: *app
selector:
app: *app

0 comments on commit e0172d7

Please sign in to comment.