Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Hetzner SD in ScrapeConfig CRD #6436

Merged
238 changes: 232 additions & 6 deletions Documentation/api.md

Large diffs are not rendered by default.

363 changes: 363 additions & 0 deletions bundle.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

400 changes: 400 additions & 0 deletions jsonnet/prometheus-operator/scrapeconfigs-crd.json

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions pkg/apis/monitoring/v1alpha1/scrapeconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ type ScrapeConfigSpec struct {
// DockerSDConfigs defines a list of Docker service discovery configurations.
// +optional
DockerSDConfigs []DockerSDConfig `json:"dockerSDConfigs,omitempty"`
// HetznerSDConfigs defines a list of Hetzner service discovery configurations.
// +optional
HetznerSDConfigs []HetznerSDConfig `json:"hetznerSDConfigs,omitempty"`
// RelabelConfigs defines how to rewrite the target's labels before scraping.
// Prometheus Operator automatically adds relabelings for a few standard Kubernetes fields.
// The original scrape job's name is available via the `__tmp_prometheus_job_name` label.
Expand Down Expand Up @@ -814,3 +817,44 @@ type DockerSDConfig struct {
// +optional
EnableHTTP2 *bool `json:"enableHTTP2,omitempty"`
}

// HetznerSDConfig allow retrieving scrape targets from Hetzner Cloud API and Robot API.
// This service discovery uses the public IPv4 address by default, but that can be changed with relabeling
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#hetzner_sd_config
// +k8s:openapi-gen=true
type HetznerSDConfig struct {
// The Hetzner role of entities that should be discovered.
// +kubebuilder:validation:Enum=hcloud;Hcloud;robot;Robot
// +required
slashpai marked this conversation as resolved.
Show resolved Hide resolved
Role string `json:"role"`
// BasicAuth information to use on every scrape request, required when role is robot.
// Role hcloud does not support basic auth.
// +optional
BasicAuth *v1.BasicAuth `json:"basicAuth,omitempty"`
// Authorization header configuration, required when role is hcloud.
// Role robot does not support bearer token authentication.
// +optional
Authorization *v1.SafeAuthorization `json:"authorization,omitempty"`
// Optional OAuth 2.0 configuration.
// Cannot be used at the same time as `basic_auth` or `authorization`.
// +optional
OAuth2 *v1.OAuth2 `json:"oauth2,omitempty"`
// ProxyConfig allows customizing the proxy behaviour for this scrape config.
// +optional
v1.ProxyConfig `json:",inline"`
// Configure whether HTTP requests follow HTTP 3xx redirects.
// +optional
FollowRedirects *bool `json:"followRedirects,omitempty"`
// Whether to enable HTTP2.
// +optional
EnableHTTP2 *bool `json:"enableHTTP2,omitempty"`
// TLS configuration to use on every scrape request.
// +optional
TLSConfig *v1.SafeTLSConfig `json:"tlsConfig,omitempty"`
// The port to scrape metrics from.
// +optional
Port *int `json:"port,omitempty"`
// The time after which the servers are refreshed.
// +optional
RefreshInterval *v1.Duration `json:"refreshInterval,omitempty"`
}
63 changes: 63 additions & 0 deletions pkg/apis/monitoring/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

154 changes: 154 additions & 0 deletions pkg/client/applyconfiguration/monitoring/v1alpha1/hetznersdconfig.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/client/applyconfiguration/utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions pkg/prometheus/promcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3511,6 +3511,59 @@ func (cg *ConfigGenerator) generateScrapeConfig(
})
}

// HetznerSDConfig
if len(sc.Spec.HetznerSDConfigs) > 0 {
configs := make([][]yaml.MapItem, len(sc.Spec.HetznerSDConfigs))
for i, config := range sc.Spec.HetznerSDConfigs {
assetStoreKey := fmt.Sprintf("scrapeconfig/%s/%s/hetznersdconfig/%d", sc.GetNamespace(), sc.GetName(), i)
configs[i] = cg.addBasicAuthToYaml(configs[i], assetStoreKey, store, config.BasicAuth)
configs[i] = cg.addSafeAuthorizationToYaml(configs[i], fmt.Sprintf("scrapeconfig/auth/%s/%s/hetznersdconfig/%d", sc.GetNamespace(), sc.GetName(), i), store, config.Authorization)
configs[i] = cg.addOAuth2ToYaml(configs[i], config.OAuth2, store.OAuth2Assets, assetStoreKey)
configs[i] = cg.addProxyConfigtoYaml(ctx, configs[i], sc.GetNamespace(), store, config.ProxyConfig)

configs[i] = append(configs[i], yaml.MapItem{
Key: "role",
slashpai marked this conversation as resolved.
Show resolved Hide resolved
Value: strings.ToLower(config.Role),
})

if config.FollowRedirects != nil {
configs[i] = append(configs[i], yaml.MapItem{
Key: "follow_redirects",
Value: config.FollowRedirects,
})
}

if config.EnableHTTP2 != nil {
configs[i] = append(configs[i], yaml.MapItem{
Key: "enable_http2",
Value: config.EnableHTTP2,
})
}

if config.TLSConfig != nil {
configs[i] = addSafeTLStoYaml(configs[i], sc.GetNamespace(), *config.TLSConfig)
}

if config.Port != nil {
configs[i] = append(configs[i], yaml.MapItem{
Key: "port",
Value: config.Port,
})
}

if config.RefreshInterval != nil {
configs[i] = append(configs[i], yaml.MapItem{
Key: "refresh_interval",
Value: config.RefreshInterval,
})
}
}
cfg = append(cfg, yaml.MapItem{
Key: "hetzner_sd_configs",
Value: configs,
})
}

if sc.Spec.MetricRelabelConfigs != nil {
cfg = append(cfg, yaml.MapItem{Key: "metric_relabel_configs", Value: generateRelabelConfig(labeler.GetRelabelingConfigs(sc.TypeMeta, sc.ObjectMeta, sc.Spec.MetricRelabelConfigs))})
}
Expand Down