github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/util.go (about)

     1  /*
     2  Copyright 2019 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package build
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
    25  )
    26  
    27  // MergeWithPreviousBuilds merges previous or prebuilt build artifacts with
    28  // builds. If an artifact is already present in builds, the same artifact from
    29  // previous will be replaced at the same position.
    30  func MergeWithPreviousBuilds(builds, previous []graph.Artifact) []graph.Artifact {
    31  	updatedBuilds := map[string]graph.Artifact{}
    32  	for _, build := range builds {
    33  		updatedBuilds[build.ImageName] = build
    34  	}
    35  
    36  	added := map[string]bool{}
    37  	var merged []graph.Artifact
    38  
    39  	for _, artifact := range previous {
    40  		if updated, found := updatedBuilds[artifact.ImageName]; found {
    41  			merged = append(merged, updated)
    42  		} else {
    43  			merged = append(merged, artifact)
    44  		}
    45  		added[artifact.ImageName] = true
    46  	}
    47  
    48  	for _, artifact := range builds {
    49  		if !added[artifact.ImageName] {
    50  			merged = append(merged, artifact)
    51  		}
    52  	}
    53  
    54  	return merged
    55  }
    56  
    57  func TagWithDigest(tag, digest string) string {
    58  	digestSuffix := "@" + digest
    59  	if strings.HasSuffix(tag, digestSuffix) {
    60  		return tag
    61  	}
    62  	return tag + digestSuffix
    63  }
    64  
    65  func TagWithImageID(ctx context.Context, tag string, imageID string, localDocker docker.LocalDaemon) (string, error) {
    66  	return localDocker.TagWithImageID(ctx, tag, imageID)
    67  }