Skip to content

Commit 2a476d4

Browse files
committed
Move ParseImageReferences to cri util
Avoids importing image service for utility function. Signed-off-by: Derek McGowan <[email protected]>
1 parent 3baf5ed commit 2a476d4

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

pkg/cri/server/container_status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"fmt"
2323

2424
"github.com/containerd/containerd/v2/errdefs"
25-
"github.com/containerd/containerd/v2/pkg/cri/server/images"
2625
containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container"
26+
"github.com/containerd/containerd/v2/pkg/cri/util"
2727

2828
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
2929
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
@@ -49,7 +49,7 @@ func (c *criService) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
4949
return nil, fmt.Errorf("failed to get image %q: %w", imageRef, err)
5050
}
5151
} else {
52-
repoTags, repoDigests := images.ParseImageReferences(image.References)
52+
repoTags, repoDigests := util.ParseImageReferences(image.References)
5353
if len(repoTags) > 0 {
5454
// Based on current behavior of dockershim, this field should be
5555
// image tag.

pkg/cri/server/images/image_status.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import (
2525

2626
"github.com/containerd/containerd/v2/errdefs"
2727
imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image"
28+
"github.com/containerd/containerd/v2/pkg/cri/util"
2829
"github.com/containerd/containerd/v2/tracing"
2930
"github.com/containerd/log"
30-
docker "github.com/distribution/reference"
3131

3232
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
3333
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
@@ -65,7 +65,7 @@ func (c *CRIImageService) ImageStatus(ctx context.Context, r *runtime.ImageStatu
6565

6666
// toCRIImage converts internal image object to CRI runtime.Image.
6767
func toCRIImage(image imagestore.Image) *runtime.Image {
68-
repoTags, repoDigests := ParseImageReferences(image.References)
68+
repoTags, repoDigests := util.ParseImageReferences(image.References)
6969
runtimeImage := &runtime.Image{
7070
Id: image.ID,
7171
RepoTags: repoTags,
@@ -101,24 +101,6 @@ func getUserFromImage(user string) (*int64, string) {
101101
return &uid, ""
102102
}
103103

104-
// ParseImageReferences parses a list of arbitrary image references and returns
105-
// the repotags and repodigests
106-
func ParseImageReferences(refs []string) ([]string, []string) {
107-
var tags, digests []string
108-
for _, ref := range refs {
109-
parsed, err := docker.ParseAnyReference(ref)
110-
if err != nil {
111-
continue
112-
}
113-
if _, ok := parsed.(docker.Canonical); ok {
114-
digests = append(digests, parsed.String())
115-
} else if _, ok := parsed.(docker.Tagged); ok {
116-
tags = append(tags, parsed.String())
117-
}
118-
}
119-
return tags, digests
120-
}
121-
122104
// TODO (mikebrow): discuss moving this struct and / or constants for info map for some or all of these fields to CRI
123105
type verboseImageInfo struct {
124106
ChainID string `json:"chainID"`

pkg/cri/server/images/image_status_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2727

2828
imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image"
29+
"github.com/containerd/containerd/v2/pkg/cri/util"
2930
)
3031

3132
func TestImageStatus(t *testing.T) {
@@ -84,7 +85,7 @@ func TestParseImageReferences(t *testing.T) {
8485
"gcr.io/library/busybox:1.2",
8586
}
8687
expectedDigests := []string{"gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582"}
87-
tags, digests := ParseImageReferences(refs)
88+
tags, digests := util.ParseImageReferences(refs)
8889
assert.Equal(t, expectedTags, tags)
8990
assert.Equal(t, expectedDigests, digests)
9091
}

pkg/cri/util/references.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import reference "github.com/distribution/reference"
20+
21+
// ParseImageReferences parses a list of arbitrary image references and returns
22+
// the repotags and repodigests
23+
func ParseImageReferences(refs []string) ([]string, []string) {
24+
var tags, digests []string
25+
for _, ref := range refs {
26+
parsed, err := reference.ParseAnyReference(ref)
27+
if err != nil {
28+
continue
29+
}
30+
if _, ok := parsed.(reference.Canonical); ok {
31+
digests = append(digests, parsed.String())
32+
} else if _, ok := parsed.(reference.Tagged); ok {
33+
tags = append(tags, parsed.String())
34+
}
35+
}
36+
return tags, digests
37+
}

0 commit comments

Comments
 (0)