Skip to content

Commit

Permalink
report/feat: opct adm baseline (get|list|indexer|publish)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtulio committed Aug 1, 2024
1 parent 9265246 commit 48e1612
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 75 deletions.
27 changes: 13 additions & 14 deletions internal/opct/summary/consolidated.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func NewConsolidatedSummary(in *ConsolidatedSummaryInput) *ConsolidatedSummary {
KubernetesConformance: &OpenshiftTestsSuite{Name: "kubernetesConformance"},
},
},
BaselineAPI: &baseline.BaselineConfig{},
}
}

Expand Down Expand Up @@ -372,7 +373,7 @@ func (cs *ConsolidatedSummary) applyFilterBaselineAPI() error {
// loadBaselineFromAPI query the the OPCT "backend" looking for the baseline results.
func (cs *ConsolidatedSummary) loadBaselineFromAPI() error {
if os.Getenv("OPCT_DISABLE_FILTER_BASELINE") == "1" {
log.Warnf("Filter Baseline was explicited disabled by OPCT_DISABLE_FILTER_BASELINE")
log.Warnf("Unable to load baseline from API, filter Baseline is explicited disabled by OPCT_DISABLE_FILTER_BASELINE")
return nil
}
// Path to S3 Object /api/v0/result/summary/{ocpVersion}/{platformType}
Expand Down Expand Up @@ -422,17 +423,9 @@ func (cs *ConsolidatedSummary) applyFilterBaselineAPIForPlugin(pluginName string
switch pluginName {
case plugin.PluginNameKubernetesConformance:
providerSummary = cs.GetProvider().GetOpenShift().GetResultK8SValidated()
e2eFailuresBaseline, err = cs.BaselineAPI.GetBuffer().GetPriorityFailuresFromPlugin(plugin.PluginNameKubernetesConformance)
if err != nil {
log.Errorf("failed to get priority failures from plugin: %w", err)
}

case plugin.PluginNameOpenShiftConformance:
providerSummary = cs.GetProvider().GetOpenShift().GetResultOCPValidated()
e2eFailuresBaseline, err = cs.BaselineAPI.GetBuffer().GetPriorityFailuresFromPlugin(plugin.PluginNameOpenShiftConformance)
if err != nil {
log.Errorf("failed to get priority failures from plugin: %w", err)
}

case plugin.PluginNameOpenShiftUpgrade:
providerSummary = cs.GetProvider().GetOpenShift().GetResultConformanceUpgrade()
Expand All @@ -444,7 +437,16 @@ func (cs *ConsolidatedSummary) applyFilterBaselineAPIForPlugin(pluginName string
return fmt.Errorf("suite not found to apply filter: Baseline API")
}

b := cs.BaselineAPI.GetBuffer()
if b != nil {
e2eFailuresBaseline, err = b.GetPriorityFailuresFromPlugin(pluginName)
if err != nil {
log.Errorf("failed to get priority failures from plugin: %w", err)

Check failure on line 444 in internal/opct/summary/consolidated.go

View workflow job for this annotation

GitHub Actions / go-lint

printf: github.com/sirupsen/logrus.Errorf does not support error-wrapping directive %w (govet)
}
}

if skipFilter {
log.Warn("Filter Baseline was explicited disabled by OPCT_DISABLE_FILTER_BASELINE")
providerSummary.FailedFilter4 = providerSummary.FailedFilter3
}

Expand All @@ -463,10 +465,7 @@ func (cs *ConsolidatedSummary) applyFilterBaselineAPIForPlugin(pluginName string
}
providerSummary.FailedExcludedFilter4 = append(providerSummary.FailedExcludedFilter4, v)
}
// fmt.Printf("\n\n>>> FailedFilter4: (%d) %v\n\n", len(providerSummary.FailedFilter4), providerSummary.FailedFilter4)
// fmt.Printf("\n\n>>> FailedExcludedFilter4: (%d) %v\n\n", len(providerSummary.FailedExcludedFilter4), providerSummary.FailedExcludedFilter4)

fmt.Printf("\n>>> %s: e2eFailuresBaseline(%d) e2eFailuresPipeline(%d) FailedFilter4(%d) FailedExcludedFilter4(%d)\n",
log.Debugf("Debug filter BaselineAPI on pipeline for plugin %s: e2eFailuresBaseline(%d) e2eFailuresPipeline(%d) FailedFilter4(%d) FailedExcludedFilter4(%d)\n",
pluginName, len(e2eFailuresBaseline), len(e2eFailuresPipeline),
len(providerSummary.FailedFilter4), len(providerSummary.FailedExcludedFilter4))
sort.Strings(providerSummary.FailedFilter4)
Expand Down Expand Up @@ -683,7 +682,7 @@ func (cs *ConsolidatedSummary) SaveResults(path string) error {
return err
}

fmt.Printf("\n Data Saved to directory '%s'\n", path)
log.Infof("#> Data Saved to directory %q", path)
cs.Timers.Add("cs-save/results")
return nil
}
Expand Down
14 changes: 10 additions & 4 deletions internal/report/baseline/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)

// createS3Client creates an S3 client with the specified region
func createS3Client(region string) (*s3.S3, error) {
func createS3Client(region string) (*s3.S3, *s3manager.Uploader, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
})
if err != nil {
return nil, err
return nil, nil, err
}

svc := s3.New(sess)
return svc, nil

// upload managers https://docs.aws.amazon.com/sdk-for-go/api/service/s3/
// Create an uploader with the session and default options
uploader := s3manager.NewUploader(sess)

return svc, uploader, nil
}

// createS3Client creates an S3 client with the specified region
// createCloudFrontClient creates an S3 client with the specified region
func createCloudFrontClient(region string) (*cloudfront.CloudFront, error) {
sess, err := session.NewSessionWithOptions(session.Options{
Profile: "opct",
Expand Down
29 changes: 29 additions & 0 deletions internal/report/baseline/baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"os"

"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/go-retryablehttp"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -51,6 +53,30 @@ func NewBaselineReportSummary() *BaselineConfig {
}
}

func (brs *BaselineConfig) createS3Clients() (*s3.S3, *s3manager.Uploader, error) {
if !brs.checkRequiredParams() {
return nil, nil, fmt.Errorf("missing required parameters or dependencies to enable this feature. Please wait for stable release to use it")
}

// create s3 client
svcS3, uploader, err := createS3Client(brs.bucketRegion)
if err != nil {
return nil, nil, err
}

// Check if the bucket exists
bucketExists, err := checkBucketExists(svcS3, brs.bucketName)
if err != nil {
return nil, nil, err
}

if !bucketExists {
return nil, nil, fmt.Errorf("the OPCT storage does not exists")
}

return svcS3, uploader, nil
}

func (brs *BaselineConfig) ReadReportSummaryIndexFromAPI() (*baselineIndex, error) {
resp, err := brs.ReadReportSummaryFromAPI(objectPathBaselineReportSummaryPath)
if err != nil {
Expand Down Expand Up @@ -181,5 +207,8 @@ func (brs *BaselineConfig) checkRequiredParams() bool {
}

func (brs *BaselineConfig) GetBuffer() *BaselineData {
if brs.buffer == nil {
return nil
}
return brs.buffer
}
82 changes: 40 additions & 42 deletions internal/report/baseline/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/davecgh/go-spew/spew"
log "github.com/sirupsen/logrus"
)

Expand All @@ -33,32 +32,8 @@ type baselineIndex struct {
Latest map[string]*baselineIndexItem `json:"latest"`
}

func (brs *BaselineConfig) createS3Client() (*s3.S3, error) {
if !brs.checkRequiredParams() {
return nil, fmt.Errorf("missing required parameters or dependencies to enable this feature. Please wait for stable release to use it")
}

// create s3 client
svcS3, err := createS3Client(brs.bucketRegion)
if err != nil {
return nil, err
}

// Check if the bucket exists
bucketExists, err := checkBucketExists(svcS3, brs.bucketName)
if err != nil {
return nil, err
}

if !bucketExists {
return nil, fmt.Errorf("the OPCT storage does not exists")
}

return svcS3, nil
}

func (brs *BaselineConfig) CreateBaselineIndex() error {
svcS3, err := brs.createS3Client()
svcS3, _, err := brs.createS3Clients()
if err != nil {
return fmt.Errorf("failed to create S3 client and validate bucket: %w", err)
}
Expand All @@ -73,6 +48,7 @@ func (brs *BaselineConfig) CreateBaselineIndex() error {
LastUpdate: time.Now().Format(time.RFC3339),
Latest: make(map[string]*baselineIndexItem),
}
// calculate the index for each object (summary)
for _, obj := range objects {
// Keys must have the following format: {ocpVersion}_{platformType}_{timestamp}.json
objectKey := *obj.Key
Expand All @@ -81,42 +57,66 @@ func (brs *BaselineConfig) CreateBaselineIndex() error {
if name == "index.json" {
continue
}

// read the object to extract metadata/tags from 'setup.api'
objReader, err := svcS3.GetObject(&s3.GetObjectInput{
Bucket: aws.String(brs.bucketName),
Key: aws.String(objectKey),
})
if err != nil {
return fmt.Errorf("failed to get bucket: %w", err)
log.Errorf("failed to get object %s: %v", objectKey, err)
continue
}

defer objReader.Body.Close()
bd := &BaselineData{}
body, err := io.ReadAll(objReader.Body)
if err != nil {
log.Errorf("failed to read object data %s: %v", objectKey, err)
continue
}

bd.SetRawData(body)
tags, err := bd.GetSetupTags()
if err != nil {
log.Errorf("failed to get tags from setup.api: %w", err)
log.Errorf("failed to deserialize tags/metadata from summary data: %v", err)
}

fmt.Println("Processing summary: ", name)
// openShiftVersion := strings.Split(name, "_")[0]
openShiftVersion := tags["openshift_version"].(string)
platformType := tags["platform_type"].(string)
date := tags["executionDate"].(string)
// platformType := strings.Split(name, "_")[1]
log.Infof("Processing summary object: %s", name)
log.Infof("Processing metadata: %v", tags)
openShiftVersion := strings.Split(name, "_")[0]
if _, ok := tags["openshiftVersionMajor"]; ok {
openShiftVersion = tags["openshiftVersionMajor"].(string)
} else {
log.Warnf("missing openshiftVersionMajor tag in metadata, extracting from name: %v", openShiftVersion)
}

platformType := strings.Split(name, "_")[1]
if _, ok := tags["platformType"]; ok {
platformType = tags["platformType"].(string)
} else {
log.Warnf("missing platformType tag in metadata, extracting from name: %v", platformType)
}

executionDate := strings.Split(name, "_")[2]
if _, ok := tags["executionDate"]; ok {
executionDate = tags["executionDate"].(string)
} else {
log.Warnf("missing executionDate tag in metadata, extracting from name: %v", executionDate)
}

// Creating summary item for baseline result
res := &baselineIndexItem{
Date: date,
Date: executionDate,
Name: strings.Split(name, ".json")[0],
Path: objectKey,
Size: fmt.Sprintf("%d", *obj.Size),
OpenShiftVersion: openShiftVersion,
PlatformType: platformType,
Tags: tags,
}
spew.Dump(res)
// spew.Dump(res)
index.Results = append(index.Results, res)

latestIndexKey := fmt.Sprintf("%s_%s", openShiftVersion, platformType)
existing, ok := index.Latest[latestIndexKey]
if !ok {
Expand All @@ -131,9 +131,7 @@ func (brs *BaselineConfig) CreateBaselineIndex() error {
}
}

// Save the index to the bucket
// upload artifact to bucket
// Convert index to JSON
// Save the new index to the bucket.
indexJSON, err := json.Marshal(index)
if err != nil {
return fmt.Errorf("unable to save index to json: %w", err)
Expand Down Expand Up @@ -167,7 +165,7 @@ func (brs *BaselineConfig) CreateBaselineIndex() error {
},
})
if err != nil {
log.Warnf("failed to create cache invalidation: %w", err)
log.Warnf("failed to create cache invalidation: %v", err)
fmt.Printf(`Index updated. Run the following command to invalidate index.cache:
aws cloudfront create-invalidation \
--distribution-id %s \
Expand All @@ -180,7 +178,7 @@ aws cloudfront create-invalidation \

// ListObjects lists all the objects in the bucket.
func ListObjects(svc *s3.S3, bucketRegion, bucketName, path string) ([]*s3.Object, error) {

Check failure on line 180 in internal/report/baseline/indexer.go

View workflow job for this annotation

GitHub Actions / go-staticcheck

argument svc is overwritten before first use (SA4009)

Check failure on line 180 in internal/report/baseline/indexer.go

View workflow job for this annotation

GitHub Actions / go-lint

SA4009: argument svc is overwritten before first use (staticcheck)
svc, err := createS3Client(bucketRegion)
svc, _, err := createS3Client(bucketRegion)

Check failure on line 181 in internal/report/baseline/indexer.go

View workflow job for this annotation

GitHub Actions / go-staticcheck

assignment to svc

Check failure on line 181 in internal/report/baseline/indexer.go

View workflow job for this annotation

GitHub Actions / go-lint

SA4009(related information): assignment to svc (staticcheck)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 48e1612

Please sign in to comment.