Move ParseImageReferences to cri util
Avoids importing image service for utility function. Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
parent
3baf5edb8b
commit
2a476d4214
@ -22,8 +22,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/containerd/containerd/v2/errdefs"
|
"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"
|
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"
|
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
|
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)
|
return nil, fmt.Errorf("failed to get image %q: %w", imageRef, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
repoTags, repoDigests := images.ParseImageReferences(image.References)
|
repoTags, repoDigests := util.ParseImageReferences(image.References)
|
||||||
if len(repoTags) > 0 {
|
if len(repoTags) > 0 {
|
||||||
// Based on current behavior of dockershim, this field should be
|
// Based on current behavior of dockershim, this field should be
|
||||||
// image tag.
|
// image tag.
|
||||||
|
@ -25,9 +25,9 @@ import (
|
|||||||
|
|
||||||
"github.com/containerd/containerd/v2/errdefs"
|
"github.com/containerd/containerd/v2/errdefs"
|
||||||
imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image"
|
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/containerd/v2/tracing"
|
||||||
"github.com/containerd/log"
|
"github.com/containerd/log"
|
||||||
docker "github.com/distribution/reference"
|
|
||||||
|
|
||||||
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
runtime "k8s.io/cri-api/pkg/apis/runtime/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.
|
// toCRIImage converts internal image object to CRI runtime.Image.
|
||||||
func toCRIImage(image imagestore.Image) *runtime.Image {
|
func toCRIImage(image imagestore.Image) *runtime.Image {
|
||||||
repoTags, repoDigests := ParseImageReferences(image.References)
|
repoTags, repoDigests := util.ParseImageReferences(image.References)
|
||||||
runtimeImage := &runtime.Image{
|
runtimeImage := &runtime.Image{
|
||||||
Id: image.ID,
|
Id: image.ID,
|
||||||
RepoTags: repoTags,
|
RepoTags: repoTags,
|
||||||
@ -101,24 +101,6 @@ func getUserFromImage(user string) (*int64, string) {
|
|||||||
return &uid, ""
|
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
|
// TODO (mikebrow): discuss moving this struct and / or constants for info map for some or all of these fields to CRI
|
||||||
type verboseImageInfo struct {
|
type verboseImageInfo struct {
|
||||||
ChainID string `json:"chainID"`
|
ChainID string `json:"chainID"`
|
||||||
|
@ -26,6 +26,7 @@ import (
|
|||||||
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
|
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
|
||||||
|
|
||||||
imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image"
|
imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image"
|
||||||
|
"github.com/containerd/containerd/v2/pkg/cri/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestImageStatus(t *testing.T) {
|
func TestImageStatus(t *testing.T) {
|
||||||
@ -84,7 +85,7 @@ func TestParseImageReferences(t *testing.T) {
|
|||||||
"gcr.io/library/busybox:1.2",
|
"gcr.io/library/busybox:1.2",
|
||||||
}
|
}
|
||||||
expectedDigests := []string{"gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582"}
|
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, expectedTags, tags)
|
||||||
assert.Equal(t, expectedDigests, digests)
|
assert.Equal(t, expectedDigests, digests)
|
||||||
}
|
}
|
||||||
|
37
pkg/cri/util/references.go
Normal file
37
pkg/cri/util/references.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user