-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logic to check for Job readiness
- Loading branch information
1 parent
c9feaab
commit 377ce21
Showing
29 changed files
with
2,003 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// Copyright 2016-2019, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package await | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/golang/glog" | ||
"github.com/pkg/errors" | ||
"github.com/pulumi/pulumi-kubernetes/pkg/await/states" | ||
"github.com/pulumi/pulumi-kubernetes/pkg/clients" | ||
"github.com/pulumi/pulumi-kubernetes/pkg/kinds" | ||
"github.com/pulumi/pulumi-kubernetes/pkg/logging" | ||
"github.com/pulumi/pulumi-kubernetes/pkg/metadata" | ||
"github.com/pulumi/pulumi/pkg/diag" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/watch" | ||
) | ||
|
||
const ( | ||
DefaultJobTimeoutMins = 10 | ||
) | ||
|
||
type jobInitAwaiter struct { | ||
job *unstructured.Unstructured | ||
config createAwaitConfig | ||
state *states.StateChecker | ||
errors logging.TimeOrderedLogSet | ||
resource ResourceId | ||
} | ||
|
||
func makeJobInitAwaiter(c createAwaitConfig) *jobInitAwaiter { | ||
return &jobInitAwaiter{ | ||
config: c, | ||
job: c.currentOutputs, | ||
state: states.NewJobChecker(), | ||
resource: ResourceIdFromUnstructured(c.currentOutputs), | ||
} | ||
} | ||
|
||
func (jia *jobInitAwaiter) Await() error { | ||
jobClient, err := clients.ResourceClient(kinds.Job, jia.config.currentInputs.GetNamespace(), jia.config.clientSet) | ||
if err != nil { | ||
return errors.Wrapf(err, | ||
"Could not make client to watch Job %q", | ||
jia.config.currentInputs.GetName()) | ||
} | ||
jobWatcher, err := jobClient.Watch(metav1.ListOptions{}) | ||
if err != nil { | ||
return errors.Wrapf(err, "Couldn't set up watch for Job object %q", | ||
jia.config.currentInputs.GetName()) | ||
} | ||
defer jobWatcher.Stop() | ||
|
||
podAggregator, err := NewPodAggregator(ResourceIdFromUnstructured(jia.job), jia.config.clientSet) | ||
if err != nil { | ||
return errors.Wrapf(err, "Could not create PodAggregator for %s", jia.resource.GVKString()) | ||
} | ||
defer podAggregator.Stop() | ||
|
||
timeout := metadata.TimeoutDuration(jia.config.timeout, jia.config.currentInputs, DefaultJobTimeoutMins*60) | ||
for { | ||
if jia.state.Ready() { | ||
return nil | ||
} | ||
|
||
// Else, wait for updates. | ||
select { | ||
case <-jia.config.ctx.Done(): | ||
return &cancellationError{ | ||
object: jia.job, | ||
subErrors: jia.errorMessages(), | ||
} | ||
case <-time.After(timeout): | ||
return &timeoutError{ | ||
object: jia.job, | ||
subErrors: jia.errorMessages(), | ||
} | ||
case event := <-jobWatcher.ResultChan(): | ||
err := jia.processJobEvent(event) | ||
if err != nil { | ||
return err | ||
} | ||
case messages := <-podAggregator.ResultChan(): | ||
for _, message := range messages { | ||
jia.errors.Add(message) | ||
jia.config.logMessage(message) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (jia *jobInitAwaiter) Read() error { | ||
jobClient, err := clients.ResourceClient(kinds.Job, jia.config.currentInputs.GetNamespace(), jia.config.clientSet) | ||
if err != nil { | ||
return errors.Wrapf(err, | ||
"Could not make client to get Job %q", | ||
jia.config.currentInputs.GetName()) | ||
} | ||
// Get live version of Job. | ||
job, err := jobClient.Get(jia.config.currentInputs.GetName(), metav1.GetOptions{}) | ||
if err != nil { | ||
// IMPORTANT: Do not wrap this error! If this is a 404, the provider need to know so that it | ||
// can mark the Pod as having been deleted. | ||
return err | ||
} | ||
|
||
_ = jia.processJobEvent(watchAddedEvent(job)) | ||
|
||
// Check whether we've succeeded. | ||
if jia.state.Ready() { | ||
return nil | ||
} | ||
|
||
return &initializationError{ | ||
subErrors: jia.errorMessages(), | ||
object: job, | ||
} | ||
} | ||
|
||
func (jia *jobInitAwaiter) processJobEvent(event watch.Event) error { | ||
job, err := clients.JobFromUnstructured(event.Object.(*unstructured.Unstructured)) | ||
if err != nil { | ||
glog.V(3).Infof("Failed to unmarshal Job event: %v", err) | ||
return nil | ||
} | ||
|
||
// Do nothing if this is not the job we're waiting for. | ||
if job.GetName() != jia.config.currentInputs.GetName() { | ||
return nil | ||
} | ||
|
||
messages := jia.state.Update(job) | ||
for _, message := range messages.MessagesWithSeverity(diag.Warning, diag.Error) { | ||
jia.errors.Add(message) | ||
} | ||
for _, message := range messages { | ||
jia.config.logMessage(message) | ||
} | ||
|
||
if len(messages.Errors()) > 0 { | ||
return &initializationError{ | ||
subErrors: jia.errorMessages(), | ||
object: jia.job, | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (jia *jobInitAwaiter) errorMessages() []string { | ||
messages := make([]string, 0) | ||
for _, message := range jia.errors.Messages { | ||
messages = append(messages, message.S) | ||
} | ||
|
||
return messages | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{ | ||
"apiVersion": "batch/v1", | ||
"kind": "Job", | ||
"metadata": { | ||
"annotations": { | ||
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"batch/v1\",\"kind\":\"Job\",\"metadata\":{\"labels\":{\"app.kubernetes.io/managed-by\":\"pulumi\"},\"name\":\"foo\"},\"spec\":{\"backoffLimit\":1,\"template\":{\"spec\":{\"activeDeadlineSeconds\":10,\"containers\":[{\"command\":[\"perl\",\"-Mbignum=bpi\",\"-wle\",\"print bpi(2000)\"],\"image\":\"perl-fail\",\"name\":\"pi\"}],\"restartPolicy\":\"Never\"}}}}\n" | ||
}, | ||
"creationTimestamp": "2019-09-04T18:19:35Z", | ||
"labels": { | ||
"app.kubernetes.io/managed-by": "pulumi" | ||
}, | ||
"name": "foo", | ||
"namespace": "default", | ||
"resourceVersion": "1127282", | ||
"selfLink": "/apis/batch/v1/namespaces/default/jobs/foo", | ||
"uid": "8c668c31-cf40-11e9-8c3a-025000000001" | ||
}, | ||
"spec": { | ||
"backoffLimit": 1, | ||
"completions": 1, | ||
"parallelism": 1, | ||
"selector": { | ||
"matchLabels": { | ||
"controller-uid": "8c668c31-cf40-11e9-8c3a-025000000001" | ||
} | ||
}, | ||
"template": { | ||
"metadata": { | ||
"creationTimestamp": null, | ||
"labels": { | ||
"controller-uid": "8c668c31-cf40-11e9-8c3a-025000000001", | ||
"job-name": "foo" | ||
} | ||
}, | ||
"spec": { | ||
"activeDeadlineSeconds": 10, | ||
"containers": [ | ||
{ | ||
"command": [ | ||
"perl", | ||
"-Mbignum=bpi", | ||
"-wle", | ||
"print bpi(2000)" | ||
], | ||
"image": "perl-fail", | ||
"imagePullPolicy": "Always", | ||
"name": "pi", | ||
"resources": {}, | ||
"terminationMessagePath": "/dev/termination-log", | ||
"terminationMessagePolicy": "File" | ||
} | ||
], | ||
"dnsPolicy": "ClusterFirst", | ||
"restartPolicy": "Never", | ||
"schedulerName": "default-scheduler", | ||
"securityContext": {}, | ||
"terminationGracePeriodSeconds": 30 | ||
} | ||
} | ||
}, | ||
"status": { | ||
"conditions": [ | ||
{ | ||
"lastProbeTime": "2019-09-04T18:19:55Z", | ||
"lastTransitionTime": "2019-09-04T18:19:55Z", | ||
"message": "Job has reached the specified backoff limit", | ||
"reason": "BackoffLimitExceeded", | ||
"status": "True", | ||
"type": "Failed" | ||
} | ||
], | ||
"failed": 2, | ||
"startTime": "2019-09-04T18:19:35Z" | ||
} | ||
} |
Oops, something went wrong.