Add created OCI annotation

Signed-off-by: Andrew Block <andy.block@gmail.com>
pull/12177/head
Andrew Block 2 years ago
parent 03911aeab7
commit c4870d990c
No known key found for this signature in database
GPG Key ID: 02DFE631AEF35EBC

@ -498,6 +498,7 @@ type (
pushOperation struct { pushOperation struct {
provData []byte provData []byte
strictMode bool strictMode bool
test bool
} }
) )
@ -551,7 +552,7 @@ func (c *Client) Push(data []byte, ref string, options ...PushOption) (*PushResu
descriptors = append(descriptors, provDescriptor) descriptors = append(descriptors, provDescriptor)
} }
ociAnnotations := generateOCIAnnotations(meta) ociAnnotations := generateOCIAnnotations(meta, operation.test)
manifestData, manifest, err := content.GenerateManifest(&configDescriptor, ociAnnotations, descriptors...) manifestData, manifest, err := content.GenerateManifest(&configDescriptor, ociAnnotations, descriptors...)
if err != nil { if err != nil {
@ -616,6 +617,13 @@ func PushOptStrictMode(strictMode bool) PushOption {
} }
} }
// PushOptTest returns a function that sets whether test setting on push
func PushOptTest(test bool) PushOption {
return func(operation *pushOperation) {
operation.test = test
}
}
// Tags provides a sorted list all semver compliant tags for a given repository // Tags provides a sorted list all semver compliant tags for a given repository
func (c *Client) Tags(ref string) ([]string, error) { func (c *Client) Tags(ref string) ([]string, error) {
parsedReference, err := registry.ParseReference(ref) parsedReference, err := registry.ParseReference(ref)

@ -23,6 +23,7 @@ import (
"io" "io"
"net/http" "net/http"
"strings" "strings"
"time"
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@ -163,10 +164,10 @@ func NewRegistryClientWithTLS(out io.Writer, certFile, keyFile, caFile string, i
} }
// generateOCIAnnotations will generate OCI annotations to include within the OCI manifest // generateOCIAnnotations will generate OCI annotations to include within the OCI manifest
func generateOCIAnnotations(meta *chart.Metadata) map[string]string { func generateOCIAnnotations(meta *chart.Metadata, test bool) map[string]string {
// Get annotations from Chart attributes // Get annotations from Chart attributes
ociAnnotations := generateChartOCIAnnotations(meta) ociAnnotations := generateChartOCIAnnotations(meta, test)
// Copy Chart annotations // Copy Chart annotations
annotations: annotations:
@ -187,7 +188,7 @@ annotations:
} }
// getChartOCIAnnotations will generate OCI annotations from the provided chart // getChartOCIAnnotations will generate OCI annotations from the provided chart
func generateChartOCIAnnotations(meta *chart.Metadata) map[string]string { func generateChartOCIAnnotations(meta *chart.Metadata, test bool) map[string]string {
chartOCIAnnotations := map[string]string{} chartOCIAnnotations := map[string]string{}
chartOCIAnnotations = addToMap(chartOCIAnnotations, ocispec.AnnotationDescription, meta.Description) chartOCIAnnotations = addToMap(chartOCIAnnotations, ocispec.AnnotationDescription, meta.Description)
@ -195,6 +196,10 @@ func generateChartOCIAnnotations(meta *chart.Metadata) map[string]string {
chartOCIAnnotations = addToMap(chartOCIAnnotations, ocispec.AnnotationVersion, meta.Version) chartOCIAnnotations = addToMap(chartOCIAnnotations, ocispec.AnnotationVersion, meta.Version)
chartOCIAnnotations = addToMap(chartOCIAnnotations, ocispec.AnnotationURL, meta.Home) chartOCIAnnotations = addToMap(chartOCIAnnotations, ocispec.AnnotationURL, meta.Home)
if !test {
chartOCIAnnotations = addToMap(chartOCIAnnotations, ocispec.AnnotationCreated, time.Now().UTC().Format(time.RFC3339))
}
if len(meta.Sources) > 0 { if len(meta.Sources) > 0 {
chartOCIAnnotations = addToMap(chartOCIAnnotations, ocispec.AnnotationSource, meta.Sources[0]) chartOCIAnnotations = addToMap(chartOCIAnnotations, ocispec.AnnotationSource, meta.Sources[0])
} }

@ -137,7 +137,7 @@ func TestGenerateOCIChartAnnotations(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
result := generateChartOCIAnnotations(tt.chart) result := generateChartOCIAnnotations(tt.chart, true)
if !reflect.DeepEqual(tt.expect, result) { if !reflect.DeepEqual(tt.expect, result) {
t.Errorf("%s: expected map %v, got %v", tt.name, tt.expect, result) t.Errorf("%s: expected map %v, got %v", tt.name, tt.expect, result)
@ -206,7 +206,7 @@ func TestGenerateOCIAnnotations(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
result := generateOCIAnnotations(tt.chart) result := generateOCIAnnotations(tt.chart, true)
if !reflect.DeepEqual(tt.expect, result) { if !reflect.DeepEqual(tt.expect, result) {
t.Errorf("%s: expected map %v, got %v", tt.name, tt.expect, result) t.Errorf("%s: expected map %v, got %v", tt.name, tt.expect, result)

@ -216,7 +216,7 @@ func initCompromisedRegistryTestServer() string {
func testPush(suite *TestSuite) { func testPush(suite *TestSuite) {
// Bad bytes // Bad bytes
ref := fmt.Sprintf("%s/testrepo/testchart:1.2.3", suite.DockerRegistryHost) ref := fmt.Sprintf("%s/testrepo/testchart:1.2.3", suite.DockerRegistryHost)
_, err := suite.RegistryClient.Push([]byte("hello"), ref) _, err := suite.RegistryClient.Push([]byte("hello"), ref, PushOptTest(true))
suite.NotNil(err, "error pushing non-chart bytes") suite.NotNil(err, "error pushing non-chart bytes")
// Load a test chart // Load a test chart
@ -227,20 +227,20 @@ func testPush(suite *TestSuite) {
// non-strict ref (chart name) // non-strict ref (chart name)
ref = fmt.Sprintf("%s/testrepo/boop:%s", suite.DockerRegistryHost, meta.Version) ref = fmt.Sprintf("%s/testrepo/boop:%s", suite.DockerRegistryHost, meta.Version)
_, err = suite.RegistryClient.Push(chartData, ref) _, err = suite.RegistryClient.Push(chartData, ref, PushOptTest(true))
suite.NotNil(err, "error pushing non-strict ref (bad basename)") suite.NotNil(err, "error pushing non-strict ref (bad basename)")
// non-strict ref (chart name), with strict mode disabled // non-strict ref (chart name), with strict mode disabled
_, err = suite.RegistryClient.Push(chartData, ref, PushOptStrictMode(false)) _, err = suite.RegistryClient.Push(chartData, ref, PushOptStrictMode(false), PushOptTest(true))
suite.Nil(err, "no error pushing non-strict ref (bad basename), with strict mode disabled") suite.Nil(err, "no error pushing non-strict ref (bad basename), with strict mode disabled")
// non-strict ref (chart version) // non-strict ref (chart version)
ref = fmt.Sprintf("%s/testrepo/%s:latest", suite.DockerRegistryHost, meta.Name) ref = fmt.Sprintf("%s/testrepo/%s:latest", suite.DockerRegistryHost, meta.Name)
_, err = suite.RegistryClient.Push(chartData, ref) _, err = suite.RegistryClient.Push(chartData, ref, PushOptTest(true))
suite.NotNil(err, "error pushing non-strict ref (bad tag)") suite.NotNil(err, "error pushing non-strict ref (bad tag)")
// non-strict ref (chart version), with strict mode disabled // non-strict ref (chart version), with strict mode disabled
_, err = suite.RegistryClient.Push(chartData, ref, PushOptStrictMode(false)) _, err = suite.RegistryClient.Push(chartData, ref, PushOptStrictMode(false), PushOptTest(true))
suite.Nil(err, "no error pushing non-strict ref (bad tag), with strict mode disabled") suite.Nil(err, "no error pushing non-strict ref (bad tag), with strict mode disabled")
// basic push, good ref // basic push, good ref
@ -249,7 +249,7 @@ func testPush(suite *TestSuite) {
meta, err = extractChartMeta(chartData) meta, err = extractChartMeta(chartData)
suite.Nil(err, "no error extracting chart meta") suite.Nil(err, "no error extracting chart meta")
ref = fmt.Sprintf("%s/testrepo/%s:%s", suite.DockerRegistryHost, meta.Name, meta.Version) ref = fmt.Sprintf("%s/testrepo/%s:%s", suite.DockerRegistryHost, meta.Name, meta.Version)
_, err = suite.RegistryClient.Push(chartData, ref) _, err = suite.RegistryClient.Push(chartData, ref, PushOptTest(true))
suite.Nil(err, "no error pushing good ref") suite.Nil(err, "no error pushing good ref")
_, err = suite.RegistryClient.Pull(ref) _, err = suite.RegistryClient.Pull(ref)
@ -267,7 +267,7 @@ func testPush(suite *TestSuite) {
// push with prov // push with prov
ref = fmt.Sprintf("%s/testrepo/%s:%s", suite.DockerRegistryHost, meta.Name, meta.Version) ref = fmt.Sprintf("%s/testrepo/%s:%s", suite.DockerRegistryHost, meta.Name, meta.Version)
result, err := suite.RegistryClient.Push(chartData, ref, PushOptProvData(provData)) result, err := suite.RegistryClient.Push(chartData, ref, PushOptProvData(provData), PushOptTest(true))
suite.Nil(err, "no error pushing good ref with prov") suite.Nil(err, "no error pushing good ref with prov")
_, err = suite.RegistryClient.Pull(ref) _, err = suite.RegistryClient.Pull(ref)

Loading…
Cancel
Save