Skip to content

Commit c2f8f79

Browse files
Fix consider appKind when filtering target pods (#680)
* Fix consider appKind when filtering target pods Signed-off-by: MichaelMorris <[email protected]> * Implemted review comment Signed-off-by: MichaelMorris <[email protected]> --------- Signed-off-by: MichaelMorris <[email protected]>
1 parent 6992748 commit c2f8f79

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

pkg/utils/common/pods.go

+28-9
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})
@@ -294,7 +295,11 @@ func GetTargetPodsWhenTargetPodsENVNotSet(podAffPerc int, clients clients.Client
294295
if err != nil {
295296
return finalPods, cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Target: fmt.Sprintf("{podLabel: %s, namespace: %s}", label, target.Namespace), Reason: err.Error()}
296297
}
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...)
298303
}
299304
}
300305
}
@@ -310,6 +315,20 @@ func GetTargetPodsWhenTargetPodsENVNotSet(podAffPerc int, clients clients.Client
310315
return filterPodsByPercentage(finalPods, podAffPerc), nil
311316
}
312317

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+
313332
func filterPodsByPercentage(finalPods core_v1.PodList, podAffPerc int) core_v1.PodList {
314333
finalPods = removeDuplicatePods(finalPods)
315334

@@ -367,7 +386,7 @@ func GetExperimentPod(name, namespace string, clients clients.ClientSets) (*core
367386
return pod, nil
368387
}
369388

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

373392
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
391410
return containerID, nil
392411
}
393412

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

397416
var containerID string

0 commit comments

Comments
 (0)