k3sUtil.groovy 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // 配置configMap Environment
  2. // - SERVICE: service配置
  3. // - K3S: k3s配置
  4. // - configMapPath: 指定configMapPath,默认使用"projdir/configmap-env"
  5. def applyConfigMapEnv(Object SERVICE, Object K3S, String configMapPath='projdir/configmap-env.ini', String defaultNS='ahxpm') {
  6. println "applying k3s configmap environment from ${configMapPath}"
  7. configmap_env_name = "${SERVICE.name}${SERVICE.version}-env"
  8. configmap_env = "${SERVICE.name}${SERVICE.version}-env.yaml"
  9. sh "/k3s/kubectl create configmap ${configmap_env_name} --dry-run=client --from-env-file=${configMapPath} -o yaml > ${configmap_env}"
  10. sh "/k3s/kubectl --kubeconfig ${env.WORKSPACE}/${K3S.kubeconfig} apply -f ${configmap_env} -n ${defaultNS}"
  11. return configmap_env_name
  12. }
  13. // 配置configMap
  14. // - SERVICE: service配置
  15. // - K3S: k3s配置
  16. // - configMapPath: 指定configMapPath,默认使用./configmap/
  17. def applyConfigMapConfig(Object SERVICE, Object K3S, String configMapPath = './configmap/', String defaultNS='ahxpm') {
  18. println "applying k3s configmap config from ${configMapPath}"
  19. configmap_name = "${SERVICE.name}${SERVICE.version}-config"
  20. configmap_file = "${SERVICE.name}-configmap.yaml"
  21. sh "/k3s/kubectl create configmap ${configmap_name} --dry-run=client --from-file=${configMapPath} -o yaml > ${configmap_file}"
  22. sh "/k3s/kubectl --kubeconfig ${env.WORKSPACE}/${K3S.kubeconfig} apply -f ${configmap_file} -n ${defaultNS}"
  23. return configmap_name
  24. }
  25. // 生成deployment.yaml
  26. // - template: deployment(template).yaml模板文件路径
  27. // - kvs: key/value替换deployment(template)
  28. def generateDeployment(Map kvs, String template="deployment.yaml") {
  29. if (!fileExists(template)) {
  30. println "skip generating deployment.yaml, file ${template} not exists"
  31. return
  32. }
  33. println "generating k8s deployment... replace Key/Value in file: ${template}."
  34. kvs.each {
  35. def key, val ->
  36. print(val)
  37. value = val
  38. .replace('/', '\\/')
  39. .replace('"', '\\"')
  40. sh "sed -i \"s/${key}/${value}/g\" ${template}"
  41. }
  42. }
  43. // 生成service.yaml
  44. // - template: service(template).yaml模板文件路径
  45. // - kvs: key/value替换service(template).yaml
  46. def generateService(Map kvs, String template="service.yaml") {
  47. if (!fileExists(template)) {
  48. println "skip generating service.yaml, file ${template} not exists"
  49. return
  50. }
  51. println "generating k8s service.yaml... replace Key/Value in file: ${template}."
  52. kvs.each {
  53. def key, val ->
  54. print(val)
  55. value = val
  56. .replace('/', '\\/')
  57. .replace('"', '\\"')
  58. sh "sed -i \"s/${key}/${value}/g\" ${template}"
  59. }
  60. }
  61. // 配置deployment.yaml
  62. def applyDeployment(Object K3S, String deployment="deployment.yaml") {
  63. if (!fileExists(deployment)) {
  64. println "ignore, file ${deployment} not exists"
  65. return
  66. }
  67. println "applying k8s deployment file: ${deployment} ..."
  68. sh "/k3s/kubectl --kubeconfig ${env.WORKSPACE}/${K3S.kubeconfig} apply -f ${deployment}"
  69. }
  70. // 配置service.yaml
  71. def applyService(K3S, String service="service.yaml") {
  72. if (!fileExists(service)) {
  73. println "ignore, file ${service} not exists"
  74. return
  75. }
  76. println "applying k8s deployment file: ${service} ..."
  77. sh "/k3s/kubectl --kubeconfig ${env.WORKSPACE}/${K3S.kubeconfig} apply -f ${service}"
  78. }
  79. return this