1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-28 18:38:40 +00:00

bug: making images consistent with image (#9147)

* adding `ReferenceWithTag` and `GetReferenceWithTag` + Populating them

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

* Adding tests for the same

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

* `ReferenceWithTag()` -> `ReferenceWithTag`

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

* `Strings()` -> `ReferenceWithTag`

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

* added `ReferenceWithTag` to image_test

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

* sorting out linter

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

---------

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>
Co-authored-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
This commit is contained in:
Shubham Singh 2023-12-22 03:15:50 -08:00 committed by GitHub
parent b54e6230c5
commit 6aaa06702f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 159 additions and 81 deletions

View file

@ -292,8 +292,8 @@ func (ctx *context) AddElement(data interface{}, index, nesting int) error {
func (ctx *context) AddImageInfo(info apiutils.ImageInfo, cfg config.Configuration) error {
data := map[string]interface{}{
"reference": info.String(),
"referenceWithTag": info.ReferenceWithTag(),
"reference": info.Reference,
"referenceWithTag": info.ReferenceWithTag,
"registry": info.Registry,
"path": info.Path,
"name": info.Name,

View file

@ -24,10 +24,12 @@ func Test_extractImageInfo(t *testing.T) {
"initContainers": {
"init": {
imageutils.ImageInfo{
Registry: "index.docker.io",
Name: "busybox",
Path: "busybox",
Tag: "v1.2.3",
Registry: "index.docker.io",
Name: "busybox",
Path: "busybox",
Tag: "v1.2.3",
Reference: "index.docker.io/busybox:v1.2.3",
ReferenceWithTag: "index.docker.io/busybox:v1.2.3",
},
"/spec/initContainers/0/image",
},
@ -35,10 +37,12 @@ func Test_extractImageInfo(t *testing.T) {
"containers": {
"nginx": {
imageutils.ImageInfo{
Registry: "docker.io",
Name: "nginx",
Path: "nginx",
Tag: "latest",
Registry: "docker.io",
Name: "nginx",
Path: "nginx",
Tag: "latest",
Reference: "docker.io/nginx:latest",
ReferenceWithTag: "docker.io/nginx:latest",
},
"/spec/containers/0/image",
},
@ -46,10 +50,12 @@ func Test_extractImageInfo(t *testing.T) {
"ephemeralContainers": {
"ephemeral": {
imageutils.ImageInfo{
Registry: "docker.io",
Name: "nginx",
Path: "test/nginx",
Tag: "latest",
Registry: "docker.io",
Name: "nginx",
Path: "test/nginx",
Tag: "latest",
Reference: "docker.io/test/nginx:latest",
ReferenceWithTag: "docker.io/test/nginx:latest",
},
"/spec/ephemeralContainers/0/image",
},
@ -62,10 +68,12 @@ func Test_extractImageInfo(t *testing.T) {
"containers": {
"nginx": {
imageutils.ImageInfo{
Registry: "docker.io",
Name: "nginx",
Path: "test/nginx",
Tag: "latest",
Registry: "docker.io",
Name: "nginx",
Path: "test/nginx",
Tag: "latest",
Reference: "docker.io/test/nginx:latest",
ReferenceWithTag: "docker.io/test/nginx:latest",
},
"/spec/containers/0/image",
},
@ -78,11 +86,13 @@ func Test_extractImageInfo(t *testing.T) {
"initContainers": {
"init": {
imageutils.ImageInfo{
Registry: "fictional.registry.example:10443",
Name: "imagename",
Path: "imagename",
Tag: "tag",
Digest: "sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
Registry: "fictional.registry.example:10443",
Name: "imagename",
Path: "imagename",
Tag: "tag",
Digest: "sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
Reference: "fictional.registry.example:10443/imagename@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
ReferenceWithTag: "fictional.registry.example:10443/imagename:tag",
},
"/spec/template/spec/initContainers/0/image",
},
@ -90,10 +100,12 @@ func Test_extractImageInfo(t *testing.T) {
"containers": {
"myapp": {
imageutils.ImageInfo{
Registry: "fictional.registry.example:10443",
Name: "imagename",
Path: "imagename",
Tag: "latest",
Registry: "fictional.registry.example:10443",
Name: "imagename",
Path: "imagename",
Tag: "latest",
Reference: "fictional.registry.example:10443/imagename:latest",
ReferenceWithTag: "fictional.registry.example:10443/imagename:latest",
},
"/spec/template/spec/containers/0/image",
},
@ -101,11 +113,13 @@ func Test_extractImageInfo(t *testing.T) {
"ephemeralContainers": {
"ephemeral": {
imageutils.ImageInfo{
Registry: "fictional.registry.example:10443",
Name: "imagename",
Path: "imagename",
Tag: "tag",
Digest: "sha256:eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
Registry: "fictional.registry.example:10443",
Name: "imagename",
Path: "imagename",
Tag: "tag",
Digest: "sha256:eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
Reference: "fictional.registry.example:10443/imagename@sha256:eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
ReferenceWithTag: "fictional.registry.example:10443/imagename:tag",
},
"/spec/template/spec/ephemeralContainers/0/image",
},
@ -118,10 +132,12 @@ func Test_extractImageInfo(t *testing.T) {
"containers": {
"hello": {
imageutils.ImageInfo{
Registry: "test.example.com",
Name: "my-app",
Path: "test/my-app",
Tag: "v2",
Registry: "test.example.com",
Name: "my-app",
Path: "test/my-app",
Tag: "v2",
Reference: "test.example.com/test/my-app:v2",
ReferenceWithTag: "test.example.com/test/my-app:v2",
},
"/spec/jobTemplate/spec/template/spec/containers/0/image",
},
@ -139,28 +155,34 @@ func Test_extractImageInfo(t *testing.T) {
"custom": {
"/spec/steps/0/image": {
imageutils.ImageInfo{
Registry: "docker.io",
Name: "ubuntu",
Path: "ubuntu",
Tag: "latest",
Registry: "docker.io",
Name: "ubuntu",
Path: "ubuntu",
Tag: "latest",
Reference: "docker.io/ubuntu:latest",
ReferenceWithTag: "docker.io/ubuntu:latest",
},
"/spec/steps/0/image",
},
"/spec/steps/1/image": {
imageutils.ImageInfo{
Registry: "gcr.io",
Name: "build-example",
Path: "example-builders/build-example",
Tag: "latest",
Registry: "gcr.io",
Name: "build-example",
Path: "example-builders/build-example",
Tag: "latest",
Reference: "gcr.io/example-builders/build-example:latest",
ReferenceWithTag: "gcr.io/example-builders/build-example:latest",
},
"/spec/steps/1/image",
},
"/spec/steps/2/image": {
imageutils.ImageInfo{
Registry: "gcr.io",
Name: "push-example",
Path: "example-builders/push-example",
Tag: "latest",
Registry: "gcr.io",
Name: "push-example",
Path: "example-builders/push-example",
Tag: "latest",
Reference: "gcr.io/example-builders/push-example:latest",
ReferenceWithTag: "gcr.io/example-builders/push-example:latest",
},
"/spec/steps/2/image",
},
@ -177,19 +199,23 @@ func Test_extractImageInfo(t *testing.T) {
"steps": {
"dockerfile-pushexample": {
imageutils.ImageInfo{
Registry: "gcr.io",
Name: "push-example",
Path: "example-builders/push-example",
Tag: "latest",
Registry: "gcr.io",
Name: "push-example",
Path: "example-builders/push-example",
Tag: "latest",
Reference: "gcr.io/example-builders/push-example:latest",
ReferenceWithTag: "gcr.io/example-builders/push-example:latest",
},
"/spec/steps/1/image",
},
"ubuntu-example": {
imageutils.ImageInfo{
Registry: "docker.io",
Name: "ubuntu",
Path: "ubuntu",
Tag: "latest",
Registry: "docker.io",
Name: "ubuntu",
Path: "ubuntu",
Tag: "latest",
Reference: "docker.io/ubuntu:latest",
ReferenceWithTag: "docker.io/ubuntu:latest",
},
"/spec/steps/0/image",
},
@ -207,10 +233,12 @@ func Test_extractImageInfo(t *testing.T) {
"steps": {
"echo": {
imageutils.ImageInfo{
Registry: "docker.io",
Name: "alpine",
Path: "alpine",
Tag: "latest",
Registry: "docker.io",
Name: "alpine",
Path: "alpine",
Tag: "latest",
Reference: "docker.io/alpine:latest",
ReferenceWithTag: "docker.io/alpine:latest",
},
"/spec/steps/0/image",
},
@ -228,10 +256,12 @@ func Test_extractImageInfo(t *testing.T) {
"custom": {
"/spec/source/registry/url": {
imageutils.ImageInfo{
Registry: "docker.io",
Name: "fedora-cloud-registry-disk-demo",
Path: "kubevirt/fedora-cloud-registry-disk-demo",
Tag: "latest",
Registry: "docker.io",
Name: "fedora-cloud-registry-disk-demo",
Path: "kubevirt/fedora-cloud-registry-disk-demo",
Tag: "latest",
Reference: "docker.io/kubevirt/fedora-cloud-registry-disk-demo:latest",
ReferenceWithTag: "docker.io/kubevirt/fedora-cloud-registry-disk-demo:latest",
},
"/spec/source/registry/url",
},

View file

@ -23,6 +23,12 @@ type ImageInfo struct {
// Digest is the image digest portion e.g. `sha256:128c6e3534b842a2eec139999b8ce8aa9a2af9907e2b9269550809d18cd832a3`
Digest string `json:"digest,omitempty"`
// Reference is an optional readable reference for the image e.g. `docker.io/nginx`
Reference string `json:"reference,omitempty"`
// ReferenceWithTag is an optional readable reference for the image along with the image tag e.g. `docker.io/nginx:v10`
ReferenceWithTag string `json:"referenceWithTag,omitempty"`
}
func (i *ImageInfo) String() string {
@ -39,14 +45,6 @@ func (i *ImageInfo) String() string {
}
}
func (i *ImageInfo) ReferenceWithTag() string {
if i.Registry != "" {
return fmt.Sprintf("%s/%s:%s", i.Registry, i.Path, i.Tag)
} else {
return fmt.Sprintf("%s:%s", i.Path, i.Tag)
}
}
func GetImageInfo(image string, cfg config.Configuration) (*ImageInfo, error) {
// adding the default domain in order to properly parse image info
fullImageName := addDefaultRegistry(image, cfg)
@ -55,7 +53,7 @@ func GetImageInfo(image string, cfg config.Configuration) (*ImageInfo, error) {
return nil, fmt.Errorf("bad image: %s, defaultRegistry: %s, enableDefaultRegistryMutation: %t: %w", fullImageName, config.Configuration.GetDefaultRegistry(cfg), config.Configuration.GetEnableDefaultRegistryMutation(cfg), err)
}
var registry, path, name, tag, digest string
var registry, path, name, tag, digest, referenceWithTag string
if named, ok := ref.(reference.Named); ok {
registry = reference.Domain(named)
path = reference.Path(named)
@ -77,13 +75,23 @@ func GetImageInfo(image string, cfg config.Configuration) (*ImageInfo, error) {
registry = ""
}
return &ImageInfo{
Registry: registry,
Name: name,
Path: path,
Tag: tag,
Digest: digest,
}, nil
if registry != "" {
referenceWithTag = fmt.Sprintf("%s/%s:%s", registry, path, tag)
} else {
referenceWithTag = fmt.Sprintf("%s:%s", path, tag)
}
imageInfo := &ImageInfo{
Registry: registry,
Name: name,
Path: path,
Tag: tag,
Digest: digest,
ReferenceWithTag: referenceWithTag,
}
imageInfo.Reference = imageInfo.String()
return imageInfo, nil
}
// addDefaultRegistry always adds default registry

View file

@ -142,7 +142,47 @@ func Test_ReferenceWithTag(t *testing.T) {
for _, test := range testCases {
imageInfo, err := GetImageInfo(test.input, cfg)
assert.NoError(t, err)
assert.Equal(t, test.expected, imageInfo.ReferenceWithTag())
assert.Equal(t, test.expected, imageInfo.ReferenceWithTag)
}
}
func Test_ReferenceAndReferenceWithTag(t *testing.T) {
testCases := []struct {
input string
expectedReference string
expectedReferenceWithTag string
}{{
input: "nginx",
expectedReference: "docker.io/nginx:latest",
expectedReferenceWithTag: "docker.io/nginx:latest",
}, {
input: "nginx:v10.3",
expectedReference: "docker.io/nginx:v10.3",
expectedReferenceWithTag: "docker.io/nginx:v10.3",
}, {
input: "docker.io/test/nginx:v10.3",
expectedReference: "docker.io/test/nginx:v10.3",
expectedReferenceWithTag: "docker.io/test/nginx:v10.3",
}, {
input: "test/nginx",
expectedReference: "docker.io/test/nginx:latest",
expectedReferenceWithTag: "docker.io/test/nginx:latest",
}, {
input: "localhost:4443/test/nginx",
expectedReference: "localhost:4443/test/nginx:latest",
expectedReferenceWithTag: "localhost:4443/test/nginx:latest",
}, {
input: "docker.io/test/centos@sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f",
expectedReference: "docker.io/test/centos@sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f",
expectedReferenceWithTag: "docker.io/test/centos:",
}}
cfg, err := initializeMockConfig("docker.io", true)
assert.NoError(t, err)
for _, test := range testCases {
imageInfo, err := GetImageInfo(test.input, cfg)
assert.NoError(t, err)
assert.Equal(t, test.expectedReference, imageInfo.Reference)
assert.Equal(t, test.expectedReferenceWithTag, imageInfo.ReferenceWithTag)
}
}