Skip to content

Instantly share code, notes, and snippets.

@bouroo
Last active February 29, 2024 21:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bouroo/c237d83fa904be540a92d6f89fee6ec7 to your computer and use it in GitHub Desktop.
Save bouroo/c237d83fa904be540a92d6f89fee6ec7 to your computer and use it in GitHub Desktop.
k8s kustomization generate from kubectl
#!/usr/bin/env bash
#==============================================================================
# title: gen_kustomization.sh
# description: This script automatically creates kustomization deployment files from kubectl
# author: Kawin Viriyaprasopsook <kawin.v@kkumail.com>
# usage: bash gen_kustomization.sh
# notes: need `kubectl kubectl-convert kubectl-neat` packages
#==============================================================================
mkdir -p kustomization && cd kustomization
# Get all namespaces except system, cattle, and kube
namespaces=$(kubectl get namespaces -o custom-columns=:metadata.name --no-headers | grep -E -v '^(system|cattle|kube)')
# Loop through each namespace
for namespace in $namespaces; do
echo "Namespace: $namespace"
# Create a directory for the namespace
mkdir -p "$namespace"
# Export the namespace's resources to YAML files
# kubectl get project "$namespace" -o yaml | kubectl convert -f - | kubectl neat -f - >"$namespace/project.yaml"
kubectl get namespace "$namespace" -o yaml | kubectl convert -f - | kubectl neat -f - >"$namespace/namespace.yaml"
kubectl get deployment -n "$namespace" -o yaml | kubectl convert -f - | kubectl neat -f - >"$namespace/deployment.yaml"
kubectl get service -n "$namespace" -o yaml | kubectl convert -f - | kubectl neat -f - >"$namespace/service.yaml"
kubectl get ingress -n "$namespace" -o yaml | kubectl convert -f - | kubectl neat -f - >"$namespace/ingress.yaml"
kubectl get HorizontalPodAutoscaler -n "$namespace" -o yaml | kubectl convert -f - | kubectl neat -f - >"$namespace/HorizontalPodAutoscaler.yaml"
kubectl get ServiceEntry -n "$namespace" -o yaml | kubectl neat -f - >"$namespace/ServiceEntry.yaml"
# Enable Istio auto injection at deploy time
if grep -q 'istio-injection' "$namespace/namespace.yaml"; then
echo "Already istio-injection, skipping..."
else
sed -i '/labels:/a \ istio-injection: enabled' "$namespace/namespace.yaml"
fi
# Clean up externalIP, clusterIP, and publicEndpoints endpoints for deployment on a different cluster
sed -i '/externalIPs:/d' "$namespace/"*.yaml
sed -i '/clusterIP:/d' "$namespace/"*.yaml
sed -i '/publicEndpoints:/d' "$namespace/"*.yaml
# Create a kustomization.yaml file for the namespace
cat <<EOL >$namespace/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: $namespace
resources:
# - project.yaml
- namespace.yaml
- deployment.yaml
- service.yaml
- ingress.yaml
- HorizontalPodAutoscaler.yaml
- ServiceEntry.yaml
EOL
done
cd ..
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment