Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package * refactor: move auth and security related structs to a utility package, fix import error in utility package * refactor: Azure SD, remove SD struct from config * refactor: DNS SD, remove SD struct from config into dns package * refactor: ec2 SD, move SD struct from config into the ec2 package * refactor: file SD, move SD struct from config to file discovery package * refactor: gce, move SD struct from config to gce discovery package * refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil * refactor: consul, move SD struct from config into consul discovery package * refactor: marathon, move SD struct from config into marathon discovery package * refactor: triton, move SD struct from config to triton discovery package, fix test * refactor: zookeeper, move SD structs from config to zookeeper discovery package * refactor: openstack, remove SD struct from config, move into openstack discovery package * refactor: kubernetes, move SD struct from config into kubernetes discovery package * refactor: notifier, use targetgroup package instead of config * refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup * refactor: retrieval, use targetgroup package instead of config.TargetGroup * refactor: storage, use config util package * refactor: discovery manager, use targetgroup package instead of config.TargetGroup * refactor: use HTTPClient and TLS config from configUtil instead of config * refactor: tests, use targetgroup package instead of config.TargetGroup * refactor: fix tagetgroup.Group pointers that were removed by mistake * refactor: openstack, kubernetes: drop prefixes * refactor: remove import aliases forced due to vscode bug * refactor: move main SD struct out of config into discovery/config * refactor: rename configUtil to config_util * refactor: rename yamlUtil to yaml_config * refactor: kubernetes, remove prefixes * refactor: move the TargetGroup package to discovery/ * refactor: fix order of importspull/3610/head dev
parent
380cacd3a4
commit
ec94df49d4
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,73 @@ |
||||
// Copyright 2016 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package config |
||||
|
||||
import ( |
||||
"github.com/prometheus/prometheus/discovery/azure" |
||||
"github.com/prometheus/prometheus/discovery/consul" |
||||
"github.com/prometheus/prometheus/discovery/dns" |
||||
"github.com/prometheus/prometheus/discovery/ec2" |
||||
"github.com/prometheus/prometheus/discovery/file" |
||||
"github.com/prometheus/prometheus/discovery/gce" |
||||
"github.com/prometheus/prometheus/discovery/kubernetes" |
||||
"github.com/prometheus/prometheus/discovery/marathon" |
||||
"github.com/prometheus/prometheus/discovery/openstack" |
||||
"github.com/prometheus/prometheus/discovery/targetgroup" |
||||
"github.com/prometheus/prometheus/discovery/triton" |
||||
"github.com/prometheus/prometheus/discovery/zookeeper" |
||||
|
||||
yaml_util "github.com/prometheus/prometheus/util/yaml" |
||||
) |
||||
|
||||
// ServiceDiscoveryConfig configures lists of different service discovery mechanisms.
|
||||
type ServiceDiscoveryConfig struct { |
||||
// List of labeled target groups for this job.
|
||||
StaticConfigs []*targetgroup.Group `yaml:"static_configs,omitempty"` |
||||
// List of DNS service discovery configurations.
|
||||
DNSSDConfigs []*dns.SDConfig `yaml:"dns_sd_configs,omitempty"` |
||||
// List of file service discovery configurations.
|
||||
FileSDConfigs []*file.SDConfig `yaml:"file_sd_configs,omitempty"` |
||||
// List of Consul service discovery configurations.
|
||||
ConsulSDConfigs []*consul.SDConfig `yaml:"consul_sd_configs,omitempty"` |
||||
// List of Serverset service discovery configurations.
|
||||
ServersetSDConfigs []*zookeeper.ServersetSDConfig `yaml:"serverset_sd_configs,omitempty"` |
||||
// NerveSDConfigs is a list of Nerve service discovery configurations.
|
||||
NerveSDConfigs []*zookeeper.NerveSDConfig `yaml:"nerve_sd_configs,omitempty"` |
||||
// MarathonSDConfigs is a list of Marathon service discovery configurations.
|
||||
MarathonSDConfigs []*marathon.SDConfig `yaml:"marathon_sd_configs,omitempty"` |
||||
// List of Kubernetes service discovery configurations.
|
||||
KubernetesSDConfigs []*kubernetes.SDConfig `yaml:"kubernetes_sd_configs,omitempty"` |
||||
// List of GCE service discovery configurations.
|
||||
GCESDConfigs []*gce.SDConfig `yaml:"gce_sd_configs,omitempty"` |
||||
// List of EC2 service discovery configurations.
|
||||
EC2SDConfigs []*ec2.SDConfig `yaml:"ec2_sd_configs,omitempty"` |
||||
// List of OpenStack service discovery configurations.
|
||||
OpenstackSDConfigs []*openstack.SDConfig `yaml:"openstack_sd_configs,omitempty"` |
||||
// List of Azure service discovery configurations.
|
||||
AzureSDConfigs []*azure.SDConfig `yaml:"azure_sd_configs,omitempty"` |
||||
// List of Triton service discovery configurations.
|
||||
TritonSDConfigs []*triton.SDConfig `yaml:"triton_sd_configs,omitempty"` |
||||
|
||||
// Catches all undefined fields and must be empty after parsing.
|
||||
XXX map[string]interface{} `yaml:",inline"` |
||||
} |
||||
|
||||
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
||||
func (c *ServiceDiscoveryConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { |
||||
type plain ServiceDiscoveryConfig |
||||
if err := unmarshal((*plain)(c)); err != nil { |
||||
return err |
||||
} |
||||
return yaml_util.CheckOverflow(c.XXX, "service discovery config") |
||||
} |
||||
@ -0,0 +1,91 @@ |
||||
// Copyright 2013 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package targetgroup |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
|
||||
"github.com/prometheus/common/model" |
||||
"github.com/prometheus/prometheus/util/yaml" |
||||
) |
||||
|
||||
// Group is a set of targets with a common label set(production , test, staging etc.).
|
||||
type Group struct { |
||||
// Targets is a list of targets identified by a label set. Each target is
|
||||
// uniquely identifiable in the group by its address label.
|
||||
Targets []model.LabelSet |
||||
// Labels is a set of labels that is common across all targets in the group.
|
||||
Labels model.LabelSet |
||||
|
||||
// Source is an identifier that describes a group of targets.
|
||||
Source string |
||||
} |
||||
|
||||
func (tg Group) String() string { |
||||
return tg.Source |
||||
} |
||||
|
||||
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
||||
func (tg *Group) UnmarshalYAML(unmarshal func(interface{}) error) error { |
||||
g := struct { |
||||
Targets []string `yaml:"targets"` |
||||
Labels model.LabelSet `yaml:"labels"` |
||||
XXX map[string]interface{} `yaml:",inline"` |
||||
}{} |
||||
if err := unmarshal(&g); err != nil { |
||||
return err |
||||
} |
||||
tg.Targets = make([]model.LabelSet, 0, len(g.Targets)) |
||||
for _, t := range g.Targets { |
||||
tg.Targets = append(tg.Targets, model.LabelSet{ |
||||
model.AddressLabel: model.LabelValue(t), |
||||
}) |
||||
} |
||||
tg.Labels = g.Labels |
||||
return yaml.CheckOverflow(g.XXX, "static_config") |
||||
} |
||||
|
||||
// MarshalYAML implements the yaml.Marshaler interface.
|
||||
func (tg Group) MarshalYAML() (interface{}, error) { |
||||
g := &struct { |
||||
Targets []string `yaml:"targets"` |
||||
Labels model.LabelSet `yaml:"labels,omitempty"` |
||||
}{ |
||||
Targets: make([]string, 0, len(tg.Targets)), |
||||
Labels: tg.Labels, |
||||
} |
||||
for _, t := range tg.Targets { |
||||
g.Targets = append(g.Targets, string(t[model.AddressLabel])) |
||||
} |
||||
return g, nil |
||||
} |
||||
|
||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||
func (tg *Group) UnmarshalJSON(b []byte) error { |
||||
g := struct { |
||||
Targets []string `json:"targets"` |
||||
Labels model.LabelSet `json:"labels"` |
||||
}{} |
||||
if err := json.Unmarshal(b, &g); err != nil { |
||||
return err |
||||
} |
||||
tg.Targets = make([]model.LabelSet, 0, len(g.Targets)) |
||||
for _, t := range g.Targets { |
||||
tg.Targets = append(tg.Targets, model.LabelSet{ |
||||
model.AddressLabel: model.LabelValue(t), |
||||
}) |
||||
} |
||||
tg.Labels = g.Labels |
||||
return nil |
||||
} |
||||
@ -0,0 +1,138 @@ |
||||
// Copyright 2013 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package config |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net/url" |
||||
|
||||
yaml_util "github.com/prometheus/prometheus/util/yaml" |
||||
) |
||||
|
||||
// Secret special type for storing secrets.
|
||||
type Secret string |
||||
|
||||
// UnmarshalYAML implements the yaml.Unmarshaler interface for Secrets.
|
||||
func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error { |
||||
type plain Secret |
||||
return unmarshal((*plain)(s)) |
||||
} |
||||
|
||||
// MarshalYAML implements the yaml.Marshaler interface for Secrets.
|
||||
func (s Secret) MarshalYAML() (interface{}, error) { |
||||
if s != "" { |
||||
return "<secret>", nil |
||||
} |
||||
return nil, nil |
||||
} |
||||
|
||||
// TLSConfig configures the options for TLS connections.
|
||||
type TLSConfig struct { |
||||
// The CA cert to use for the targets.
|
||||
CAFile string `yaml:"ca_file,omitempty"` |
||||
// The client cert file for the targets.
|
||||
CertFile string `yaml:"cert_file,omitempty"` |
||||
// The client key file for the targets.
|
||||
KeyFile string `yaml:"key_file,omitempty"` |
||||
// Used to verify the hostname for the targets.
|
||||
ServerName string `yaml:"server_name,omitempty"` |
||||
// Disable target certificate validation.
|
||||
InsecureSkipVerify bool `yaml:"insecure_skip_verify"` |
||||
|
||||
// Catches all undefined fields and must be empty after parsing.
|
||||
XXX map[string]interface{} `yaml:",inline"` |
||||
} |
||||
|
||||
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
||||
func (c *TLSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { |
||||
type plain TLSConfig |
||||
if err := unmarshal((*plain)(c)); err != nil { |
||||
return err |
||||
} |
||||
return yaml_util.CheckOverflow(c.XXX, "TLS config") |
||||
} |
||||
|
||||
// BasicAuth contains basic HTTP authentication credentials.
|
||||
type BasicAuth struct { |
||||
Username string `yaml:"username"` |
||||
Password Secret `yaml:"password"` |
||||
|
||||
// Catches all undefined fields and must be empty after parsing.
|
||||
XXX map[string]interface{} `yaml:",inline"` |
||||
} |
||||
|
||||
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
||||
func (a *BasicAuth) UnmarshalYAML(unmarshal func(interface{}) error) error { |
||||
type plain BasicAuth |
||||
err := unmarshal((*plain)(a)) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return yaml_util.CheckOverflow(a.XXX, "basic_auth") |
||||
} |
||||
|
||||
// URL is a custom URL type that allows validation at configuration load time.
|
||||
type URL struct { |
||||
*url.URL |
||||
} |
||||
|
||||
// UnmarshalYAML implements the yaml.Unmarshaler interface for URLs.
|
||||
func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error { |
||||
var s string |
||||
if err := unmarshal(&s); err != nil { |
||||
return err |
||||
} |
||||
|
||||
urlp, err := url.Parse(s) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
u.URL = urlp |
||||
return nil |
||||
} |
||||
|
||||
// MarshalYAML implements the yaml.Marshaler interface for URLs.
|
||||
func (u URL) MarshalYAML() (interface{}, error) { |
||||
if u.URL != nil { |
||||
return u.String(), nil |
||||
} |
||||
return nil, nil |
||||
} |
||||
|
||||
// HTTPClientConfig configures an HTTP client.
|
||||
type HTTPClientConfig struct { |
||||
// The HTTP basic authentication credentials for the targets.
|
||||
BasicAuth *BasicAuth `yaml:"basic_auth,omitempty"` |
||||
// The bearer token for the targets.
|
||||
BearerToken Secret `yaml:"bearer_token,omitempty"` |
||||
// The bearer token file for the targets.
|
||||
BearerTokenFile string `yaml:"bearer_token_file,omitempty"` |
||||
// HTTP proxy server to use to connect to the targets.
|
||||
ProxyURL URL `yaml:"proxy_url,omitempty"` |
||||
// TLSConfig to use to connect to the targets.
|
||||
TLSConfig TLSConfig `yaml:"tls_config,omitempty"` |
||||
|
||||
// Catches all undefined fields and must be empty after parsing.
|
||||
XXX map[string]interface{} `yaml:",inline"` |
||||
} |
||||
|
||||
func (c *HTTPClientConfig) Validate() error { |
||||
if len(c.BearerToken) > 0 && len(c.BearerTokenFile) > 0 { |
||||
return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured") |
||||
} |
||||
if c.BasicAuth != nil && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) { |
||||
return fmt.Errorf("at most one of basic_auth, bearer_token & bearer_token_file must be configured") |
||||
} |
||||
return nil |
||||
} |
||||
@ -0,0 +1,31 @@ |
||||
// Copyright 2013 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package yaml |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strings" |
||||
) |
||||
|
||||
// CheckOverflow checks for unknown config params and raises an error if found
|
||||
func CheckOverflow(m map[string]interface{}, ctx string) error { |
||||
if len(m) > 0 { |
||||
var keys []string |
||||
for k := range m { |
||||
keys = append(keys, k) |
||||
} |
||||
return fmt.Errorf("unknown fields in %s: %s", ctx, strings.Join(keys, ", ")) |
||||
} |
||||
return nil |
||||
} |
||||
Loading…
Reference in new issue