-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making ratchet compatible with tekton by adding tekton parser
Added a tekton parser and a check to see if current image uses a tag, if so, keep that tag and and the updated sha ref Signed-off-by: Arif Ashogbon <146236570+arifash01@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package parser | ||
|
||
import ( | ||
"fmt" | ||
|
||
// Using banydonk/yaml instead of the default yaml pkg because the default | ||
// pkg incorrectly escapes unicode. https://github.com/go-yaml/yaml/issues/737 | ||
"github.com/braydonk/yaml" | ||
"github.com/sethvargo/ratchet/resolver" | ||
) | ||
|
||
type Tekton struct{} | ||
|
||
// Parse pulls the Tekton Ci refs from the documents. | ||
func (d *Tekton) Parse(nodes []*yaml.Node) (*RefsList, error) { | ||
var refs RefsList | ||
for i, node := range nodes { | ||
if err := d.parseOne(&refs, node); err != nil { | ||
return nil, fmt.Errorf("failed to parse node %d: %w", i, err) | ||
} | ||
} | ||
|
||
return &refs, nil | ||
} | ||
|
||
func (d *Tekton) parseOne(refs *RefsList, node *yaml.Node) error { | ||
if node == nil { | ||
return nil | ||
} | ||
|
||
if node.Kind != yaml.DocumentNode { | ||
return fmt.Errorf("expected document node, got %v", node.Kind) | ||
} | ||
|
||
for _, docMap := range node.Content { | ||
if docMap.Kind != yaml.MappingNode { | ||
continue | ||
} | ||
|
||
// Confirm it's a tekton file then proceed to look for image keyword | ||
for _, stepsMap := range docMap.Content { | ||
if stepsMap.Value == "apiVersion" { | ||
d.findSpecs(refs, docMap) | ||
break | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
func (d *Tekton) findSpecs(refs *RefsList, node *yaml.Node) error { | ||
for i, specsMap := range node.Content { | ||
if specsMap.Value == "spec" { | ||
specs := node.Content[i+1] | ||
d.findImages(refs, specs) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (d *Tekton) findImages(refs *RefsList, node *yaml.Node) error { | ||
for i, property := range node.Content { | ||
if property.Value == "image" { | ||
image := node.Content[i+1] | ||
ref := resolver.NormalizeContainerRef(image.Value) | ||
refs.Add(ref, image) | ||
break | ||
} else { | ||
d.findImages(refs, property) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package parser | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/braydonk/yaml" | ||
) | ||
|
||
func TestTekton_Parse(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
name string | ||
in string | ||
exp []string | ||
}{ | ||
{ | ||
name: "mostly_empty_file", | ||
in: ` | ||
jobs: | ||
`, | ||
exp: []string{}, | ||
}, | ||
{ | ||
name: "steps", | ||
in: ` | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: Task | ||
metadata: | ||
name: testData | ||
spec: | ||
params: | ||
- name: username | ||
type: string | ||
steps: | ||
- name: git | ||
image: alpine/git | ||
`, | ||
exp: []string{ | ||
"container://alpine/git", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
m := helperStringToYAML(t, tc.in) | ||
|
||
refs, err := new(Tekton).Parse([]*yaml.Node{m}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if got, want := refs.Refs(), tc.exp; !reflect.DeepEqual(got, want) { | ||
t.Errorf("expected %q to be %q", got, want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: Task | ||
metadata: | ||
name: testData | ||
spec: | ||
params: | ||
- name: username | ||
type: string | ||
steps: | ||
- name: build | ||
image: golang:1.12 |