|
| 1 | +package jobrunevents |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "cloud.google.com/go/storage" |
| 12 | + log "github.com/sirupsen/logrus" |
| 13 | + |
| 14 | + "github.com/openshift/sippy/pkg/api" |
| 15 | + "github.com/openshift/sippy/pkg/dataloader/prowloader/gcs" |
| 16 | + "github.com/openshift/sippy/pkg/db" |
| 17 | +) |
| 18 | + |
| 19 | +// eventsJSONRegex matches paths like artifacts/*e2e*/gather-extra/artifacts/events.json |
| 20 | +var eventsJSONRegex = regexp.MustCompile(`gather-extra/artifacts/events\.json$`) |
| 21 | + |
| 22 | +// KubeEvent represents a flattened Kubernetes Event for the API response |
| 23 | +type KubeEvent struct { |
| 24 | + FirstTimestamp string `json:"firstTimestamp"` |
| 25 | + LastTimestamp string `json:"lastTimestamp"` |
| 26 | + Namespace string `json:"namespace"` |
| 27 | + Kind string `json:"kind"` |
| 28 | + Name string `json:"name"` |
| 29 | + Type string `json:"type"` |
| 30 | + Reason string `json:"reason"` |
| 31 | + Message string `json:"message"` |
| 32 | + Count int `json:"count"` |
| 33 | + Source string `json:"source"` |
| 34 | +} |
| 35 | + |
| 36 | +// rawKubeEvent is the Kubernetes Event structure from events.json |
| 37 | +type rawKubeEvent struct { |
| 38 | + FirstTimestamp string `json:"firstTimestamp"` |
| 39 | + LastTimestamp string `json:"lastTimestamp"` |
| 40 | + EventTime string `json:"eventTime"` |
| 41 | + InvolvedObject map[string]interface{} `json:"involvedObject"` |
| 42 | + Type string `json:"type"` |
| 43 | + Reason string `json:"reason"` |
| 44 | + Message string `json:"message"` |
| 45 | + Count int `json:"count"` |
| 46 | + Source map[string]string `json:"source"` |
| 47 | + Metadata map[string]interface{} `json:"metadata"` |
| 48 | + ReportingComp string `json:"reportingComponent"` |
| 49 | +} |
| 50 | + |
| 51 | +// EventListResponse is the API response for job run events |
| 52 | +type EventListResponse struct { |
| 53 | + Items []KubeEvent `json:"items"` |
| 54 | + JobRunURL string `json:"jobRunURL"` |
| 55 | +} |
| 56 | + |
| 57 | +// JobRunEvents fetches events.json for a given job run from the GCS path |
| 58 | +// artifacts/*e2e*/gather-extra/artifacts/events.json |
| 59 | +func JobRunEvents(gcsClient *storage.Client, dbc *db.DB, jobRunID int64, gcsBucket, gcsPath string, logger *log.Entry) (*EventListResponse, error) { |
| 60 | + jobRunURL := fmt.Sprintf("https://prow.ci.openshift.org/view/gs/%s/%s", gcsBucket, gcsPath) |
| 61 | + |
| 62 | + jobRun, err := api.FetchJobRun(dbc, jobRunID, false, nil, logger) |
| 63 | + if err != nil { |
| 64 | + logger.WithError(err).Debugf("failed to fetch job run %d", jobRunID) |
| 65 | + if gcsPath == "" { |
| 66 | + return nil, errors.New("no GCS path given and no job run found in DB") |
| 67 | + } |
| 68 | + } else { |
| 69 | + jobRunURL = jobRun.URL |
| 70 | + gcsBucket = jobRun.GCSBucket |
| 71 | + _, path, found := strings.Cut(jobRunURL, "/"+gcsBucket+"/") |
| 72 | + if !found { |
| 73 | + return nil, fmt.Errorf("job run URL %q does not contain bucket %q", jobRun.URL, gcsBucket) |
| 74 | + } |
| 75 | + gcsPath = path |
| 76 | + } |
| 77 | + |
| 78 | + gcsJobRun := gcs.NewGCSJobRun(gcsClient.Bucket(gcsBucket), gcsPath) |
| 79 | + matches, err := gcsJobRun.FindAllMatches([]*regexp.Regexp{eventsJSONRegex}) |
| 80 | + if err != nil { |
| 81 | + return &EventListResponse{JobRunURL: jobRunURL}, err |
| 82 | + } |
| 83 | + |
| 84 | + if len(matches) == 0 || len(matches[0]) == 0 { |
| 85 | + logger.Info("no events.json file found") |
| 86 | + return &EventListResponse{Items: []KubeEvent{}, JobRunURL: jobRunURL}, nil |
| 87 | + } |
| 88 | + |
| 89 | + eventsPath := matches[0][0] |
| 90 | + logger.WithField("events_path", eventsPath).Info("found events.json") |
| 91 | + |
| 92 | + content, err := gcsJobRun.GetContent(context.TODO(), eventsPath) |
| 93 | + if err != nil { |
| 94 | + logger.WithError(err).Errorf("error getting content for file: %s", eventsPath) |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + var rawEvents struct { |
| 99 | + Items []rawKubeEvent `json:"items"` |
| 100 | + } |
| 101 | + if err := json.Unmarshal(content, &rawEvents); err != nil { |
| 102 | + logger.WithError(err).Error("error unmarshaling events.json") |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + events := make([]KubeEvent, 0, len(rawEvents.Items)) |
| 107 | + for _, raw := range rawEvents.Items { |
| 108 | + evt := flattenEvent(raw) |
| 109 | + events = append(events, evt) |
| 110 | + } |
| 111 | + |
| 112 | + return &EventListResponse{Items: events, JobRunURL: jobRunURL}, nil |
| 113 | +} |
| 114 | + |
| 115 | +func flattenEvent(raw rawKubeEvent) KubeEvent { |
| 116 | + evt := KubeEvent{ |
| 117 | + FirstTimestamp: raw.FirstTimestamp, |
| 118 | + LastTimestamp: raw.LastTimestamp, |
| 119 | + Type: raw.Type, |
| 120 | + Reason: raw.Reason, |
| 121 | + Message: raw.Message, |
| 122 | + Count: raw.Count, |
| 123 | + } |
| 124 | + if raw.Count == 0 { |
| 125 | + evt.Count = 1 |
| 126 | + } |
| 127 | + if raw.InvolvedObject != nil { |
| 128 | + if k, ok := raw.InvolvedObject["kind"].(string); ok { |
| 129 | + evt.Kind = k |
| 130 | + } |
| 131 | + if n, ok := raw.InvolvedObject["name"].(string); ok { |
| 132 | + evt.Name = n |
| 133 | + } |
| 134 | + } |
| 135 | + if raw.Metadata != nil { |
| 136 | + if ns, ok := raw.Metadata["namespace"].(string); ok { |
| 137 | + evt.Namespace = ns |
| 138 | + } |
| 139 | + } |
| 140 | + if raw.Source != nil && raw.Source["component"] != "" { |
| 141 | + evt.Source = raw.Source["component"] |
| 142 | + } else if raw.ReportingComp != "" { |
| 143 | + evt.Source = raw.ReportingComp |
| 144 | + } |
| 145 | + return evt |
| 146 | +} |
0 commit comments