|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + |
| 11 | + batchv1 "k8s.io/api/batch/v1" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/util/wait" |
| 16 | + clientset "k8s.io/client-go/kubernetes" |
| 17 | + "k8s.io/client-go/tools/clientcmd" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + defaultJobExistenceTimeout = 3 * time.Minute |
| 22 | + defaultJobExecutionTimeout = 45 * time.Minute |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + defaultLogLevel = "info" |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + opts := &waitForJobOpts{} |
| 31 | + |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "waitforjob JOBNAME [OPTIONS]", |
| 34 | + Short: "wait for job", |
| 35 | + Long: "Contains various utilities for running and testing hive", |
| 36 | + Run: func(cmd *cobra.Command, args []string) { |
| 37 | + if len(args) != 1 { |
| 38 | + cmd.Usage() |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | + opts.jobName = args[0] |
| 42 | + opts.Run() |
| 43 | + }, |
| 44 | + } |
| 45 | + cmd.PersistentFlags().StringVar(&opts.logLevel, "log-level", defaultLogLevel, "Log level (debug,info,warn,error,fatal)") |
| 46 | + cmd.PersistentFlags().DurationVar(&opts.existenceTimeout, "job-existence-timeout", defaultJobExistenceTimeout, "Maximum time to wait for the named job to be created") |
| 47 | + cmd.PersistentFlags().DurationVar(&opts.executionTimeout, "job-execution-timeout", defaultJobExecutionTimeout, "Maximum time to wait for the job to execute") |
| 48 | + cmd.Execute() |
| 49 | +} |
| 50 | + |
| 51 | +type waitForJobOpts struct { |
| 52 | + logLevel string |
| 53 | + jobName string |
| 54 | + existenceTimeout time.Duration |
| 55 | + executionTimeout time.Duration |
| 56 | +} |
| 57 | + |
| 58 | +func (w *waitForJobOpts) Run() { |
| 59 | + // Set log level |
| 60 | + level, err := log.ParseLevel(w.logLevel) |
| 61 | + if err != nil { |
| 62 | + log.WithError(err).Fatal("Cannot parse log level") |
| 63 | + } |
| 64 | + log.SetLevel(level) |
| 65 | + log.Debug("debug logging enabled") |
| 66 | + |
| 67 | + client, namespace, err := w.localClient() |
| 68 | + if err != nil { |
| 69 | + log.WithError(err).Fatal("failed to obtain client") |
| 70 | + } |
| 71 | + |
| 72 | + if err := w.waitForJobExistence(client, namespace); err != nil { |
| 73 | + log.WithError(err).Fatal("job existence failed") |
| 74 | + } |
| 75 | + |
| 76 | + if err := w.waitForJobExecution(client, namespace); err != nil { |
| 77 | + log.WithError(err).Fatal("job execution failed") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func (w *waitForJobOpts) waitForJobExistence(client clientset.Interface, namespace string) error { |
| 82 | + logger := log.WithField("waiting-for-existence", fmt.Sprintf("job (%s/%s)", namespace, w.jobName)) |
| 83 | + err := wait.PollImmediate(10*time.Second, w.existenceTimeout, func() (bool, error) { |
| 84 | + logger.Debug("Retrieving job") |
| 85 | + _, err := client.BatchV1().Jobs(namespace).Get(w.jobName, metav1.GetOptions{}) |
| 86 | + if err != nil { |
| 87 | + if errors.IsNotFound(err) { |
| 88 | + logger.Debug("job does not exist yet") |
| 89 | + } else { |
| 90 | + logger.WithError(err).Warning("unexpected error retrieving job") |
| 91 | + } |
| 92 | + return false, nil |
| 93 | + } |
| 94 | + logger.Info("Job found") |
| 95 | + return true, nil |
| 96 | + }) |
| 97 | + return err |
| 98 | +} |
| 99 | + |
| 100 | +func (w *waitForJobOpts) waitForJobExecution(client clientset.Interface, namespace string) error { |
| 101 | + logger := log.WithField("waiting-for-run", fmt.Sprintf("job (%s/%s)", namespace, w.jobName)) |
| 102 | + err := wait.PollImmediate(30*time.Second, w.executionTimeout, func() (bool, error) { |
| 103 | + logger.Debug("Retrieving job") |
| 104 | + job, err := client.BatchV1().Jobs(namespace).Get(w.jobName, metav1.GetOptions{}) |
| 105 | + if err != nil { |
| 106 | + logger.WithError(err).Error("Could not fetch job") |
| 107 | + return false, err |
| 108 | + } |
| 109 | + if isFailed(job) { |
| 110 | + logger.Error("Job has failed") |
| 111 | + return false, fmt.Errorf("Job %s/%s has failed.", namespace, w.jobName) |
| 112 | + } |
| 113 | + if isSuccessful(job) { |
| 114 | + logger.Info("Job has finished successfully") |
| 115 | + return true, nil |
| 116 | + } |
| 117 | + logger.Debug("Job has not completed yet") |
| 118 | + return false, nil |
| 119 | + }) |
| 120 | + return err |
| 121 | +} |
| 122 | + |
| 123 | +func (w *waitForJobOpts) localClient() (clientset.Interface, string, error) { |
| 124 | + log.Debug("Creating cluster client") |
| 125 | + rules := clientcmd.NewDefaultClientConfigLoadingRules() |
| 126 | + kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{}) |
| 127 | + cfg, err := kubeconfig.ClientConfig() |
| 128 | + if err != nil { |
| 129 | + log.WithError(err).Error("Cannot obtain client config") |
| 130 | + return nil, "", err |
| 131 | + } |
| 132 | + namespace, _, err := kubeconfig.Namespace() |
| 133 | + if err != nil { |
| 134 | + log.WithError(err).Error("Cannot obtain default namespace from client config") |
| 135 | + return nil, "", err |
| 136 | + } |
| 137 | + |
| 138 | + kubeClient, err := clientset.NewForConfig(cfg) |
| 139 | + if err != nil { |
| 140 | + log.WithError(err).Error("Cannot create kubernetes client from client config") |
| 141 | + return nil, "", err |
| 142 | + } |
| 143 | + |
| 144 | + return kubeClient, namespace, nil |
| 145 | +} |
| 146 | + |
| 147 | +func getJobConditionStatus(job *batchv1.Job, conditionType batchv1.JobConditionType) corev1.ConditionStatus { |
| 148 | + for _, condition := range job.Status.Conditions { |
| 149 | + if condition.Type == conditionType { |
| 150 | + return condition.Status |
| 151 | + } |
| 152 | + } |
| 153 | + return corev1.ConditionFalse |
| 154 | +} |
| 155 | + |
| 156 | +func isSuccessful(job *batchv1.Job) bool { |
| 157 | + return getJobConditionStatus(job, batchv1.JobComplete) == corev1.ConditionTrue |
| 158 | +} |
| 159 | + |
| 160 | +func isFailed(job *batchv1.Job) bool { |
| 161 | + return getJobConditionStatus(job, batchv1.JobFailed) == corev1.ConditionTrue |
| 162 | +} |
0 commit comments