mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-24 21:24:05 -08:00
Fix k8s target discovery when not running inside k8s
When using Kubernetes service discovery on a Prometheus instance that's not running inside Kubernetes, the creation of the service discovery fails with a "no such file or directory" error as the special `/var/run/secrets/kubernetes.io/serviceaccount/namespace` file is not there. This commit moves the code that reads this file into the if-branch where no `APIServer.URL` is given (that one basically makes Prometheus assume it is running inside of a Kubernetes cluster). Signed-off-by: Georg Gadinger <nilsding@nilsding.org>
This commit is contained in:
parent
3c9faa25bf
commit
4663f814d6
|
@ -263,7 +263,7 @@ func (d *Discovery) getNamespaces() []string {
|
|||
return []string{apiv1.NamespaceAll}
|
||||
}
|
||||
|
||||
if includeOwnNamespace {
|
||||
if includeOwnNamespace && d.ownNamespace != "" {
|
||||
return append(namespaces, d.ownNamespace)
|
||||
}
|
||||
|
||||
|
@ -276,8 +276,9 @@ func New(l log.Logger, conf *SDConfig) (*Discovery, error) {
|
|||
l = log.NewNopLogger()
|
||||
}
|
||||
var (
|
||||
kcfg *rest.Config
|
||||
err error
|
||||
kcfg *rest.Config
|
||||
err error
|
||||
ownNamespace string
|
||||
)
|
||||
if conf.KubeConfig != "" {
|
||||
kcfg, err = clientcmd.BuildConfigFromFlags("", conf.KubeConfig)
|
||||
|
@ -291,6 +292,13 @@ func New(l log.Logger, conf *SDConfig) (*Discovery, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ownNamespaceContents, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not determine the pod's namespace: %w", err)
|
||||
}
|
||||
ownNamespace = string(ownNamespaceContents)
|
||||
|
||||
level.Info(l).Log("msg", "Using pod service account via in-cluster config")
|
||||
} else {
|
||||
rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "kubernetes_sd")
|
||||
|
@ -310,11 +318,6 @@ func New(l log.Logger, conf *SDConfig) (*Discovery, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
ownNamespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not determine the pod's namespace: %w", err)
|
||||
}
|
||||
|
||||
return &Discovery{
|
||||
client: c,
|
||||
logger: l,
|
||||
|
@ -322,7 +325,7 @@ func New(l log.Logger, conf *SDConfig) (*Discovery, error) {
|
|||
namespaceDiscovery: &conf.NamespaceDiscovery,
|
||||
discoverers: make([]discovery.Discoverer, 0),
|
||||
selectors: mapSelector(conf.Selectors),
|
||||
ownNamespace: string(ownNamespace),
|
||||
ownNamespace: ownNamespace,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue