1818package org .apache .openwhisk .core .containerpool .kubernetes .test
1919
2020import io .fabric8 .kubernetes .api .model .policy .PodDisruptionBudgetBuilder
21- import io .fabric8 .kubernetes .api .model .{IntOrString , LabelSelectorBuilder , Pod }
21+ import io .fabric8 .kubernetes .api .model .{
22+ EnvVar ,
23+ EnvVarSource ,
24+ IntOrString ,
25+ LabelSelectorBuilder ,
26+ ObjectFieldSelector ,
27+ Pod
28+ }
2229import io .fabric8 .kubernetes .client .utils .Serialization
2330import org .apache .openwhisk .common .{ConfigMapValue , TransactionId }
2431import org .apache .openwhisk .core .containerpool .kubernetes .{
@@ -46,20 +53,33 @@ class WhiskPodBuilderTests extends FlatSpec with Matchers with KubeClientSupport
4653
4754 behavior of " WhiskPodBuilder"
4855
56+ def config (configMap : Option [ConfigMapValue ] = None , affinity : Option [KubernetesInvokerNodeAffinity ] = None ) =
57+ KubernetesClientConfig (
58+ KubernetesClientTimeoutConfig (1 .seconds, 2 .seconds),
59+ affinity.getOrElse(KubernetesInvokerNodeAffinity (false , " k" , " v" )),
60+ false ,
61+ None ,
62+ configMap,
63+ Some (KubernetesCpuScalingConfig (300 , 3 .MB , 1000 )),
64+ false ,
65+ Some (Map (" POD_UID" -> " metadata.uid" )))
66+
4967 it should " build a new pod" in {
50- val builder = new WhiskPodBuilder (kubeClient, affinity)
51- assertPodSettings(builder)
68+ val c = config()
69+ val builder = new WhiskPodBuilder (kubeClient, c)
70+ assertPodSettings(builder, c)
5271 }
5372 it should " build set cpu scaled based on memory, if enabled in configuration" in {
54- val builder = new WhiskPodBuilder (kubeClient, affinity)
5573 val config = KubernetesClientConfig (
5674 KubernetesClientTimeoutConfig (1 .second, 1 .second),
5775 KubernetesInvokerNodeAffinity (false , " k" , " v" ),
5876 true ,
5977 None ,
6078 None ,
6179 Some (KubernetesCpuScalingConfig (300 , 3 .MB , 1000 )),
62- false )
80+ false ,
81+ None )
82+ val builder = new WhiskPodBuilder (kubeClient, config)
6383
6484 val (pod, _) = builder.buildPodSpec(name, testImage, 2 .MB , Map (" foo" -> " bar" ), Map (" fooL" -> " barV" ), config)
6585 withClue(Serialization .asYaml(pod)) {
@@ -89,7 +109,8 @@ class WhiskPodBuilderTests extends FlatSpec with Matchers with KubeClientSupport
89109 None ,
90110 None ,
91111 None ,
92- false )
112+ false ,
113+ None )
93114 val (pod4, _) = builder.buildPodSpec(name, testImage, 7 .MB , Map (" foo" -> " bar" ), Map (" fooL" -> " barV" ), config2)
94115 withClue(Serialization .asYaml(pod4)) {
95116 val c = getActionContainer(pod4)
@@ -121,8 +142,9 @@ class WhiskPodBuilderTests extends FlatSpec with Matchers with KubeClientSupport
121142 | image : "busybox"
122143 |""" .stripMargin
123144
124- val builder = new WhiskPodBuilder (kubeClient, affinity.copy(enabled = false ), Some (ConfigMapValue (template)))
125- val pod = assertPodSettings(builder)
145+ val c = config(Some (ConfigMapValue (template)))
146+ val builder = new WhiskPodBuilder (kubeClient, c)
147+ val pod = assertPodSettings(builder, c)
126148
127149 val ac = getActionContainer(pod)
128150 ac.getSecurityContext.getCapabilities.getDrop.asScala should contain(" TEST_CAP" )
@@ -136,8 +158,9 @@ class WhiskPodBuilderTests extends FlatSpec with Matchers with KubeClientSupport
136158 }
137159
138160 it should " build a pod disruption budget for the pod, if enabled" in {
139- val builder = new WhiskPodBuilder (kubeClient, affinity)
140- assertPodSettings(builder, true )
161+ val c = config()
162+ val builder = new WhiskPodBuilder (kubeClient, c)
163+ assertPodSettings(builder, c)
141164 }
142165
143166 it should " extend existing pod template with affinity" in {
@@ -155,28 +178,24 @@ class WhiskPodBuilderTests extends FlatSpec with Matchers with KubeClientSupport
155178 | values:
156179 | - "test"""" .stripMargin
157180
158- val builder = new WhiskPodBuilder (kubeClient, affinity.copy(enabled = true ), Some (ConfigMapValue (template)))
159- val pod = assertPodSettings(builder)
181+ val c = config(Some (ConfigMapValue (template)), Some (affinity.copy(enabled = true )))
182+ val builder =
183+ new WhiskPodBuilder (kubeClient, c)
184+ val pod = assertPodSettings(builder, c)
160185
161186 val terms =
162187 pod.getSpec.getAffinity.getNodeAffinity.getRequiredDuringSchedulingIgnoredDuringExecution.getNodeSelectorTerms.asScala
163188 terms.exists(_.getMatchExpressions.asScala.exists(_.getKey == " nodelabel" )) shouldBe true
164189 }
165190
166- private def assertPodSettings (builder : WhiskPodBuilder , pdbEnabled : Boolean = false ): Pod = {
167- val config = KubernetesClientConfig (
168- KubernetesClientTimeoutConfig (1 .second, 1 .second),
169- KubernetesInvokerNodeAffinity (false , " k" , " v" ),
170- true ,
171- None ,
172- None ,
173- Some (KubernetesCpuScalingConfig (300 , 3 .MB , 1000 )),
174- pdbEnabled)
191+ private def assertPodSettings (builder : WhiskPodBuilder , config : KubernetesClientConfig ): Pod = {
175192 val labels = Map (" fooL" -> " barV" )
176193 val (pod, pdb) = builder.buildPodSpec(name, testImage, memLimit, Map (" foo" -> " bar" ), labels, config)
177194 withClue(Serialization .asYaml(pod)) {
178195 val c = getActionContainer(pod)
179- c.getEnv.asScala.exists(_.getName == " foo" ) shouldBe true
196+ c.getEnv.asScala.shouldBe(Seq (
197+ new EnvVar (" foo" , " bar" , null ),
198+ new EnvVar (" POD_UID" , null , new EnvVarSource (null , new ObjectFieldSelector (null , " metadata.uid" ), null , null ))))
180199
181200 c.getResources.getLimits.asScala.get(" memory" ).map(_.getAmount) shouldBe Some (" 10Mi" )
182201 c.getResources.getLimits.asScala.get(" cpu" ).map(_.getAmount) shouldBe Some (" 900m" )
@@ -195,7 +214,7 @@ class WhiskPodBuilderTests extends FlatSpec with Matchers with KubeClientSupport
195214 terms.exists(_.getMatchExpressions.asScala.exists(_.getKey == affinity.key)) shouldBe true
196215 }
197216 }
198- if (pdbEnabled) {
217+ if (config. pdbEnabled) {
199218 println(" matching pdb..." )
200219 pdb shouldBe Some (
201220 new PodDisruptionBudgetBuilder ().withNewMetadata
0 commit comments