github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/k8s/chaos/experiments.go (about)

     1  package chaos
     2  
     3  import (
     4  	"github.com/cdk8s-team/cdk8s-core-go/cdk8s/v2"
     5  
     6  	"github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/config"
     7  	networkChaos "github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/imports/k8s/networkchaos/chaosmeshorg"
     8  	podChaos "github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/imports/k8s/podchaos/chaosmeshorg"
     9  	"github.com/smartcontractkit/chainlink-testing-framework/libs/utils/ptr"
    10  )
    11  
    12  var (
    13  	FOREVER = ptr.Ptr("999h")
    14  )
    15  
    16  type ManifestFunc func(namespace string, props *Props) (cdk8s.App, string, string)
    17  
    18  type Props struct {
    19  	LabelsSelector *map[string]*string
    20  	ContainerNames *[]*string
    21  	DurationStr    string
    22  	Delay          string
    23  	FromLabels     *map[string]*string
    24  	ToLabels       *map[string]*string
    25  }
    26  
    27  func blankManifest(namespace string) (cdk8s.App, cdk8s.Chart) {
    28  	app := cdk8s.NewApp(&cdk8s.AppProps{
    29  		YamlOutputType: cdk8s.YamlOutputType_FILE_PER_APP,
    30  	})
    31  	return app, cdk8s.NewChart(app, ptr.Ptr("root"), &cdk8s.ChartProps{
    32  		Namespace: ptr.Ptr(namespace),
    33  	})
    34  }
    35  
    36  func NewKillPods(namespace string, props *Props) (cdk8s.App, string, string) {
    37  	config.JSIIGlobalMu.Lock()
    38  	defer config.JSIIGlobalMu.Unlock()
    39  	app, root := blankManifest(namespace)
    40  	c := podChaos.NewPodChaos(root, ptr.Ptr("experiment"), &podChaos.PodChaosProps{
    41  		Spec: &podChaos.PodChaosSpec{
    42  			Action: podChaos.PodChaosSpecAction_POD_KILL,
    43  			Mode:   podChaos.PodChaosSpecMode_ALL,
    44  			Selector: &podChaos.PodChaosSpecSelector{
    45  				LabelSelectors: props.LabelsSelector,
    46  			},
    47  			Duration: FOREVER,
    48  		},
    49  	})
    50  	return app, *c.Name(), "podchaos"
    51  }
    52  
    53  func NewFailPods(namespace string, props *Props) (cdk8s.App, string, string) {
    54  	config.JSIIGlobalMu.Lock()
    55  	defer config.JSIIGlobalMu.Unlock()
    56  	app, root := blankManifest(namespace)
    57  	c := podChaos.NewPodChaos(root, ptr.Ptr("experiment"), &podChaos.PodChaosProps{
    58  		Spec: &podChaos.PodChaosSpec{
    59  			Action: podChaos.PodChaosSpecAction_POD_FAILURE,
    60  			Mode:   podChaos.PodChaosSpecMode_ALL,
    61  			Selector: &podChaos.PodChaosSpecSelector{
    62  				LabelSelectors: props.LabelsSelector,
    63  			},
    64  			Duration: ptr.Ptr(props.DurationStr),
    65  		},
    66  	})
    67  	return app, *c.Name(), "podchaos"
    68  }
    69  
    70  func NewFailContainers(namespace string, props *Props) (cdk8s.App, string, string) {
    71  	config.JSIIGlobalMu.Lock()
    72  	defer config.JSIIGlobalMu.Unlock()
    73  	app, root := blankManifest(namespace)
    74  	c := podChaos.NewPodChaos(root, ptr.Ptr("experiment"), &podChaos.PodChaosProps{
    75  		Spec: &podChaos.PodChaosSpec{
    76  			Action: podChaos.PodChaosSpecAction_POD_KILL,
    77  			Mode:   podChaos.PodChaosSpecMode_ALL,
    78  			Selector: &podChaos.PodChaosSpecSelector{
    79  				LabelSelectors: props.LabelsSelector,
    80  			},
    81  			ContainerNames: props.ContainerNames,
    82  			Duration:       FOREVER,
    83  		},
    84  	})
    85  	return app, *c.Name(), "podchaos"
    86  }
    87  
    88  func NewContainerKill(namespace string, props *Props) (cdk8s.App, string, string) {
    89  	config.JSIIGlobalMu.Lock()
    90  	defer config.JSIIGlobalMu.Unlock()
    91  	app, root := blankManifest(namespace)
    92  	c := podChaos.NewPodChaos(root, ptr.Ptr("experiment"), &podChaos.PodChaosProps{
    93  		Spec: &podChaos.PodChaosSpec{
    94  			Action: podChaos.PodChaosSpecAction_POD_KILL,
    95  			Mode:   podChaos.PodChaosSpecMode_ALL,
    96  			Selector: &podChaos.PodChaosSpecSelector{
    97  				LabelSelectors: props.LabelsSelector,
    98  			},
    99  			Duration: FOREVER,
   100  		},
   101  	})
   102  	return app, *c.Name(), "podchaos"
   103  }
   104  
   105  func NewNetworkPartition(namespace string, props *Props) (cdk8s.App, string, string) {
   106  	config.JSIIGlobalMu.Lock()
   107  	defer config.JSIIGlobalMu.Unlock()
   108  	app, root := blankManifest(namespace)
   109  	c := networkChaos.NewNetworkChaos(root, ptr.Ptr("experiment"), &networkChaos.NetworkChaosProps{
   110  		Spec: &networkChaos.NetworkChaosSpec{
   111  			Action: networkChaos.NetworkChaosSpecAction_PARTITION,
   112  			Mode:   networkChaos.NetworkChaosSpecMode_ALL,
   113  			Selector: &networkChaos.NetworkChaosSpecSelector{
   114  				LabelSelectors: props.FromLabels,
   115  			},
   116  			Direction:       networkChaos.NetworkChaosSpecDirection_BOTH,
   117  			Duration:        ptr.Ptr(props.DurationStr),
   118  			ExternalTargets: nil,
   119  			Loss: &networkChaos.NetworkChaosSpecLoss{
   120  				Loss: ptr.Ptr("100"),
   121  			},
   122  			Target: &networkChaos.NetworkChaosSpecTarget{
   123  				Mode: networkChaos.NetworkChaosSpecTargetMode_ALL,
   124  				Selector: &networkChaos.NetworkChaosSpecTargetSelector{
   125  					LabelSelectors: props.ToLabels,
   126  				},
   127  			},
   128  		},
   129  	})
   130  	return app, *c.Name(), "networkchaos"
   131  }
   132  
   133  func NewNetworkLatency(namespace string, props *Props) (cdk8s.App, string, string) {
   134  	app, root := blankManifest(namespace)
   135  	c := networkChaos.NewNetworkChaos(root, ptr.Ptr("experiment"), &networkChaos.NetworkChaosProps{
   136  		Spec: &networkChaos.NetworkChaosSpec{
   137  			Action: networkChaos.NetworkChaosSpecAction_DELAY,
   138  			Mode:   networkChaos.NetworkChaosSpecMode_ALL,
   139  			Selector: &networkChaos.NetworkChaosSpecSelector{
   140  				LabelSelectors: props.FromLabels,
   141  			},
   142  			Direction: networkChaos.NetworkChaosSpecDirection_BOTH,
   143  			Duration:  ptr.Ptr(props.DurationStr),
   144  			Delay: &networkChaos.NetworkChaosSpecDelay{
   145  				Latency:     ptr.Ptr(props.Delay),
   146  				Correlation: ptr.Ptr("100"),
   147  			},
   148  			Target: &networkChaos.NetworkChaosSpecTarget{
   149  				Mode: networkChaos.NetworkChaosSpecTargetMode_ALL,
   150  				Selector: &networkChaos.NetworkChaosSpecTargetSelector{
   151  					LabelSelectors: props.ToLabels,
   152  				},
   153  			},
   154  		},
   155  	})
   156  	return app, *c.Name(), "networkchaos"
   157  }