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
14 changes: 9 additions & 5 deletions cmd/relacs/konflux/list_conforma_runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,19 @@ var listConformaRunsCmd = &cobra.Command{
Short: "List the conforma pipeline runs triggered by a given Git tag",
Long: `List the conforma pipeline run results for a specific Git tag.

Only conforma runs triggered directly on the tag's commit (via
pipelines-as-code) are considered; the conforma run for the custom
snapshot is not included.`,
RunE: common.RunCommand(runListConformaRuns),
By default (scenario acs-conforma-dev), only conforma runs triggered
directly on the tag's commit (via pipelines-as-code) are considered.
With --scenario acs-conforma-prod-like (only supported for the
stackrox repository), the conforma runs attached to the tag's custom
snapshot are listed instead.`,
RunE: common.RunCommand(runListConformaRuns),
}

func init() {
listConformaRunsCmd.Flags().String("org", config.GitHubOrganization, "GitHub organization (optional)")
listConformaRunsCmd.Flags().String("repo", config.MainGitHubRepository, "GitHub repository (optional)")
listConformaRunsCmd.Flags().String("tag", "", "Git tag (required)")
listConformaRunsCmd.Flags().String("scenario", konflux.ConformaScenarioDev, "Conforma scenario: "+konflux.ConformaScenarioDev+" or "+konflux.ConformaScenarioProdLike+" (the latter is only supported for the stackrox repository)")

//nolint:errcheck
listConformaRunsCmd.MarkFlagRequired("tag")
Expand All @@ -80,8 +83,9 @@ func runListConformaRuns(cmd *cobra.Command, args []string, c *client.Client) (a
org, _ := cmd.Flags().GetString("org")
repo, _ := cmd.Flags().GetString("repo")
tag, _ := cmd.Flags().GetString("tag")
scenario, _ := cmd.Flags().GetString("scenario")

entries, err := konflux.ListConformaRuns(ctx, c, org, repo, tag)
entries, err := konflux.ListConformaRuns(ctx, c, org, repo, tag, scenario)
if err != nil {
return nil, fmt.Errorf("failed to list conforma runs: %w", err)
}
Expand Down
14 changes: 9 additions & 5 deletions cmd/relacs/konflux/list_conforma_violations.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ var listConformaViolationsCmd = &cobra.Command{
Short: "List the Conforma violations for a given Git tag",
Long: `Retrieve the Conforma violations reported by the conforma pipeline runs for a given Git tag.

Only conforma runs triggered directly on the tag's commit (via
pipelines-as-code) are considered; the conforma run for the custom
snapshot is not included.`,
RunE: common.RunCommand(runListConformaViolations),
By default (scenario acs-conforma-dev), only conforma runs triggered
directly on the tag's commit (via pipelines-as-code) are considered.
With --scenario acs-conforma-prod-like (only supported for the
stackrox repository), the violations of the conforma runs attached
to the tag's custom snapshot are listed instead.`,
RunE: common.RunCommand(runListConformaViolations),
}

