Files
actions-runner-controller/controllers/actions.github.com/helpers_bench_test.go
T
Nikola Jokic e481f69cff Replace integrity-hash annotations with explicit API state fields
The `actions.github.com/integrity-hash` annotation was used as an opaque
fingerprint to detect spec drift across AutoscalingRunnerSet,
EphemeralRunnerSet and the listener resources. Hashes are brittle: they
change whenever unrelated serialization details change, they are invisible
to users, and they are not restart-safe. FNV-32a also carries a real
collision risk, where the consequence is an update silently never applied.

Replace it with explicit, typed state:

- `AutoscalingRunnerSetStatus.ObservedGeneration` drives the Pending phase
  transition via `metadata.generation` instead of an annotation hash.
- `EphemeralRunnerSetSpec.ActionableRevision` and
  `EphemeralRunnerSetStatus.AppliedActionableRevision` form a restart-safe
  applied marker. The revision is bumped by the AutoscalingRunnerSet
  controller when `EphemeralRunnerSpec` changes, and only advanced in status
  after idle/pending runner cleanup succeeds.
- `EphemeralRunnerSetStatus.FinishedRunnerCleanupPatchID` records the
  listener patch ID for which finished runners were reaped, so scale-up is
  suppressed until the listener publishes a fresh desired state. This
  prevents creating a replacement runner for a job that already completed.
- Listener pod recreation compares pod specs semantically instead of
  comparing hash annotations.

Drift detection uses `apiequality.Semantic`, not `cmp` or `reflect`:

- `Semantic.DeepEqual` for the EphemeralRunnerSpec. Most PodSpec collection
  fields carry `omitempty`, so a template containing an explicitly empty
  value (`env: []`) is dropped when the EphemeralRunnerSet is written and
  reads back as nil. A strict comparison reports drift on every reconcile,
  bumping ActionableRevision each time and deleting every idle and pending
  runner, forever. Semantic treats nil and empty as equal, understands
  resource.Quantity, and cannot panic on unexported fields the way cmp can.
  It is also roughly six times cheaper than cmp.Equal on a realistic spec.
- `Semantic.DeepDerivative` for the listener pod, because the live pod
  carries many fields the desired pod never sets (nodeName, dnsPolicy,
  default tolerations, the kube-api-access volume, ...). DeepEqual there
  would spin in a delete/create loop. Container port length is checked
  separately, since ports come from the --listener-metrics-addr flag rather
  than from a resource, so disabling metrics would otherwise leave the port
  on the pod forever.

Drift detection is deliberately not short-circuited on
metadata.generation. Re-registration changes the runner scale set ID
through an annotation, and metadata changes do not bump generation, so a
generation-based shortcut would leave the EphemeralRunnerSet pointing at a
scale set that no longer exists. The measured saving did not justify the
risk.

Additionally:

- Count deleting runners toward the scale-up total so terminating runners
  are not double-replaced.
- Cleanup of finished runners is no longer deferred; failures now surface as
  reconcile errors instead of being logged and swallowed.
- Status patches for the new fields use `RetryOnConflict` against a freshly
  read object.
- Keep merging EphemeralRunnerSet annotations and labels rather than
  overwriting them, so metadata applied by admission webhooks or other
  controllers is preserved. Drift detection compares against the merge
  result so foreign keys cannot cause a permanent patch loop.
- Add unit tests and benchmarks for both drift checks, including a guard
  that fails if the listener comparison is ever tightened to DeepEqual.
- Cover the re-registration path, which previously had no assertion that
  the new runner scale set ID reaches the EphemeralRunnerSet at all.
2026-09-07 22:08:17 +02:00

123 lines
4.5 KiB
Go

