-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add crane mutate to set labels and entrypoint (#982)
- Loading branch information
Showing
8 changed files
with
171 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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" | ||
"log" | ||
"strings" | ||
|
||
"github.com/google/go-containerregistry/pkg/crane" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
"github.com/google/go-containerregistry/pkg/v1/mutate" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewCmdMutate creates a new cobra.Command for the mutate subcommand. | ||
func NewCmdMutate(options *[]crane.Option) *cobra.Command { | ||
var lbls []string | ||
var entrypoint string | ||
var newRef string | ||
|
||
mutateCmd := &cobra.Command{ | ||
Use: "mutate", | ||
Short: "Modify image labels and annotations", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(_ *cobra.Command, args []string) { | ||
// Pull image and get config. | ||
ref := args[0] | ||
img, err := crane.Pull(ref, *options...) | ||
if err != nil { | ||
log.Fatalf("pulling %s: %v", ref, err) | ||
} | ||
cfg, err := img.ConfigFile() | ||
if err != nil { | ||
log.Fatalf("getting config: %v", err) | ||
} | ||
cfg = cfg.DeepCopy() | ||
|
||
// Set labels. | ||
if cfg.Config.Labels == nil { | ||
cfg.Config.Labels = map[string]string{} | ||
} | ||
labels := map[string]string{} | ||
for _, l := range lbls { | ||
parts := strings.SplitN(l, "=", 2) | ||
if len(parts) == 1 { | ||
log.Fatalf("parsing label %q, not enough parts", l) | ||
} | ||
labels[parts[0]] = parts[1] | ||
} | ||
for k, v := range labels { | ||
cfg.Config.Labels[k] = v | ||
} | ||
|
||
// Set entrypoint. | ||
if entrypoint != "" { | ||
// NB: This doesn't attempt to do anything smart about splitting the string into multiple entrypoint elements. | ||
cfg.Config.Entrypoint = []string{entrypoint} | ||
} | ||
|
||
// Mutate and write image. | ||
img, err = mutate.Config(img, cfg.Config) | ||
if err != nil { | ||
log.Fatalf("mutating config: %v", err) | ||
} | ||
|
||
// If the new ref isn't provided, write over the original image. | ||
// If that ref was provided by digest (e.g., output from | ||
// another crane command), then strip that and push the | ||
// mutated image by digest instead. | ||
if newRef == "" { | ||
newRef = ref | ||
} | ||
digest, err := img.Digest() | ||
if err != nil { | ||
log.Fatalf("digesting new image: %v", err) | ||
} | ||
r, err := name.ParseReference(newRef) | ||
if err != nil { | ||
log.Fatalf("parsing %s: %v", newRef, err) | ||
} | ||
if _, ok := r.(name.Digest); ok { | ||
newRef = r.Context().Digest(digest.String()).String() | ||
} | ||
if err := crane.Push(img, newRef, *options...); err != nil { | ||
log.Fatalf("pushing %s: %v", newRef, err) | ||
} | ||
fmt.Println(r.Context().Digest(digest.String())) | ||
}, | ||
} | ||
mutateCmd.Flags().StringSliceVarP(&lbls, "label", "l", nil, "New labels to add") | ||
mutateCmd.Flags().StringVar(&entrypoint, "entrypoint", "", "New entrypoing to set") | ||
mutateCmd.Flags().StringVarP(&newRef, "tag", "t", "", "New tag to apply to mutated image. If not provided, push by digest to the original image repository.") | ||
return mutateCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters