diff --git a/pkg/utils/common/pods.go b/pkg/utils/common/pods.go index df9b5f015..e00a15964 100644 --- a/pkg/utils/common/pods.go +++ b/pkg/utils/common/pods.go @@ -3,8 +3,6 @@ package common import ( "context" "fmt" - "github.com/litmuschaos/litmus-go/pkg/cerrors" - "github.com/palantir/stacktrace" "math/rand" "os" "os/exec" @@ -12,6 +10,9 @@ import ( "strings" "time" + "github.com/litmuschaos/litmus-go/pkg/cerrors" + "github.com/palantir/stacktrace" + "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" "github.com/litmuschaos/litmus-go/pkg/clients" "github.com/litmuschaos/litmus-go/pkg/log" @@ -25,7 +26,7 @@ import ( v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -//DeletePod deletes the specified pod and wait until it got terminated +// DeletePod deletes the specified pod and wait until it got terminated func DeletePod(podName, podLabel, namespace string, timeout, delay int, clients clients.ClientSets) error { 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 return waitForPodTermination(podLabel, namespace, timeout, delay, clients) } -//DeleteAllPod deletes all the pods with matching labels and wait until all the pods got terminated +// DeleteAllPod deletes all the pods with matching labels and wait until all the pods got terminated func DeleteAllPod(podLabel, namespace string, timeout, delay int, clients clients.ClientSets) error { 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) ( return true, nil } -//GetPodList check for the availability of the target pod for the chaos execution +// GetPodList check for the availability of the target pod for the chaos execution // if the target pod is not defined it will derive the random target pod list using pod affected percentage func GetPodList(targetPods string, podAffPerc int, clients clients.ClientSets, chaosDetails *types.ChaosDetails) (core_v1.PodList, error) { finalPods := core_v1.PodList{} @@ -189,7 +190,7 @@ func CheckForAvailabilityOfPod(namespace, name string, clients clients.ClientSet return true, nil } -//FilterNonChaosPods remove the chaos pods(operator, runner) for the podList +// FilterNonChaosPods remove the chaos pods(operator, runner) for the podList // it filter when the applabels are not defined and it will select random pods from appns func FilterNonChaosPods(ns, labels string, clients clients.ClientSets, chaosDetails *types.ChaosDetails) (core_v1.PodList, error) { 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 if err != nil { return finalPods, cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Target: fmt.Sprintf("{podLabel: %s, namespace: %s}", label, target.Namespace), Reason: err.Error()} } - finalPods.Items = append(finalPods.Items, pods.Items...) + filteredPods, err := filterPodsByOwnerKind(pods.Items, target, clients) + if err != nil { + return finalPods, stacktrace.Propagate(err, "could not identify parent type from pod") + } + finalPods.Items = append(finalPods.Items, filteredPods...) } } } @@ -310,6 +315,20 @@ func GetTargetPodsWhenTargetPodsENVNotSet(podAffPerc int, clients clients.Client return filterPodsByPercentage(finalPods, podAffPerc), nil } +func filterPodsByOwnerKind(pods []core_v1.Pod, target types.AppDetails, clients clients.ClientSets) ([]core_v1.Pod, error) { + var filteredPods []core_v1.Pod + for _, pod := range pods { + parentType, _, err := workloads.GetPodOwnerTypeAndName(&pod, clients.DynamicClient) + if err != nil { + return nil, err + } + if target.Kind == parentType { + filteredPods = append(filteredPods, pod) + } + } + return filteredPods, nil +} + func filterPodsByPercentage(finalPods core_v1.PodList, podAffPerc int) core_v1.PodList { finalPods = removeDuplicatePods(finalPods) @@ -367,7 +386,7 @@ func GetExperimentPod(name, namespace string, clients clients.ClientSets) (*core return pod, nil } -//GetContainerID derive the container id of the application container +// GetContainerID derive the container id of the application container func GetContainerID(appNamespace, targetPod, targetContainer string, clients clients.ClientSets, source string) (string, error) { 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 return containerID, nil } -//GetRuntimeBasedContainerID extract out the container id of the target container based on the container runtime +// GetRuntimeBasedContainerID extract out the container id of the target container based on the container runtime func GetRuntimeBasedContainerID(containerRuntime, socketPath, targetPods, appNamespace, targetContainer string, clients clients.ClientSets, source string) (string, error) { var containerID string