@@ -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 })
@@ -367,7 +368,7 @@ func GetExperimentPod(name, namespace string, clients clients.ClientSets) (*core
367
368
return pod , nil
368
369
}
369
370
370
- //GetContainerID derive the container id of the application container
371
+ // GetContainerID derive the container id of the application container
371
372
func GetContainerID (appNamespace , targetPod , targetContainer string , clients clients.ClientSets , source string ) (string , error ) {
372
373
373
374
pod , err := clients .KubeClient .CoreV1 ().Pods (appNamespace ).Get (context .Background (), targetPod , v1.GetOptions {})
@@ -391,7 +392,7 @@ func GetContainerID(appNamespace, targetPod, targetContainer string, clients cli
391
392
return containerID , nil
392
393
}
393
394
394
- //GetRuntimeBasedContainerID extract out the container id of the target container based on the container runtime
395
+ // GetRuntimeBasedContainerID extract out the container id of the target container based on the container runtime
395
396
func GetRuntimeBasedContainerID (containerRuntime , socketPath , targetPods , appNamespace , targetContainer string , clients clients.ClientSets , source string ) (string , error ) {
396
397
397
398
var containerID string
@@ -543,22 +544,35 @@ func FilterPodsForNodes(targetPodList core_v1.PodList, containerName string) map
543
544
544
545
for _ , pod := range targetPodList .Items {
545
546
546
- td := target {
547
- Name : pod .Name ,
548
- Namespace : pod .Namespace ,
549
- TargetContainer : containerName ,
550
- }
547
+ var containerNames []string
551
548
552
- if td .TargetContainer == "" {
553
- td .TargetContainer = pod .Spec .Containers [0 ].Name
549
+ switch containerName {
550
+ case "ALL" :
551
+ for _ , container := range pod .Spec .Containers {
552
+ containerNames = append (containerNames , container .Name )
553
+ }
554
+ case "" :
555
+ containerNames = append (containerNames , pod .Spec .Containers [0 ].Name )
556
+ default :
557
+ containerNames = append (containerNames , containerName )
554
558
}
555
559
556
- if targets [pod .Spec .NodeName ] == nil {
557
- targets [pod .Spec .NodeName ] = & TargetsDetails {
558
- Target : []target {td },
560
+ for _ , targetName := range containerNames {
561
+
562
+ td := target {
563
+ Name : pod .Name ,
564
+ Namespace : pod .Namespace ,
565
+ TargetContainer : targetName ,
559
566
}
560
- } else {
561
- targets [pod .Spec .NodeName ].Target = append (targets [pod .Spec .NodeName ].Target , td )
567
+
568
+ if targets [pod .Spec .NodeName ] == nil {
569
+ targets [pod .Spec .NodeName ] = & TargetsDetails {
570
+ Target : []target {td },
571
+ }
572
+ } else {
573
+ targets [pod .Spec .NodeName ].Target = append (targets [pod .Spec .NodeName ].Target , td )
574
+ }
575
+
562
576
}
563
577
}
564
578
return targets
0 commit comments