PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/kubernetes-steps/src/main/resources/io/fabric8/kubernetes/pipeline/Kubernetes.groovy

https://gitlab.com/CORP-RESELLER/kubernetes-pipeline-plugin
Groovy | 358 lines | 273 code | 66 blank | 19 comment | 6 complexity | af2b1e22d8175f0d1348a09115a9f834 MD5 | raw file
  1. /*
  2. * Copyright (C) 2015 Original Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.fabric8.kubernetes.pipeline
  17. class Kubernetes implements Serializable {
  18. private org.jenkinsci.plugins.workflow.cps.CpsScript script
  19. public final image
  20. public Kubernetes(org.jenkinsci.plugins.workflow.cps.CpsScript script) {
  21. this.script = script
  22. this.image = new Image(this)
  23. }
  24. private <V> V node(Closure<V> body) {
  25. if (script.env.HOME != null) { // http://unix.stackexchange.com/a/123859/26736
  26. // Already inside a node block.
  27. body()
  28. } else {
  29. script.node {
  30. body()
  31. }
  32. }
  33. }
  34. public Pod pod(String name = "jenkins-pod", String image = "", String serviceAccount = "", Boolean privileged = false, Map<String, String> secrets = new HashMap(), Map<String, String> hostPaths = new HashMap(), Map<String, String> emptyDirs = new HashMap<>(), Map<String, String> env = new HashMap<>()) {
  35. return new Pod(this, name, image, serviceAccount, privileged, secrets, hostPaths, emptyDirs, env)
  36. }
  37. public Image image() {
  38. return new Image(this)
  39. }
  40. public Image image(String name) {
  41. return new NamedImage(this, name)
  42. }
  43. public static class Pod implements Serializable {
  44. private final Kubernetes kubernetes
  45. private final String name
  46. private final String image
  47. private final String serviceAccount
  48. private final Boolean privileged
  49. private final Map secrets
  50. private final Map hostPathMounts
  51. private final Map emptyDirs
  52. private final Map env
  53. Pod(Kubernetes kubernetes, String name, String image, String serviceAccount, Boolean privileged, Map<String, String> secrets, Map<String, String> hostPathMounts, Map<String, String> emptyDirs, Map<String, String> env) {
  54. this.kubernetes = kubernetes
  55. this.name = name
  56. this.image = image
  57. this.serviceAccount = serviceAccount
  58. this.privileged = privileged
  59. this.secrets = secrets
  60. this.hostPathMounts = hostPathMounts
  61. this.emptyDirs = emptyDirs
  62. this.env = env
  63. }
  64. public Pod withName(String name) {
  65. return new Pod(kubernetes, name, image, serviceAccount, privileged, secrets, hostPathMounts, emptyDirs, env)
  66. }
  67. public Pod withImage(String image) {
  68. return new Pod(kubernetes, name, image, serviceAccount, privileged, secrets, hostPathMounts, emptyDirs, env)
  69. }
  70. public Pod withServiceAccount(String serviceAccount) {
  71. return new Pod(kubernetes, name, image, serviceAccount, privileged, secrets, hostPathMounts, emptyDirs, env)
  72. }
  73. public Pod withPrivileged(Boolean privileged) {
  74. return new Pod(kubernetes, name, image, serviceAccount, privileged, secrets, hostPathMounts, emptyDirs, env)
  75. }
  76. public Pod withSecret(String secretName, String mountPath) {
  77. Map<String, String> newSecrets = new HashMap<>(secrets)
  78. newSecrets.put(secretName, mountPath)
  79. return new Pod(kubernetes, name, image, serviceAccount, privileged, newSecrets, hostPathMounts, emptyDirs, env)
  80. }
  81. public Pod withHostPathMount(String hostPath, String mountPath) {
  82. Map<String, String> newHostPathMounts = new HashMap<>(hostPathMounts)
  83. newHostPathMounts.put(hostPath, mountPath)
  84. return new Pod(kubernetes, name, image, serviceAccount, privileged, secrets, newHostPathMounts, emptyDirs, env)
  85. }
  86. public Pod withEmptyDir(String mountPath) {
  87. return withEmptyDir(mountPath, null)
  88. }
  89. public Pod withEmptyDir(String mountPath, String medium) {
  90. Map<String, String> newEmptyDirs = new HashMap<>(emptyDirs)
  91. newEmptyDirs.put(emptyDir, medium)
  92. return new Pod(kubernetes, name, image, serviceAccount, privileged, secrets, hostPathMounts, newEmptyDirs, env)
  93. }
  94. public Pod withEnvVar(String key, String value) {
  95. Map<String, String> newEnv = new HashMap<>(env)
  96. newEnv.put(key, value)
  97. return new Pod(kubernetes, name, image, serviceAccount, privileged, secrets, hostPathMounts, emptyDirs, newEnv)
  98. }
  99. public <V> V inside(Closure<V> body) {
  100. kubernetes.node {
  101. kubernetes.script.withPod(name: name, image: image, serviceAccount: serviceAccount, privileged: privileged, secrets: secrets, hostPathMounts: hostPathMounts, emptyDirs: emptyDirs, env: env) {
  102. body()
  103. }
  104. }
  105. }
  106. }
  107. public class Image implements Serializable {
  108. private final Kubernetes kubernetes
  109. Image(Kubernetes kubernetes) {
  110. this.kubernetes = kubernetes
  111. }
  112. //Shortcut to build
  113. Pod build(String name, String path = ".", boolean rm = false, long timeout = 600000L) {
  114. this.withName(name)
  115. .build()
  116. .withTimeout(timeout)
  117. .removingIntermediate(rm)
  118. .fromPath(path)
  119. }
  120. //Shortcut to tag
  121. void tag(String name, String tag, String repo = "") {
  122. this.withName(name)
  123. .tag()
  124. .inRepository(repo != null && !repo.isEmpty() ? repo : name)
  125. .withTag(tag)
  126. }
  127. //Shortcut to push
  128. void push(String name, String tagName = "latest", long timeout = 600000L) {
  129. this.withName(name)
  130. .push()
  131. .withTimeout(timeout)
  132. .withTag(tagName)
  133. .toRegistry()
  134. }
  135. NamedImage withName(String name) {
  136. return new NamedImage(kubernetes, name)
  137. }
  138. }
  139. private static class NamedImage implements Serializable {
  140. private final Kubernetes kubernetes
  141. private final String name
  142. NamedImage(Kubernetes kubernetes, String name) {
  143. this.kubernetes = kubernetes
  144. this.name = name
  145. }
  146. Pod toPod() {
  147. return new Pod(kubernetes, "jenkins-buildpod", name, "", false, new HashMap<String, String>(), new HashMap<String, String>(), new HashMap<String, String>(), new HashMap<String, String>())
  148. }
  149. BuildImage build() {
  150. return new BuildImage(kubernetes, name, false, 600000L, null, null, null, new ArrayList<String>())
  151. }
  152. PushImage push() {
  153. return new PushImage(kubernetes, name, null, 600000L, null, null, null)
  154. }
  155. void tag() {
  156. new TagImage(kubernetes, name, null, null, false, null, null, null)
  157. }
  158. }
  159. private static class BuildImage implements Serializable {
  160. private final Kubernetes kubernetes
  161. private final String name
  162. private final Boolean rm
  163. private final long timeout
  164. private final String username
  165. private final String password
  166. private final String email
  167. private final List<String> ignorePatterns
  168. BuildImage(Kubernetes kubernetes, String name, Boolean rm, long timeout, String username, String password, String email, List<String> ignorePatterns) {
  169. this.kubernetes = kubernetes
  170. this.name = name
  171. this.rm = rm
  172. this.timeout = timeout
  173. this.username = username
  174. this.password = password
  175. this.email = email
  176. this.ignorePatterns = ignorePatterns != null ? ignorePatterns : new ArrayList<String>()
  177. }
  178. BuildImage removingIntermediate() {
  179. return removingIntermediate(true)
  180. }
  181. BuildImage removingIntermediate(Boolean rm) {
  182. return new BuildImage(kubernetes, name, rm, timeout, username, password, email, ignorePatterns)
  183. }
  184. BuildImage withTimeout(long timeout) {
  185. return new BuildImage(kubernetes, name, rm, timeout, username, password, email, ignorePatterns)
  186. }
  187. BuildImage withUsername(String username) {
  188. return new BuildImage(kubernetes, name, rm, timeout, username, password, email, ignorePatterns)
  189. }
  190. BuildImage withPassword(String password) {
  191. return new BuildImage(kubernetes, name, rm, timeout, username, password, email, ignorePatterns)
  192. }
  193. BuildImage withEmail(String email) {
  194. return new BuildImage(kubernetes, name, rm, timeout, username, password, email, ignorePatterns)
  195. }
  196. BuildImage ignoringPattern(String pattern) {
  197. List<String> newIgnorePatterns = new ArrayList<>(ignorePatterns)
  198. newIgnorePatterns.add(pattern)
  199. return new BuildImage(kubernetes, name, rm, timeout, username, password, email, newIgnorePatterns)
  200. }
  201. Pod fromPath(String path) {
  202. kubernetes.node {
  203. kubernetes.script.buildImage(name: name, rm: rm, path: path, timeout: timeout, username: username, password: password, email: email, ignorePatterns: ignorePatterns)
  204. }
  205. return new NamedImage(kubernetes, name).toPod()
  206. }
  207. }
  208. private static class TagImage implements Serializable {
  209. private final Kubernetes kubernetes
  210. private final String name
  211. private final String repo
  212. private final String tagName
  213. private final Boolean force
  214. private final String username
  215. private final String password
  216. private final String email
  217. TagImage(Kubernetes kubernetes, String name, String repo, String tagName, Boolean force, String username, String password, String email) {
  218. this.kubernetes = kubernetes
  219. this.name = name
  220. this.repo = repo
  221. this.tagName = tagName
  222. this.force = force
  223. this.username = username
  224. this.password = password
  225. this.email = email
  226. }
  227. TagImage inRepository(String repo) {
  228. return new TagImage(kubernetes, name, repo, tagName, force, username, password, email)
  229. }
  230. TagImage force() {
  231. return new TagImage(kubernetes, name, repo, tagName, true, username, password, email)
  232. }
  233. TagImage withUsername(String username) {
  234. return new TagImage(kubernetes, name, repo, tagName, force, username, password, email)
  235. }
  236. TagImage withPassword(String password) {
  237. return new TagImage(kubernetes, name, repo, tagName, force, username, password, email)
  238. }
  239. TagImage withEmail(String email) {
  240. return new TagImage(kubernetes, name, repo, tagName, force, username, password, email)
  241. }
  242. void withTag(String tagName) {
  243. kubernetes.node {
  244. kubernetes.script.tagImage(name: name, repo: repo, tag: tagName, force: force, username: username, password: password, email: email)
  245. }
  246. }
  247. }
  248. private static class PushImage implements Serializable {
  249. private final Kubernetes kubernetes
  250. private final String name
  251. private final String tagName
  252. private final long timeout
  253. private final String username
  254. private final String password
  255. private final String email
  256. PushImage(Kubernetes kubernetes, String name, String tagName, long timeout, String username, String password, String email) {
  257. this.kubernetes = kubernetes
  258. this.name = name
  259. this.tagName = tagName
  260. this.timeout = timeout
  261. this.username = username
  262. this.password = password
  263. this.email = email
  264. }
  265. PushImage force() {
  266. return new PushImage(kubernetes, name, tagName, timeout, username, password, email)
  267. }
  268. PushImage withTag(String tagName) {
  269. return new PushImage(kubernetes, name, tagName, timeout, username, password, email)
  270. }
  271. PushImage withTimeout(long timeout) {
  272. return new PushImage(kubernetes, name, tagName, timeout, username, password, email)
  273. }
  274. PushImage withUsername(String username) {
  275. return new PushImage(kubernetes, name, tagName, timeout, username, password, email)
  276. }
  277. PushImage withPassword(String password) {
  278. return new PushImage(kubernetes, name, tagName, timeout, username, password, email)
  279. }
  280. PushImage withEmail(String email) {
  281. return new PushImage(kubernetes, name, tagName, timeout, username, password, email)
  282. }
  283. void toRegistry(String registry) {
  284. kubernetes.node {
  285. kubernetes.script.pushImage(name: name, tagName: tagName, timeout: timeout, registry: registry, username: username, password: password, email: email)
  286. }
  287. }
  288. void toRegistry() {
  289. toRegistry('')
  290. }
  291. }
  292. }