diff --git a/pkg/cri/server/container_status.go b/pkg/cri/server/container_status.go index 0d5748e00..025522af3 100644 --- a/pkg/cri/server/container_status.go +++ b/pkg/cri/server/container_status.go @@ -22,8 +22,8 @@ import ( "fmt" "github.com/containerd/containerd/v2/errdefs" - "github.com/containerd/containerd/v2/pkg/cri/server/images" containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container" + "github.com/containerd/containerd/v2/pkg/cri/util" runtimespec "github.com/opencontainers/runtime-spec/specs-go" runtime "k8s.io/cri-api/pkg/apis/runtime/v1" @@ -49,7 +49,7 @@ func (c *criService) ContainerStatus(ctx context.Context, r *runtime.ContainerSt return nil, fmt.Errorf("failed to get image %q: %w", imageRef, err) } } else { - repoTags, repoDigests := images.ParseImageReferences(image.References) + repoTags, repoDigests := util.ParseImageReferences(image.References) if len(repoTags) > 0 { // Based on current behavior of dockershim, this field should be // image tag. diff --git a/pkg/cri/server/images/image_status.go b/pkg/cri/server/images/image_status.go index cb3eba329..a54fc08b7 100644 --- a/pkg/cri/server/images/image_status.go +++ b/pkg/cri/server/images/image_status.go @@ -25,9 +25,9 @@ import ( "github.com/containerd/containerd/v2/errdefs" imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image" + "github.com/containerd/containerd/v2/pkg/cri/util" "github.com/containerd/containerd/v2/tracing" "github.com/containerd/log" - docker "github.com/distribution/reference" imagespec "github.com/opencontainers/image-spec/specs-go/v1" runtime "k8s.io/cri-api/pkg/apis/runtime/v1" @@ -65,7 +65,7 @@ func (c *CRIImageService) ImageStatus(ctx context.Context, r *runtime.ImageStatu // toCRIImage converts internal image object to CRI runtime.Image. func toCRIImage(image imagestore.Image) *runtime.Image { - repoTags, repoDigests := ParseImageReferences(image.References) + repoTags, repoDigests := util.ParseImageReferences(image.References) runtimeImage := &runtime.Image{ Id: image.ID, RepoTags: repoTags, @@ -101,24 +101,6 @@ func getUserFromImage(user string) (*int64, string) { return &uid, "" } -// ParseImageReferences parses a list of arbitrary image references and returns -// the repotags and repodigests -func ParseImageReferences(refs []string) ([]string, []string) { - var tags, digests []string - for _, ref := range refs { - parsed, err := docker.ParseAnyReference(ref) - if err != nil { - continue - } - if _, ok := parsed.(docker.Canonical); ok { - digests = append(digests, parsed.String()) - } else if _, ok := parsed.(docker.Tagged); ok { - tags = append(tags, parsed.String()) - } - } - return tags, digests -} - // TODO (mikebrow): discuss moving this struct and / or constants for info map for some or all of these fields to CRI type verboseImageInfo struct { ChainID string `json:"chainID"` diff --git a/pkg/cri/server/images/image_status_test.go b/pkg/cri/server/images/image_status_test.go index d545b255f..cc38b2237 100644 --- a/pkg/cri/server/images/image_status_test.go +++ b/pkg/cri/server/images/image_status_test.go @@ -26,6 +26,7 @@ import ( runtime "k8s.io/cri-api/pkg/apis/runtime/v1" imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image" + "github.com/containerd/containerd/v2/pkg/cri/util" ) func TestImageStatus(t *testing.T) { @@ -84,7 +85,7 @@ func TestParseImageReferences(t *testing.T) { "gcr.io/library/busybox:1.2", } expectedDigests := []string{"gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582"} - tags, digests := ParseImageReferences(refs) + tags, digests := util.ParseImageReferences(refs) assert.Equal(t, expectedTags, tags) assert.Equal(t, expectedDigests, digests) } diff --git a/pkg/cri/util/references.go b/pkg/cri/util/references.go new file mode 100644 index 000000000..4813f89e1 --- /dev/null +++ b/pkg/cri/util/references.go @@ -0,0 +1,37 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package util + +import reference "github.com/distribution/reference" + +// ParseImageReferences parses a list of arbitrary image references and returns +// the repotags and repodigests +func ParseImageReferences(refs []string) ([]string, []string) { + var tags, digests []string + for _, ref := range refs { + parsed, err := reference.ParseAnyReference(ref) + if err != nil { + continue + } + if _, ok := parsed.(reference.Canonical); ok { + digests = append(digests, parsed.String()) + } else if _, ok := parsed.(reference.Tagged); ok { + tags = append(tags, parsed.String()) + } + } + return tags, digests +}