actions-runner-controller icon indicating copy to clipboard operation
actions-runner-controller copied to clipboard

Multi-tenancy doesn't work with Github Apps with no explicit Enterprise URL defined.

Open bm1216 opened this issue 4 years ago • 14 comments

Controller Version

623c84f

Helm Chart Version

0.20.2

CertManager Version

1.2.0

Deployment Method

Helm

cert-manager installation

We have installed cert-manager from an official helm chart. Anyhow, certificates aren't really the issue here.

Checks

  • [X] This isn't a question or user support case (For Q&A and community support, go to Discussions. It might also be a good idea to contract with any of contributors and maintainers if your business is so critical and therefore you need priority support
  • [X] I've read releasenotes before submitting this issue and I'm sure it's not due to any recently-introduced backward-incompatible changes
  • [X] My actions-runner-controller version (v0.x.y) does support the feature
  • [X] I've already upgraded ARC (including the CRDs, see charts/actions-runner-controller/docs/UPGRADING.md for details) to the latest and it didn't fix the issue

Resource Definitions

apiVersion: v1
kind: Secret
metadata:
  name: sre-controller
  namespace: nolimits
data:
  github_app_id: ***
  github_app_installation_id: ***
  github_app_private_key: ***
---
apiVersion: actions.summerwind.dev/v1alpha1
kind: RunnerDeployment
metadata:
  name: sre-runner
  namespace: nolimits
spec:
  template:
    spec:
      image: summerwind/actions-runner-dind
      dockerdWithinRunnerContainer: true
      organization: XXX
      labels:
        - k8s
      githubAPICredentialsFrom:
        secretRef:
          name: sre-controller

To Reproduce

1. Create a k8s secret containing the Github App data. Ensure that this secret is different from the one being used as the `controller-manager` secret.
2. Create a runnerdeployment and reference the secret using the `GithubAPICredentialsFrom` field.

Describe the bug

The controller is not able to spawn runner pods for RunnerDeployment Objects (as well as HRAs) with GithubAPICredentialsFrom defined.

It fails due to the error defined here: https://gist.github.com/bm1216/090d015d1fee081857e5f6add01ce936.

This doesn't seem to be a problem with the provided app credentials, as copying and renaming the controller-manager secret and providing it via GithubAPICredentialsFrom also leads to the same error. I can see that its picking up the secret in the GitHubAPICredentialsFrom field as the installation id in the logs matches the secret.

Upon further investigation, the issue seems to be with the URL its using. its using a https://github.com/api/v3 URL as opposed to using https://api.github.com for the token calls. This is odd, considering I haven't describe an enterprise URL at any point during the setup

Describe the expected behavior

The controller should be able to spawn runner pods based on the Custom Resource provided.

Controller Logs

https://gist.github.com/bm1216/090d015d1fee081857e5f6add01ce936

Runner Pod Logs

The runner pod is not able to spawn.

Additional Context

I understand that the Multi-tenancy feature isn't officially released yet. However, we really like the feature and are looking forward to using it ASAP. Following this comment - https://github.com/actions-runner-controller/actions-runner-controller/pull/1371#issuecomment-1210057401, I tried to use the latest image and ran into this issue. Any help would be appreciated. I'm thinking there might be a difference between the default client created by the controller and the client created using the GithubAPICredentialsRef secret.

Thanks for all the work you are doing - we really appreciate it.

bm1216 avatar Aug 30 '22 19:08 bm1216

@bm1216 Hey! Thanks for testing. Could you try restarting ARC by recreating all the ARC pods (not runner pods) after ensuring your secret contains a set of valid credentials? I remember I implemented our multi-tenancy support to cache the github auth config for the lifetime of the respective resource (hra, runnerdeploy, runnerset, etc) so if you had a wrong creds in the secret first and never restarted ARC after fixing the secret, the fix will never take effect.

mumoshu avatar Aug 31 '22 01:08 mumoshu

I've read through the current implementation and was unable to spot any suspicious code. It should just work with the correct credentials.. and that's why I'm wondering if this might be caused by an issue like I explained above.

mumoshu avatar Aug 31 '22 01:08 mumoshu

JFYI https://github.com/actions-runner-controller/actions-runner-controller/blob/623c84fa529b9dad4d6522759d5f730f2374b967/controllers/multi_githubclient.go#L278-L312 is where we create the github client from data provided via GithubAPICredentialsRef.

