-
Notifications
You must be signed in to change notification settings - Fork 80
/
type_image.go
123 lines (108 loc) · 3.75 KB
/
type_image.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
package v1alpha2
import (
"encoding/json"
"errors"
"fmt"
)
// ImageType defines the content type for mirrored images
type ImageType int
const (
TypeInvalid ImageType = iota
TypeOCPRelease
TypeOCPReleaseContent
TypeCincinnatiGraph
TypeOperatorCatalog
TypeOperatorBundle
TypeOperatorRelatedImage
TypeGeneric
)
// ImageTypeString defines the string
// respresentation of every ImageType.
var imageTypeStrings = map[ImageType]string{
TypeOCPReleaseContent: "ocpReleaseContent",
TypeCincinnatiGraph: "cincinnatiGraph",
TypeOCPRelease: "ocpRelease",
TypeOperatorCatalog: "operatorCatalog",
TypeOperatorBundle: "operatorBundle",
TypeOperatorRelatedImage: "operatorRelatedImage",
TypeGeneric: "generic",
}
var imageStringsType = map[string]ImageType{
"ocpReleaseContent": TypeOCPReleaseContent,
"cincinnatiGraph": TypeCincinnatiGraph,
"ocpRelease": TypeOCPRelease,
"operatorCatalog": TypeOperatorCatalog,
"operatorBundle": TypeOperatorBundle,
"operatorRelatedImage": TypeOperatorRelatedImage,
"generic": TypeGeneric,
}
// String returns the string representation
// of an Image Type
func (it ImageType) String() string {
return imageTypeStrings[it]
}
// MarshalJSON marshals the ImageType as a quoted json string
func (it ImageType) MarshalJSON() ([]byte, error) {
if err := it.validate(); err != nil {
return nil, err
}
return json.Marshal(it.String())
}
// UnmarshalJSON unmarshals a quoted json string to the ImageType
func (it *ImageType) UnmarshalJSON(b []byte) error {
var j string
if err := json.Unmarshal(b, &j); err != nil {
return err
}
*it = imageStringsType[j]
return nil
}
func (it ImageType) validate() error {
if _, found := imageTypeStrings[it]; found {
return nil
}
switch it {
case TypeInvalid:
// TypeInvalid is the default value for the concrete type, which means the field was not set.
return errors.New("must set image type")
default:
return fmt.Errorf("unknown image type %v", it)
}
}
// Association between an image and its children, either image layers or child manifests.
type Association struct {
// Name of the image.
Name string `json:"name"`
// Path to image in new location (archive or registry)
Path string `json:"path"`
// ID of the image. Joining this value with "manifests" and Path
// will produce a path to the image's manifest.
ID string `json:"id"`
// TagSymlink of the blob specified by ID.
// This value must be a filename on disk in the "blobs" dir
TagSymlink string `json:"tagSymlink"`
// Type of the image in the context of this tool.
// See the ImageType enum for options.
Type ImageType `json:"type"`
// ManifestDigests of images if the image is a docker manifest list or OCI index.
// These manifests refer to image manifests by content SHA256 digest.
// LayerDigests and Manifests are mutually exclusive.
ManifestDigests []string `json:"manifestDigests,omitempty"`
// LayerDigests of a single manifest if the image is not a docker manifest list
// or OCI index. These digests refer to image layer blobs by content SHA256 digest.
// LayerDigests and Manifests are mutually exclusive.
LayerDigests []string `json:"layerDigests,omitempty"`
}
// Validate checks that the Association fields are set as expected
func (a Association) Validate() error {
if len(a.ManifestDigests) != 0 && len(a.LayerDigests) != 0 {
return fmt.Errorf("image %q: child descriptors cannot contain both manifests and image layers", a.Name)
}
if len(a.ManifestDigests) == 0 && len(a.LayerDigests) == 0 {
return fmt.Errorf("image %q: child descriptors must contain at least one manifest or image layer", a.Name)
}
if a.ID == "" && a.TagSymlink == "" {
return fmt.Errorf("image %q: tag or ID must be set", a.Name)
}
return a.Type.validate()
}