Skip to content

Commit

Permalink
Import from openshift-pipelines/tektoncd-catalog
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed Jan 25, 2024
1 parent 0c57360 commit 47ef305
Show file tree
Hide file tree
Showing 10,870 changed files with 3,218,962 additions and 1 deletion.
The diff you're trying to view is too large. We only load the first 3000 changed files.
6 changes: 6 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
32 changes: 32 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on:
pull_request:
branches: [ main ]
push:
branches: [ main ]

permissions:
contents: read

name: build-test

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
- name: Set up Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5
with:
go-version: 1.20.x
- run: go env
- name: go build
run: go build -v ./...
- name: go test
run: go test -v ./...
- name: build catalog-cd
run: go build -o catalog-cd -v .
- name: Upload catalog-cd binary
uses: actions/upload-artifact@1eb3cb2b3e0f29609092a73eb033bb759a334595 # v4.1.0
with:
name: catalog-cd-bin
path: catalog-cd
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

# Go workspace file
go.work

/catalog-cd
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# catalog-cd
The catalog-cd is the tool(kit) to manage a catalog repository as well as helping Tekton resources (Task, Pipeline, …) authors management in external repostiories.

The `catalog-cd` is the tool(kit) to manage a catalog repository as well
as helping Tekton resources (`Task`, `Pipeline`, …) authors management in
external repostiories.

*This is a work in progress and **very early stages***.
318 changes: 318 additions & 0 deletions go.mod

Large diffs are not rendered by default.

1,605 changes: 1,605 additions & 0 deletions go.sum

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions internal/assert/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package assert

import (
"context"
"errors"
"fmt"
"regexp"

"github.com/openshift-pipelines/catalog-cd/internal/config"
"github.com/openshift-pipelines/catalog-cd/internal/probe"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
)

// Assert defines the Assert role, responsible for asserting the final status of the different type
// of resources asserted by this application.
type Assert interface {
// Status asserts the instance status conditions.
Status() error

// Results asserts the instance results against the slice of regular expressions.
Results(_ []regexp.Regexp) error
}

// ErrUnsupportedGV the resource group version informed (subject) is not supported.
var ErrUnsupportedGV = errors.New("unsupported resource group-version")

// NewAssert proxy the Assert instantiation based on the subject resource group-version (GVR).
func NewAssert(ctx context.Context, cfg *config.Config, subject *probe.Subject) (Assert, error) {
switch subject.GVR {
case v1beta1.SchemeGroupVersion.WithResource("taskruns"):
return NewTaskRunAssert(cfg, subject)
case v1beta1.SchemeGroupVersion.WithResource("pipelineruns"):
return NewPipelineRunAssert(ctx, cfg, subject)
default:
return nil, fmt.Errorf("%w: %q (%q)", ErrUnsupportedGV, subject.Fullname, subject.GVR)
}
}
78 changes: 78 additions & 0 deletions internal/assert/pipelinerun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package assert