func init() {
listConformaViolationsCmd.Flags().String("org", config.GitHubOrganization, "GitHub organization (optional)")
listConformaViolationsCmd.Flags().String("repo", config.MainGitHubRepository, "GitHub repository (optional)")
listConformaViolationsCmd.Flags().String("tag", "", "Git tag (required)")
listConformaViolationsCmd.Flags().String("scenario", konflux.ConformaScenarioDev, "Conforma scenario: "+konflux.ConformaScenarioDev+" or "+konflux.ConformaScenarioProdLike+" (the latter is only supported for the stackrox repository)")

//nolint:errcheck
listConformaViolationsCmd.MarkFlagRequired("tag")
Expand All @@ -72,8 +75,9 @@ func runListConformaViolations(cmd *cobra.Command, args []string, c *client.Clie
org, _ := cmd.Flags().GetString("org")
repo, _ := cmd.Flags().GetString("repo")
tag, _ := cmd.Flags().GetString("tag")
scenario, _ := cmd.Flags().GetString("scenario")

result, err := konflux.ListConformaViolations(ctx, c, org, repo, tag)
result, err := konflux.ListConformaViolations(ctx, c, org, repo, tag, scenario)
if err != nil {
return nil, fmt.Errorf("failed to list conforma violations: %w", err)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ const (
KonfluxTenant = "rh-acs-tenant"
KonfluxApplicationPrefix = "acs"
KonfluxHost = "konflux-ui.apps.stone-prd-rh01.pg1f.p1.openshiftapps.com"
KubearchiveAPIURL = "https://kubearchive-api-server-product-kubearchive.apps.stone-prd-rh01.pg1f.p1.openshiftapps.com"
ImageTagSuffix = "fast"
KubearchiveAPIURL = "https://kubearchive-api-server-product-kubearchive.apps.stone-prd-rh01.pg1f.p1.openshiftapps.com"
ConformaDevScenarioPrefix = "acs-conforma-dev"
ConformaProdLikeScenarioName = "acs-conforma-prod-like"
ImageTagSuffix = "fast"
IssueSource = "redhat.atlassian.net"
JiraBaseURL = "https://redhat.atlassian.net"
JiraProjectKey = "ROX"
Expand Down
17 changes: 12 additions & 5 deletions pkg/konflux/conforma.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,22 @@ type ConformaViolations struct {
}

// ListConformaViolations returns the violations reported by the conforma
// pipeline runs for the given tag. Runs that are still in progress are listed
// in InProgressRuns, and runs whose report could not be retrieved (e.g. they
// failed before the report step ran) in SkippedRuns.
func ListConformaViolations(ctx context.Context, c *client.Client, org, repo, tag string) (ConformaViolations, error) {
pipelineRuns, err := getConformaPipelineRuns(ctx, c, org, repo, tag)
// pipeline runs for the given tag and scenario. Runs that are still in
// progress are listed in InProgressRuns, and runs whose report could not be
// retrieved (e.g. they failed before the report step ran) in SkippedRuns.
func ListConformaViolations(ctx context.Context, c *client.Client, org, repo, tag, scenario string) (ConformaViolations, error) {
pipelineRuns, err := conformaPipelineRunsForScenario(ctx, c, org, repo, tag, scenario)
if err != nil {
return ConformaViolations{}, fmt.Errorf("failed to get conforma pipeline runs: %w", err)
}

return violationsFromPipelineRuns(ctx, c, pipelineRuns, tag)
}

// violationsFromPipelineRuns lists the violations reported by the given
// conforma pipeline runs, reading the report of the most recent run per
// component and scenario.
func violationsFromPipelineRuns(ctx context.Context, c *client.Client, pipelineRuns []kflx.PipelineRun, tag string) (ConformaViolations, error) {
conformaRuns := latestPipelineRunPerComponentAndScenario(pipelineRuns)
if len(conformaRuns) == 0 {
return ConformaViolations{}, fmt.Errorf("no conforma pipeline runs found for tag %s", tag)
Expand Down
55 changes: 53 additions & 2 deletions pkg/konflux/conforma_runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ import (

"github.com/stackrox/relacs/pkg/client"
kflx "github.com/stackrox/relacs/pkg/client/konflux"
"github.com/stackrox/relacs/pkg/config"
"github.com/stackrox/relacs/pkg/github"
"github.com/stackrox/relacs/pkg/utils"
)

// Conforma scenarios selectable via the --scenario flag.
const (
ConformaScenarioDev = config.ConformaDevScenarioPrefix
ConformaScenarioProdLike = config.ConformaProdLikeScenarioName
)

type ConformaRunEntry struct {
Name string `json:"name" yaml:"name"`
Component string `json:"component" yaml:"component"`
Expand All @@ -20,8 +27,8 @@ type ConformaRunEntry struct {
URL string `json:"url" yaml:"url"`
}

func ListConformaRuns(ctx context.Context, c *client.Client, org, repo, tag string) ([]ConformaRunEntry, error) {
pipelineRuns, err := getConformaPipelineRuns(ctx, c, org, repo, tag)
func ListConformaRuns(ctx context.Context, c *client.Client, org, repo, tag, scenario string) ([]ConformaRunEntry, error) {
pipelineRuns, err := conformaPipelineRunsForScenario(ctx, c, org, repo, tag, scenario)
if err != nil {
return nil, err
}
Expand All @@ -38,6 +45,50 @@ func ListConformaRuns(ctx context.Context, c *client.Client, org, repo, tag stri
return entries, nil
}

// conformaPipelineRunsForScenario returns the conforma pipeline runs for the
// given tag and scenario. The dev scenario discovers runs triggered by
// pipelines-as-code on the tag's commit; the prod-like scenario returns the
// runs attached to the tag's custom snapshot and is only supported for the
// main repository.
func conformaPipelineRunsForScenario(ctx context.Context, c *client.Client, org, repo, tag, scenario string) ([]kflx.PipelineRun, error) {
switch scenario {
case ConformaScenarioDev:
return getConformaPipelineRuns(ctx, c, org, repo, tag)
case ConformaScenarioProdLike:
if repo != config.MainGitHubRepository {
return nil, fmt.Errorf("the %s scenario is only supported for the %s repository", ConformaScenarioProdLike, config.MainGitHubRepository)
}
return getProdLikeConformaRuns(ctx, c, org, repo, tag)
default:
return nil, fmt.Errorf("unknown conforma scenario %q, must be %s or %s", scenario, ConformaScenarioDev, ConformaScenarioProdLike)
}
}

// getProdLikeConformaRuns returns the acs-conforma-prod-like pipeline runs
// attached to the custom snapshot for the given tag.
func getProdLikeConformaRuns(ctx context.Context, c *client.Client, org, repo, tag string) ([]kflx.PipelineRun, error) {
snapshot, err := GetCustomSnapshot(ctx, c, config.KonfluxApplicationPrefix, org, repo, tag)
if err != nil {
return nil, fmt.Errorf("failed to get custom snapshot: %w", err)
}

application := snapshot.Metadata.Labels["appstudio.openshift.io/application"]
scenarioName, err := utils.Application{Name: application}.GetResourceWithVersion(config.ConformaProdLikeScenarioName)
if err != nil {
return nil, fmt.Errorf("failed to get conforma scenario name: %w", err)
}

labels := []string{
"appstudio.openshift.io/snapshot=" + snapshot.Metadata.Name,
"test.appstudio.openshift.io/scenario=" + scenarioName,
}
pipelineRuns, err := c.GetKonflux().GetPipelineRuns(ctx, labels)
if err != nil {
return nil, fmt.Errorf("failed to get pipeline runs: %w", err)
}
return pipelineRuns, nil
}

func getConformaPipelineRuns(ctx context.Context, c *client.Client, org, repo, tag string) ([]kflx.PipelineRun, error) {
commitObj, err := github.GetCommitForTag(ctx, c, org, repo, tag)
if err != nil {
Expand Down
122 changes: 121 additions & 1 deletion tests/konflux/conforma_violations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,127 @@ func (s *ConformaViolationsTestSuite) mockConformaReportUnavailable(runName stri
func (s *ConformaViolationsTestSuite) listConformaViolations() (relacskonflux.ConformaViolations, error) {
testClient, err := helpers.SetupTestClient()
s.Require().NoError(err)
return relacskonflux.ListConformaViolations(s.ctx, testClient, config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag)
return relacskonflux.ListConformaViolations(s.ctx, testClient, config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag, relacskonflux.ConformaScenarioDev)
}

// mockProdLikeCustomSnapshot mocks the custom snapshot for the test tag and
// returns its name. The snapshot's application label is versioned, from which
// the versioned prod-like scenario name is derived.
func (s *ConformaViolationsTestSuite) mockProdLikeCustomSnapshot() string {
helpers.MockLightweightTag(config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag, conformaTestSHA)

labels := strings.Join([]string{
"pac.test.appstudio.openshift.io/url-org=" + config.GitHubOrganization,
"pac.test.appstudio.openshift.io/url-repository=" + config.MainGitHubRepository,
"pac.test.appstudio.openshift.io/sha=" + conformaTestSHA,
"acs-style-snapshot=true",
"acs-product-version=" + conformaTestTag,
"appstudio.openshift.io/application=acs-4-10",
}, ",")

helpers.MockK8sSnapshots(labels, map[string]interface{}{
"items": []map[string]interface{}{
{
"metadata": map[string]interface{}{
"name": "acs-4-10-custom-snapshot",
"uid": "uid-snapshot",
"creationTimestamp": "2025-01-01T00:00:00Z",
"labels": map[string]interface{}{
"appstudio.openshift.io/application": "acs-4-10",
},
},
},
},
})
helpers.MockQueryResourcesEmpty(config.KubearchiveAPIURL, "appstudio.redhat.com", "v1alpha1", "snapshots", config.KonfluxTenant)
return "acs-4-10-custom-snapshot"
}

// mockProdLikeConformaRuns mocks the prod-like conforma pipeline runs attached
// to the given custom snapshot.
func (s *ConformaViolationsTestSuite) mockProdLikeConformaRuns(snapshotName string, runs ...map[string]interface{}) {
labels := strings.Join([]string{
"appstudio.openshift.io/snapshot=" + snapshotName,
"test.appstudio.openshift.io/scenario=acs-conforma-prod-like-4-10",
}, ",")

helpers.MockK8sPipelineRuns(labels, map[string]interface{}{
"items": runs,
})
helpers.MockQueryResourcesEmpty(config.KubearchiveAPIURL, "tekton.dev", "v1", "pipelineruns", config.KonfluxTenant)
}

func (s *ConformaViolationsTestSuite) prodLikeConformaPipelineRun(name, uid, component string, conditions []map[string]interface{}) map[string]interface{} {
return map[string]interface{}{
"metadata": map[string]interface{}{
"name": name,
"uid": uid,
"creationTimestamp": "2025-01-01T00:00:00Z",
"labels": map[string]interface{}{
"appstudio.openshift.io/component": component,
"test.appstudio.openshift.io/scenario": "acs-conforma-prod-like-4-10",
},
},
"status": map[string]interface{}{
"conditions": conditions,
},
}
}

func (s *ConformaViolationsTestSuite) listConformaViolationsForScenario(repo, scenario string) (relacskonflux.ConformaViolations, error) {
testClient, err := helpers.SetupTestClient()
s.Require().NoError(err)
return relacskonflux.ListConformaViolations(s.ctx, testClient, config.GitHubOrganization, repo, conformaTestTag, scenario)
}

func (s *ConformaViolationsTestSuite) TestListConformaViolations_ProdLike() {
snapshotName := s.mockProdLikeCustomSnapshot()
run := s.prodLikeConformaPipelineRun("acs-conforma-prod-like-4-10-abc12", "uid-prod-like", "main-4-10", s.succeededConditions())
s.mockProdLikeConformaRuns(snapshotName, run)
s.mockConformaReport("acs-conforma-prod-like-4-10-abc12")

result, err := s.listConformaViolationsForScenario(config.MainGitHubRepository, relacskonflux.ConformaScenarioProdLike)

s.Require().NoError(err)
s.Len(result.Violations.Violations, 1)
s.Equal("violation one", result.Violations.Violations[0].Violation)
s.Empty(result.SkippedRuns)
s.Empty(result.InProgressRuns)
s.True(gock.IsDone())
}

func (s *ConformaViolationsTestSuite) TestListConformaViolations_ProdLikeNonMainRepo() {
_, err := s.listConformaViolationsForScenario("collector", relacskonflux.ConformaScenarioProdLike)

s.Require().Error(err)
s.Contains(err.Error(), "only supported for the stackrox repository")
s.True(gock.IsDone())
}

func (s *ConformaViolationsTestSuite) TestListConformaViolations_UnknownScenario() {
_, err := s.listConformaViolationsForScenario(config.MainGitHubRepository, "acs-conforma-other")

s.Require().Error(err)
s.Contains(err.Error(), "unknown conforma scenario")
s.True(gock.IsDone())
}

func (s *ConformaViolationsTestSuite) TestListConformaRuns_ProdLike() {
snapshotName := s.mockProdLikeCustomSnapshot()
run := s.prodLikeConformaPipelineRun("acs-conforma-prod-like-4-10-abc12", "uid-prod-like", "main-4-10", s.succeededConditions())
s.mockProdLikeConformaRuns(snapshotName, run)

testClient, err := helpers.SetupTestClient()
s.Require().NoError(err)
entries, err := relacskonflux.ListConformaRuns(s.ctx, testClient, config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag, relacskonflux.ConformaScenarioProdLike)

s.Require().NoError(err)
s.Require().Len(entries, 1)
s.Equal("acs-conforma-prod-like-4-10-abc12", entries[0].Name)
s.Equal("main-4-10", entries[0].Component)
s.Equal("acs-conforma-prod-like-4-10", entries[0].Scenario)
s.Equal("Succeeded", entries[0].Status)
s.True(gock.IsDone())
}

func (s *ConformaViolationsTestSuite) TestListConformaViolations_InProgress() {
Expand Down
Loading