Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 88 additions & 26 deletions tools/codegen/cmd/featuregate-test-analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"net/http"
"net/url"
Expand All @@ -16,7 +17,6 @@ import (
"strings"
"time"

"github.com/russross/blackfriday"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -170,6 +170,7 @@ func (o *FeatureGateTestAnalyzerOptions) Run(ctx context.Context) error {
fmt.Fprintf(o.Out, "No new Default FeatureGates found.\n")
}

featureGateHTMLData := []utils.HTMLFeatureGate{}
recentlyEnabledFeatureGates := sets.KeySet(recentlyEnabledFeatureGatesToClusterProfiles)
for _, enabledFeatureGate := range sets.List(recentlyEnabledFeatureGates) {
clusterProfiles := recentlyEnabledFeatureGatesToClusterProfiles[enabledFeatureGate]
Expand Down Expand Up @@ -197,6 +198,7 @@ func (o *FeatureGateTestAnalyzerOptions) Run(ctx context.Context) error {
fmt.Fprintf(o.Out, "INSUFFICIENT CI testing for %q.\n", enabledFeatureGate)
}
errs = append(errs, currErrs...)
featureGateHTMLData = append(featureGateHTMLData, buildHTMLFeatureGateData(enabledFeatureGate, testingResults, currErrs))

}

Expand All @@ -207,39 +209,99 @@ func (o *FeatureGateTestAnalyzerOptions) Run(ctx context.Context) error {
errs = append(errs, err)
}

htmlContent := blackfriday.Run(summaryMarkdown)
htmlBytes := []byte{}
htmlBytes = append(htmlBytes, []byte(htmlHeader)...)
htmlBytes = append(htmlBytes, htmlContent...)
htmlFilename := filepath.Join(o.OutputDir, "feature-promotion-summary.html")
if err := os.WriteFile(htmlFilename, htmlBytes, 0644); err != nil {
if err := writeHTMLFromTemplate(htmlFilename, featureGateHTMLData); err != nil {
errs = append(errs, err)
}
}

return errors.Join(errs...)
}

const htmlHeader = `<head>
<meta charset="UTF-8"><title>FeatureGate Promotion Summary</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.1/css/bootstrap.min.css" integrity="sha512-T584yQ/tdRR5QwOpfvDfVQUidzfgc2339Lc8uBDtcp/wYu80d7jwBgAxbyMh0a9YM9F8N3tdErpFI8iaGx6x5g==" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-icons/1.5.0/font/bootstrap-icons.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<style>
@media (max-width: 992px) {
.container {
width: 100%;
max-width: none;
}
}
table, th, td {
border: 1px solid;
padding: 10px;
}
</style>
</head>

`
func buildHTMLFeatureGateData(name string, testingResults map[JobVariant]*TestingResults, errs []error) utils.HTMLFeatureGate {
jobVariantsSet := sets.KeySet(testingResults)
jobVariants := jobVariantsSet.UnsortedList()
sort.Sort(OrderedJobVariants(jobVariants))

variants := make([]utils.HTMLVariantColumn, 0, len(jobVariants))
for i, jv := range jobVariants {
variants = append(variants, utils.HTMLVariantColumn{
Topology: jv.Topology,
Cloud: jv.Cloud,
Architecture: jv.Architecture,
NetworkStack: jv.NetworkStack,
ColIndex: i + 1,
})
}

allTests := sets.Set[string]{}
for _, variantTestingResults := range testingResults {
for _, currTestingResult := range variantTestingResults.TestResults {
allTests.Insert(currTestingResult.TestName)
}
}

tests := make([]utils.HTMLTestRow, 0, len(allTests))
for _, testName := range sets.List(allTests) {
row := utils.HTMLTestRow{
TestName: testName,
Cells: make([]utils.HTMLTestCell, len(jobVariants)),
}
for i, jobVariant := range jobVariants {
allTesting := testingResults[jobVariant]
testResults := testResultByName(allTesting.TestResults, testName)
cell := utils.HTMLTestCell{}
if testResults == nil {
cell.Failed = true
} else {
var passPercent float32
if testResults.TotalRuns > 0 {
passPercent = float32(testResults.SuccessfulRuns) / float32(testResults.TotalRuns)
}
cell.PassPercent = int(passPercent * 100)
cell.SuccessfulRuns = testResults.SuccessfulRuns
cell.TotalRuns = testResults.TotalRuns
cell.FailedRuns = testResults.FailedRuns
if testResults.TotalRuns < requiredNumberOfTestRunsPerVariant || passPercent < requiredPassRateOfTestsPerVariant {
cell.Failed = true
}
}
row.Cells[i] = cell
}
tests = append(tests, row)
}

return utils.HTMLFeatureGate{
Name: name,
Sufficient: len(errs) == 0,
Variants: variants,
Tests: tests,
}
}