import (
"context"
"regexp"

"github.com/openshift-pipelines/catalog-cd/internal/config"
"github.com/openshift-pipelines/catalog-cd/internal/probe"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/status"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// PipelineRunAssert asserts the elements of a PipelineRun instance.
type PipelineRunAssert struct {
cfg *config.Config // global configuration
pr *v1beta1.PipelineRun // pipelinerun instance (subject)
taskRunStatus map[string]*v1beta1.PipelineRunTaskRunStatus // taskrun children instances
}

var _ Assert = &PipelineRunAssert{}

// Status asserts the instance status conditions.
func (a *PipelineRunAssert) Status() error {
return allStepsSucceeded(a.pr.Status.GetConditions())
}

// Results asserts the PipelineRun children TaskRun instances.
func (a *PipelineRunAssert) Results(rules []regexp.Regexp) error {
results := []v1beta1.TaskRunResult{}
for _, tr := range a.taskRunStatus {
results = append(results, tr.Status.TaskRunResults...)
}

a.cfg.Infof("### Asserting results:\n")
for _, re := range rules {
a.cfg.Infof("# - regexp: '%v'\n", re.String())
result, err := v1beta1TaskRunResultMatchesRegexp(re, results...)
if err != nil {
a.cfg.Infof("# match: \"\"\n")
return err
}
a.cfg.Infof("# match: %q\n", result)
}
return nil
}

// NewPipelineRunAssert instantiates the PipelineRunAssert with a up-to-date PipelineRun instance and
// the children TaskRun statuses.
func NewPipelineRunAssert(
ctx context.Context,
cfg *config.Config,
subject *probe.Subject,
) (*PipelineRunAssert, error) {
cs := cfg.GetClientsOrPanic().Tekton
ns := subject.Namespace()

// retrieving the latest changes for the informed subject
pr, err := cs.TektonV1beta1().PipelineRuns(ns).Get(ctx, subject.Name(), metav1.GetOptions{})
if err != nil {
return nil, err
}
printStatusConditions(cfg, pr.Status.GetConditions())

// going after the status of the TaskRun children instances
taskRunStatus, _, err := status.GetFullPipelineTaskStatuses(ctx, cs, ns, pr)
if err != nil {
return nil, err
}
printPipelineRunResults(cfg, taskRunStatus)

return &PipelineRunAssert{
cfg: cfg,
pr: pr,
taskRunStatus: taskRunStatus,
}, nil
}
62 changes: 62 additions & 0 deletions internal/assert/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package assert

import (
"github.com/openshift-pipelines/catalog-cd/internal/config"

v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"knative.dev/pkg/apis"
)

// printStatusConditions prints out the status conditions.
func printStatusConditions(cfg *config.Config, conditions apis.Conditions) {
if len(conditions) == 0 {
return
}

cfg.Infof("### Status conditions:\n")
for _, c := range conditions {
cfg.Infof("# - type: %q\n", c.Type)
cfg.Infof("# status: %q\n", c.Status)
cfg.Infof("# reason: %q\n", c.Reason)
if c.Severity != "" {
cfg.Infof("# severity: %q\n", c.Severity)
}
cfg.Infof("# message: %q\n", c.Message)
}
}

// printTaskRunResults prints out the TaskRun results.
func printTaskRunResults(cfg *config.Config, results []v1.TaskRunResult) {
if len(results) == 0 {
return
}

cfg.Infof("### TaskRun results:\n")
for _, r := range results {
cfg.Infof("# - name: %q\n", r.Name)
cfg.Infof("# type: %q\n", r.Type)
cfg.Infof("# value: %q\n", r.Value.StringVal)
}
}

// printPipelineRunResults prints out each TaskRun status.
func printPipelineRunResults(
cfg *config.Config,
taskRunStatus map[string]*v1beta1.PipelineRunTaskRunStatus,
) {
if len(taskRunStatus) == 0 {
return
}

cfg.Infof("### PipelineRun results:\n")
for name, tr := range taskRunStatus {
cfg.Infof("# - taskRunName: %q\n", name)
cfg.Infof("# results:\n")
for _, r := range tr.Status.TaskRunResults {
cfg.Infof("# - name: %q\n", r.Name)
cfg.Infof("# type: %q\n", r.Value.Type)
cfg.Infof("# value: %q\n", r.Value.StringVal)
}
}
}
55 changes: 55 additions & 0 deletions internal/assert/taskrun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package assert

import (
"regexp"

"github.com/openshift-pipelines/catalog-cd/internal/config"
"github.com/openshift-pipelines/catalog-cd/internal/probe"

tkntaskrun "github.com/tektoncd/cli/pkg/taskrun"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
)

// TaskRunAssert asserts a TaskRun instance.
type TaskRunAssert struct {
cfg *config.Config // global configuration
tr *v1.TaskRun // taskrun instance (subject)
}

var _ Assert = &TaskRunAssert{}

// Status assert the TaskRun status conditions.
func (a *TaskRunAssert) Status() error {
return allStepsSucceeded(a.tr.Status.GetConditions())
}

// Results asserts the TaskRun results against the informed regular expressions.
func (a *TaskRunAssert) Results(rules []regexp.Regexp) error {
a.cfg.Infof("### Asserting results:\n")
for _, re := range rules {
a.cfg.Infof("# - regexp: '%v'\n", re.String())
result, err := taskRunResultMatchesRegexp(re, a.tr.Status.Results...)
if err != nil {
a.cfg.Infof("# match: \"\"\n")
return err
}
a.cfg.Infof("# match: %q\n", result)
}
return nil
}

// NewTaskRunAssert instantiate the TaskRunAssert by loading the TaskRun (subject) resource.
func NewTaskRunAssert(cfg *config.Config, subject *probe.Subject) (*TaskRunAssert, error) {
cs := cfg.GetClientsOrPanic()
tr, err := tkntaskrun.GetTaskRun(subject.GVR, cs, subject.Name(), subject.Namespace())
if err != nil {
return nil, err
}
printStatusConditions(cfg, tr.Status.GetConditions())
printTaskRunResults(cfg, tr.Status.Results)

return &TaskRunAssert{
cfg: cfg,
tr: tr,
}, nil
}
56 changes: 56 additions & 0 deletions internal/assert/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package assert

import (
"errors"
"fmt"
"regexp"

v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
)

var (
// ErrUnmatchedRegexp regular expression doesn't match any results.
ErrUnmatchedRegexp = errors.New("unmatched regexp")

// ErrStepFailed indicates one or more steps (status condition) failed.
ErrStepFailed = errors.New("failed step")
)

// v1beta1TaskRunResultMatchesRegexp asserts one of the informed results matches the regexp.
func v1beta1TaskRunResultMatchesRegexp(
re regexp.Regexp,
results ...v1beta1.TaskRunResult,
) (string, error) {
for _, result := range results {
kv := fmt.Sprintf("%s=%s", result.Name, result.Value.StringVal)
if re.MatchString(kv) {
return kv, nil
}
}
return "", fmt.Errorf("%w: '%v'", ErrUnmatchedRegexp, re.String())
}

// taskRunResultMatchesRegexp asserts one of the informed results matches the regexp.
func taskRunResultMatchesRegexp(re regexp.Regexp, results ...v1.TaskRunResult) (string, error) {
for _, result := range results {
kv := fmt.Sprintf("%s=%s", result.Name, result.Value.StringVal)
if re.MatchString(kv) {
return kv, nil
}
}
return "", fmt.Errorf("%w: '%v'", ErrUnmatchedRegexp, re.String())
}

// allStepsSucceeded asserts all the status conditions have succeeded.
func allStepsSucceeded(conditions apis.Conditions) error {
for _, condition := range conditions {
if condition.Type != apis.ConditionSucceeded || condition.Status != corev1.ConditionTrue {
continue
}
return nil
}
return fmt.Errorf("%w: %d conditions inspected", ErrStepFailed, len(conditions))
}
Loading

0 comments on commit 47ef305

Please sign in to comment.