github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/boom/application/applications/metricspersisting/servicemonitor/app.go (about)

     1  package servicemonitor
     2  
     3  type ConfigEndpoint struct {
     4  	Port              string
     5  	TargetPort        string
     6  	Interval          string
     7  	Scheme            string
     8  	Path              string
     9  	BearerTokenFile   string
    10  	MetricRelabelings []*ConfigRelabeling
    11  	Relabelings       []*ConfigRelabeling
    12  	TLSConfig         *ConfigTLSConfig
    13  	HonorLabels       bool
    14  }
    15  
    16  type ConfigTLSConfig struct {
    17  	CaFile             string
    18  	ServerName         string
    19  	InsecureSkipVerify bool
    20  }
    21  
    22  type ConfigRelabeling struct {
    23  	Action       string
    24  	Regex        string
    25  	SourceLabels []string
    26  	TargetLabel  string
    27  	Replacement  string
    28  }
    29  
    30  type Config struct {
    31  	Name                  string
    32  	Endpoints             []*ConfigEndpoint
    33  	NamespaceSelector     []string
    34  	MonitorMatchingLabels map[string]string
    35  	ServiceMatchingLabels map[string]string
    36  	JobName               string
    37  }
    38  
    39  func SpecToValues(config *Config) *Values {
    40  
    41  	endpoints := make([]*Endpoint, 0)
    42  	for _, endpoint := range config.Endpoints {
    43  		metricRels := make([]*MetricRelabeling, 0)
    44  		for _, relabel := range endpoint.MetricRelabelings {
    45  			rel := &MetricRelabeling{
    46  				Action:       relabel.Action,
    47  				Regex:        relabel.Regex,
    48  				SourceLabels: relabel.SourceLabels,
    49  				TargetLabel:  relabel.TargetLabel,
    50  				Replacement:  relabel.Replacement,
    51  			}
    52  			metricRels = append(metricRels, rel)
    53  		}
    54  		rels := make([]*Relabeling, 0)
    55  		for _, relabel := range endpoint.Relabelings {
    56  			rel := &Relabeling{
    57  				Action:       relabel.Action,
    58  				Regex:        relabel.Regex,
    59  				SourceLabels: relabel.SourceLabels,
    60  				TargetLabel:  relabel.TargetLabel,
    61  				Replacement:  relabel.Replacement,
    62  			}
    63  			rels = append(rels, rel)
    64  		}
    65  		valueEndpoint := &Endpoint{
    66  			Port:              endpoint.Port,
    67  			TargetPort:        endpoint.TargetPort,
    68  			Interval:          endpoint.Interval,
    69  			Scheme:            endpoint.Scheme,
    70  			Path:              endpoint.Path,
    71  			BearerTokenFile:   endpoint.BearerTokenFile,
    72  			MetricRelabelings: metricRels,
    73  			Relabelings:       rels,
    74  			HonorLabels:       endpoint.HonorLabels,
    75  		}
    76  		if endpoint.TLSConfig != nil {
    77  			t := &TLSConfig{
    78  				CaFile:             endpoint.TLSConfig.CaFile,
    79  				ServerName:         endpoint.TLSConfig.ServerName,
    80  				InsecureSkipVerify: endpoint.TLSConfig.InsecureSkipVerify,
    81  			}
    82  			valueEndpoint.TLSConfig = t
    83  		}
    84  
    85  		endpoints = append(endpoints, valueEndpoint)
    86  	}
    87  
    88  	values := &Values{
    89  		Name:             config.Name,
    90  		AdditionalLabels: config.MonitorMatchingLabels,
    91  		Selector: &Selector{
    92  			MatchLabels: config.ServiceMatchingLabels,
    93  		},
    94  		NamespaceSelector: &NamespaceSelector{
    95  			Any: true,
    96  		},
    97  		JobLabel:  config.JobName,
    98  		Endpoints: endpoints,
    99  	}
   100  
   101  	if len(config.NamespaceSelector) != 0 {
   102  		values.NamespaceSelector = &NamespaceSelector{
   103  			Any:        false,
   104  			MatchNames: config.NamespaceSelector,
   105  		}
   106  	}
   107  
   108  	return values
   109  }