Skip to content

Workers spawned in kubernetes mode do not inherit service account from runner #2992

Closed

Description

@johnjeffers

What would you like added?

Workers spawned by runners in kubernetes mode should retain the service account of the runner that spawned them. Currently, spawned workers are using the default service account instead of the service account I have assigned to my workers.

Why is this needed?

This breaks IRSA in EKS, which depends on the service account to grant AWS permissions to the pod.

Additional context

It looks like this same issue was mentioned in #2754 but that issue was closed with no clear resolution.

Activity

  1. added
    enhancement
    needs triage
    on Oct 15, 2023
  2. github-actions commented on Oct 15, 2023

    @github-actions
    Contributor

    Hello! Thank you for filing an issue.

    The maintainers will triage your issue shortly.

    In the meantime, please take a look at the troubleshooting guide for bug reports.

    If this is a feature request, please review our contribution guidelines.

  3. nikola-jokic commented on Oct 16, 2023

    @nikola-jokic
    Collaborator

    Hey @johnjeffers,

    I would not agree that the job pods created by the hook should inherit the service account assigned to the runner pod. Job pods should not be an extension of the runner, but rather the container in which the workflow should be executed. The runner needs to have permissions, so it can create the container with appropriate resources.

    However, you can customize your job pods by leveraging hook extensions explained here.

  4. johnjeffers commented on Oct 16, 2023

    @johnjeffers
    Author

    Update 2024-05-08 -- this is out of date now. See below.

    @nikola-jokic thanks, that's the piece of the puzzle I was missing. I was able to get the workers running with the service account that I needed.

    If anyone else runs into this problem, here's how I solved it.

    Create a configmap with a pod template that specifies the serviceAccountName you want the workers to use:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: pod-templates
    data:
      default.yaml: |
        ---
        apiVersion: v1
        kind: PodTemplate
        metadata:
          name: runner-pod-template
          labels:
            app: runner-pod-template
        spec:
          serviceAccountName: gha-runners   # <-- use this serviceaccount
          securityContext:
            fsGroup: 123  # provides access to /home/runner/_work directory in ephemeral volume
    

    In your runner scale set template (in the values file for the helm chart), mount the configmap as a volume, and refer to its location with the ACTIONS_RUNNER_CONTAINER_HOOK_TEMPLATE env var:

    template:
      spec:
        serviceAccountName: gha-runners
        securityContext:
          fsGroup: 123  # provides access to /home/runner/_work directory in ephemeral volume
        containers:
          - name: runner
            image: ghcr.io/actions/actions-runner:latest
            command: ["/home/runner/run.sh"]
            env:
              - name: ACTIONS_RUNNER_REQUIRE_JOB_CONTAINER
                value: "false"
              - name: ACTIONS_RUNNER_CONTAINER_HOOK_TEMPLATE
                value: /home/runner/pod-templates/default.yaml
            volumeMounts:
              - name: pod-templates
                mountPath: /home/runner/pod-templates
                readOnly: true
        volumes:
          - name: pod-templates
            configMap:
              name: pod-templates
    
  5. nikola-jokic commented on Oct 16, 2023

    @nikola-jokic
    Collaborator

    Thank you for providing a write-up!

  6. ArnaldoMoralesSirt commented on Apr 27, 2024

    @ArnaldoMoralesSirt

    Hi, I am trying to applying the solution provided by @johnjeffers but without sucess. I am trying to run a terraform container as a job pod, but I always get the "no valid credential sources found" when trying to run a terraform plan.

    I have created the IRSA properly and it works fine when I assign it to a customized pod that I am testing.

    When my runner and job pods are created, I can see that the container and the dind are getting the role properly:

      dind:
        Container ID:
        Image:         docker:dind
    
    (truncated)
    
        Environment:
          AWS_STS_REGIONAL_ENDPOINTS:   regional
          AWS_DEFAULT_REGION:           eu-west-1
          AWS_REGION:                   eu-west-1
          AWS_ROLE_ARN:                 arn:aws:iam::XXXXXXXXXXXXX:role/EKSPodsRole
          AWS_WEB_IDENTITY_TOKEN_FILE:  /var/run/secrets/eks.amazonaws.com/serviceaccount/token 
    

    Any idea about how to solve this? Thanks.

  7. johnjeffers commented on May 8, 2024

    @johnjeffers
    Author

    @ArnaldoMoralesSirt Things have changed since I posted that. If you look at the values file for the runner scale set chart, you'll see what your DinD template should look like now.

    https://github.com/actions/actions-runner-controller/blob/master/charts/gha-runner-scale-set/values.yaml#L113

    So you basically just need to copy the format of the DinD template from the values file, but add a line for the serviceaccount that's tied to your IRSA role, plus any other changes you'd like to customize your DinD runner.

    So, you start with this:

    template:
      spec:
        initContainers:
        - name: init-dind-externals
          image: ghcr.io/actions/actions-runner:latest
          command: ["cp", "-r", "-v", "/home/runner/externals/.", "/home/runner/tmpDir/"]
          volumeMounts:
            - name: dind-externals
              mountPath: /home/runner/tmpDir
        containers:
        - name: runner
          image: ghcr.io/actions/actions-runner:latest
          command: ["/home/runner/run.sh"]
          env:
            - name: DOCKER_HOST
              value: unix:///var/run/docker.sock
          volumeMounts:
            - name: work
              mountPath: /home/runner/_work
            - name: dind-sock
              mountPath: /var/run
        - name: dind
          image: docker:dind
          args:
            - dockerd
            - --host=unix:///var/run/docker.sock
            - --group=$(DOCKER_GROUP_GID)
          env:
            - name: DOCKER_GROUP_GID
              value: "123"
          securityContext:
            privileged: true
          volumeMounts:
            - name: work
              mountPath: /home/runner/_work
            - name: dind-sock
              mountPath: /var/run
            - name: dind-externals
              mountPath: /home/runner/externals
        volumes:
        - name: work
          emptyDir: {}
        - name: dind-sock
          emptyDir: {}
        - name: dind-externals
          emptyDir: {}
    

    Add/change what you need, to end up with something like this. Comments inline for anything that was added or changed.

      template:
        spec:
          serviceAccountName: my-serviceaccount  # <--- add your serviceaccount name
          initContainers:
          - name: init-dind-externals
            image: ghcr.io/actions/actions-runner:latest
            command: ["cp", "-r", "-v", "/home/runner/externals/.", "/home/runner/tmpDir/"]
            volumeMounts:
              - name: dind-externals
                mountPath: /home/runner/tmpDir
          containers:
          - name: runner
            image: my-custom-dind-image:1.0  # <--- add your custom DinD image if you want, otherwise use docker:dind
            command: ["/home/runner/run.sh"]
            env:
              - name: DOCKER_HOST
                value: unix:///var/run/docker.sock
              - name: RUNNER_ALLOW_RUNASROOT  # <--- this is a bad idea, but if your DinD image runs a root you need this
                value: "1"
            volumeMounts:
              - name: work
                mountPath: /home/runner/_work
              - name: dind-sock
                mountPath: /var/run
          - name: dind
            image: docker:dind
            args:
              - dockerd
              - --host=unix:///var/run/docker.sock
              - --group=$(DOCKER_GROUP_GID)
            env:
              - name: DOCKER_GROUP_GID
                value: "123"
            securityContext:
              privileged: true
            volumeMounts:
              - name: work
                mountPath: /home/runner/_work
              - name: dind-sock
                mountPath: /var/run
              - name: dind-externals
                mountPath: /home/runner/externals
          volumes:
          - name: work
            emptyDir: {}
          - name: dind-sock
            emptyDir: {}
          - name: dind-externals
            emptyDir: {}
    
  8. NicolasEspiau-stilll commented on May 10, 2024

    @NicolasEspiau-stilll

    hello everyone,

    I have the same issue but in kubernetes mode.

    I would like to automate Helm deployment within my EKS cluster and I struggling because at the helm step the runner launches a workflow pod which has the default service account which is not allowed to interact with the cluster.

    I can't find where I can set the service account for the workflow pods (which I assume are the job pods) in kubernetes mode.

    I've tried the solution with the hook extensions but it seems to be out of date (since 2 days) and indeed it doesn't work.

  9. gdubicki commented on May 22, 2024

    @gdubicki

    I think I am having the same problem as you, @NicolasEspiau-stilll, although it my case it's GKE, but I don't think it matters for now.

    Did you manage to resolve this?

  10. NicolasEspiau-stilll commented on May 23, 2024

    @NicolasEspiau-stilll

    Hello @gdubicki
    Yes I did find a workaround that works well.
    I didn't invent anything, I juste tried something that was mentioned here with the podTemplate and it works even if it's considered out of date.

    That's the only thing that solve my issue so far.

  11. cedef commented on Sep 17, 2024

    @cedef

    @johnjeffers Thank you so much for your solution, you saved my day ;-)
    Just to add my 2 cents, you can also only patch what is in the template using YAML override
    In my case I just needed to override 2 parameters:

    containerMode.type: dind
    template.spec.serviceAccountName: gha-runner
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementneeds triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions