@@ -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 --
0 commit comments