Skip to content

Commit

Permalink
[TEP-0115] Git-based Versioning Validation
Browse files Browse the repository at this point in the history
Prior to this change, the Catlin validation only supports directory-based catalog path validation (i.e. ./<catalog>/<kind>/<name>/<version>/<name>.yaml). The git-based versioning catalog is proposed in TEP-0115: https://github.com/tektoncd/community/blob/main/teps/0115-tekton-catalog-git-based-versioning.md. with a new file path layout (i.e. ./<catalog>/<kind>/<name>/<name>.yaml).

This commit introduces a new flag `versioning` to the `catlin validate` command, indicating the versioning type of the catalog to validate. The value can be set to either `directory` or `git`. The default value is `directory`.
  • Loading branch information
QuanZhang-William authored and tekton-robot committed Oct 7, 2022
1 parent d69104e commit ee16b75
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: maven-git-versioning
labels:
app.kubernetes.io/version: "0.1"
annotations:
tekton.dev/pipelines.minVersion: "0.12.1"
tekton.dev/tags: build-automation
tekton.dev/categories: Build Tools
tekton.dev/displayName: "maven"
spec:
description: >-
This Task can be used to run a Maven build.
params:
- name: GOALS
type: array
steps:
- name: execute-goals
image: gcr.io/cloud-builders/mvn:3.6.8@sha256:57523fc43394d6d9d2414ee8d1c85ed7a13460cbb268c3cd16d28cfb3859e641
command:
- mvn
args:
- $(params.GOALS)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: npm-git-versioning
labels:
app.kubernetes.io/version: "0.1"
annotations:
tekton.dev/pipelines.minVersion: "0.17.0"
tekton.dev/tags: build-automation
tekton.dev/categories: Build Tools
tekton.dev/displayName: "npm"
spec:
description: >-
This task can be used to run npm goals on a project.
This task can be used to run npm goals on a project
where package.json is present and has some pre-defined
npm scripts.
params:
- name: GOALS
type: array
steps:
- name: execute-goals
image: docker.io/library/node:12-alpine@sha256:12048cdfd75d944df35f3144132d9bdeee78015fbd6df765edad1be46599b110
command:
- npm
args:
- $(params.GOALS)
30 changes: 23 additions & 7 deletions pkg/cmd/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,34 @@ func validResourcePath() cobra.PositionalArgs {
}
}

func validateFlags(cmd *cobra.Command, args []string, versioning string) error {
if versioning != validator.DirectoryBasedVersioning && versioning != validator.GitBasedVersioning {
return fmt.Errorf("invalid versioning: %s, expecting git or directory", versioning)
}

return nil
}

func Command(cli app.CLI) *cobra.Command {
return &cobra.Command{
var versioning string
cmd := &cobra.Command{
Use: "validate",
Aliases: []string{"verify"},
Args: validResourcePath(),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return validateFlags(cmd, args, versioning)
},
RunE: func(cmd *cobra.Command, args []string) error {
return validateResources(cli, args)
return validateResources(cli, args, versioning)
},
}

cmd.PersistentFlags().StringVar(&versioning, "versioning", "directory", "Versioning type of catalog (directory/git)")

return cmd
}

func validateResources(cli app.CLI, args []string) error {
func validateResources(cli app.CLI, args []string, versioning string) error {

out := cli.Stream().Out
for _, filePath := range args {
Expand All @@ -113,15 +129,15 @@ func validateResources(cli app.CLI, args []string) error {
fileWithPath = fileWithPath + "/" + file.Name()
}
fmt.Fprintf(out, "FILE: %s\n", fileWithPath)
err = validate(cli, fileWithPath)
err = validate(cli, fileWithPath, versioning)
if err != nil {
return err
}
}
}
} else if filepath.Ext(filePath) == ".yaml" {
fmt.Fprintf(out, "FILE: %s\n", filePath)
err := validate(cli, filePath)
err := validate(cli, filePath, versioning)
if err != nil {
return err
}
Expand All @@ -130,7 +146,7 @@ func validateResources(cli app.CLI, args []string) error {
return nil
}

func validate(cli app.CLI, path string) error {
func validate(cli app.CLI, path, versioning string) error {

r, err := os.Open(path)
if err != nil {
Expand All @@ -154,7 +170,7 @@ func validate(cli app.CLI, path string) error {

// run validators
validators := []validator.Validator{
validator.NewPathValidator(res, path),
validator.NewPathValidator(res, path, versioning),
validator.NewContentValidator(res, cat),
validator.ForKind(res),
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/cmd/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,30 @@ func TestValidate(t *testing.T) {
wantError: false,
want: "",
},
{
name: "single filepath - with directory versioning flag",
args: []string{"./testdata/task/maven/0.1", "--versioning", "directory"},
wantError: false,
want: "",
},
{
name: "single filepath git versioning",
args: []string{"./testdata/task/maven-git-versioning", "--versioning", "git"},
wantError: false,
want: "",
},
{
name: "multiple filepath",
args: []string{"./testdata/task/maven/0.1", "./testdata/task/npm/0.1"},
wantError: false,
want: "",
},
{
name: "multiple filepath git versioning",
args: []string{"./testdata/task/maven-git-versioning", "./testdata/task/npm-git-versioning", "--versioning", "git"},
wantError: false,
want: "",
},
}

for _, tp := range testParams {
Expand Down Expand Up @@ -72,6 +90,18 @@ func TestValidateError(t *testing.T) {
wantError: true,
want: "Error: ./testdata/task/black/0.1/black.yaml failed validation\n",
},
{
name: "versioning mismatch - git versioning",
args: []string{"./testdata/task/maven/0.1", "--versioning", "git"},
wantError: true,
want: "Error: ./testdata/task/maven/0.1/maven.yaml failed validation\n",
},
{
name: "versioning mismatch - directory versioning",
args: []string{"./testdata/task/maven-git-versioning", "--versioning", "directory"},
wantError: true,
want: "Error: ./testdata/task/maven-git-versioning/maven-git-versioning.yaml failed validation\n",
},
}

for _, tp := range testParams {
Expand Down
25 changes: 20 additions & 5 deletions pkg/validator/file_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ import (
"github.com/tektoncd/catlin/pkg/parser"
)

const (
GitBasedVersioning = "git"
DirectoryBasedVersioning = "directory"
)

type PathValidator struct {
path string
res *parser.Resource
path string
versioning string
res *parser.Resource
}

var _ Validator = (*PathValidator)(nil)

func NewPathValidator(r *parser.Resource, path string) *PathValidator {
return &PathValidator{path: path, res: r}
func NewPathValidator(r *parser.Resource, path, versioning string) *PathValidator {
return &PathValidator{path: path, versioning: versioning, res: r}
}

func (v *PathValidator) Validate() Result {
Expand All @@ -46,7 +52,16 @@ func (v *PathValidator) Validate() Result {
return result
}

expectedPath := filepath.Join(kind, name, version, name+".yaml")
var expectedPath string
switch v.versioning {
case GitBasedVersioning:
expectedPath = filepath.Join(kind, name, name+".yaml")
case DirectoryBasedVersioning:
expectedPath = filepath.Join(kind, name, version, name+".yaml")
default:
result.Error("invalid versioning, expecting git or directory, but got: %s", v.versioning)
return result
}

if !strings.HasSuffix(absPath, expectedPath) {
result.Error("Resource path is invalid; expected path: %s", expectedPath)
Expand Down

0 comments on commit ee16b75

Please sign in to comment.