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