Skip to content

Commit f7047ef

Browse files
author
Marko Mikulicic
committed
Implement digest field in ociImageRef lens
1 parent de02be6 commit f7047ef

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

man/knot8.1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,11 @@ Sometimes there is no lens that works with your actual field format.
436436
When the relevant parts of the field format can be expressed with a regular expression you can
437437
use the "regexp" lens, where the format is expressed in-line in the field definition itself.
438438
.Pp
439-
For example, the ociImageRef lens currently doesn't support the digest field; as an alternative we can use
440-
the regexp lens to select the digest body.
439+
For example, you can locate a docker image name inside of some configuration file (e.g. a jsonnet file)
440+
and then use the ociImageRef lens to further parse the image reference.
441441
.
442442
.Bd -literal -offset indent
443-
field.knot8.io/workerImageDigest: /spec/template/spec/containers/~{"name":"runner"}/image/~(regexp)/@sha256:([a-f0-9]*)/1
443+
field.knot8.io/workerImageDigest: "/data/worker-ubuntu16-04.jsonnet/~(regexp)/{ name: 'container-image', value: 'docker:~1~1([^']*)/1/~(ociImageRef)/digest"
444444
.Ed
445445
.
446446
. When the ociImageRef finally implements the digest field we can rewrite this field definition while maintaining backward compatibility.

pkg/lensed/oci.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (OCIImageRef) Apply(src []byte, vals []Setter) ([]byte, error) {
3131
return nil, fmt.Errorf("unexpected path len. got: %d, max: %d", l, m)
3232
}
3333

34-
r, err := regexp.Compile("^([^:]*)(:(.*))?$")
34+
r, err := regexp.Compile("^([^:@]*)(:([^@]*))?(@sha256:([a-f0-9]*))?$")
3535
if err != nil {
3636
return nil, err
3737
}
@@ -43,10 +43,11 @@ func (OCIImageRef) Apply(src []byte, vals []Setter) ([]byte, error) {
4343
comp = 1
4444
case "tag":
4545
comp = 3
46+
case "digest":
47+
comp = 5
4648
default:
4749
return nil, fmt.Errorf("unknown ociImageRef field %q", p)
4850
}
49-
5051
start, end := indices[2*comp+0], indices[2*comp+1]
5152

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

6364
if start == -1 {
6465
start, end = indices[1], indices[1]
65-
newval = append([]byte(":"), newval...)
66+
var sep string
67+
switch p := path[0]; p {
68+
case "tag":
69+
sep = ":"
70+
case "digest":
71+
sep = "@sha256:"
72+
default:
73+
return nil, fmt.Errorf("unknown ociImageRef field %q", p)
74+
}
75+
newval = append([]byte(sep), newval...)
6676
}
6777

6878
ops = append(ops, splice.Span(start, end).With(string(newval)))

pkg/lensed/oci_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ func TestOCIImageRef(t *testing.T) {
3535
},
3636
"image: foo/bar:baz",
3737
},
38+
{
39+
"image: foo/bar@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf",
40+
[]Mapping{
41+
{"/image/~(ociImageRef)/digest", "7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc"},
42+
},
43+
"image: foo/bar@sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc",
44+
},
45+
{
46+
"image: foo/bar:1.0@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf",
47+
[]Mapping{
48+
{"/image/~(ociImageRef)/digest", "7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc"},
49+
},
50+
"image: foo/bar:1.0@sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc",
51+
},
52+
{
53+
"image: foo/bar",
54+
[]Mapping{
55+
{"/image/~(ociImageRef)/digest", "7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc"},
56+
},
57+
"image: foo/bar@sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc",
58+
},
3859
}
3960

4061
for i, tc := range testCases {

pkg/lensed/regexp_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ func TestRegexp(t *testing.T) {
4646
},
4747
out,
4848
},
49+
{
50+
"foo:YmFy",
51+
[]Mapping{
52+
{"~(regexp)/foo:(.*)/1/~(base64)", "baz"},
53+
},
54+
"foo:YmF6",
55+
},
4956
}
5057

5158
for i, tc := range testCases {

0 commit comments

Comments
 (0)