github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/build/util.go (about) 1 /* 2 Copyright 2020 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 "path/filepath" 21 22 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 23 tag "github.com/GoogleContainerTools/skaffold/pkg/skaffold/tag/util" 24 ) 25 26 func matchBuildersToImages(builders []InitBuilder, images []string) ([]ArtifactInfo, []InitBuilder, []string) { 27 images = tag.StripTags(images, true) 28 29 var artifactInfos []ArtifactInfo 30 var unresolvedImages = make(sortedSet) 31 for _, image := range images { 32 builderIdx := findExactlyOneMatchingBuilder(builders, image) 33 34 // exactly one builder found for the image 35 if builderIdx != -1 { 36 // save the pair 37 artifactInfos = append(artifactInfos, ArtifactInfo{ImageName: image, Builder: builders[builderIdx]}) 38 // remove matched builder from builderConfigs 39 builders = append(builders[:builderIdx], builders[builderIdx+1:]...) 40 } else { 41 // No definite pair found, add to images list 42 unresolvedImages.add(image) 43 } 44 } 45 return artifactInfos, builders, unresolvedImages.values() 46 } 47 48 func findExactlyOneMatchingBuilder(builderConfigs []InitBuilder, image string) int { 49 matchingConfigIndex := -1 50 for i, config := range builderConfigs { 51 if image != config.ConfiguredImage() { 52 continue 53 } 54 // Found more than one match; 55 if matchingConfigIndex != -1 { 56 return -1 57 } 58 matchingConfigIndex = i 59 } 60 return matchingConfigIndex 61 } 62 63 // Artifacts takes builder image pairs and workspaces and creates a list of latest.Artifacts from the data. 64 func Artifacts(artifactInfos []ArtifactInfo) []*latest.Artifact { 65 var artifacts []*latest.Artifact 66 67 for _, info := range artifactInfos { 68 // Don't create artifact build config for "None" builder 69 if info.Builder.Name() == NoneBuilderName { 70 continue 71 } 72 workspace := info.Workspace 73 if workspace == "" { 74 workspace = filepath.Dir(info.Builder.Path()) 75 } 76 artifact := &latest.Artifact{ 77 ImageName: info.ImageName, 78 ArtifactType: info.Builder.ArtifactType(workspace), 79 } 80 81 if workspace != "." { 82 // to make skaffold.yaml more portable across OS-es we should always generate /-delimited filepaths 83 artifact.Workspace = filepath.ToSlash(workspace) 84 } 85 86 artifacts = append(artifacts, artifact) 87 } 88 89 return artifacts 90 }