1515package forge_test
1616
1717import (
18+ "fmt"
1819 "time"
1920
2021 . "github.com/onsi/ginkgo/v2"
@@ -200,8 +201,9 @@ var _ = Describe("Pod forging", func() {
200201 const saName = "service-account"
201202
202203 var (
203- apiServerSupport forge.APIServerSupportType
204- remote , original * corev1.PodSpec
204+ apiServerSupport forge.APIServerSupportType
205+ remote , original * corev1.PodSpec
206+ homeAPIServerHost , homeAPIServerPort string
205207 )
206208
207209 BeforeEach (func () {
@@ -217,7 +219,7 @@ var _ = Describe("Pod forging", func() {
217219
218220 JustBeforeEach (func () {
219221 original = remote .DeepCopy ()
220- forge .APIServerSupportMutator (apiServerSupport , saName , SASecretRetriever , KubernetesServiceIPGetter )(remote )
222+ forge .APIServerSupportMutator (apiServerSupport , saName , SASecretRetriever , KubernetesServiceIPGetter , homeAPIServerHost , homeAPIServerPort )(remote )
221223 })
222224
223225 When ("API server support is enabled" , func () {
@@ -228,11 +230,12 @@ var _ = Describe("Pod forging", func() {
228230 })
229231
230232 It ("should appropriately mutate the remote containers" , func () {
231- Expect (remote .Containers ).To (Equal (forge .RemoteContainersAPIServerSupport (original .Containers , saName )))
233+ Expect (remote .Containers ).To (Equal (forge .RemoteContainersAPIServerSupport (original .Containers , saName , homeAPIServerHost , homeAPIServerPort )))
232234 })
233235
234236 It ("should appropriately mutate the remote init containers" , func () {
235- Expect (remote .InitContainers ).To (Equal (forge .RemoteContainersAPIServerSupport (original .InitContainers , saName )))
237+ Expect (remote .InitContainers ).To (Equal (forge .RemoteContainersAPIServerSupport (original .InitContainers , saName ,
238+ homeAPIServerHost , homeAPIServerPort )))
236239 })
237240
238241 It ("should appropriately mutate host aliases" , func () {
@@ -241,12 +244,57 @@ var _ = Describe("Pod forging", func() {
241244 }
242245
243246 When ("legacy mode is set" , func () {
244- BeforeEach (func () { apiServerSupport = forge .APIServerSupportLegacy })
247+ BeforeEach (func () {
248+ apiServerSupport = forge .APIServerSupportLegacy
249+ homeAPIServerHost = ""
250+ homeAPIServerPort = ""
251+ })
245252 WhenBody ()
246253 })
247254
248255 When ("token API mode is set" , func () {
249- BeforeEach (func () { apiServerSupport = forge .APIServerSupportTokenAPI })
256+ BeforeEach (func () {
257+ apiServerSupport = forge .APIServerSupportTokenAPI
258+ homeAPIServerHost = ""
259+ homeAPIServerPort = ""
260+ })
261+ WhenBody ()
262+ })
263+ })
264+
265+ When ("API server support is enabled with custom host API server Host & Port" , func () {
266+
267+ WhenBody := func () {
268+ It ("should correctly mutate the volumes" , func () {
269+ Expect (remote .Volumes ).To (Equal (forge .RemoteVolumes (original .Volumes , apiServerSupport ,
270+ func () string { return SASecretRetriever (saName ) })))
271+ })
272+
273+ It ("should appropriately mutate the remote containers" , func () {
274+ Expect (remote .Containers ).To (Equal (forge .RemoteContainersAPIServerSupport (original .Containers , saName , homeAPIServerHost , homeAPIServerPort )))
275+ })
276+
277+ It ("should appropriately mutate the remote init containers" , func () {
278+ Expect (remote .InitContainers ).To (Equal (forge .RemoteContainersAPIServerSupport (original .InitContainers , saName ,
279+ homeAPIServerHost , homeAPIServerPort )))
280+ })
281+ }
282+
283+ When ("legacy mode is set with custom host API server Host & Port" , func () {
284+ BeforeEach (func () {
285+ apiServerSupport = forge .APIServerSupportLegacy
286+ homeAPIServerHost = "custom.apiserver.com"
287+ homeAPIServerPort = "6443"
288+ })
289+ WhenBody ()
290+ })
291+
292+ When ("token API mode is set with custom host API server Host & Port" , func () {
293+ BeforeEach (func () {
294+ apiServerSupport = forge .APIServerSupportTokenAPI
295+ homeAPIServerHost = "custom1.apiserver.com"
296+ homeAPIServerPort = "6443"
297+ })
250298 WhenBody ()
251299 })
252300 })
@@ -392,6 +440,7 @@ var _ = Describe("Pod forging", func() {
392440 Describe ("the RemoteContainersAPIServerSupport function" , func () {
393441 var container corev1.Container
394442 var output []corev1.Container
443+ var homeAPIServerHost , homeAPIServerPort = "" , ""
395444
396445 BeforeEach (func () {
397446 container = corev1.Container {
@@ -410,7 +459,7 @@ var _ = Describe("Pod forging", func() {
410459 })
411460
412461 JustBeforeEach (func () {
413- output = forge .RemoteContainersAPIServerSupport ([]corev1.Container {container }, "service-account-name" )
462+ output = forge .RemoteContainersAPIServerSupport ([]corev1.Container {container }, "service-account-name" , homeAPIServerHost , homeAPIServerPort )
414463 })
415464
416465 It ("should propagate all container values" , func () {
@@ -443,6 +492,62 @@ var _ = Describe("Pod forging", func() {
443492 })
444493 })
445494
495+ Describe ("the RemoteContainersAPIServerSupport function with custom host API server Host & Port" , func () {
496+ var container corev1.Container
497+ var output []corev1.Container
498+ var homeAPIServerHost , homeAPIServerPort = "custom.apiserver.com" , "6443"
499+
500+ BeforeEach (func () {
501+ container = corev1.Container {
502+ Name : "foo" ,
503+ Image : "foo/bar:v0.1-alpha3" ,
504+ Args : []string {"--first" , "--second" },
505+ Ports : []corev1.ContainerPort {{Name : "foo-port" , ContainerPort : 8080 }},
506+ Env : []corev1.EnvVar {
507+ {Name : "ENV_1" , Value : "VALUE_1" },
508+ {Name : "ENV_2" , Value : "VALUE_2" },
509+ {Name : "ENV_3" , ValueFrom : & corev1.EnvVarSource {ConfigMapKeyRef : & corev1.ConfigMapKeySelector {Key : "foo" }}},
510+ {Name : "ENV_4" , ValueFrom : & corev1.EnvVarSource {FieldRef : & corev1.ObjectFieldSelector {FieldPath : "spec.nodeName" }}},
511+ {Name : "ENV_5" , ValueFrom : & corev1.EnvVarSource {FieldRef : & corev1.ObjectFieldSelector {FieldPath : "spec.serviceAccountName" }}},
512+ },
513+ }
514+ })
515+
516+ JustBeforeEach (func () {
517+ output = forge .RemoteContainersAPIServerSupport ([]corev1.Container {container }, "service-account-name" , homeAPIServerHost , homeAPIServerPort )
518+ })
519+
520+ It ("should propagate all container values" , func () {
521+ // Remove the environment variables from the containers, as checked in the test below.
522+ envcleaner := func (c corev1.Container ) corev1.Container {
523+ c .Env = nil
524+ return c
525+ }
526+
527+ Expect (output ).To (HaveLen (1 ))
528+ Expect (envcleaner (output [0 ])).To (Equal (envcleaner (container )))
529+ })
530+
531+ It ("should configure the appropriate environment variables" , func () {
532+ Expect (output ).To (HaveLen (1 ))
533+ Expect (output [0 ].Env ).To (ConsistOf (
534+ corev1.EnvVar {Name : "ENV_1" , Value : "VALUE_1" },
535+ corev1.EnvVar {Name : "ENV_2" , Value : "VALUE_2" },
536+ corev1.EnvVar {Name : "ENV_3" , ValueFrom : & corev1.EnvVarSource {ConfigMapKeyRef : & corev1.ConfigMapKeySelector {Key : "foo" }}},
537+ corev1.EnvVar {Name : "ENV_4" , ValueFrom : & corev1.EnvVarSource {FieldRef : & corev1.ObjectFieldSelector {FieldPath : "spec.nodeName" }}},
538+ corev1.EnvVar {Name : "ENV_5" , Value : "service-account-name" },
539+ corev1.EnvVar {Name : "KUBERNETES_SERVICE_PORT" , Value : homeAPIServerPort },
540+ corev1.EnvVar {Name : "KUBERNETES_SERVICE_HOST" , Value : homeAPIServerHost },
541+ corev1.EnvVar {Name : "KUBERNETES_PORT" , Value : fmt .Sprintf ("tcp://%s:%s" , homeAPIServerHost , homeAPIServerPort )},
542+ corev1.EnvVar {Name : fmt .Sprintf ("KUBERNETES_PORT_%s_TCP" , homeAPIServerPort ),
543+ Value : fmt .Sprintf ("tcp://%s:%s" , homeAPIServerHost , homeAPIServerPort )},
544+ corev1.EnvVar {Name : fmt .Sprintf ("KUBERNETES_PORT_%s_TCP_PROTO" , homeAPIServerPort ), Value : "tcp" },
545+ corev1.EnvVar {Name : fmt .Sprintf ("KUBERNETES_PORT_%s_TCP_PORT" , homeAPIServerPort ), Value : homeAPIServerPort },
546+ corev1.EnvVar {Name : fmt .Sprintf ("KUBERNETES_PORT_%s_TCP_ADDR" , homeAPIServerPort ), Value : homeAPIServerHost },
547+ ))
548+ })
549+ })
550+
446551 Describe ("the RemoteTolerations function" , func () {
447552 var (
448553 included , excluded corev1.Toleration
0 commit comments