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

feat: add support for k8shook pod template #3174

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apis/actions.summerwind.net/v1alpha1/runner_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ type RunnerConfig struct {
// +optional
ContainerMode string `json:"containerMode,omitempty"`

// +optional
PodTemplateFile string `json:"PodTemplateFile,omitempty"`

GitHubAPICredentialsFrom *GitHubAPICredentialsFrom `json:"githubAPICredentialsFrom,omitempty"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ spec:
spec:
description: RunnerSpec defines the desired state of Runner
properties:
PodTemplateFile:
type: string
affinity:
description: Affinity is a group of affinity scheduling rules.
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ spec:
spec:
description: RunnerSpec defines the desired state of Runner
properties:
PodTemplateFile:
type: string
affinity:
description: Affinity is a group of affinity scheduling rules.
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spec:
spec:
description: RunnerSpec defines the desired state of Runner
properties:
PodTemplateFile:
type: string
affinity:
description: Affinity is a group of affinity scheduling rules.
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ spec:
spec:
description: RunnerSetSpec defines the desired state of RunnerSet
properties:
PodTemplateFile:
type: string
containerMode:
type: string
dockerEnabled:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ spec:
spec:
description: RunnerSpec defines the desired state of Runner
properties:
PodTemplateFile:
type: string
affinity:
description: Affinity is a group of affinity scheduling rules.
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ spec:
spec:
description: RunnerSpec defines the desired state of Runner
properties:
PodTemplateFile:
type: string
affinity:
description: Affinity is a group of affinity scheduling rules.
properties:
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/actions.summerwind.dev_runners.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spec:
spec:
description: RunnerSpec defines the desired state of Runner
properties:
PodTemplateFile:
type: string
affinity:
description: Affinity is a group of affinity scheduling rules.
properties:
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/actions.summerwind.dev_runnersets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ spec:
spec:
description: RunnerSetSpec defines the desired state of RunnerSet
properties:
PodTemplateFile:
type: string
containerMode:
type: string
dockerEnabled:
Expand Down
139 changes: 138 additions & 1 deletion controllers/actions.summerwind.net/new_runner_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func newRunnerPod(template corev1.Pod, runnerSpec arcv1alpha1.RunnerConfig, githubBaseURL string, d RunnerPodDefaults) (corev1.Pod, error) {
return newRunnerPodWithContainerMode("", template, runnerSpec, githubBaseURL, d)
return newRunnerPodWithContainerMode(runnerSpec.ContainerMode, template, runnerSpec, githubBaseURL, d)
}

func setEnv(c *corev1.Container, name, value string) {
Expand Down Expand Up @@ -223,6 +223,91 @@ func TestNewRunnerPod(t *testing.T) {
},
}

baseKubernetesMode := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"actions-runner-controller/inject-registration-token": "true",
"actions-runner": "",
},
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
workGenericEphemeralVolume,
{
Name: "runner",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
},
Containers: []corev1.Container{
{
Name: "runner",
Image: "default-runner-image",
Env: []corev1.EnvVar{
{
Name: "RUNNER_ORG",
Value: "",
},
{
Name: "RUNNER_REPO",
Value: "",
},
{
Name: "RUNNER_ENTERPRISE",
Value: "",
},
{
Name: "RUNNER_LABELS",
Value: "",
},
{
Name: "RUNNER_GROUP",
Value: "",
},
{
Name: "DOCKER_ENABLED",
Value: "false",
},
{
Name: "DOCKERD_IN_RUNNER",
Value: "false",
},
{
Name: "GITHUB_URL",
Value: "api.github.com",
},
{
Name: "RUNNER_WORKDIR",
Value: "/runner/_work",
},
{
Name: "RUNNER_EPHEMERAL",
Value: "true",
},
{
Name: "RUNNER_STATUS_UPDATE_HOOK",
Value: "false",
},
{
Name: "GITHUB_ACTIONS_RUNNER_EXTRA_USER_AGENT",
Value: "actions-runner-controller/NA",
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "runner",
MountPath: "/runner",
},
},
ImagePullPolicy: corev1.PullAlways,
SecurityContext: &corev1.SecurityContext{},
},
},
RestartPolicy: corev1.RestartPolicyNever,
},
}

boolPtr := func(v bool) *bool {
return &v
}
Expand Down Expand Up @@ -598,6 +683,57 @@ func TestNewRunnerPod(t *testing.T) {
}
}),
},
{
description: "it should set container hook file as an env var when PodTemplateFile is set",
template: corev1.Pod{
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
workGenericEphemeralVolume,
},
},
},
config: arcv1alpha1.RunnerConfig{
ContainerMode: "kubernetes",
PodTemplateFile: "/templates/example-template.yaml",
},
want: newTestPod(baseKubernetesMode, func(p *corev1.Pod) {
envVars := []corev1.EnvVar{
{
Name: "ACTIONS_RUNNER_CONTAINER_HOOKS",
Value: defaultRunnerHookPath,
},
{
Name: "ACTIONS_RUNNER_REQUIRE_JOB_CONTAINER",
Value: "true",
},
{
Name: "ACTIONS_RUNNER_POD_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
{
Name: "ACTIONS_RUNNER_JOB_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
{
Name: "ACTIONS_RUNNER_REQUIRE_SAME_NODE",
Value: "true",
},
{
Name: "ACTIONS_RUNNER_CONTAINER_HOOK_TEMPLATE",
Value: "/templates/example-template.yaml",
},
}
p.Spec.Containers[0].Env = append(p.Spec.Containers[0].Env, envVars...)
}),
},
}

var (
Expand All @@ -619,6 +755,7 @@ func TestNewRunnerPod(t *testing.T) {
DockerGID: "1234",
UseRunnerStatusUpdateHook: false,
})

require.NoError(t, err)
require.Equal(t, tc.want, got)
})
Expand Down
20 changes: 15 additions & 5 deletions controllers/actions.summerwind.net/runner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
"context"
"errors"
"fmt"
"k8s.io/apimachinery/pkg/api/resource"
"reflect"
"strconv"
"strings"
"time"

"k8s.io/apimachinery/pkg/api/resource"

"github.com/actions/actions-runner-controller/build"
"github.com/actions/actions-runner-controller/hash"
"github.com/go-logr/logr"
Expand Down Expand Up @@ -730,13 +731,13 @@ func mutatePod(pod *corev1.Pod, token string) *corev1.Pod {
return updated
}

func runnerHookEnvs(pod *corev1.Pod) ([]corev1.EnvVar, error) {
func runnerHookEnvs(pod *corev1.Pod, podTemplateFile string) ([]corev1.EnvVar, error) {
isRequireSameNode, err := isRequireSameNode(pod)
if err != nil {
return nil, err
}

return []corev1.EnvVar{
envVars := []corev1.EnvVar{
{
Name: "ACTIONS_RUNNER_CONTAINER_HOOKS",
Value: defaultRunnerHookPath,
Expand Down Expand Up @@ -765,7 +766,16 @@ func runnerHookEnvs(pod *corev1.Pod) ([]corev1.EnvVar, error) {
Name: "ACTIONS_RUNNER_REQUIRE_SAME_NODE",
Value: strconv.FormatBool(isRequireSameNode),
},
}, nil
}

if podTemplateFile != "" {
envVars = append(envVars, corev1.EnvVar{
Name: "ACTIONS_RUNNER_CONTAINER_HOOK_TEMPLATE",
Value: podTemplateFile,
})
}

return envVars, nil
}

func newRunnerPodWithContainerMode(containerMode string, template corev1.Pod, runnerSpec v1alpha1.RunnerConfig, githubBaseURL string, d RunnerPodDefaults) (corev1.Pod, error) {
Expand Down Expand Up @@ -934,7 +944,7 @@ func newRunnerPodWithContainerMode(containerMode string, template corev1.Pod, ru

runnerContainer.Env = append(runnerContainer.Env, env...)
if containerMode == "kubernetes" {
hookEnvs, err := runnerHookEnvs(&template)
hookEnvs, err := runnerHookEnvs(&template, runnerSpec.PodTemplateFile)
if err != nil {
return corev1.Pod{}, err
}
Expand Down