@@ -3,15 +3,16 @@ package common
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "github.com/litmuschaos/litmus-go/pkg/cerrors"
7
- "github.com/palantir/stacktrace"
8
6
"math/rand"
9
7
"os"
10
8
"os/exec"
11
9
"strconv"
12
10
"strings"
13
11
"time"
14
12
13
+ "github.com/litmuschaos/litmus-go/pkg/cerrors"
14
+ "github.com/palantir/stacktrace"
15
+
15
16
"github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1"
16
17
"github.com/litmuschaos/litmus-go/pkg/clients"
17
18
"github.com/litmuschaos/litmus-go/pkg/log"
@@ -25,7 +26,7 @@ import (
25
26
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
27
)
27
28
28
- //DeletePod deletes the specified pod and wait until it got terminated
29
+ // DeletePod deletes the specified pod and wait until it got terminated
29
30
func DeletePod (podName , podLabel , namespace string , timeout , delay int , clients clients.ClientSets ) error {
30
31
31
32
if err := clients .KubeClient .CoreV1 ().Pods (namespace ).Delete (context .Background (), podName , v1.DeleteOptions {}); err != nil {
@@ -35,7 +36,7 @@ func DeletePod(podName, podLabel, namespace string, timeout, delay int, clients
35
36
return waitForPodTermination (podLabel , namespace , timeout , delay , clients )
36
37
}
37
38
38
- //DeleteAllPod deletes all the pods with matching labels and wait until all the pods got terminated
39
+ // DeleteAllPod deletes all the pods with matching labels and wait until all the pods got terminated
39
40
func DeleteAllPod (podLabel , namespace string , timeout , delay int , clients clients.ClientSets ) error {
40
41
41
42
if err := clients .KubeClient .CoreV1 ().Pods (namespace ).DeleteCollection (context .Background (), v1.DeleteOptions {}, v1.ListOptions {LabelSelector : podLabel }); err != nil {
@@ -138,7 +139,7 @@ func VerifyExistanceOfPods(namespace, pods string, clients clients.ClientSets) (
138
139
return true , nil
139
140
}
140
141
141
- //GetPodList check for the availability of the target pod for the chaos execution
142
+ // GetPodList check for the availability of the target pod for the chaos execution
142
143
// if the target pod is not defined it will derive the random target pod list using pod affected percentage
143
144
func GetPodList (targetPods string , podAffPerc int , clients clients.ClientSets , chaosDetails * types.ChaosDetails ) (core_v1.PodList , error ) {
144
145
finalPods := core_v1.PodList {}
@@ -189,7 +190,7 @@ func CheckForAvailabilityOfPod(namespace, name string, clients clients.ClientSet
189
190
return true , nil
190
191
}
191
192
192
- //FilterNonChaosPods remove the chaos pods(operator, runner) for the podList
193
+ // FilterNonChaosPods remove the chaos pods(operator, runner) for the podList
193
194
// it filter when the applabels are not defined and it will select random pods from appns
194
195
func FilterNonChaosPods (ns , labels string , clients clients.ClientSets , chaosDetails * types.ChaosDetails ) (core_v1.PodList , error ) {
195
196
podList , err := clients .KubeClient .CoreV1 ().Pods (ns ).List (context .Background (), v1.ListOptions {LabelSelector : labels })
@@ -294,7 +295,11 @@ func GetTargetPodsWhenTargetPodsENVNotSet(podAffPerc int, clients clients.Client
294
295
if err != nil {
295
296
return finalPods , cerrors.Error {ErrorCode : cerrors .ErrorTypeTargetSelection , Target : fmt .Sprintf ("{podLabel: %s, namespace: %s}" , label , target .Namespace ), Reason : err .Error ()}
296
297
}
297
- finalPods .Items = append (finalPods .Items , pods .Items ... )
298
+ filteredPods , err := filterPodsByOwnerKind (pods .Items , target , clients )
299
+ if err != nil {
300
+ return finalPods , stacktrace .Propagate (err , "could not identify parent type from pod" )
301
+ }
302
+ finalPods .Items = append (finalPods .Items , filteredPods ... )
298
303
}
299
304
}
300
305
}
@@ -310,6 +315,20 @@ func GetTargetPodsWhenTargetPodsENVNotSet(podAffPerc int, clients clients.Client
310
315
return filterPodsByPercentage (finalPods , podAffPerc ), nil
311
316
}
312
317
318
+ func filterPodsByOwnerKind (pods []core_v1.Pod , target types.AppDetails , clients clients.ClientSets ) ([]core_v1.Pod , error ) {
319
+ var filteredPods []core_v1.Pod
320
+ for _ , pod := range pods {
321
+ parentType , _ , err := workloads .GetPodOwnerTypeAndName (& pod , clients .DynamicClient )
322
+ if err != nil {
323
+ return nil , err
324
+ }
325
+ if target .Kind == parentType {
326
+ filteredPods = append (filteredPods , pod )
327
+ }
328
+ }
329
+ return filteredPods , nil
330
+ }
331
+
313
332
func filterPodsByPercentage (finalPods core_v1.PodList , podAffPerc int ) core_v1.PodList {
314
333
finalPods = removeDuplicatePods (finalPods )
315
334
@@ -367,7 +386,7 @@ func GetExperimentPod(name, namespace string, clients clients.ClientSets) (*core
367
386
return pod , nil
368
387
}
369
388
370
- //GetContainerID derive the container id of the application container
389
+ // GetContainerID derive the container id of the application container
371
390
func GetContainerID (appNamespace , targetPod , targetContainer string , clients clients.ClientSets , source string ) (string , error ) {
372
391
373
392
pod , err := clients .KubeClient .CoreV1 ().Pods (appNamespace ).Get (context .Background (), targetPod , v1.GetOptions {})
@@ -391,7 +410,7 @@ func GetContainerID(appNamespace, targetPod, targetContainer string, clients cli
391
410
return containerID , nil
392
411
}
393
412
394
- //GetRuntimeBasedContainerID extract out the container id of the target container based on the container runtime
413
+ // GetRuntimeBasedContainerID extract out the container id of the target container based on the container runtime
395
414
func GetRuntimeBasedContainerID (containerRuntime , socketPath , targetPods , appNamespace , targetContainer string , clients clients.ClientSets , source string ) (string , error ) {
396
415
397
416
var containerID string
0 commit comments