Skip to content

Add annotate command for adding annotations for indexes and images #2119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions cmd/crane/cmd/annotate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2021 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

"github.com/google/go-containerregistry/pkg/crane"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/spf13/cobra"
)

// NewCmdAnnotate creates a new cobra.Command for the annotate subcommand.
func NewCmdAnnotate(options *[]crane.Option) *cobra.Command {
var annotations map[string]string
var newRef string

annotateCmd := &cobra.Command{
Use: "annotate",
Short: "Modify image or index annotations. The manifest is updated there on the registry.",
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
ref := args[0]

desc, err := crane.Head(ref, *options...)
if err != nil {
return err
}

if newRef == "" {
newRef = ref
}

if desc.MediaType.IsIndex() {
d, err := crane.Get(ref, *options...)
if err != nil {
return err
}
idx, err := d.ImageIndex()
if err != nil {
return err
}
annotated := mutate.Annotations(idx, annotations).(v1.ImageIndex)
err = crane.PushIndex(annotated, newRef, *options...)
if err != nil {
return err
}
fmt.Println("Index pushed with annotations.")
} else if desc.MediaType.IsImage() {
img, err := crane.Pull(ref, *options...)
if err != nil {
return err
}
annotated := mutate.Annotations(img, annotations).(v1.Image)
err = crane.Push(annotated, newRef, *options...)
if err != nil {
return err
}
fmt.Println("Image pushed with annotations.")
} else {
return fmt.Errorf("unsupported manifest type only indexes and images are currently supported: %s", desc.ArtifactType)
}

return nil
},
}
annotateCmd.Flags().StringToStringVarP(&annotations, "annotation", "a", nil, "New annotations to add")
annotateCmd.Flags().StringVarP(&newRef, "tag", "t", "", "New tag reference to apply to annotated image/index. If not provided, push by digest to the original repository.")
return annotateCmd
}
1 change: 1 addition & 0 deletions cmd/crane/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func New(use, short string, options []crane.Option) *cobra.Command {
}

root.AddCommand(
NewCmdAnnotate(&options),
NewCmdAppend(&options),
NewCmdAuth(options, "crane", "auth"),
NewCmdBlob(&options),
Expand Down
1 change: 1 addition & 0 deletions cmd/crane/doc/crane.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions cmd/crane/doc/crane_annotate.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions pkg/crane/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func Push(img v1.Image, dst string, opt ...Option) error {
return remote.Write(tag, img, o.Remote...)
}

// PushIndex pushes the v1.ImageIndex idx to a registry as dst.
func PushIndex(idx v1.ImageIndex, dst string, opt ...Option) error {
o := makeOptions(opt...)
tag, err := name.ParseReference(dst, o.Name...)
if err != nil {
return fmt.Errorf("parsing reference %q: %w", dst, err)
}
return remote.Put(tag, idx, o.Remote...)
}

// Upload pushes the v1.Layer to a given repo.
func Upload(layer v1.Layer, repo string, opt ...Option) error {
o := makeOptions(opt...)
Expand Down