...

Source file src/k8s.io/kubernetes/test/instrumentation/sort/main.go

Documentation: k8s.io/kubernetes/test/instrumentation/sort

     1  /*
     2  Copyright 2023 The Kubernetes 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 main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"sort"
    23  
    24  	flag "github.com/spf13/pflag"
    25  	"gopkg.in/yaml.v2"
    26  	"k8s.io/component-base/metrics"
    27  )
    28  
    29  func main() {
    30  	var sortFile string
    31  	flag.StringVar(&sortFile, "sort-file", "", "file of metrics to sort")
    32  	flag.Parse()
    33  	dat, err := os.ReadFile(sortFile)
    34  	if err == nil {
    35  		var parsedMetrics []metric
    36  		err = yaml.Unmarshal(dat, &parsedMetrics)
    37  		if err != nil {
    38  			fmt.Fprintf(os.Stderr, "%s\n", err)
    39  			os.Exit(1)
    40  		}
    41  		sort.Sort(byFQName(parsedMetrics))
    42  		data, err := yaml.Marshal(parsedMetrics)
    43  		if err != nil {
    44  			fmt.Fprintf(os.Stderr, "%s\n", err)
    45  			os.Exit(1)
    46  		}
    47  
    48  		fmt.Print(string(data))
    49  	}
    50  }
    51  
    52  type metric struct {
    53  	Name              string              `yaml:"name" json:"name"`
    54  	Subsystem         string              `yaml:"subsystem,omitempty" json:"subsystem,omitempty"`
    55  	Namespace         string              `yaml:"namespace,omitempty" json:"namespace,omitempty"`
    56  	Help              string              `yaml:"help,omitempty" json:"help,omitempty"`
    57  	Type              string              `yaml:"type,omitempty" json:"type,omitempty"`
    58  	DeprecatedVersion string              `yaml:"deprecatedVersion,omitempty" json:"deprecatedVersion,omitempty"`
    59  	StabilityLevel    string              `yaml:"stabilityLevel,omitempty" json:"stabilityLevel,omitempty"`
    60  	Labels            []string            `yaml:"labels,omitempty" json:"labels,omitempty"`
    61  	Buckets           []float64           `yaml:"buckets,omitempty" json:"buckets,omitempty"`
    62  	Objectives        map[float64]float64 `yaml:"objectives,omitempty" json:"objectives,omitempty"`
    63  	AgeBuckets        uint32              `yaml:"ageBuckets,omitempty" json:"ageBuckets,omitempty"`
    64  	BufCap            uint32              `yaml:"bufCap,omitempty" json:"bufCap,omitempty"`
    65  	MaxAge            int64               `yaml:"maxAge,omitempty" json:"maxAge,omitempty"`
    66  	ConstLabels       map[string]string   `yaml:"constLabels,omitempty" json:"constLabels,omitempty"`
    67  }
    68  
    69  func (m metric) BuildFQName() string {
    70  	return metrics.BuildFQName(m.Namespace, m.Subsystem, m.Name)
    71  }
    72  
    73  type byFQName []metric
    74  
    75  func (ms byFQName) Len() int { return len(ms) }
    76  func (ms byFQName) Less(i, j int) bool {
    77  	if ms[i].StabilityLevel < ms[j].StabilityLevel {
    78  		return true
    79  	} else if ms[i].StabilityLevel > ms[j].StabilityLevel {
    80  		return false
    81  	}
    82  	return ms[i].BuildFQName() < ms[j].BuildFQName()
    83  }
    84  func (ms byFQName) Swap(i, j int) {
    85  	ms[i], ms[j] = ms[j], ms[i]
    86  }
    87  

View as plain text