@@ -98,6 +98,222 @@ func TestPlugin_executeK8sPrimaryRolloutStage(t *testing.T) {
9898 assert .Equal (t , "simple" , service .GetName ())
9999}
100100
101+ func TestPlugin_executeK8sPrimaryRolloutStage_withIstio (t * testing.T ) {
102+ t .Parallel ()
103+
104+ ctx := t .Context ()
105+
106+ // initialize tool registry
107+ testRegistry := toolregistrytest .NewTestToolRegistry (t )
108+
109+ // read the application config from the example file
110+ appCfg := sdk .LoadApplicationConfigForTest [kubeConfigPkg.KubernetesApplicationSpec ](t , filepath .Join ("testdata" , "primary_rollout_istio" , "app.pipecd.yaml" ), "kubernetes" )
111+
112+ // initialize deploy target config and dynamic client for assertions with envtest
113+ dtConfig , dynamicClient := setupTestDeployTargetConfigAndDynamicClient (t )
114+
115+ // install istio
116+ installIstioCRDs (t , dtConfig )
117+
118+ // execute k8s sync stage
119+ {
120+ input := & sdk.ExecuteStageInput [kubeConfigPkg.KubernetesApplicationSpec ]{
121+ Request : sdk.ExecuteStageRequest [kubeConfigPkg.KubernetesApplicationSpec ]{
122+ StageName : "K8S_SYNC" ,
123+ StageConfig : []byte (`` ),
124+ TargetDeploymentSource : sdk.DeploymentSource [kubeConfigPkg.KubernetesApplicationSpec ]{
125+ ApplicationDirectory : filepath .Join ("testdata" , "primary_rollout_istio" ),
126+ CommitHash : "0123456789" ,
127+ ApplicationConfig : appCfg ,
128+ ApplicationConfigFilename : "app.pipecd.yaml" ,
129+ },
130+ Deployment : sdk.Deployment {
131+ PipedID : "piped-id" ,
132+ ApplicationID : "app-id" ,
133+ },
134+ },
135+ Client : sdk .NewClient (nil , "kubernetes" , "" , "" , logpersistertest .NewTestLogPersister (t ), testRegistry ),
136+ Logger : zaptest .NewLogger (t ),
137+ }
138+
139+ plugin := & Plugin {}
140+
141+ status := plugin .executeK8sSyncStage (ctx , input , []* sdk.DeployTarget [kubeConfigPkg.KubernetesDeployTargetConfig ]{
142+ {
143+ Name : "default" ,
144+ Config : * dtConfig ,
145+ },
146+ })
147+
148+ assert .Equal (t , sdk .StageStatusSuccess , status )
149+
150+ // check the existance of deployment, service, virutal service
151+ _ , err := dynamicClient .Resource (schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "deployments" }).Namespace ("default" ).Get (ctx , "traffic-test" , metav1.GetOptions {})
152+ assert .NoError (t , err )
153+ _ , err = dynamicClient .Resource (schema.GroupVersionResource {Group : "" , Version : "v1" , Resource : "services" }).Namespace ("default" ).Get (ctx , "traffic-test" , metav1.GetOptions {})
154+ assert .NoError (t , err )
155+ verifyVirtualServiceRouting (t , dynamicClient , "traffic-test-vs" , []expectedRoute {
156+ {
157+ host : "traffic-test" ,
158+ subset : "primary" ,
159+ weight : 100 ,
160+ },
161+ })
162+ }
163+
164+ // execute canary stage
165+ {
166+ input := & sdk.ExecuteStageInput [kubeConfigPkg.KubernetesApplicationSpec ]{
167+ Request : sdk.ExecuteStageRequest [kubeConfigPkg.KubernetesApplicationSpec ]{
168+ StageName : "K8S_CANARY_ROLLOUT" ,
169+ StageConfig : []byte (`{"replicas": "100%"}` ),
170+ TargetDeploymentSource : sdk.DeploymentSource [kubeConfigPkg.KubernetesApplicationSpec ]{
171+ ApplicationDirectory : filepath .Join ("testdata" , "primary_rollout_istio" ),
172+ CommitHash : "0123456789" ,
173+ ApplicationConfig : appCfg ,
174+ ApplicationConfigFilename : "app.pipecd.yaml" ,
175+ },
176+ Deployment : sdk.Deployment {
177+ PipedID : "piped-id" ,
178+ ApplicationID : "app-id" ,
179+ },
180+ },
181+ Client : sdk .NewClient (nil , "kubernetes" , "" , "" , logpersistertest .NewTestLogPersister (t ), testRegistry ),
182+ Logger : zaptest .NewLogger (t ),
183+ }
184+
185+ plugin := & Plugin {}
186+
187+ status := plugin .executeK8sCanaryRolloutStage (ctx , input , []* sdk.DeployTarget [kubeConfigPkg.KubernetesDeployTargetConfig ]{
188+ {
189+ Name : "default" ,
190+ Config : * dtConfig ,
191+ },
192+ })
193+
194+ assert .Equal (t , sdk .StageStatusSuccess , status )
195+
196+ // check the existance of deployment, service, virutal service
197+ _ , err := dynamicClient .Resource (schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "deployments" }).Namespace ("default" ).Get (ctx , "traffic-test" , metav1.GetOptions {})
198+ assert .NoError (t , err )
199+ _ , err = dynamicClient .Resource (schema.GroupVersionResource {Group : "" , Version : "v1" , Resource : "services" }).Namespace ("default" ).Get (ctx , "traffic-test" , metav1.GetOptions {})
200+ assert .NoError (t , err )
201+ _ , err = dynamicClient .Resource (schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "deployments" }).Namespace ("default" ).Get (ctx , "traffic-test-canary" , metav1.GetOptions {})
202+ assert .NoError (t , err )
203+ verifyVirtualServiceRouting (t , dynamicClient , "traffic-test-vs" , []expectedRoute {
204+ {
205+ host : "traffic-test" ,
206+ subset : "primary" ,
207+ weight : 100 ,
208+ },
209+ })
210+ }
211+
212+ // execute traffic routing stage
213+ {
214+ input := & sdk.ExecuteStageInput [kubeConfigPkg.KubernetesApplicationSpec ]{
215+ Request : sdk.ExecuteStageRequest [kubeConfigPkg.KubernetesApplicationSpec ]{
216+ StageName : "K8S_TRAFFIC_ROUTING" ,
217+ StageConfig : []byte (`{"canary": "30%"}` ),
218+ TargetDeploymentSource : sdk.DeploymentSource [kubeConfigPkg.KubernetesApplicationSpec ]{
219+ ApplicationDirectory : filepath .Join ("testdata" , "primary_rollout_istio" ),
220+ CommitHash : "0123456789" ,
221+ ApplicationConfig : appCfg ,
222+ ApplicationConfigFilename : "app.pipecd.yaml" ,
223+ },
224+ Deployment : sdk.Deployment {
225+ PipedID : "piped-id" ,
226+ ApplicationID : "app-id" ,
227+ },
228+ },
229+ Client : sdk .NewClient (nil , "kubernetes" , "" , "" , logpersistertest .NewTestLogPersister (t ), testRegistry ),
230+ Logger : zaptest .NewLogger (t ),
231+ }
232+
233+ plugin := & Plugin {}
234+
235+ status := plugin .executeK8sTrafficRoutingStage (ctx , input , []* sdk.DeployTarget [kubeConfigPkg.KubernetesDeployTargetConfig ]{
236+ {
237+ Name : "default" ,
238+ Config : * dtConfig ,
239+ },
240+ })
241+
242+ assert .Equal (t , sdk .StageStatusSuccess , status )
243+
244+ // check the existance of deployment, service, virutal service
245+ _ , err := dynamicClient .Resource (schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "deployments" }).Namespace ("default" ).Get (ctx , "traffic-test" , metav1.GetOptions {})
246+ assert .NoError (t , err )
247+ _ , err = dynamicClient .Resource (schema.GroupVersionResource {Group : "" , Version : "v1" , Resource : "services" }).Namespace ("default" ).Get (ctx , "traffic-test" , metav1.GetOptions {})
248+ assert .NoError (t , err )
249+ _ , err = dynamicClient .Resource (schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "deployments" }).Namespace ("default" ).Get (ctx , "traffic-test-canary" , metav1.GetOptions {})
250+ assert .NoError (t , err )
251+ verifyVirtualServiceRouting (t , dynamicClient , "traffic-test-vs" , []expectedRoute {
252+ {
253+ host : "traffic-test" ,
254+ subset : "primary" ,
255+ weight : 70 ,
256+ },
257+ {
258+ host : "traffic-test" ,
259+ subset : "canary" ,
260+ weight : 30 ,
261+ },
262+ })
263+ }
264+
265+ // execute primary stage
266+ input := & sdk.ExecuteStageInput [kubeConfigPkg.KubernetesApplicationSpec ]{
267+ Request : sdk.ExecuteStageRequest [kubeConfigPkg.KubernetesApplicationSpec ]{
268+ StageName : "K8S_PRIMARY_ROLLOUT" ,
269+ StageConfig : []byte (`{}` ),
270+ TargetDeploymentSource : sdk.DeploymentSource [kubeConfigPkg.KubernetesApplicationSpec ]{
271+ ApplicationDirectory : filepath .Join ("testdata" , "primary_rollout_istio" ),
272+ CommitHash : "0123456789" ,
273+ ApplicationConfig : appCfg ,
274+ ApplicationConfigFilename : "app.pipecd.yaml" ,
275+ },
276+ Deployment : sdk.Deployment {
277+ PipedID : "piped-id" ,
278+ ApplicationID : "app-id" ,
279+ },
280+ },
281+ Client : sdk .NewClient (nil , "kubernetes" , "" , "" , logpersistertest .NewTestLogPersister (t ), testRegistry ),
282+ Logger : zaptest .NewLogger (t ),
283+ }
284+
285+ plugin := & Plugin {}
286+
287+ status := plugin .executeK8sPrimaryRolloutStage (ctx , input , []* sdk.DeployTarget [kubeConfigPkg.KubernetesDeployTargetConfig ]{
288+ {
289+ Name : "default" ,
290+ Config : * dtConfig ,
291+ },
292+ })
293+
294+ assert .Equal (t , sdk .StageStatusSuccess , status )
295+
296+ // check the existance of deployment, service, virutal service
297+ _ , err := dynamicClient .Resource (schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "deployments" }).Namespace ("default" ).Get (ctx , "traffic-test" , metav1.GetOptions {})
298+ assert .NoError (t , err )
299+ _ , err = dynamicClient .Resource (schema.GroupVersionResource {Group : "" , Version : "v1" , Resource : "services" }).Namespace ("default" ).Get (ctx , "traffic-test" , metav1.GetOptions {})
300+ assert .NoError (t , err )
301+ _ , err = dynamicClient .Resource (schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "deployments" }).Namespace ("default" ).Get (ctx , "traffic-test-canary" , metav1.GetOptions {})
302+ assert .NoError (t , err )
303+ verifyVirtualServiceRouting (t , dynamicClient , "traffic-test-vs" , []expectedRoute {
304+ {
305+ host : "traffic-test" ,
306+ subset : "primary" ,
307+ weight : 70 ,
308+ },
309+ {
310+ host : "traffic-test" ,
311+ subset : "canary" ,
312+ weight : 30 ,
313+ },
314+ })
315+ }
316+
101317func TestPlugin_executeK8sPrimaryRolloutStage_withPrune (t * testing.T ) {
102318 t .Parallel ()
103319
0 commit comments