diff --git a/config/config_test.go b/config/config_test.go index 695c0188a..198c67252 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1181,6 +1181,14 @@ var expectedErrors = []struct { filename: "kubernetes_http_config_without_api_server.bad.yml", errMsg: "to use custom HTTP client configuration please provide the 'api_server' URL explicitly", }, + { + filename: "kubernetes_kubeconfig_with_own_namespace.bad.yml", + errMsg: "cannot use 'kubeconfig_file' and 'namespaces.own_namespace' simultaneously", + }, + { + filename: "kubernetes_api_server_with_own_namespace.bad.yml", + errMsg: "cannot use 'api_server' and 'namespaces.own_namespace' simultaneously", + }, { filename: "kubernetes_kubeconfig_with_apiserver.bad.yml", errMsg: "cannot use 'kubeconfig_file' and 'api_server' simultaneously", diff --git a/config/testdata/kubernetes_api_server_with_own_namespace.bad.yml b/config/testdata/kubernetes_api_server_with_own_namespace.bad.yml new file mode 100644 index 000000000..8a7f49b4b --- /dev/null +++ b/config/testdata/kubernetes_api_server_with_own_namespace.bad.yml @@ -0,0 +1,7 @@ +scrape_configs: + - job_name: prometheus + kubernetes_sd_configs: + - role: endpoints + api_server: 'https://localhost:1234' + namespaces: + own_namespace: true diff --git a/config/testdata/kubernetes_kubeconfig_with_own_namespace.bad.yml b/config/testdata/kubernetes_kubeconfig_with_own_namespace.bad.yml new file mode 100644 index 000000000..d6999fcc6 --- /dev/null +++ b/config/testdata/kubernetes_kubeconfig_with_own_namespace.bad.yml @@ -0,0 +1,7 @@ +scrape_configs: + - job_name: prometheus + kubernetes_sd_configs: + - role: endpoints + kubeconfig_file: /home/User1/.kubeconfig + namespaces: + own_namespace: true diff --git a/discovery/kubernetes/kubernetes.go b/discovery/kubernetes/kubernetes.go index 5109b86ae..f935e9400 100644 --- a/discovery/kubernetes/kubernetes.go +++ b/discovery/kubernetes/kubernetes.go @@ -184,6 +184,12 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { if c.APIServer.URL == nil && !reflect.DeepEqual(c.HTTPClientConfig, config.DefaultHTTPClientConfig) { return errors.Errorf("to use custom HTTP client configuration please provide the 'api_server' URL explicitly") } + if c.APIServer.URL != nil && c.NamespaceDiscovery.IncludeOwnNamespace { + return errors.Errorf("cannot use 'api_server' and 'namespaces.own_namespace' simultaneously") + } + if c.KubeConfig != "" && c.NamespaceDiscovery.IncludeOwnNamespace { + return errors.Errorf("cannot use 'kubeconfig_file' and 'namespaces.own_namespace' simultaneously") + } foundSelectorRoles := make(map[Role]struct{}) allowedSelectors := map[Role][]string{ @@ -276,8 +282,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 +298,18 @@ func New(l log.Logger, conf *SDConfig) (*Discovery, error) { if err != nil { return nil, err } + + if conf.NamespaceDiscovery.IncludeOwnNamespace { + 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) + } + if len(ownNamespaceContents) == 0 { + return nil, errors.New("could not read own namespace name (empty file)") + } + 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 +329,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 +336,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 }