Skip to content

Commit

Permalink
improvements based on feedbacks
Browse files Browse the repository at this point in the history
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>
  • Loading branch information
developer-guy committed Jun 18, 2021
1 parent 2e9aeba commit 6b1b7d7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
11 changes: 6 additions & 5 deletions cmd/crane/cmd/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
cfg.Config.Labels = map[string]string{}
}

labels, err := splitKeyValuePairsInFormOfAnEqualSign(lbls)
labels, err := splitKeyVals(lbls)
if err != nil {
log.Fatal(err)
}
Expand All @@ -63,7 +63,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
cfg.Config.Labels[k] = v
}

annotations, err := splitKeyValuePairsInFormOfAnEqualSign(anntns)
annotations, err := splitKeyVals(anntns)
if err != nil {
log.Fatal(err)
}
Expand All @@ -81,9 +81,9 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
}

// Mutate and write image.
img, err = mutate.ManifestAnnotations(img, annotations)
img, err = mutate.Annotations(img, annotations)
if err != nil {
log.Fatalf("mutating config: %v", err)
log.Fatalf("mutating annotations: %v", err)
}

// If the new ref isn't provided, write over the original image.
Expand Down Expand Up @@ -117,7 +117,8 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
return mutateCmd
}

func splitKeyValuePairsInFormOfAnEqualSign(kvPairs []string) (map[string]string, error) {
// splitKeyVals splits key value pairs which is in form hello=world
func splitKeyVals(kvPairs []string) (map[string]string, error) {
m := map[string]string{}
for _, l := range kvPairs {
parts := strings.SplitN(l, "=", 2)
Expand Down
8 changes: 4 additions & 4 deletions pkg/v1/mutate/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ func Config(base v1.Image, cfg v1.Config) (v1.Image, error) {
return ConfigFile(base, cf)
}

// ManifestAnnotations mutates the provided v1.Image to have the provided annotations
func ManifestAnnotations(base v1.Image, annotations map[string]string) (v1.Image, error) {
// Annotations mutates the provided v1.Image to have the provided annotations
func Annotations(base v1.Image, annotations map[string]string) (v1.Image, error) {
m, err := base.Manifest()
if err != nil {
return nil, err
}

if len(m.Annotations) < 1 {
if m.Annotations == nil {
m.Annotations = map[string]string{}
}

Expand All @@ -133,7 +133,7 @@ func ManifestAnnotations(base v1.Image, annotations map[string]string) (v1.Image
manifest: m.DeepCopy(),
}

return image, err
return image, nil
}

// ConfigFile mutates the provided v1.Image to have the provided v1.ConfigFile
Expand Down
10 changes: 3 additions & 7 deletions pkg/v1/mutate/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,16 @@ func TestMutateConfig(t *testing.T) {
}
}

func TestMutateManifestAnnotations(t *testing.T) {
func TestAnnotations(t *testing.T) {
source := sourceImage(t)

newAnnotations := map[string]string{
"im.the.first.annotation": "hello world",
}

result, err := mutate.ManifestAnnotations(source, newAnnotations)
result, err := mutate.Annotations(source, newAnnotations)
if err != nil {
t.Fatalf("failed to mutate a config: %v", err)
}

if manifestsAreEqual(t, source, result) {
t.Error("mutating the manifest annotations MUST mutate the manifest")
t.Fatalf("failed to mutate annotations: %v", err)
}

if configDigestsAreEqual(t, source, result) {
Expand Down

0 comments on commit 6b1b7d7

Please sign in to comment.