...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/jvmchaos_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  // JVMChaosSpec defines the desired state of JVMChaos
    23  type JVMChaosSpec struct {
    24  	ContainerSelector `json:",inline"`
    25  
    26  	// Duration represents the duration of the chaos action
    27  	// +optional
    28  	Duration *string `json:"duration,omitempty" webhook:"Duration"`
    29  
    30  	// Action defines the specific jvm chaos action.
    31  	// Supported action: latency;return;exception;stress;gc;ruleData
    32  	// +kubebuilder:validation:Enum=latency;return;exception;stress;gc;ruleData;mysql
    33  	Action JVMChaosAction `json:"action"`
    34  
    35  	// JVMParameter represents the detail about jvm chaos action definition
    36  	// +optional
    37  	JVMParameter `json:",inline"`
    38  
    39  	// RemoteCluster represents the remote cluster where the chaos will be deployed
    40  	// +optional
    41  	RemoteCluster string `json:"remoteCluster,omitempty"`
    42  }
    43  
    44  // JVMChaosAction represents the chaos action about jvm
    45  type JVMChaosAction string
    46  
    47  const (
    48  	// JVMLatencyAction represents the JVM chaos action of invoke latency
    49  	JVMLatencyAction JVMChaosAction = "latency"
    50  
    51  	// JVMReturnAction represents the JVM chaos action of return value
    52  	JVMReturnAction JVMChaosAction = "return"
    53  
    54  	// JVMExceptionAction represents the JVM chaos action of throwing custom exceptions
    55  	JVMExceptionAction JVMChaosAction = "exception"
    56  
    57  	// JVMStressAction represents the JVM chaos action of stress like CPU and memory
    58  	JVMStressAction JVMChaosAction = "stress"
    59  
    60  	// JVMGCAction represents the JVM chaos action of trigger garbage collection
    61  	JVMGCAction JVMChaosAction = "gc"
    62  
    63  	// JVMRuleDataAction represents inject fault with byteman's rule
    64  	// refer to https://downloads.jboss.org/byteman/4.0.14/byteman-programmers-guide.html#the-byteman-rule-language
    65  	JVMRuleDataAction JVMChaosAction = "ruleData"
    66  
    67  	// JVMMySQLAction represents the JVM chaos action of mysql java client fault injection
    68  	JVMMySQLAction JVMChaosAction = "mysql"
    69  )
    70  
    71  // JVMParameter represents the detail about jvm chaos action definition
    72  type JVMParameter struct {
    73  	JVMCommonSpec `json:",inline"`
    74  
    75  	JVMClassMethodSpec `json:",inline"`
    76  
    77  	JVMStressCfgSpec `json:",inline"`
    78  
    79  	JVMMySQLSpec `json:",inline"`
    80  
    81  	// +optional
    82  	// byteman rule name, should be unique, and will generate one if not set
    83  	Name string `json:"name"`
    84  
    85  	// +optional
    86  	// the return value for action 'return'
    87  	ReturnValue string `json:"value"`
    88  
    89  	// +optional
    90  	// the exception which needs to throw for action `exception`
    91  	// or the exception message needs to throw in action `mysql`
    92  	ThrowException string `json:"exception"`
    93  
    94  	// +optional
    95  	// the latency duration for action 'latency', unit ms
    96  	// or the latency duration in action `mysql`
    97  	LatencyDuration int `json:"latency"`
    98  
    99  	// +optional
   100  	// the byteman rule's data for action 'ruleData'
   101  	RuleData string `json:"ruleData"`
   102  }
   103  
   104  // JVMCommonSpec is the common specification for JVMChaos
   105  type JVMCommonSpec struct {
   106  	// +optional
   107  	// the port of agent server, default 9277
   108  	Port int32 `json:"port,omitempty"`
   109  
   110  	// the pid of Java process which needs to attach
   111  	Pid int `json:"pid,omitempty"`
   112  }
   113  
   114  // JVMClassMethodSpec is the specification for class and method
   115  type JVMClassMethodSpec struct {
   116  	// +optional
   117  	// Java class
   118  	Class string `json:"class,omitempty"`
   119  
   120  	// +optional
   121  	// the method in Java class
   122  	Method string `json:"method,omitempty"`
   123  }
   124  
   125  // JVMStressSpec is the specification for stress
   126  type JVMStressCfgSpec struct {
   127  	// +optional
   128  	// the CPU core number needs to use, only set it when action is stress
   129  	CPUCount int `json:"cpuCount,omitempty"`
   130  
   131  	// +optional
   132  	// the memory type needs to locate, only set it when action is stress, the value can be 'stack' or 'heap'
   133  	MemoryType string `json:"memType,omitempty"`
   134  }
   135  
   136  // JVMMySQLSpec is the specification of MySQL fault injection in JVM
   137  // only when SQL match the Database, Table and SQLType, JVMChaos mesh will inject fault
   138  // for examle:
   139  //
   140  //	SQL is "select * from test.t1",
   141  //	only when ((Database == "test" || Database == "") && (Table == "t1" || Table == "") && (SQLType == "select" || SQLType == "")) is true, JVMChaos will inject fault
   142  type JVMMySQLSpec struct {
   143  	// the version of mysql-connector-java, only support 5.X.X(set to "5") and 8.X.X(set to "8") now
   144  	MySQLConnectorVersion string `json:"mysqlConnectorVersion,omitempty"`
   145  
   146  	// the match database
   147  	// default value is "", means match all database
   148  	Database string `json:"database,omitempty"`
   149  
   150  	// the match table
   151  	// default value is "", means match all table
   152  	Table string `json:"table,omitempty"`
   153  
   154  	// the match sql type
   155  	// default value is "", means match all SQL type.
   156  	// The value can be 'select', 'insert', 'update', 'delete', 'replace'.
   157  	SQLType string `json:"sqlType,omitempty"`
   158  }
   159  
   160  // JVMChaosStatus defines the observed state of JVMChaos
   161  type JVMChaosStatus struct {
   162  	ChaosStatus `json:",inline"`
   163  }
   164  
   165  // +kubebuilder:object:root=true
   166  // +kubebuilder:printcolumn:name="action",type=string,JSONPath=`.spec.action`
   167  // +kubebuilder:printcolumn:name="duration",type=string,JSONPath=`.spec.duration`
   168  // +chaos-mesh:experiment
   169  
   170  // JVMChaos is the Schema for the jvmchaos API
   171  type JVMChaos struct {
   172  	metav1.TypeMeta   `json:",inline"`
   173  	metav1.ObjectMeta `json:"metadata,omitempty"`
   174  
   175  	Spec   JVMChaosSpec   `json:"spec,omitempty"`
   176  	Status JVMChaosStatus `json:"status,omitempty"`
   177  }
   178  
   179  var _ InnerObjectWithSelector = (*JVMChaos)(nil)
   180  var _ InnerObject = (*JVMChaos)(nil)
   181  
   182  func init() {
   183  	SchemeBuilder.Register(&JVMChaos{}, &JVMChaosList{})
   184  }
   185  
   186  func (obj *JVMChaos) GetSelectorSpecs() map[string]interface{} {
   187  	return map[string]interface{}{
   188  		".": &obj.Spec.ContainerSelector,
   189  	}
   190  }
   191