generate-secrets.ps1 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Generate Kubernetes Secret and ConfigMap Templates for All Services
  2. # 为所有微服务生成 Kubernetes Secret 和 ConfigMap 模板
  3. param(
  4. [string]$OutputDir = "d:\coding-area\devops\helm\kubernetes-secrets",
  5. [string]$Namespace = "production"
  6. )
  7. # Service list
  8. $services = @(
  9. 'shop-recycle-account',
  10. 'shop-recycle-agent-pc-web',
  11. 'shop-recycle-async-web',
  12. 'shop-recycle-customer-wechat-web',
  13. 'shop-recycle-data-statistics',
  14. 'shop-recycle-dealdata-service',
  15. 'shop-recycle-dispatcher',
  16. 'shop-recycle-erp-pc-web',
  17. 'shop-recycle-gateway',
  18. 'shop-recycle-gateway-out',
  19. 'shop-recycle-gateway-out-upgrade',
  20. 'shop-recycle-import-web',
  21. 'shop-recycle-login-center',
  22. 'shop-recycle-marketer-pc-web',
  23. 'shop-recycle-merchant',
  24. 'shop-recycle-merchant-pc-web',
  25. 'shop-recycle-merchant-wechat-web',
  26. 'shop-recycle-msg',
  27. 'shop-recycle-order-center',
  28. 'shop-recycle-order-search',
  29. 'shop-recycle-oss-web',
  30. 'shop-recycle-out-web',
  31. 'shop-recycle-payment',
  32. 'shop-recycle-payment-web',
  33. 'shop-recycle-pis',
  34. 'shop-recycle-platform',
  35. 'shop-recycle-platform-pc-web',
  36. 'shop-recycle-sche',
  37. 'shop-recycle-store',
  38. 'shop-recycle-store-pc-web',
  39. 'shop-recycle-store-wechat-web',
  40. 'shop-recycle-wechat',
  41. 'shop-recycle-wechat-web',
  42. 'shop-recycle-ws-web'
  43. )
  44. $basePath = "d:\coding-area\devops\helm\conf"
  45. # Create output directories
  46. $secretsDir = "$OutputDir\secrets"
  47. $configmapsDir = "$OutputDir\configmaps"
  48. if (-not (Test-Path $secretsDir)) {
  49. New-Item -ItemType Directory -Path $secretsDir -Force | Out-Null
  50. }
  51. if (-not (Test-Path $configmapsDir)) {
  52. New-Item -ItemType Directory -Path $configmapsDir -Force | Out-Null
  53. }
  54. Write-Host "Generating Kubernetes Secret and ConfigMap templates..." -ForegroundColor Cyan
  55. Write-Host "Output Directory: $OutputDir" -ForegroundColor Yellow
  56. Write-Host ""
  57. foreach ($service in $services) {
  58. $ymlPath = "$basePath\$service\conf\application.yml"
  59. if (Test-Path $ymlPath) {
  60. $content = Get-Content $ymlPath -Raw
  61. $lines = $content -split "`n"
  62. $secretData = @()
  63. $configData = @()
  64. foreach ($line in $lines) {
  65. if ($line.Trim() -eq '' -or $line.TrimStart().StartsWith('#')) {
  66. continue
  67. }
  68. $lower = $line.ToLower()
  69. # Determine if this is sensitive data
  70. $isSensitive = $false
  71. if ($lower -match 'password|secret|key|token|username|user|credential|auth|api-key|app-id|app-secret') {
  72. $isSensitive = $true
  73. }
  74. # Skip password/secret values that are null or empty
  75. if ($lower -match ':\s*null|:\s*""\s*$|:\s*$') {
  76. continue
  77. }
  78. $fieldName = ($line -split ':')[0].Trim()
  79. $fieldValue = ($line -split ':', 2)[1].Trim() -replace '"', '' -replace "'", ""
  80. if ($isSensitive) {
  81. if ($fieldValue -and $fieldValue -ne 'null') {
  82. $secretData += " $fieldName" + ": `"" + '${' + $fieldName.ToUpper().Replace('.', '_').Replace('-', '_') + "}`""
  83. }
  84. } else {
  85. if ($fieldValue -and $fieldValue -ne 'null') {
  86. $configData += " $fieldName" + ": `"$fieldValue`""
  87. }
  88. }
  89. }
  90. # Generate Secret YAML
  91. $secretYaml = @"
  92. apiVersion: v1
  93. kind: Secret
  94. metadata:
  95. name: $service-secret
  96. namespace: $Namespace
  97. type: Opaque
  98. stringData:
  99. "@
  100. if ($secretData.Count -gt 0) {
  101. $secretYaml += "`n" + ($secretData -join "`n")
  102. } else {
  103. $secretYaml += "`n # No sensitive data found or all values are empty"
  104. }
  105. # Generate ConfigMap YAML
  106. $configYaml = @"
  107. apiVersion: v1
  108. kind: ConfigMap
  109. metadata:
  110. name: $service-configmap
  111. namespace: $Namespace
  112. data:
  113. "@
  114. if ($configData.Count -gt 0) {
  115. $configYaml += "`n" + ($configData -join "`n")
  116. } else {
  117. $configYaml += "`n # No non-sensitive configuration data found"
  118. }
  119. # Write files
  120. $secretFile = "$secretsDir\$service-secret.yaml"
  121. $configFile = "$configmapsDir\$service-configmap.yaml"
  122. $secretYaml | Out-File -FilePath $secretFile -Encoding UTF8
  123. $configYaml | Out-File -FilePath $configFile -Encoding UTF8
  124. Write-Host "Generated: $service" -ForegroundColor Green
  125. Write-Host " Secret: $($secretData.Count) fields -> $secretFile"
  126. Write-Host " ConfigMap: $($configData.Count) fields -> $configFile"
  127. Write-Host ""
  128. }
  129. }
  130. # Create apply script
  131. $applyScript = @"
  132. #!/bin/bash
  133. # Apply all Secrets and ConfigMaps to Kubernetes
  134. echo "Applying Secrets..."
  135. kubectl apply -f $secretsDir -n $Namespace
  136. echo "Applying ConfigMaps..."
  137. kubectl apply -f $configmapsDir -n $Namespace
  138. echo "Verification:"
  139. kubectl get secrets -n $Namespace | grep -E 'shop-recycle.*-secret'
  140. kubectl get configmaps -n $Namespace | grep -E 'shop-recycle.*-configmap'
  141. echo "Done!"
  142. "@
  143. $applyScript | Out-File -FilePath "$OutputDir\apply-secrets.sh" -Encoding UTF8
  144. Write-Host "========== SUMMARY ==========" -ForegroundColor Cyan
  145. Write-Host "Total services processed: $($services.Count)"
  146. Write-Host "Secrets directory: $secretsDir"
  147. Write-Host "ConfigMaps directory: $configmapsDir"
  148. Write-Host "Apply script: $OutputDir\apply-secrets.sh"
  149. Write-Host ""
  150. Write-Host "Next steps:" -ForegroundColor Yellow
  151. Write-Host "1. Review all generated YAML files"
  152. Write-Host "2. Replace placeholder values with actual credentials"
  153. Write-Host "3. Run: bash $OutputDir\apply-secrets.sh"
  154. Write-Host ""