package actionsgithubcom
import (
"fmt"
"testing"
"github.com/actions/actions-runner-controller/apis/actions.github.com/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
// benchmarkEphemeralRunnerSet builds a runner set roughly the size of a real
// ARC deployment: a runner container, a dind sidecar, an init container,
// resource limits, volumes and a dozen environment variables.
func benchmarkEphemeralRunnerSet() *v1alpha1.EphemeralRunnerSet {
env := make([]corev1.EnvVar, 0, 12)
for i := range 12 {
env = append(env, corev1.EnvVar{Name: fmt.Sprintf("VAR_%d", i), Value: fmt.Sprintf("value-%d", i)})
}
q := resource.MustParse
return &v1alpha1.EphemeralRunnerSet{
Spec: v1alpha1.EphemeralRunnerSetSpec{
Replicas: 10,
PatchID: 7,
EphemeralRunnerSpec: v1alpha1.EphemeralRunnerSpec{
GitHubConfigURL: "https://github.com/octo-org",
GitHubConfigSecret: "gh-config",
RunnerScaleSetID: 42,
PodTemplateSpec: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
ServiceAccountName: "runner-sa",
RestartPolicy: corev1.RestartPolicyNever,
NodeSelector: map[string]string{"kubernetes.io/os": "linux", "node.kubernetes.io/pool": "runners"},
Tolerations: []corev1.Toleration{{
Key: "dedicated", Operator: corev1.TolerationOpEqual,
Value: "runners", Effect: corev1.TaintEffectNoSchedule,
}},
Volumes: []corev1.Volume{
{Name: "work", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
{Name: "dind-sock", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
},
InitContainers: []corev1.Container{{
Name: "init-dind", Image: "docker:dind",
Command: []string{"cp"},
Args: []string{"-r", "/usr/local/bin/.", "/dind"},
VolumeMounts: []corev1.VolumeMount{{Name: "dind-sock", MountPath: "/dind"}},
}},
Containers: []corev1.Container{
{
Name: "runner",
Image: "ghcr.io/actions/actions-runner:2.337.0",
Command: []string{"/home/runner/run.sh"},
Env: env,
VolumeMounts: []corev1.VolumeMount{
{Name: "work", MountPath: "/home/runner/_work"},
{Name: "dind-sock", MountPath: "/var/run"},
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{corev1.ResourceCPU: q("500m"), corev1.ResourceMemory: q("1Gi")},
Limits: corev1.ResourceList{corev1.ResourceCPU: q("2"), corev1.ResourceMemory: q("4Gi")},
},
},
{
Name: "dind", Image: "docker:dind", Env: env[:6],
VolumeMounts: []corev1.VolumeMount{{Name: "dind-sock", MountPath: "/var/run"}},
},
},
},
},
},
},
}
}
// BenchmarkEphemeralRunnerSetActionableSpecChanged measures the drift check that
// runs on every AutoscalingRunnerSet reconcile. Reconciles are driven by
// EphemeralRunnerSet status churn via Owns(), so this executes constantly and
// its allocation count feeds directly into controller GC pressure.
//
// For reference on the machine this was written on: Semantic.DeepEqual is around
// 65us/241 allocs, versus 332us/405 allocs for cmp.Equal. If this regresses by an
// order of magnitude, something switched the comparison back to a reflection
// heavy implementation.
func BenchmarkEphemeralRunnerSetActionableSpecChanged(b *testing.B) {
b.Run("no drift", func(b *testing.B) {
current, desired := benchmarkEphemeralRunnerSet(), benchmarkEphemeralRunnerSet()
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if ephemeralRunnerSetActionableSpecChanged(current, desired) {
b.Fatal("expected no drift")
}
}
})
b.Run("drift", func(b *testing.B) {
current, desired := benchmarkEphemeralRunnerSet(), benchmarkEphemeralRunnerSet()
desired.Spec.EphemeralRunnerSpec.PodTemplateSpec.Spec.Containers[0].Image = "ghcr.io/actions/actions-runner:2.338.0"
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if !ephemeralRunnerSetActionableSpecChanged(current, desired) {
b.Fatal("expected drift")
}
}
})
}
// BenchmarkListenerPodSpecRequiresRecreation measures the listener pod drift
// check, which also runs on every AutoscalingListener reconcile.
func BenchmarkListenerPodSpecRequiresRecreation(b *testing.B) {
desired := desiredListenerPod()
live := livePodFromDesired(desired)
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if listenerPodSpecRequiresRecreation(live, desired) {
b.Fatal("expected no recreation")
}
}
}