-
Notifications
You must be signed in to change notification settings - Fork 62
/
lock_files.go
136 lines (106 loc) · 2.63 KB
/
lock_files.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package lockfiles
import (
"fmt"
"io/ioutil"
ctlimg "github.com/k14s/imgpkg/pkg/imgpkg/image"
regname "github.com/google/go-containerregistry/pkg/name"
"gopkg.in/yaml.v2"
)
const (
ImagesLockKind string = "ImagesLock"
BundleLockKind string = "BundleLock"
ImagesLockAPIVersion string = "imgpkg.carvel.dev/v1alpha1"
BundleLockAPIVersion string = "imgpkg.carvel.dev/v1alpha1"
BundleDir string = ".imgpkg"
ImageLockFile string = "images.yml"
)
type BundleLock struct {
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Spec BundleSpec
}
type BundleSpec struct {
Image ImageLocation
}
type ImageLock struct {
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Spec ImageSpec
}
func (il *ImageLock) UnmarshalYAML(unmarshal func(interface{}) error) error {
// needed to avoid infinite recursion
type imageLockAlias ImageLock
var alias imageLockAlias
err := unmarshal(&alias)
if err != nil {
return err
}
for _, image := range alias.Spec.Images {
if _, err := regname.NewDigest(image.Image); err != nil {
return fmt.Errorf("Expected ref to be in digest form, got %s", image.Image)
}
}
*il = ImageLock(alias)
return nil
}
type ImageSpec struct {
Images []ImageDesc
}
type ImageDesc struct {
Image string
Annotations map[string]string
}
type ImageLocation struct {
DigestRef string `yaml:"url,omitempty"`
OriginalTag string `yaml:"tag,omitempty"`
}
type Lock struct {
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
}
func ReadLockFile(path string) (Lock, error) {
var lock Lock
err := readPathInto(path, &lock)
return lock, err
}
func ReadBundleLockFile(path string) (BundleLock, error) {
var bundleLock BundleLock
err := readPathInto(path, &bundleLock)
return bundleLock, err
}
func ReadImageLockFile(path string) (ImageLock, error) {
var imgLock ImageLock
err := readPathInto(path, &imgLock)
return imgLock, err
}
func readPathInto(path string, obj interface{}) error {
bs, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return yaml.Unmarshal(bs, obj)
}
func (il *ImageLock) CheckForBundles(reg ctlimg.Registry) ([]string, error) {
var bundles []string
for _, img := range il.Spec.Images {
imgRef := img.Image
parsedRef, err := regname.ParseReference(imgRef)
if err != nil {
return nil, err
}
image, err := reg.Image(parsedRef)
if err != nil {
return nil, err
}
isBundle, err := IsBundle(image)
if err != nil {
return nil, err
}
if isBundle {
bundles = append(bundles, imgRef)
}
}
return bundles, nil
}