Skip to content

Commit 651ac64

Browse files
enable ALL for TARGET_CONTAINER
Signed-off-by: MichaelMorris <[email protected]>
1 parent 6992748 commit 651ac64

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

pkg/utils/common/pods.go

+34-20
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package common
33
import (
44
"context"
55
"fmt"
6-
"github.com/litmuschaos/litmus-go/pkg/cerrors"
7-
"github.com/palantir/stacktrace"
86
"math/rand"
97
"os"
108
"os/exec"
119
"strconv"
1210
"strings"
1311
"time"
1412

13+
"github.com/litmuschaos/litmus-go/pkg/cerrors"
14+
"github.com/palantir/stacktrace"
15+
1516
"github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1"
1617
"github.com/litmuschaos/litmus-go/pkg/clients"
1718
"github.com/litmuschaos/litmus-go/pkg/log"
@@ -25,7 +26,7 @@ import (
2526
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
)
2728

28-
//DeletePod deletes the specified pod and wait until it got terminated
29+
// DeletePod deletes the specified pod and wait until it got terminated
2930
func DeletePod(podName, podLabel, namespace string, timeout, delay int, clients clients.ClientSets) error {
3031

3132
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
3536
return waitForPodTermination(podLabel, namespace, timeout, delay, clients)
3637
}
3738

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
3940
func DeleteAllPod(podLabel, namespace string, timeout, delay int, clients clients.ClientSets) error {
4041

4142
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) (
138139
return true, nil
139140
}
140141

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
142143
// if the target pod is not defined it will derive the random target pod list using pod affected percentage
143144
func GetPodList(targetPods string, podAffPerc int, clients clients.ClientSets, chaosDetails *types.ChaosDetails) (core_v1.PodList, error) {
144145
finalPods := core_v1.PodList{}
@@ -189,7 +190,7 @@ func CheckForAvailabilityOfPod(namespace, name string, clients clients.ClientSet
189190
return true, nil
190191
}
191192

192-
//FilterNonChaosPods remove the chaos pods(operator, runner) for the podList
193+
// FilterNonChaosPods remove the chaos pods(operator, runner) for the podList
193194
// it filter when the applabels are not defined and it will select random pods from appns
194195
func FilterNonChaosPods(ns, labels string, clients clients.ClientSets, chaosDetails *types.ChaosDetails) (core_v1.PodList, error) {
195196
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
367368
return pod, nil
368369
}
369370

370-
//GetContainerID derive the container id of the application container
371+
// GetContainerID derive the container id of the application container
371372
func GetContainerID(appNamespace, targetPod, targetContainer string, clients clients.ClientSets, source string) (string, error) {
372373

373374
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
391392
return containerID, nil
392393
}
393394

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
395396
func GetRuntimeBasedContainerID(containerRuntime, socketPath, targetPods, appNamespace, targetContainer string, clients clients.ClientSets, source string) (string, error) {
396397

397398
var containerID string
@@ -543,22 +544,35 @@ func FilterPodsForNodes(targetPodList core_v1.PodList, containerName string) map
543544

544545
for _, pod := range targetPodList.Items {
545546

546-
td := target{
547-
Name: pod.Name,
548-
Namespace: pod.Namespace,
549-
TargetContainer: containerName,
550-
}
547+
var containerNames []string
551548

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)
554558
}
555559

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,
559566
}
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+
562576
}
563577
}
564578
return targets

0 commit comments

Comments
 (0)