...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/kernelchaos_types.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package v1alpha1
    17  
    18  import (
    19  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    20  )
    21  
    22  // +kubebuilder:object:root=true
    23  // +kubebuilder:printcolumn:name="duration",type=string,JSONPath=`.spec.duration`
    24  // +chaos-mesh:experiment
    25  
    26  // KernelChaos is the Schema for the kernelchaos API
    27  type KernelChaos struct {
    28  	metav1.TypeMeta   `json:",inline"`
    29  	metav1.ObjectMeta `json:"metadata,omitempty"`
    30  
    31  	// Spec defines the behavior of a kernel chaos experiment
    32  	Spec KernelChaosSpec `json:"spec"`
    33  
    34  	// +optional
    35  	// Most recently observed status of the kernel chaos experiment
    36  	Status KernelChaosStatus `json:"status"`
    37  }
    38  
    39  var _ InnerObjectWithSelector = (*KernelChaos)(nil)
    40  var _ InnerObject = (*KernelChaos)(nil)
    41  
    42  // KernelChaosSpec defines the desired state of KernelChaos
    43  type KernelChaosSpec struct {
    44  	ContainerSelector `json:",inline"`
    45  
    46  	// FailKernRequest defines the request of kernel injection
    47  	FailKernRequest FailKernRequest `json:"failKernRequest"`
    48  
    49  	// Duration represents the duration of the chaos action
    50  	Duration *string `json:"duration,omitempty" webhook:"Duration"`
    51  
    52  	// RemoteCluster represents the remote cluster where the chaos will be deployed
    53  	// +optional
    54  	RemoteCluster string `json:"remoteCluster,omitempty"`
    55  }
    56  
    57  // FailKernRequest defines the injection conditions
    58  type FailKernRequest struct {
    59  	// FailType indicates what to fail, can be set to '0' / '1' / '2'
    60  	// If `0`, indicates slab to fail (should_failslab)
    61  	// If `1`, indicates alloc_page to fail (should_fail_alloc_page)
    62  	// If `2`, indicates bio to fail (should_fail_bio)
    63  	// You can read:
    64  	//   1. https://www.kernel.org/doc/html/latest/fault-injection/fault-injection.html
    65  	//   2. http://github.com/iovisor/bcc/blob/master/tools/inject_example.txt
    66  	// to learn more
    67  	// +kubebuilder:validation:Maximum=2
    68  	// +kubebuilder:validation:Minimum=0
    69  	FailType int32 `json:"failtype"`
    70  
    71  	// Headers indicates the appropriate kernel headers you need.
    72  	// Eg: "linux/mmzone.h", "linux/blkdev.h" and so on
    73  	Headers []string `json:"headers,omitempty"`
    74  
    75  	// Callchain indicate a special call chain, such as:
    76  	//     ext4_mount
    77  	//       -> mount_subtree
    78  	//          -> ...
    79  	//             -> should_failslab
    80  	// With an optional set of predicates and an optional set of
    81  	// parameters, which used with predicates. You can read call chan
    82  	// and predicate examples from https://github.com/chaos-mesh/bpfki/tree/develop/examples
    83  	// to learn more.
    84  	// If no special call chain, just keep Callchain empty, which means it will fail at any call chain
    85  	// with slab alloc (eg: kmalloc).
    86  	Callchain []Frame `json:"callchain,omitempty"`
    87  
    88  	// Probability indicates the fails with probability.
    89  	// If you want 1%, please set this field with 1.
    90  	// +kubebuilder:validation:Minimum=0
    91  	// +kubebuilder:validation:Maximum=100
    92  	Probability uint32 `json:"probability,omitempty"`
    93  
    94  	// Times indicates the max times of fails.
    95  	// +kubebuilder:validation:Minimum=0
    96  	Times uint32 `json:"times,omitempty"`
    97  }
    98  
    99  // Frame defines the function signature and predicate in function's body
   100  type Frame struct {
   101  	// Funcname can be find from kernel source or `/proc/kallsyms`, such as `ext4_mount`
   102  	Funcname string `json:"funcname,omitempty"`
   103  
   104  	// Parameters is used with predicate, for example, if you want to inject slab error
   105  	// in `d_alloc_parallel(struct dentry *parent, const struct qstr *name)` with a special
   106  	// name `bananas`, you need to set it to `struct dentry *parent, const struct qstr *name`
   107  	// otherwise omit it.
   108  	Parameters string `json:"parameters,omitempty"`
   109  
   110  	// Predicate will access the arguments of this Frame, example with Parameters's, you can
   111  	// set it to `STRNCMP(name->name, "bananas", 8)` to make inject only with it, or omit it
   112  	// to inject for all d_alloc_parallel call chain.
   113  	Predicate string `json:"predicate,omitempty"`
   114  }
   115  
   116  // KernelChaosStatus defines the observed state of KernelChaos
   117  type KernelChaosStatus struct {
   118  	ChaosStatus `json:",inline"`
   119  }
   120  
   121  func (obj *KernelChaos) GetSelectorSpecs() map[string]interface{} {
   122  	return map[string]interface{}{
   123  		".": &obj.Spec.PodSelector,
   124  	}
   125  }
   126