Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix consider appKind when filtering target pods #680

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions pkg/utils/common/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package common
import (
"context"
"fmt"
"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/palantir/stacktrace"
"math/rand"
"os"
"os/exec"
"strconv"
"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"
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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...)
}
}
}
Expand All @@ -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)

Expand Down Expand Up @@ -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{})
Expand All @@ -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
Expand Down
Loading