@@ -161,6 +161,39 @@ func TestPatchPodSpec(t *testing.T) {
161161 },
162162 },
163163 },
164+ {
165+ name : "can patch image" ,
166+ podspec : & corev1.PodSpec {
167+ Containers : []corev1.Container {
168+ {
169+ Name : "container-0" ,
170+ Image : "alpine:a" ,
171+ Command : []string {
172+ "echo hello world" ,
173+ },
174+ },
175+ },
176+ },
177+ patch : & corev1.PodSpec {
178+ Containers : []corev1.Container {
179+ {
180+ Name : "container-0" ,
181+ Image : "alpine:b" ,
182+ },
183+ },
184+ },
185+ want : & corev1.PodSpec {
186+ Containers : []corev1.Container {
187+ {
188+ Name : "container-0" ,
189+ Image : "alpine:b" ,
190+ Command : []string {
191+ "echo hello world" ,
192+ },
193+ },
194+ },
195+ },
196+ },
164197 }
165198
166199 for _ , test := range cases {
@@ -882,6 +915,73 @@ func TestProhibitKubernetesPlugin(t *testing.T) {
882915 require .Error (t , err )
883916}
884917
918+ func TestCustomImageSyntax_pluginTakesTopPriority (t * testing.T ) {
919+ t .Parallel ()
920+
921+ pluginsYAML := `- github.com/buildkite-plugins/kubernetes-buildkite-plugin:
922+ podSpecPatch:
923+ containers:
924+ - name: container-0
925+ image: "x-image:plugin"`
926+
927+ pluginsJSON , err := yaml .YAMLToJSONStrict ([]byte (pluginsYAML ))
928+ require .NoError (t , err )
929+
930+ job := & api.AgentJob {
931+ ID : "abc" ,
932+ Env : map [string ]string {
933+ "BUILDKITE_PLUGINS" : string (pluginsJSON ),
934+ "BUILDKITE_IMAGE" : "x-image:job" ,
935+ },
936+ }
937+ sjob := & api.AgentScheduledJob {
938+ AgentQueryRules : []string {"queue=kubernetes" },
939+ }
940+ worker := New (zaptest .NewLogger (t ), nil , nil , Config {
941+ Image : "buildkite/agent:latest" ,
942+ })
943+ inputs , err := worker .ParseJob (job , sjob )
944+ require .NoError (t , err )
945+ kjob , err := worker .Build (& corev1.PodSpec {}, false , inputs )
946+ require .NoError (t , err )
947+
948+ commandContainer := findContainer (t , kjob .Spec .Template .Spec .Containers , CommandContainerName )
949+ require .Equal (t , "x-image:plugin" , commandContainer .Image )
950+ }
951+
952+ // Job level image syntax takes priority over controller setting
953+ func TestCustomImageSyntax_jobLevelImagePriority (t * testing.T ) {
954+ t .Parallel ()
955+
956+ job := & api.AgentJob {
957+ ID : "abc" ,
958+ Env : map [string ]string {
959+ "BUILDKITE_IMAGE" : "x-image:job" ,
960+ },
961+ }
962+ sjob := & api.AgentScheduledJob {
963+ AgentQueryRules : []string {"queue=kubernetes" },
964+ }
965+ worker := New (zaptest .NewLogger (t ), nil , nil , Config {
966+ Image : "buildkite/agent:latest" ,
967+ PodSpecPatch : & corev1.PodSpec {
968+ Containers : []corev1.Container {
969+ {
970+ Name : "container-0" ,
971+ Image : "alpine:controller" ,
972+ },
973+ },
974+ },
975+ })
976+ inputs , err := worker .ParseJob (job , sjob )
977+ require .NoError (t , err )
978+ kjob , err := worker .Build (& corev1.PodSpec {}, false , inputs )
979+ require .NoError (t , err )
980+
981+ commandContainer := findContainer (t , kjob .Spec .Template .Spec .Containers , CommandContainerName )
982+ require .Equal (t , "x-image:job" , commandContainer .Image )
983+ }
984+
885985func TestImagePullPolicies (t * testing.T ) {
886986 t .Parallel ()
887987
0 commit comments