Last active
February 29, 2024 21:22
-
-
Save bouroo/c237d83fa904be540a92d6f89fee6ec7 to your computer and use it in GitHub Desktop.
k8s kustomization generate from kubectl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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