-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable storing of Run Events as Record #748
Open
khrm
wants to merge
1
commit into
tektoncd:main
Choose a base branch
from
khrm:eventStore
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ package dynamic | |
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"runtime/pprof" | ||
|
@@ -45,6 +46,7 @@ import ( | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
"knative.dev/pkg/apis" | ||
"knative.dev/pkg/controller" | ||
"knative.dev/pkg/logging" | ||
|
@@ -57,6 +59,9 @@ var ( | |
// Reconciler implements common reconciler behavior across different Tekton Run | ||
// Object types. | ||
type Reconciler struct { | ||
// KubeClientSet allows us to talk to the k8s for core APIs | ||
KubeClientSet kubernetes.Interface | ||
|
||
resultsClient *results.Client | ||
objectClient ObjectClient | ||
cfg *reconciler.Config | ||
|
@@ -82,9 +87,10 @@ type IsReadyForDeletion func(ctx context.Context, object results.Object) (bool, | |
type AfterDeletion func(ctx context.Context, object results.Object) error | ||
|
||
// NewDynamicReconciler creates a new dynamic Reconciler. | ||
func NewDynamicReconciler(rc pb.ResultsClient, lc pb.LogsClient, oc ObjectClient, cfg *reconciler.Config) *Reconciler { | ||
func NewDynamicReconciler(kubeClientSet kubernetes.Interface, rc pb.ResultsClient, lc pb.LogsClient, oc ObjectClient, cfg *reconciler.Config) *Reconciler { | ||
return &Reconciler{ | ||
resultsClient: results.NewClient(rc, lc), | ||
KubeClientSet: kubeClientSet, | ||
objectClient: oc, | ||
cfg: cfg, | ||
// Always true predicate. | ||
|
@@ -213,6 +219,26 @@ func (r *Reconciler) Reconcile(ctx context.Context, o results.Object) error { | |
} | ||
} | ||
|
||
// CreateEvents if enabled | ||
if r.cfg.StoreEvent { | ||
if err := r.storeEvents(ctx, o); err != nil { | ||
logger.Errorw("Error storing eventlist", | ||
zap.String("namespace", o.GetNamespace()), | ||
zap.String("kind", o.GetObjectKind().GroupVersionKind().Kind), | ||
zap.String("name", o.GetName()), | ||
zap.Error(err), | ||
) | ||
if ctxCancel != nil { | ||
ctxCancel() | ||
} | ||
return err | ||
} | ||
logger.Debugw("Successfully store eventlist", | ||
zap.String("namespace", o.GetNamespace()), | ||
zap.String("kind", o.GetObjectKind().GroupVersionKind().Kind), | ||
zap.String("name", o.GetName()), | ||
) | ||
} | ||
logger = logger.With(zap.String("results.tekton.dev/result", res.Name), | ||
zap.String("results.tekton.dev/record", rec.Name)) | ||
logger.Debugw("Record has been successfully upserted into API server", timeTakenField) | ||
|
@@ -572,3 +598,64 @@ func (r *Reconciler) streamLogs(ctx context.Context, o results.Object, logType, | |
|
||
return nil | ||
} | ||
|
||
// storeEvents streams logs to the API server | ||
func (r *Reconciler) storeEvents(ctx context.Context, o results.Object) error { | ||
logger := logging.FromContext(ctx) | ||
condition := o.GetStatusCondition().GetCondition(apis.ConditionSucceeded) | ||
GVK := o.GetObjectKind().GroupVersionKind() | ||
if !GVK.Empty() && | ||
(GVK.Kind == "TaskRun" || GVK.Kind == "PipelineRun") && | ||
condition != nil && | ||
condition.Type == "Succeeded" && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand correctly, we are only storing events in case of Success. Should this include more conditions? |
||
!condition.IsUnknown() { | ||
|
||
rec, err := r.resultsClient.GetEventListRecord(ctx, o) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if rec != nil { | ||
// It means we have already stored events | ||
eventListName := rec.GetName() | ||
// Update Events annotation if it doesn't exist | ||
return r.addResultsAnnotations(ctx, o, annotation.Annotation{Name: annotation.EventList, Value: eventListName}) | ||
} | ||
|
||
events, err := r.KubeClientSet.CoreV1().Events(o.GetNamespace()).List(ctx, metav1.ListOptions{ | ||
FieldSelector: "involvedObject.uid=" + string(o.GetUID()), | ||
}) | ||
if err != nil { | ||
logger.Errorf("Failed to store events - retrieve", | ||
zap.String("namespace", o.GetNamespace()), | ||
zap.String("kind", o.GetObjectKind().GroupVersionKind().Kind), | ||
zap.String("name", o.GetName()), | ||
zap.String("err", err.Error()), | ||
) | ||
return err | ||
} | ||
|
||
eventList, err := json.Marshal(events) | ||
if err != nil { | ||
logger.Errorf("Failed to store events - marshal", | ||
zap.String("namespace", o.GetNamespace()), | ||
zap.String("kind", o.GetObjectKind().GroupVersionKind().Kind), | ||
zap.String("name", o.GetName()), | ||
zap.String("err", err.Error()), | ||
) | ||
return err | ||
} | ||
|
||
rec, err = r.resultsClient.PutEventList(ctx, o, eventList) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := r.addResultsAnnotations(ctx, o, annotation.Annotation{Name: annotation.EventList, Value: rec.GetName()}); err != nil { | ||
return err | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be off by default
events are even more numerous than secrets and config map on a k8s cluster - we should not impact performance by default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be false. It was enabled for testing. Will change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bump @khrm - you still need to change this default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 should be off by default