Skip to content

Commit 9d98004

Browse files
fix: network-partion targets not found and chaos injection failure
Signed-off-by: dashashutosh80 <[email protected]>
1 parent 07de11e commit 9d98004

File tree

5 files changed

+43
-62
lines changed

5 files changed

+43
-62
lines changed

chaoslib/litmus/pod-network-partition/lib/network-policy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package lib
22

33
import (
44
"fmt"
5-
"github.com/litmuschaos/litmus-go/pkg/cerrors"
6-
"github.com/litmuschaos/litmus-go/pkg/clients"
7-
"github.com/palantir/stacktrace"
85
"strings"
96

107
network_chaos "github.com/litmuschaos/litmus-go/chaoslib/litmus/network-chaos/lib"
8+
"github.com/litmuschaos/litmus-go/pkg/cerrors"
9+
"github.com/litmuschaos/litmus-go/pkg/clients"
1110
experimentTypes "github.com/litmuschaos/litmus-go/pkg/generic/pod-network-partition/types"
11+
"github.com/palantir/stacktrace"
1212
"gopkg.in/yaml.v2"
1313
corev1 "k8s.io/api/core/v1"
1414
networkv1 "k8s.io/api/networking/v1"
@@ -53,7 +53,7 @@ func (np *NetworkPolicy) getNetworkPolicyDetails(experimentsDetails *experimentT
5353
setNamespaceSelector(experimentsDetails.NamespaceSelector)
5454

5555
// sets the ports for the traffic control
56-
if err := np.setPort(experimentsDetails.PORTS); err != nil {
56+
if err := np.setPort(experimentsDetails.Ports); err != nil {
5757
return stacktrace.Propagate(err, "could not set port")
5858
}
5959

chaoslib/litmus/pod-network-partition/lib/pod-network-partition.go

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/litmuschaos/litmus-go/pkg/utils/retry"
2525
"github.com/litmuschaos/litmus-go/pkg/utils/stringutils"
2626
"github.com/sirupsen/logrus"
27-
corev1 "k8s.io/api/core/v1"
2827
networkv1 "k8s.io/api/networking/v1"
2928
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3029
)
@@ -48,23 +47,11 @@ func PrepareAndInjectChaos(ctx context.Context, experimentsDetails *experimentTy
4847
// Catch and relay certain signal(s) to abort channel.
4948
signal.Notify(abort, os.Interrupt, syscall.SIGTERM)
5049

51-
// validate the appLabels
52-
if chaosDetails.AppDetail == nil {
53-
return cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Reason: "provide the appLabel"}
50+
// Network policy will be created based on POD_SELECTOR and NAMESPACE_SELECTOR
51+
if experimentsDetails.PodSelector == "" && experimentsDetails.NamespaceSelector == "" {
52+
return cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Reason: "provide POD_SELECTOR or NAMESPACE_SELECTOR for network policy"}
5453
}
5554

56-
// Get the target pod details for the chaos execution
57-
targetPodList, err := common.GetPodList("", 100, clients, chaosDetails)
58-
if err != nil {
59-
return stacktrace.Propagate(err, "could not get target pods")
60-
}
61-
62-
podNames := []string{}
63-
for _, pod := range targetPodList.Items {
64-
podNames = append(podNames, pod.Name)
65-
}
66-
log.Infof("Target pods list for chaos, %v", podNames)
67-
6855
// generate a unique string
6956
runID := stringutils.GetRunID()
7057

@@ -91,7 +78,7 @@ func PrepareAndInjectChaos(ctx context.Context, experimentsDetails *experimentTy
9178
})
9279

9380
// watching for the abort signal and revert the chaos
94-
go abortWatcher(experimentsDetails, clients, chaosDetails, resultDetails, &targetPodList, runID)
81+
go abortWatcher(experimentsDetails, clients, chaosDetails, resultDetails, runID)
9582

9683
// run the probes during chaos
9784
if len(resultDetails.ProbeDetails) != 0 {
@@ -109,10 +96,8 @@ func PrepareAndInjectChaos(ctx context.Context, experimentsDetails *experimentTy
10996
if err := createNetworkPolicy(ctx, experimentsDetails, clients, np, runID); err != nil {
11097
return stacktrace.Propagate(err, "could not create network policy")
11198
}
112-
// updating chaos status to injected for the target pods
113-
for _, pod := range targetPodList.Items {
114-
common.SetTargets(pod.Name, "injected", "pod", chaosDetails)
115-
}
99+
// Set chaos status to injected
100+
common.SetTargets("network-policy", "injected", "pod", chaosDetails)
116101
}
117102

118103
// verify the presence of network policy inside cluster
@@ -124,14 +109,12 @@ func PrepareAndInjectChaos(ctx context.Context, experimentsDetails *experimentTy
124109
common.WaitForDuration(experimentsDetails.ChaosDuration)
125110

126111
// deleting the network policy after chaos duration over
127-
if err := deleteNetworkPolicy(experimentsDetails, clients, &targetPodList, chaosDetails, experimentsDetails.Timeout, experimentsDetails.Delay, runID); err != nil {
112+
if err := deleteNetworkPolicy(experimentsDetails, clients, experimentsDetails.Timeout, experimentsDetails.Delay, runID); err != nil {
128113
return stacktrace.Propagate(err, "could not delete network policy")
129114
}
130115

131-
// updating chaos status to reverted for the target pods
132-
for _, pod := range targetPodList.Items {
133-
common.SetTargets(pod.Name, "reverted", "pod", chaosDetails)
134-
}
116+
// Set chaos status to reverted
117+
common.SetTargets("network-policy", "reverted", "pod", chaosDetails)
135118

136119
//Waiting for the ramp time after chaos injection
137120
if experimentsDetails.RampTime != 0 {
@@ -176,7 +159,7 @@ func createNetworkPolicy(ctx context.Context, experimentsDetails *experimentType
176159
}
177160

178161
// deleteNetworkPolicy deletes the network policy and wait until the network policy deleted completely
179-
func deleteNetworkPolicy(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, targetPodList *corev1.PodList, chaosDetails *types.ChaosDetails, timeout, delay int, runID string) error {
162+
func deleteNetworkPolicy(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, timeout, delay int, runID string) error {
180163
name := experimentsDetails.ExperimentName + "-np-" + runID
181164
labels := "name=" + experimentsDetails.ExperimentName + "-np-" + runID
182165
if err := clients.KubeClient.NetworkingV1().NetworkPolicies(experimentsDetails.AppNS).Delete(context.Background(), name, v1.DeleteOptions{}); err != nil {
@@ -200,9 +183,6 @@ func deleteNetworkPolicy(experimentsDetails *experimentTypes.ExperimentDetails,
200183
return err
201184
}
202185

203-
for _, pod := range targetPodList.Items {
204-
common.SetTargets(pod.Name, "reverted", "pod", chaosDetails)
205-
}
206186
return nil
207187
}
208188

@@ -225,7 +205,7 @@ func checkExistenceOfPolicy(experimentsDetails *experimentTypes.ExperimentDetail
225205
}
226206

227207
// abortWatcher continuously watch for the abort signals
228-
func abortWatcher(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, chaosDetails *types.ChaosDetails, resultDetails *types.ResultDetails, targetPodList *corev1.PodList, runID string) {
208+
func abortWatcher(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, chaosDetails *types.ChaosDetails, resultDetails *types.ResultDetails, runID string) {
229209
// waiting till the abort signal received
230210
<-abort
231211

@@ -245,7 +225,7 @@ func abortWatcher(experimentsDetails *experimentTypes.ExperimentDetails, clients
245225
continue
246226
}
247227

248-
if err := deleteNetworkPolicy(experimentsDetails, clients, targetPodList, chaosDetails, 2, 1, runID); err != nil {
228+
if err := deleteNetworkPolicy(experimentsDetails, clients, 2, 1, runID); err != nil {
249229
log.Errorf("unable to delete network policy, err: %v", err)
250230
}
251231
retry--

pkg/generic/pod-network-partition/environment/environment.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ func GetENV(experimentDetails *experimentTypes.ExperimentDetails) {
1717
experimentDetails.ChaosDuration, _ = strconv.Atoi(types.Getenv("TOTAL_CHAOS_DURATION", "30"))
1818
experimentDetails.RampTime, _ = strconv.Atoi(types.Getenv("RAMP_TIME", "0"))
1919
experimentDetails.AppNS = types.Getenv("APP_NAMESPACE", "")
20-
experimentDetails.AppLabel = types.Getenv("APP_LABEL", "")
21-
experimentDetails.AppKind = types.Getenv("APP_KIND", "")
20+
experimentDetails.AppLabel = types.Getenv("APP_LABEL", "") // Target pod labels on which network partition will be applied
2221
experimentDetails.ChaosUID = clientTypes.UID(types.Getenv("CHAOS_UID", ""))
2322
experimentDetails.InstanceID = types.Getenv("INSTANCE_ID", "")
2423
experimentDetails.ChaosPodName = types.Getenv("POD_NAME", "")
@@ -29,18 +28,7 @@ func GetENV(experimentDetails *experimentTypes.ExperimentDetails) {
2928
experimentDetails.DestinationHosts = types.Getenv("DESTINATION_HOSTS", "")
3029
experimentDetails.LIBImagePullPolicy = types.Getenv("LIB_IMAGE_PULL_POLICY", "Always")
3130
experimentDetails.PolicyTypes = types.Getenv("POLICY_TYPES", "all")
32-
experimentDetails.PodSelector = types.Getenv("POD_SELECTOR", "")
33-
experimentDetails.NamespaceSelector = types.Getenv("NAMESPACE_SELECTOR", "")
34-
experimentDetails.PORTS = types.Getenv("PORTS", "")
35-
36-
experimentDetails.AppNS, experimentDetails.AppKind, experimentDetails.AppLabel = getAppDetails()
37-
}
38-
39-
func getAppDetails() (string, string, string) {
40-
targets := types.Getenv("TARGETS", "")
41-
app := types.GetTargets(targets)
42-
if len(app) != 0 {
43-
return app[0].Namespace, app[0].Kind, app[0].Labels[0]
44-
}
45-
return "", "", ""
31+
experimentDetails.PodSelector = types.Getenv("POD_SELECTOR", "") // Pod selector to filter pods for network partition ingress or egress rules
32+
experimentDetails.NamespaceSelector = types.Getenv("NAMESPACE_SELECTOR", "") // Namespace selector to filter namespaces for network partition ingress or egress rules
33+
experimentDetails.Ports = types.Getenv("PORTS", "") // Ports to be used for network partition
4634
}

pkg/generic/pod-network-partition/types/types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ type ExperimentDetails struct {
1212
RampTime int
1313
AppNS string
1414
AppLabel string
15-
AppKind string
1615
ChaosUID clientTypes.UID
1716
InstanceID string
1817
LIBImagePullPolicy string
@@ -26,5 +25,5 @@ type ExperimentDetails struct {
2625
PolicyTypes string
2726
PodSelector string
2827
NamespaceSelector string
29-
PORTS string
28+
Ports string
3029
}

pkg/utils/common/pods.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func GetHelperLabels(labels map[string]string, runID, experimentName string) map
119119
return labels
120120
}
121121

122-
// VerifyExistanceOfPods check the availibility of list of pods
123-
func VerifyExistanceOfPods(namespace, pods string, clients clients.ClientSets) (bool, error) {
122+
// VerifyExistenceOfPods check the availability of list of pods
123+
func VerifyExistenceOfPods(namespace, pods string, clients clients.ClientSets) (bool, error) {
124124

125125
if strings.TrimSpace(pods) == "" {
126126
return false, nil
@@ -150,7 +150,7 @@ func GetPodList(targetPods string, podAffPerc int, clients clients.ClientSets, c
150150
namespace = chaosDetails.ChaosNamespace
151151
}
152152

153-
isPodsAvailable, err := VerifyExistanceOfPods(namespace, targetPods, clients)
153+
isPodsAvailable, err := VerifyExistenceOfPods(namespace, targetPods, clients)
154154
if err != nil {
155155
return core_v1.PodList{}, stacktrace.Propagate(err, "could not verify existence of TARGET_PODS")
156156
}
@@ -274,12 +274,26 @@ func GetTargetPodsWhenTargetPodsENVNotSet(podAffPerc int, clients clients.Client
274274
for _, target := range chaosDetails.AppDetail {
275275
switch target.Kind {
276276
case "pod":
277-
for _, name := range target.Names {
278-
pod, err := clients.KubeClient.CoreV1().Pods(target.Namespace).Get(context.Background(), name, v1.GetOptions{})
279-
if err != nil {
280-
return finalPods, cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Target: fmt.Sprintf("{podName: %s, namespace: %s}", name, target.Namespace), Reason: err.Error()}
277+
if target.Names != nil {
278+
for _, name := range target.Names {
279+
pod, err := clients.KubeClient.CoreV1().Pods(target.Namespace).Get(context.Background(), name, v1.GetOptions{})
280+
if err != nil {
281+
return finalPods, cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Target: fmt.Sprintf("{podName: %s, namespace: %s}", name, target.Namespace), Reason: err.Error()}
282+
}
283+
finalPods.Items = append(finalPods.Items, *pod)
284+
}
285+
} else {
286+
for _, label := range target.Labels {
287+
pods, err := clients.KubeClient.CoreV1().Pods(target.Namespace).List(context.Background(), v1.ListOptions{LabelSelector: label})
288+
if err != nil {
289+
return finalPods, cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Target: fmt.Sprintf("{podLabel: %s, namespace: %s}", label, target.Namespace), Reason: err.Error()}
290+
}
291+
filteredPods, err := filterPodsByOwnerKind(pods.Items, target, clients)
292+
if err != nil {
293+
return finalPods, stacktrace.Propagate(err, "could not identify parent type from pod")
294+
}
295+
finalPods.Items = append(finalPods.Items, filteredPods...)
281296
}
282-
finalPods.Items = append(finalPods.Items, *pod)
283297
}
284298
podKind = true
285299
default:

0 commit comments

Comments
 (0)