github.com/nak3/source-to-image@v1.1.10-0.20180319140719-2ed55639898d/pkg/util/labels.go (about) 1 package util 2 3 import ( 4 "fmt" 5 6 "github.com/openshift/source-to-image/pkg/api" 7 "github.com/openshift/source-to-image/pkg/scm/git" 8 ) 9 10 // GenerateOutputImageLabels generate the labels based on the s2i Config 11 // and source repository informations. 12 func GenerateOutputImageLabels(info *git.SourceInfo, config *api.Config) map[string]string { 13 labels := map[string]string{} 14 namespace := api.DefaultNamespace 15 if len(config.LabelNamespace) > 0 { 16 namespace = config.LabelNamespace 17 } 18 19 labels = GenerateLabelsFromConfig(labels, config, namespace) 20 labels = GenerateLabelsFromSourceInfo(labels, info, namespace) 21 return labels 22 } 23 24 // GenerateLabelsFromConfig generate the labels based on build s2i Config 25 func GenerateLabelsFromConfig(labels map[string]string, config *api.Config, namespace string) map[string]string { 26 if len(config.Description) > 0 { 27 labels[api.KubernetesNamespace+"description"] = config.Description 28 } 29 30 if len(config.DisplayName) > 0 { 31 labels[api.KubernetesNamespace+"display-name"] = config.DisplayName 32 } else { 33 labels[api.KubernetesNamespace+"display-name"] = config.Tag 34 } 35 36 addBuildLabel(labels, "image", config.BuilderImage, namespace) 37 return labels 38 } 39 40 // GenerateLabelsFromSourceInfo generate the labels based on the source repository 41 // informations. 42 func GenerateLabelsFromSourceInfo(labels map[string]string, info *git.SourceInfo, namespace string) map[string]string { 43 if info == nil { 44 glog.V(3).Info("Unable to fetch source informations, the output image labels will not be set") 45 return labels 46 } 47 48 if len(info.AuthorName) > 0 { 49 author := fmt.Sprintf("%s <%s>", info.AuthorName, info.AuthorEmail) 50 addBuildLabel(labels, "commit.author", author, namespace) 51 } 52 53 addBuildLabel(labels, "commit.date", info.Date, namespace) 54 addBuildLabel(labels, "commit.id", info.CommitID, namespace) 55 addBuildLabel(labels, "commit.ref", info.Ref, namespace) 56 addBuildLabel(labels, "commit.message", info.Message, namespace) 57 addBuildLabel(labels, "source-location", info.Location, namespace) 58 addBuildLabel(labels, "source-context-dir", info.ContextDir, namespace) 59 return labels 60 } 61 62 // addBuildLabel adds a new "*.build.*" label into map when the 63 // value of this label is not empty 64 func addBuildLabel(to map[string]string, key, value, namespace string) { 65 if len(value) == 0 { 66 return 67 } 68 to[namespace+"build."+key] = value 69 }