-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notify slack when k8s jobs fail (#237)
* Notify slack when k8s jobs fail * trigger build * Update cmd/daemon/kubernetes/jobs.go Co-authored-by: Bjørn <bso@lunar.app> * Address feedback Co-authored-by: Bjørn <bso@lunar.app>
- Loading branch information
Showing
8 changed files
with
214 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"context" | ||
"github.com/lunarway/release-manager/internal/http" | ||
"github.com/lunarway/release-manager/internal/log" | ||
batchv1 "k8s.io/api/batch/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/watch" | ||
) | ||
|
||
func (c *Client) HandleJobErrors(ctx context.Context) error { | ||
watcher, err := c.clientset.BatchV1().Jobs("").Watch(ctx, metav1.ListOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
watcher.Stop() | ||
case e, ok := <-watcher.ResultChan(): | ||
if !ok { | ||
return ErrWatcherClosed | ||
} | ||
if e.Object == nil { | ||
continue | ||
} | ||
if e.Type == watch.Deleted { | ||
continue | ||
} | ||
job, ok := e.Object.(*batchv1.Job) | ||
if !ok { | ||
continue | ||
} | ||
|
||
// Check if we have all the annotations we need for the release-daemon | ||
if !isCorrectlyAnnotated(job.Annotations) { | ||
continue | ||
} | ||
|
||
if isJobFailed(job) { | ||
// Notify the release-manager with the job error event. | ||
err = c.exporter.SendJobErrorEvent(ctx, http.JobErrorEvent{ | ||
JobName: job.Name, | ||
Namespace: job.Namespace, | ||
Errors: jobErrorMessages(job), | ||
ArtifactID: job.Annotations["lunarway.com/artifact-id"], | ||
AuthorEmail: job.Annotations["lunarway.com/author"], | ||
}) | ||
if err != nil { | ||
log.Errorf("Failed to send job error event: %v", err) | ||
continue | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func isJobFailed(job *batchv1.Job) bool { | ||
if len(job.Status.Conditions) == 0 { | ||
return false | ||
} | ||
for _, condition := range job.Status.Conditions { | ||
if condition.Status == corev1.ConditionTrue && condition.Type == batchv1.JobFailed { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func jobErrorMessages(job *batchv1.Job) []http.JobConditionError { | ||
var errors []http.JobConditionError | ||
|
||
for _, condition := range job.Status.Conditions { | ||
if condition.Status == corev1.ConditionTrue && condition.Type == batchv1.JobFailed { | ||
errors = append(errors, http.JobConditionError { | ||
Reason: condition.Reason, | ||
Message: condition.Message, | ||
}) | ||
} | ||
} | ||
return errors | ||
} |
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,45 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/lunarway/release-manager/internal/flow" | ||
httpinternal "github.com/lunarway/release-manager/internal/http" | ||
"github.com/lunarway/release-manager/internal/log" | ||
"github.com/lunarway/release-manager/internal/slack" | ||
opentracing "github.com/opentracing/opentracing-go" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func daemonk8sJobErrorWebhook(payload *payload, flowSvc *flow.Service) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
// copy span from request context but ignore any deadlines on the request context | ||
ctx := opentracing.ContextWithSpan(context.Background(), opentracing.SpanFromContext(r.Context())) | ||
logger := log.WithContext(ctx) | ||
var event httpinternal.JobErrorEvent | ||
err := payload.decodeResponse(ctx, r.Body, &event) | ||
if err != nil { | ||
logger.Errorf("http: daemon k8s job error webhook: decode request body failed: %v", err) | ||
invalidBodyError(w) | ||
return | ||
} | ||
logger = logger.WithFields("event", event) | ||
err = flowSvc.NotifyK8SJobErrorEvent(ctx, &event) | ||
if err != nil && errors.Cause(err) != slack.ErrUnknownEmail { | ||
logger.Errorf("http: daemon k8s pod error webhook failed: %+v", err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = payload.encodeResponse(ctx, w, httpinternal.KubernetesNotifyResponse{}) | ||
if err != nil { | ||
logger.Errorf("http: daemon k8s job error webhook: environment: '%s' marshal response: %v", event.Environment, err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
logger.Infof("http: daemon k8s job error webhook: handled") | ||
} | ||
} |
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