Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. import * as pulumi from "@pulumi/pulumi";
  2. import * as k8s from "@pulumi/kubernetes";
  3. import * as k8sOutput from "@pulumi/kubernetes/types/output";
  4. import * as k8sapi from 'kubernetes-client';
  5.  
  6. const job = new k8s.batch.v1.Job("job", {
  7. spec: {
  8. template: {
  9. spec: {
  10. containers: [{
  11. name: "helloworld",
  12. image: "hello-world",
  13. }],
  14. restartPolicy: "Never",
  15. },
  16. },
  17. },
  18. });
  19.  
  20. async function waitForJob(jobMetadata: k8sOutput.meta.v1.ObjectMeta): Promise<any> {
  21. if (!pulumi.runtime.isDryRun()) {
  22. // TODO: Note that this needs to be adjusted to use the same configuration as the Pulumi
  23. // Kubernetes provider
  24. const client = new k8sapi.Client1_13({ config: k8sapi.config.fromKubeconfig() });
  25. // Wait for up to 10 minutes
  26. for (let i = 0; i < 60; i++) {
  27. const jobDetails = await client.apis.batch.v1.namespace(jobMetadata.namespace).job(jobMetadata.name).get();
  28. if (jobDetails.body && jobDetails.body.status && jobDetails.body.status.succeeded > 0) {
  29. return jobDetails.body;
  30. }
  31. pulumi.log.info(`Waiting for Job to finish (${i})`, job)
  32. // Wait for 10s between polls
  33. await new Promise(r => setTimeout(r, 10000));
  34. }
  35. throw new Error("timed out waiting for Job to complete");
  36. }
  37. }
  38.  
  39. // Compute an output value which waits for the Job to be done.
  40. const jobDone = job.metadata.apply(metadata => waitForJob(metadata));
  41.  
  42. const job2 = new k8s.batch.v1.Job("job2", {
  43. metadata: {
  44. annotations: {
  45. // This is a way to make the `job2` Job depend on the wait logic above. We take
  46. // advantage of this to write the value into an annotation, but we could also ignore the
  47. // value.
  48. "pulumi-waited-on-completion": jobDone.apply(j => j.status.completionTime),
  49. }
  50. },
  51. spec: {
  52. template: {
  53. spec: {
  54. containers: [{
  55. name: "helloworld",
  56. image: "hello-world",
  57. }],
  58. restartPolicy: "Never",
  59. },
  60. },
  61. },
  62. });
  63.  
  64. const job2Done = job2.metadata.apply(metadata => waitForJob(metadata));
  65.  
  66.  
  67. export const jobId = job.id;
  68. export const jobStatus = job.status;
  69. export const jobDoneDetails = jobDone;
  70. export const job2DoneDetails = job2Done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement