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

[Bug] K8s API authentication failing for pod #1

Open
kaiwalyakoparkar opened this issue Mar 30, 2024 · 2 comments
Open

[Bug] K8s API authentication failing for pod #1

kaiwalyakoparkar opened this issue Mar 30, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@kaiwalyakoparkar
Copy link
Owner

Project Idea: “Pod-logger” - Simple application which uses k8s api to fetch the logs from the pods in the cluster and show it on a simple ui dashboard

What I am trying to do:

Deploying podlogger as a container in a pod and trying to access k8s api through this container/pod. (So that I can get logs of any pod in cluster)

Things I tried:

Creating a sh file with all the environments and simple curl command

  • Works if you exec in the container and run it manually but fails to curl if tried to run via Dockerfile
    Based on above, I add the environments to docker file this time and added the curl command inside the docker file args/cmd to see if it runs as the container is running.
  • It fails without outputting any type of logs so I am confused if it’s running or not
    I created a separate service account with view access to logs and linked it to the pod, still fails to curl the k8s api - Returns the Json with status: failure

Contexts to understand the code:

func getStatus() string {
- Here I am trying to invoke the shell script using the api endpoint, still fails
func getEnvs() string {
- This endpoint was to see if there is an error with env loading but that doesn’t seem to be an issue, last I checked all envs I needed were loaded
func getLogs() string {
- This is the curl command I am trying to run through this endpoint

@kaiwalyakoparkar kaiwalyakoparkar added the bug Something isn't working label Mar 30, 2024
@Revolyssup
Copy link

Revolyssup commented Mar 30, 2024

  1. Dont expand env variables in exec.Command like {TOKEN}. instead:
  2. This will still cause error because your TOKEN env variable is just the path and not the actual token. Read that file and pass it
	cacert := os.Getenv("CACERT")
	tokenPath := os.Getenv("TOKEN")
	apiserver := os.Getenv("APISERVER")
	tokenFile, err := os.Open(tokenPath)
	if err != nil {
		fmt.Println("could not open token file: ", err)
		return ""
	}
	tokenData, err := io.ReadAll(tokenFile)
	if err != nil {
		fmt.Println("could not read token file: ", err)
		return ""
	}
	output := string(out)
	cmd = exec.Command("curl", "--cacert", cacert, "--header", "Authorization: Bearer "+string(tokenData), apiserver+"/api")

@kaiwalyakoparkar
Copy link
Owner Author

  1. Dont expand env variables in exec.Command like {TOKEN}. instead:
  2. This will still cause error because your TOKEN env variable is just the path and not the actual token. Read that file and pass it
	cacert := os.Getenv("CACERT")
	tokenPath := os.Getenv("TOKEN")
	apiserver := os.Getenv("APISERVER")
	tokenFile, err := os.Open(tokenPath)
	if err != nil {
		fmt.Println("could not open token file: ", err)
		return ""
	}
	tokenData, err := io.ReadAll(tokenFile)
	if err != nil {
		fmt.Println("could not read token file: ", err)
		return ""
	}
	output := string(out)
	cmd = exec.Command("curl", "--cacert", cacert, "--header", "Authorization: Bearer "+string(tokenData), apiserver+"/api")

Thank you so much @Revolyssup I will try this :D

@kaiwalyakoparkar kaiwalyakoparkar self-assigned this Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants