...
1 package pallet
2
3 import (
4 "fmt"
5
6 wh "edge-infra.dev/pkg/f8n/warehouse"
7 "edge-infra.dev/pkg/f8n/warehouse/oci"
8 )
9
10 var (
11
12 errInvalidMetadata = fmt.Errorf("invalid pallet metadata")
13 )
14
15
16 const (
17 KnnotationSource = "pallet.edge.ncr.com/source"
18 KnnotationCreated = "pallet.edge.ncr.com/created"
19 KnnotationTeam = "pallet.edge.ncr.com/team"
20 KnnotationName = "pallet.edge.ncr.com/name"
21 KnnotationVersion = "pallet.edge.ncr.com/version"
22 KnnotationVendor = "pallet.edge.ncr.com/vendor"
23 KnnotationRevision = "pallet.edge.ncr.com/revision"
24 )
25
26
27 type Metadata struct {
28
29 Name string
30 Team string
31 Vendor string
32 BuildInfo
33
34
35 Description string
36 Readme string
37 }
38
39
40
41
42 type BuildInfo struct {
43 Source string
44 Version string
45 Revision string
46 Created string
47 }
48
49 func (b *BuildInfo) Validate() error {
50 if b.Source == "" {
51 return fmt.Errorf("%w: source must be provided", errInvalidMetadata)
52 }
53 if b.Revision == "" {
54 return fmt.Errorf("%w: revision must be provided", errInvalidMetadata)
55 }
56 if b.Version == "" {
57 return fmt.Errorf("%w: version must be provided", errInvalidMetadata)
58 }
59 if b.Created == "" {
60 return fmt.Errorf("%w: created must be provided", errInvalidMetadata)
61 }
62 return nil
63 }
64
65 func (m *Metadata) Validate() error {
66 if m.Name == "" {
67 return fmt.Errorf("%w: name must be provided", errInvalidMetadata)
68 }
69 if m.Team == "" {
70 return fmt.Errorf("%w: team must be provided", errInvalidMetadata)
71 }
72
73 return m.BuildInfo.Validate()
74 }
75
76
77
78
79
80 func (m Metadata) OCIAnnotations() map[string]string {
81
82 if m.Vendor == "" {
83 m.Vendor = "NCR"
84 }
85 r := map[string]string{
86 wh.AnnotationSource: m.Source,
87 wh.AnnotationVersion: m.Version,
88 wh.AnnotationCreated: m.Created,
89 wh.AnnotationRevision: m.Revision,
90 wh.AnnotationVendor: m.Vendor,
91 wh.AnnotationTitle: m.Name,
92
93
94 wh.AnnotationName: m.Name,
95 wh.AnnotationTeam: m.Team,
96 }
97
98 if m.Description != "" {
99 r[wh.AnnotationDescription] = m.Description
100 }
101 if m.Readme != "" {
102 r[wh.AnnotationDocumentation] = m.Readme
103 }
104 return r
105 }
106
107 func (m Metadata) K8sAnnotations() map[string]string {
108 r := map[string]string{
109 KnnotationTeam: m.Team,
110 KnnotationName: m.Name,
111 KnnotationVersion: m.Version,
112 KnnotationRevision: m.Revision,
113 }
114 if m.Created != "" {
115 r[KnnotationCreated] = m.Created
116 }
117 if m.Source != "" {
118 r[KnnotationSource] = m.Source
119 }
120 return r
121 }
122
123 var requiredMetaAnnos = []string{
124 wh.AnnotationTeam,
125 wh.AnnotationName,
126 wh.AnnotationRevision,
127 wh.AnnotationSource,
128 wh.AnnotationVersion,
129 wh.AnnotationCreated,
130 wh.AnnotationVendor,
131 }
132
133
134
135
136 func metadataFromAnnotations(aa map[string]string) (Metadata, error) {
137 if _, ok := aa[wh.AnnotationVendor]; !ok {
138 aa[wh.AnnotationVendor] = "NCR"
139 }
140
141 for _, a := range requiredMetaAnnos {
142 if _, ok := aa[a]; !ok {
143 return Metadata{}, fmt.Errorf(
144 "%w: invalid pallet metadata: required annotation %s not found",
145 oci.ErrInvalidArtifact,
146 a,
147 )
148 }
149 }
150
151 meta := Metadata{
152 Name: aa[wh.AnnotationName],
153 Team: aa[wh.AnnotationTeam],
154 BuildInfo: BuildInfo{
155 Version: aa[wh.AnnotationVersion],
156 Source: aa[wh.AnnotationSource],
157 Created: aa[wh.AnnotationCreated],
158 Revision: aa[wh.AnnotationRevision],
159 },
160 Vendor: aa[wh.AnnotationVendor],
161 }
162
163 for _, a := range aa {
164 switch a {
165 case wh.AnnotationDocumentation:
166 meta.Readme = aa[wh.AnnotationDocumentation]
167 case wh.AnnotationDescription:
168 meta.Description = aa[wh.AnnotationDescription]
169 }
170 }
171
172 return meta, nil
173 }
174
View as plain text