Skip to content

Commit

Permalink
Implement digest field in ociImageRef lens
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Mikulicic committed Jul 27, 2020
1 parent de02be6 commit f7047ef
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
6 changes: 3 additions & 3 deletions man/knot8.1
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,11 @@ Sometimes there is no lens that works with your actual field format.
When the relevant parts of the field format can be expressed with a regular expression you can
use the "regexp" lens, where the format is expressed in-line in the field definition itself.
.Pp
For example, the ociImageRef lens currently doesn't support the digest field; as an alternative we can use
the regexp lens to select the digest body.
For example, you can locate a docker image name inside of some configuration file (e.g. a jsonnet file)
and then use the ociImageRef lens to further parse the image reference.
.
.Bd -literal -offset indent
field.knot8.io/workerImageDigest: /spec/template/spec/containers/~{"name":"runner"}/image/~(regexp)/@sha256:([a-f0-9]*)/1
field.knot8.io/workerImageDigest: "/data/worker-ubuntu16-04.jsonnet/~(regexp)/{ name: 'container-image', value: 'docker:~1~1([^']*)/1/~(ociImageRef)/digest"
.Ed
.
. When the ociImageRef finally implements the digest field we can rewrite this field definition while maintaining backward compatibility.
Expand Down
16 changes: 13 additions & 3 deletions pkg/lensed/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (OCIImageRef) Apply(src []byte, vals []Setter) ([]byte, error) {
return nil, fmt.Errorf("unexpected path len. got: %d, max: %d", l, m)
}

r, err := regexp.Compile("^([^:]*)(:(.*))?$")
r, err := regexp.Compile("^([^:@]*)(:([^@]*))?(@sha256:([a-f0-9]*))?$")
if err != nil {
return nil, err
}
Expand All @@ -43,10 +43,11 @@ func (OCIImageRef) Apply(src []byte, vals []Setter) ([]byte, error) {
comp = 1
case "tag":
comp = 3
case "digest":
comp = 5
default:
return nil, fmt.Errorf("unknown ociImageRef field %q", p)
}

start, end := indices[2*comp+0], indices[2*comp+1]

var oldval []byte
Expand All @@ -62,7 +63,16 @@ func (OCIImageRef) Apply(src []byte, vals []Setter) ([]byte, error) {

if start == -1 {
start, end = indices[1], indices[1]
newval = append([]byte(":"), newval...)
var sep string
switch p := path[0]; p {
case "tag":
sep = ":"
case "digest":
sep = "@sha256:"
default:
return nil, fmt.Errorf("unknown ociImageRef field %q", p)
}
newval = append([]byte(sep), newval...)
}

ops = append(ops, splice.Span(start, end).With(string(newval)))
Expand Down
21 changes: 21 additions & 0 deletions pkg/lensed/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ func TestOCIImageRef(t *testing.T) {
},
"image: foo/bar:baz",
},
{
"image: foo/bar@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf",
[]Mapping{
{"/image/~(ociImageRef)/digest", "7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc"},
},
"image: foo/bar@sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc",
},
{
"image: foo/bar:1.0@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf",
[]Mapping{
{"/image/~(ociImageRef)/digest", "7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc"},
},
"image: foo/bar:1.0@sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc",
},
{
"image: foo/bar",
[]Mapping{
{"/image/~(ociImageRef)/digest", "7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc"},
},
"image: foo/bar@sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc",
},
}

for i, tc := range testCases {
Expand Down
7 changes: 7 additions & 0 deletions pkg/lensed/regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func TestRegexp(t *testing.T) {
},
out,
},
{
"foo:YmFy",
[]Mapping{
{"~(regexp)/foo:(.*)/1/~(base64)", "baz"},
},
"foo:YmF6",
},
}

for i, tc := range testCases {
Expand Down

0 comments on commit f7047ef

Please sign in to comment.