2020-04-24 14:50:21 +00:00
|
|
|
package gce
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SDConfig represents service discovery config for gce.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#gce_sd_config
|
|
|
|
type SDConfig struct {
|
2020-04-27 08:44:28 +00:00
|
|
|
Project string `yaml:"project"`
|
|
|
|
Zone ZoneYAML `yaml:"zone"`
|
2020-11-13 14:17:03 +00:00
|
|
|
Filter string `yaml:"filter,omitempty"`
|
2020-04-24 14:50:21 +00:00
|
|
|
// RefreshInterval time.Duration `yaml:"refresh_interval"`
|
|
|
|
// refresh_interval is obtained from `-promscrape.gceSDCheckInterval` command-line option.
|
2020-11-13 14:17:03 +00:00
|
|
|
Port *int `yaml:"port,omitempty"`
|
|
|
|
TagSeparator *string `yaml:"tag_separator,omitempty"`
|
2020-04-24 14:50:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 08:44:28 +00:00
|
|
|
// ZoneYAML holds info about zones.
|
|
|
|
type ZoneYAML struct {
|
|
|
|
zones []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler
|
|
|
|
func (z *ZoneYAML) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := unmarshal(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var zones []string
|
2020-04-27 16:28:10 +00:00
|
|
|
switch t := v.(type) {
|
2020-04-27 08:44:28 +00:00
|
|
|
case string:
|
2020-04-27 16:28:10 +00:00
|
|
|
zones = []string{t}
|
2020-04-27 08:44:28 +00:00
|
|
|
case []interface{}:
|
2020-04-27 16:28:10 +00:00
|
|
|
for _, vv := range t {
|
2020-04-27 08:44:28 +00:00
|
|
|
zone, ok := vv.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected zone type detected: %T; contents: %#v", vv, vv)
|
|
|
|
}
|
|
|
|
zones = append(zones, zone)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected type unmarshaled for ZoneYAML: %T; contents: %#v", v, v)
|
|
|
|
}
|
|
|
|
z.zones = zones
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-24 14:50:21 +00:00
|
|
|
// GetLabels returns gce labels according to sdc.
|
2021-02-26 13:53:42 +00:00
|
|
|
func (sdc *SDConfig) GetLabels(baseDir string) ([]map[string]string, error) {
|
2020-04-24 14:50:21 +00:00
|
|
|
cfg, err := getAPIConfig(sdc)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot get API config: %w", err)
|
2020-04-24 14:50:21 +00:00
|
|
|
}
|
2020-05-06 12:00:02 +00:00
|
|
|
ms := getInstancesLabels(cfg)
|
2020-04-24 14:50:21 +00:00
|
|
|
return ms, nil
|
|
|
|
}
|