mumoshu avatar Aug 31 '22 01:08 mumoshu

@mumoshu Thanks for responding! Really appreciate that you looked at this so swiftly.

I have uninstalled and reinstalled the helm chart but am seeing the same issue. I'll try to get to the bottom of it but it's worrying that the issue doesn't crop up using the default cilent by the controller-manager secret.

Thanks again, @mumoshu.

bm1216 avatar Aug 31 '22 09:08 bm1216

I've isolated the issue to this bit of code:

https://github.com/actions-runner-controller/actions-runner-controller/blob/623c84fa529b9dad4d6522759d5f730f2374b967/controllers/multi_githubclient.go#L201-L205

which was added as a part of https://github.com/actions-runner-controller/actions-runner-controller/pull/1725. This adds an enterprise URL to the GitHub client which we don't really need as the GitHubAPICredentialsFrom field generally relates to organizations.

I am testing with a local run and a kind cluster and commenting out those lines have solved the issue :)

Printing out the Github clients in both situations also shows that this is the only difference between the clients. The one without this URL works and the one with this URL fails.

bm1216 avatar Aug 31 '22 12:08 bm1216

I had a look at that PR and I see where it's coming from. However, the github package does a few things depending on whether you have a enterpriseURL defined that's causing issues for us.

https://github.com/actions-runner-controller/actions-runner-controller/blob/623c84fa529b9dad4d6522759d5f730f2374b967/github/github.go#L80-L85

https://github.com/actions-runner-controller/actions-runner-controller/blob/623c84fa529b9dad4d6522759d5f730f2374b967/github/github.go#L98-L103

If the base controller doesn't have an enterpriseURL defined - the secondary clients shouldn't either imo.

bm1216 avatar Aug 31 '22 13:08 bm1216

Tagging @Jalmeida1994 since they primarily worked on the issue being discussed here. Apologies for the sudden barrage of information.

bm1216 avatar Aug 31 '22 13:08 bm1216

Hey @bm1216 and @mumoshu how are you?

Ah I think I understand the issue then! We check if the runner deployments have an EnterpriseURL and if they haven't they get the one in the main controller. What happens when the main controller hasn't got any? Well it tries to get something that doesn't exist and tries to use it as the GitHub URL right? And then 💥

I think this is it right? Really sorry about this issue! I can't believe this passed through me. As I deploy this, in an Enterprise environment and everything went great I didn't even think about this simple use case.

What do you think? Is this the error case here?

Jalmeida1994 avatar Aug 31 '22 13:08 Jalmeida1994

Probably a simple if else to check if the main controller's Enterprise URL isn't nil should fix this right? If everybody agrees with it I can add it really quickly.

Something like:

// Check if EnterpriseURL is set and if main controller has one also.
if conf.EnterpriseURL == "" && c.githubClient.GithubBaseURL != "" {
	// fallback to the controller-wide setting
	conf.EnterpriseURL = c.githubClient.GithubBaseURL
}

Jalmeida1994 avatar Aug 31 '22 13:08 Jalmeida1994

Hi @Jalmeida1994, thanks for looking at this :). I appreciate it.

You've described the issue correctly to my understanding. And that check should work.

bm1216 avatar Aug 31 '22 13:08 bm1216

Probably a simple if else to check if the main controller's Enterprise URL isn't nil should fix this right? If everybody agrees with it I can add it really quickly.

Something like:

// Check if EnterpriseURL is set and if main controller has one also.
if conf.EnterpriseURL == "" && c.githubClient.GithubBaseURL != "" {
	// fallback to the controller-wide setting
	conf.EnterpriseURL = c.githubClient.GithubBaseURL
}

Not sure exactly that would work. Since c.githubClient.GithubBaseURL would never be empty since it gets populated by NewClient().

https://github.com/actions-runner-controller/actions-runner-controller/blob/623c84fa529b9dad4d6522759d5f730f2374b967/github/github.go#L140-L145

bm1216 avatar Aug 31 '22 15:08 bm1216

I think this would be better discussed in its own PR.

bm1216 avatar Aug 31 '22 15:08 bm1216

Ahh you're right! Good catch. Need to check the issue with more time.

Jalmeida1994 avatar Aug 31 '22 20:08 Jalmeida1994

I'm currently in the middle of working on this - I think the best way to do it would be to add an IsEnterprise bool to the GitHub client struct. It solves both problems.

bm1216 avatar Sep 02 '22 14:09 bm1216