github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/util/label_copier.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"fmt"
    19  	"k8s.io/heapster/metrics/core"
    20  	"sort"
    21  	"strings"
    22  )
    23  
    24  // LabelCopier maps kubernetes objects' labels to metrics
    25  type LabelCopier struct {
    26  	// labelSeparator contains separator used to join labels into "labels" label
    27  	labelSeparator string
    28  	// storedLabels maps source label names to their destination names used in metrics
    29  	storedLabels map[string]string
    30  	// ignoredLabels contains labels to be skipped during concatenation
    31  	ignoredLabels map[string]string
    32  }
    33  
    34  // Copy copies the given set of pod labels into a set of metric labels, using the following logic:
    35  // - all labels, unless found in ignoredLabels, are concatenated into a Separator-separated key:value pairs and stored under core.LabelLabels.Key
    36  // - labels found in storedLabels are additionally stored under key provided
    37  func (this *LabelCopier) Copy(in map[string]string, out map[string]string) {
    38  	labels := make([]string, 0, len(in))
    39  
    40  	for key, value := range in {
    41  		if mappedKey, exists := this.storedLabels[key]; exists {
    42  			out[mappedKey] = value
    43  		}
    44  
    45  		if _, exists := this.ignoredLabels[key]; !exists {
    46  			labels = append(labels, fmt.Sprintf("%s:%s", key, value))
    47  		}
    48  	}
    49  
    50  	sort.Strings(labels)
    51  	out[core.LabelLabels.Key] = strings.Join(labels, this.labelSeparator)
    52  }
    53  
    54  // makeStoredLabels converts labels into a map for quicker retrieval.
    55  // Incoming labels, if desired, may contain mappings in format "newName=oldName"
    56  func makeStoredLabels(labels []string) map[string]string {
    57  	storedLabels := make(map[string]string)
    58  	for _, s := range labels {
    59  		split := strings.SplitN(s, "=", 2)
    60  		if len(split) == 1 {
    61  			storedLabels[split[0]] = split[0]
    62  		} else {
    63  			storedLabels[split[1]] = split[0]
    64  		}
    65  	}
    66  	return storedLabels
    67  }
    68  
    69  // makeIgnoredLabels converts label slice into a map for later use.
    70  func makeIgnoredLabels(labels []string) map[string]string {
    71  	ignoredLabels := make(map[string]string)
    72  	for _, s := range labels {
    73  		ignoredLabels[s] = ""
    74  	}
    75  	return ignoredLabels
    76  }
    77  
    78  // NewLabelCopier creates a new instance of LabelCopier type
    79  func NewLabelCopier(separator string, storedLabels, ignoredLabels []string) (*LabelCopier, error) {
    80  	return &LabelCopier{
    81  		labelSeparator: separator,
    82  		storedLabels:   makeStoredLabels(storedLabels),
    83  		ignoredLabels:  makeIgnoredLabels(ignoredLabels),
    84  	}, nil
    85  }