func writeHTMLFromTemplate(filename string, featureGateHTMLData []utils.HTMLFeatureGate) error {

data := utils.HTMLTemplateData{
FeatureGates: featureGateHTMLData,
}

tmpl, err := template.New("report").Parse(utils.HTMLTemplateSrc)
if err != nil {
return fmt.Errorf("error parsing HTML template: %w", err)
}

f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating HTML file: %w", err)
}
defer f.Close()

if err := tmpl.Execute(f, data); err != nil {
return fmt.Errorf("error executing HTML template: %w", err)
}

return nil
}

func checkIfTestingIsSufficient(featureGate string, testingResults map[JobVariant]*TestingResults) []error {
errs := []error{}
Expand Down
166 changes: 166 additions & 0 deletions tools/codegen/pkg/utils/html.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package utils

type HTMLTemplateData struct {
FeatureGates []HTMLFeatureGate
}

type HTMLFeatureGate struct {
Name string
Sufficient bool
Variants []HTMLVariantColumn
Tests []HTMLTestRow
}

type HTMLVariantColumn struct {
Topology string
Cloud string
Architecture string
NetworkStack string
ColIndex int
}

type HTMLTestRow struct {
TestName string
Cells []HTMLTestCell
}

type HTMLTestCell struct {
PassPercent int
SuccessfulRuns int
TotalRuns int
FailedRuns int
Failed bool
}

const HTMLTemplateSrc = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>FeatureGate Promotion Summary</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.1/css/bootstrap.min.css"
integrity="sha512-T584yQ/tdRR5QwOpfvDfVQUidzfgc2339Lc8uBDtcp/wYu80d7jwBgAxbyMh0a9YM9F8N3tdErpFI8iaGx6x5g=="
crossorigin="anonymous">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<style>
body { padding: 20px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
.sortable { cursor: pointer; user-select: none; }
.sortable:hover { background-color: #e9ecef; }
.sort-indicator::after { content: ' \2195'; opacity: 0.3; font-size: 0.8em; }
.sort-asc::after { content: ' \2191'; opacity: 1; font-size: 0.8em; }
.sort-desc::after { content: ' \2193'; opacity: 1; font-size: 0.8em; }
.fail-cell { background-color: #f8d7da; }
.pass-cell { background-color: #d4edda; }
.test-name { max-width: 500px; word-wrap: break-word; font-size: 0.85em; text-align: left; }
table { width: 100%; border-collapse: collapse; margin-bottom: 2em; }
th, td { border: 1px solid #dee2e6; padding: 8px; text-align: center; vertical-align: middle; }
th:first-child, td:first-child { text-align: left; }
th { background-color: #f8f9fa; }
.network-stack { font-weight: bold; color: #0056b3; }
.alert { padding: 12px 20px; margin-bottom: 20px; border-radius: 4px; }
.alert-success { background-color: #d4edda; border: 1px solid #c3e6cb; color: #155724; }
.alert-danger { background-color: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; }
h1 { margin-bottom: 24px; }
h2 { margin-top: 32px; margin-bottom: 12px; }
</style>
</head>
<body>
<div class="container-fluid">
<h1>FeatureGate Promotion Summary</h1>
{{if not .FeatureGates}}<p>No new Default FeatureGates found.</p>{{end}}
{{range $fgIdx, $fg := .FeatureGates}}
<h2>{{$fg.Name}}</h2>
{{if $fg.Sufficient}}
<div class="alert alert-success">Sufficient CI testing for &quot;{{$fg.Name}}&quot;.</div>
{{else}}
<div class="alert alert-danger">
<strong>INSUFFICIENT</strong> CI testing for &quot;{{$fg.Name}}&quot;.
<ul>
<li>At least five tests are expected for a feature</li>
<li>Tests must be run on every TechPreview platform (ask for an exception if your feature doesn&#39;t support a variant)</li>
<li>All tests must run at least 14 times on every platform</li>
<li>All tests must pass at least 95% of the time</li>
</ul>
</div>
{{end}}
{{if $fg.Tests}}
<table id="table-{{$fgIdx}}">
<thead>
<tr>
<th class="sortable sort-indicator" data-table="{{$fgIdx}}" data-col="0" data-sort="text">Test Name</th>
{{range $v := $fg.Variants}}
<th class="sortable sort-indicator" data-table="{{$fgIdx}}" data-col="{{$v.ColIndex}}" data-sort="percent">
{{$v.Topology}}<br>{{$v.Cloud}}<br>{{$v.Architecture}}{{if $v.NetworkStack}}<br><span class="network-stack">{{$v.NetworkStack}}</span>{{end}}
</th>
{{end}}
</tr>
</thead>
<tbody>
{{range $test := $fg.Tests}}
<tr>
<td class="test-name">{{$test.TestName}}</td>
{{range $cell := $test.Cells}}
<td class="{{if $cell.Failed}}fail-cell{{else}}pass-cell{{end}}" data-pass-percent="{{$cell.PassPercent}}">
{{if $cell.Failed}}<strong>FAIL</strong><br>{{end}}
{{$cell.PassPercent}}% ({{$cell.SuccessfulRuns}} / {{$cell.TotalRuns}})
{{if gt $cell.FailedRuns 0}}<br><small>{{$cell.FailedRuns}} failed</small>{{end}}
</td>
{{end}}
</tr>
{{end}}
</tbody>
</table>
{{end}}
{{end}}
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('th.sortable').forEach(function(th) {
th.addEventListener('click', function() {
sortTable(this.dataset.table, parseInt(this.dataset.col), this.dataset.sort, this);
});
});
});

var sortStates = {};

function sortTable(tableIdx, colIdx, sortType, header) {
var table = document.getElementById('table-' + tableIdx);
if (!table) return;
var tbody = table.querySelector('tbody');
var rows = Array.from(tbody.querySelectorAll('tr'));
var headers = table.querySelectorAll('th');

var key = tableIdx + '-' + colIdx;
if (sortStates[key] === 'asc') {
sortStates[key] = 'desc';
} else {
sortStates[key] = 'asc';
}
var ascending = sortStates[key] === 'asc';

headers.forEach(function(h) {
h.classList.remove('sort-asc', 'sort-desc');
h.classList.add('sort-indicator');
});
header.classList.remove('sort-indicator');
header.classList.add(ascending ? 'sort-asc' : 'sort-desc');

rows.sort(function(a, b) {
var valA, valB;
if (sortType === 'text') {
valA = a.cells[colIdx].textContent.trim().toLowerCase();
valB = b.cells[colIdx].textContent.trim().toLowerCase();
return ascending ? valA.localeCompare(valB) : valB.localeCompare(valA);
} else {
valA = parseInt(a.cells[colIdx].getAttribute('data-pass-percent') || '0', 10);
valB = parseInt(b.cells[colIdx].getAttribute('data-pass-percent') || '0', 10);
return ascending ? valA - valB : valB - valA;
}
});

rows.forEach(function(row) { tbody.appendChild(row); });
}
</script>
</body>
</html>
`