Advertisement
Rnery

Automatic kubernate

Dec 11th, 2023 (edited)
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.75 KB | Source Code | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Variáveis de ambiente
  4. # O nome do cluster Kubernetes
  5. export KUBECONFIG=/path/to/kubeconfig
  6. # O namespace do Kubernetes
  7. export NAMESPACE=default
  8.  
  9. # Função para exibir mensagens de erro e encerrar o script
  10. error_exit() {
  11.     echo "$1" >&2
  12.     exit 1
  13. }
  14.  
  15. # Função para validar o nome do deployment
  16. validate_deployment_name() {
  17.     local deployment_name="$1"
  18.     if [[ ! "$deployment_name" =~ ^[a-z0-9-]+$ ]]; then
  19.         error_exit "O nome do deployment deve conter apenas letras minúsculas, números e hífens."
  20.     fi
  21. }
  22.  
  23. # Função para criar um Deployment
  24. create_deployment() {
  25.     local deployment_name="$1"
  26.     local image_name="$2"
  27.  
  28.     validate_deployment_name "$deployment_name"
  29.  
  30.     kubectl create deployment "$deployment_name" --image="$image_name" --namespace="$NAMESPACE"
  31. }
  32.  
  33. # Função para deletar um Deployment
  34. delete_deployment() {
  35.     local deployment_name="$1"
  36.     kubectl delete deployment "$deployment_name" --namespace="$NAMESPACE"
  37. }
  38.  
  39. # Função principal
  40. main() {
  41.     local operation="$1"
  42.     local deployment_name="$2"
  43.     local image_name="$3"
  44.  
  45.     # Verifica se o número correto de argumentos foi fornecido
  46.     if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
  47.         error_exit "Uso: $0 <create/delete> <deployment_name> [image_name]"
  48.     fi
  49.  
  50.     # Chama a função apropriada
  51.     case "$operation" in
  52.         "create")
  53.             create_deployment "$deployment_name" "$image_name"
  54.             ;;
  55.         "delete")
  56.             delete_deployment "$deployment_name"
  57.             ;;
  58.         *)
  59.             error_exit "Uso: $0 <create/delete> <deployment_name> [image_name]"
  60.             ;;
  61.     esac
  62. }
  63.  
  64. # Chama a função principal com os argumentos passados para o script
  65. main "$@"
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement