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

fix: prometheus input plugin run outside k8s cluster error #11673

Merged
merged 1 commit into from
Aug 17, 2022

Conversation

yahaa
Copy link
Contributor

@yahaa yahaa commented Aug 15, 2022

Required for all PRs

resolves #

The first one, when we run telegra in outside of a kubernetes cluster the line config, err := rest.InClusterConfig() will be always get an error.

The second one, the method of getting rest.Config instance in loadClient function will be cause kubernetes client has error materURL, and this error can be reproduced by using the following UT:

// add this UT into plugins/inputs/prometheus/kubernetes_test.go
func TestLoadClient(t *testing.T) {
	kubeconfig := "/path_to_your_kubeconfig"
	client, err := loadClient(kubeconfig)
	if err != nil {
		t.Errorf("got err: %v", err)
		return
	}

	watcher, err := client.CoreV1().Pods("kube-system").Watch(context.TODO(), metav1.ListOptions{
		LabelSelector: "app=debug",
	})
	if err != nil {
		t.Errorf("watch pod got err: %v", err)
		return
	}
	defer watcher.Stop()
}

this UT can got watch pod got err: Get "http://localhost/api/v1/namespaces/kube-system/pods?labelSelector=app%3Ddebug&watch=true": dial tcp 127.0.0.1:80: connect: connection refused error.

@telegraf-tiger telegraf-tiger bot added the fix pr to fix corresponding bug label Aug 15, 2022
@yahaa yahaa force-pushed the fix-prometheus-input-plugin branch from 3eceac2 to c51bc9c Compare August 15, 2022 12:17
@yahaa
Copy link
Contributor Author

yahaa commented Aug 15, 2022

Hi @sspaink @powersj I was a little busy some time ago, so I closed this pr #9960,

I make a new pr again, please help me to review it.

@powersj
Copy link
Contributor

powersj commented Aug 15, 2022

Hi,

Can you remind me if you filed a bug for this issue?

Also, I understand the only way to try to start monitoring a K8s cluster is if monitor_kubernetes_pods is set to true. If you set that to false, shouldn't that avoid this?

Thanks

@powersj powersj added the waiting for response waiting for response from contributor label Aug 15, 2022
@yahaa
Copy link
Contributor Author

yahaa commented Aug 15, 2022

Hi,

Can you remind me if you filed a bug for this issue?

Also, I understand the only way to try to start monitoring a K8s cluster is if monitor_kubernetes_pods is set to true. If you set that to false, shouldn't that avoid this?

Thanks

In this case such as following input config,the telegraf process is not running properly

[[inputs.prometheus]]
  monitor_kubernetes_pods = true
  kube_config = "/path/to/kubernetes.config"

@telegraf-tiger telegraf-tiger bot removed the waiting for response waiting for response from contributor label Aug 15, 2022
@yahaa
Copy link
Contributor Author

yahaa commented Aug 15, 2022

Hi,
Can you remind me if you filed a bug for this issue?
Also, I understand the only way to try to start monitoring a K8s cluster is if monitor_kubernetes_pods is set to true. If you set that to false, shouldn't that avoid this?
Thanks

In this case such as following input config,the telegraf process is not running properly

[[inputs.prometheus]]
  monitor_kubernetes_pods = true
  kube_config = "/path/to/kubernetes.config"

In my environment, telegraf is running outside the k8s cluster, so I need to specify kubeconfig to run telegraf, and then I get an error.

After analyzing the prometheus input plugin code, I found that the problem occurs in the following two points:

  1. https://github.com/influxdata/telegraf/blob/master/plugins/inputs/prometheus/kubernetes.go#L59 always try to run inCluster mode.
  2. https://github.com/influxdata/telegraf/blob/master/plugins/inputs/prometheus/kubernetes.go#L52 using this way to get kubeconfig will cause this k8s client watch pod error (watch pod got err: Get "http://localhost/api/v1/namespaces/kube-system/pods?labelSelector=app%3Ddebug&watch=true": dial tcp 127.0.0.1:80: connect: connection refused error.)

@yahaa yahaa force-pushed the fix-prometheus-input-plugin branch from c51bc9c to a33c07d Compare August 16, 2022 05:26
@telegraf-tiger
Copy link
Contributor

@yahaa
Copy link
Contributor Author

yahaa commented Aug 17, 2022

Hi @powersj would you please help me to review this pr once again?

@MyaLongmire MyaLongmire requested a review from powersj August 17, 2022 11:22
@powersj powersj added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Aug 17, 2022
@MyaLongmire MyaLongmire merged commit 4f4d168 into influxdata:master Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix pr to fix corresponding bug ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants