Skip to content

Commit 2ee989b

Browse files
authored
Merge pull request #879 from buildkite/sneha/add-podTemplateName
feat: pass in pod-template-name annotation on jobs built from podTemplates
2 parents 157e442 + 5f2008b commit 2ee989b

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

internal/controller/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
JobURLAnnotation = "buildkite.com/job-url"
2020
PriorityAnnotation = "buildkite.com/job-priority"
2121
PipelineSlugAnnotation = "buildkite.com/pipeline-slug"
22+
PodTemplateAnnotation = "buildkite.com/pod-template-name"
2223
DefaultNamespace = "default"
2324
DefaultImagePullBackOffGracePeriod = 30 * time.Second
2425
DefaultJobCancelCheckerPollInterval = 5 * time.Second

internal/controller/scheduler/scheduler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ func (w *worker) Build(podSpec *corev1.PodSpec, skipCheckout bool, inputs buildI
340340
}
341341
kjob.Annotations[config.PriorityAnnotation] = strconv.Itoa(inputs.priority)
342342

343+
if inputs.k8sPlugin != nil && inputs.k8sPlugin.PodSpec == nil && inputs.k8sPlugin.PodTemplate != "" {
344+
kjob.Annotations[config.PodTemplateAnnotation] = inputs.k8sPlugin.PodTemplate
345+
}
346+
343347
kjob.Spec.Template.Labels = kjob.Labels
344348
kjob.Spec.Template.Annotations = kjob.Annotations
345349
kjob.Spec.BackoffLimit = ptr.To[int32](0)

internal/controller/scheduler/scheduler_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,3 +1854,50 @@ func TestBuildSafeToEvictPluginMetadataOverride(t *testing.T) {
18541854
assert.Equal(t, "true", kjob.Annotations["cluster-autoscaler.kubernetes.io/safe-to-evict"])
18551855
assert.Equal(t, "true", kjob.Spec.Template.Annotations["cluster-autoscaler.kubernetes.io/safe-to-evict"])
18561856
}
1857+
1858+
func TestBuildPodTemplateAnnotation(t *testing.T) {
1859+
t.Parallel()
1860+
1861+
pluginsYAML := `- github.com/buildkite-plugins/kubernetes-buildkite-plugin:
1862+
podTemplate: my-pod-template`
1863+
1864+
pluginsJSON, err := yaml.YAMLToJSONStrict([]byte(pluginsYAML))
1865+
require.NoError(t, err)
1866+
1867+
job := &api.AgentJob{
1868+
ID: "abc",
1869+
Command: "echo hello world",
1870+
Env: map[string]string{"BUILDKITE_PLUGINS": string(pluginsJSON)},
1871+
}
1872+
sjob := &api.AgentScheduledJob{}
1873+
worker := New(slog.Default(), nil, nil, Config{
1874+
Image: "buildkite/agent:latest",
1875+
})
1876+
inputs, err := worker.ParseJob(job, sjob)
1877+
require.NoError(t, err)
1878+
kjob, err := worker.Build(&corev1.PodSpec{}, false, inputs)
1879+
require.NoError(t, err)
1880+
1881+
require.Equal(t, "my-pod-template", kjob.Annotations[config.PodTemplateAnnotation])
1882+
require.Equal(t, "my-pod-template", kjob.Spec.Template.Annotations[config.PodTemplateAnnotation])
1883+
}
1884+
1885+
func TestBuildNoPodTemplateAnnotationWhenAbsent(t *testing.T) {
1886+
t.Parallel()
1887+
1888+
job := &api.AgentJob{
1889+
ID: "abc",
1890+
Command: "echo hello world",
1891+
}
1892+
sjob := &api.AgentScheduledJob{}
1893+
worker := New(slog.Default(), nil, nil, Config{
1894+
Image: "buildkite/agent:latest",
1895+
})
1896+
inputs, err := worker.ParseJob(job, sjob)
1897+
require.NoError(t, err)
1898+
kjob, err := worker.Build(&corev1.PodSpec{}, false, inputs)
1899+
require.NoError(t, err)
1900+
1901+
require.Empty(t, kjob.Annotations[config.PodTemplateAnnotation])
1902+
require.Empty(t, kjob.Spec.Template.Annotations[config.PodTemplateAnnotation])
1903+
}

0 commit comments

Comments
 (0)