-
Notifications
You must be signed in to change notification settings - Fork 89
/
images.go
98 lines (85 loc) · 3.23 KB
/
images.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package base
import (
"fmt"
imagedocker "github.com/containers/image/v5/docker"
dockerref "github.com/containers/image/v5/docker/reference"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/docker/registry"
"github.com/replicatedhq/kots/pkg/image"
kotsimage "github.com/replicatedhq/kots/pkg/image"
"github.com/replicatedhq/kots/pkg/k8sdoc"
kustomizeimage "sigs.k8s.io/kustomize/api/types"
)
type FindPrivateImagesOptions struct {
BaseDir string
AppSlug string
ReplicatedRegistry registry.RegistryOptions
DockerHubRegistry registry.RegistryOptions
Installation *kotsv1beta1.Installation
AllImagesPrivate bool
UseHelmInstall map[string]bool
}
type FindPrivateImagesResult struct {
Images []kustomizeimage.Image // images to be rewritten
Docs []k8sdoc.K8sDoc // docs that have rewritten images
CheckedImages []kotsv1beta1.InstallationImage // all images found in the installation
}
func FindPrivateImages(options FindPrivateImagesOptions) (*FindPrivateImagesResult, error) {
checkedImages := makeImageInfoMap(options.Installation.Spec.KnownImages)
upstreamImages, objects, err := image.GetPrivateImages(options.BaseDir, checkedImages, options.AllImagesPrivate, options.DockerHubRegistry, options.UseHelmInstall)
if err != nil {
return nil, errors.Wrap(err, "failed to list upstream images")
}
kustomizeImages := make([]kustomizeimage.Image, 0)
for _, upstreamImage := range upstreamImages {
// ParseReference requires the // prefix
ref, err := imagedocker.ParseReference(fmt.Sprintf("//%s", upstreamImage))
if err != nil {
return nil, errors.Wrapf(err, "failed to parse image ref %q", upstreamImage)
}
dockerRef := ref.DockerReference()
registryHost := dockerref.Domain(dockerRef)
if registryHost == options.ReplicatedRegistry.Endpoint {
// replicated images are also private, but we don't rewrite those
continue
}
image := kustomizeimage.Image{
Name: dockerRef.Name(),
NewName: registry.MakeProxiedImageURL(options.ReplicatedRegistry.ProxyEndpoint, options.AppSlug, upstreamImage),
}
if tagged, ok := dockerRef.(reference.Tagged); ok {
image.NewTag = tagged.Tag()
} else if can, ok := dockerRef.(reference.Canonical); ok {
image.NewTag = can.Digest().String()
} else {
image.NewTag = "latest"
}
kustomizeImages = append(kustomizeImages, kotsimage.BuildImageAltNames(image)...)
}
return &FindPrivateImagesResult{
Images: kustomizeImages,
Docs: objects,
CheckedImages: makeInstallationImages(checkedImages),
}, nil
}
func makeImageInfoMap(images []kotsv1beta1.InstallationImage) map[string]image.ImageInfo {
result := make(map[string]image.ImageInfo)
for _, i := range images {
result[i.Image] = image.ImageInfo{
IsPrivate: i.IsPrivate,
}
}
return result
}
func makeInstallationImages(images map[string]image.ImageInfo) []kotsv1beta1.InstallationImage {
result := make([]kotsv1beta1.InstallationImage, 0)
for image, info := range images {
result = append(result, kotsv1beta1.InstallationImage{
Image: image,
IsPrivate: info.IsPrivate,
})
}
return result
}