...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/podhttpchaos_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 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    19  
    20  // PodHttpChaosSpec defines the desired state of PodHttpChaos.
    21  type PodHttpChaosSpec struct {
    22  	// Rules are a list of injection rule for http request.
    23  	// +optional
    24  	Rules []PodHttpChaosRule `json:"rules,omitempty"`
    25  
    26  	// TLS is the tls config,
    27  	// will be override if there are multiple HTTPChaos experiments are applied
    28  	// +optional
    29  	TLS *PodHttpChaosTLS `json:"tls,omitempty"`
    30  }
    31  
    32  // PodHttpChaosStatus defines the actual state of PodHttpChaos.
    33  type PodHttpChaosStatus struct {
    34  	// Pid represents a running tproxy process id.
    35  	// +optional
    36  	Pid int64 `json:"pid,omitempty"`
    37  
    38  	// StartTime represents the start time of a tproxy process.
    39  	// +optional
    40  	StartTime int64 `json:"startTime,omitempty"`
    41  
    42  	// +optional
    43  	FailedMessage string `json:"failedMessage,omitempty"`
    44  
    45  	// +optional
    46  	ObservedGeneration int64 `json:"observedGeneration,omitempty"`
    47  }
    48  
    49  // PodHttpChaosRule defines the injection rule for http.
    50  type PodHttpChaosRule struct {
    51  	PodHttpChaosBaseRule `json:",inline"`
    52  
    53  	// Source represents the source of current rules
    54  	Source string `json:"source,omitempty"`
    55  
    56  	// Port represents the target port to be proxy of.
    57  	Port int32 `json:"port"`
    58  }
    59  
    60  // PodHttpChaosBaseRule defines the injection rule without source and port.
    61  type PodHttpChaosBaseRule struct {
    62  	// Target is the object to be selected and injected, <Request|Response>.
    63  	Target PodHttpChaosTarget `json:"target"`
    64  
    65  	// Selector contains the rules to select target.
    66  	Selector PodHttpChaosSelector `json:"selector"`
    67  
    68  	// Actions contains rules to inject target.
    69  	Actions PodHttpChaosActions `json:"actions"`
    70  }
    71  
    72  type PodHttpChaosSelector struct {
    73  	// Port is a rule to select server listening on specific port.
    74  	// +optional
    75  	Port *int32 `json:"port,omitempty"`
    76  
    77  	// Path is a rule to select target by uri path in http request.
    78  	// +optional
    79  	Path *string `json:"path,omitempty"`
    80  
    81  	// Method is a rule to select target by http method in request.
    82  	// +optional
    83  	Method *string `json:"method,omitempty"`
    84  
    85  	// Code is a rule to select target by http status code in response.
    86  	// +optional
    87  	Code *int32 `json:"code,omitempty"`
    88  
    89  	// RequestHeaders is a rule to select target by http headers in request.
    90  	// The key-value pairs represent header name and header value pairs.
    91  	// +optional
    92  	RequestHeaders map[string]string `json:"request_headers,omitempty"`
    93  
    94  	// ResponseHeaders is a rule to select target by http headers in response.
    95  	// The key-value pairs represent header name and header value pairs.
    96  	// +optional
    97  	ResponseHeaders map[string]string `json:"response_headers,omitempty"`
    98  }
    99  
   100  // PodHttpChaosActions defines possible actions of HttpChaos.
   101  type PodHttpChaosActions struct {
   102  	// Abort is a rule to abort a http session.
   103  	// +optional
   104  	Abort *bool `json:"abort,omitempty"`
   105  
   106  	// Delay represents the delay of the target request/response.
   107  	// A duration string is a possibly unsigned sequence of
   108  	// decimal numbers, each with optional fraction and a unit suffix,
   109  	// such as "300ms", "2h45m".
   110  	// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
   111  	// +optional
   112  	Delay *string `json:"delay,omitempty" webhook:"Delay"`
   113  
   114  	// Replace is a rule to replace some contents in target.
   115  	// +optional
   116  	Replace *PodHttpChaosReplaceActions `json:"replace,omitempty"`
   117  
   118  	// Patch is a rule to patch some contents in target.
   119  	// +optional
   120  	Patch *PodHttpChaosPatchActions `json:"patch,omitempty"`
   121  }
   122  
   123  // PodHttpChaosPatchActions defines possible patch-actions of HttpChaos.
   124  type PodHttpChaosPatchActions struct {
   125  	// Body is a rule to patch message body of target.
   126  	// +optional
   127  	Body *PodHttpChaosPatchBodyAction `json:"body,omitempty"`
   128  
   129  	// Queries is a rule to append uri queries of target(Request only).
   130  	// For example: `[["foo", "bar"], ["foo", "unknown"]]`.
   131  	// +optional
   132  	Queries [][]string `json:"queries,omitempty"`
   133  
   134  	// Headers is a rule to append http headers of target.
   135  	// For example: `[["Set-Cookie", "<one cookie>"], ["Set-Cookie", "<another cookie>"]]`.
   136  	// +optional
   137  	Headers [][]string `json:"headers,omitempty"`
   138  }
   139  
   140  // PodHttpChaosPatchBodyAction defines patch body action of HttpChaos.
   141  type PodHttpChaosPatchBodyAction struct {
   142  	// Type represents the patch type, only support `JSON` as [merge patch json](https://tools.ietf.org/html/rfc7396) currently.
   143  	Type string `json:"type"`
   144  
   145  	// Value is the patch contents.
   146  	Value string `json:"value"`
   147  }
   148  
   149  // PodHttpChaosReplaceActions defines possible replace-actions of HttpChaos.
   150  type PodHttpChaosReplaceActions struct {
   151  	// Path is rule to to replace uri path in http request.
   152  	// +optional
   153  	Path *string `json:"path,omitempty"`
   154  
   155  	// Method is a rule to replace http method in request.
   156  	// +optional
   157  	Method *string `json:"method,omitempty"`
   158  
   159  	// Code is a rule to replace http status code in response.
   160  	// +optional
   161  	Code *int32 `json:"code,omitempty"`
   162  
   163  	// Body is a rule to replace http message body in target.
   164  	// +optional
   165  	Body []byte `json:"body,omitempty"`
   166  
   167  	// Queries is a rule to replace uri queries in http request.
   168  	// For example, with value `{ "foo": "unknown" }`, the `/?foo=bar` will be altered to `/?foo=unknown`,
   169  	// +optional
   170  	Queries map[string]string `json:"queries,omitempty"`
   171  
   172  	// Headers is a rule to replace http headers of target.
   173  	// The key-value pairs represent header name and header value pairs.
   174  	// +optional
   175  	Headers map[string]string `json:"headers,omitempty"`
   176  }
   177  
   178  // PodHttpChaosTarget represents the type of an HttpChaos Action
   179  type PodHttpChaosTarget string
   180  
   181  const (
   182  	// PodHttpRequest represents injecting chaos for http request
   183  	PodHttpRequest PodHttpChaosTarget = "Request"
   184  
   185  	// PodHttpResponse represents injecting chaos for http response
   186  	PodHttpResponse PodHttpChaosTarget = "Response"
   187  )
   188  
   189  // PodHttpChaosTLS contains the tls config for HTTPChaos
   190  type PodHttpChaosTLS struct {
   191  	// SecretName represents the name of required secret resource
   192  	SecretName string `json:"secretName"`
   193  
   194  	// SecretNamespace represents the namespace of required secret resource
   195  	SecretNamespace string `json:"secretNamespace"`
   196  
   197  	// CertName represents the data name of cert file in secret, `tls.crt` for example
   198  	CertName string `json:"certName"`
   199  
   200  	// KeyName represents the data name of key file in secret, `tls.key` for example
   201  	KeyName string `json:"keyName"`
   202  
   203  	// CAName represents the data name of ca file in secret, `ca.crt` for example
   204  	// +optional
   205  	CAName *string `json:"caName,omitempty"`
   206  }
   207  
   208  // +kubebuilder:object:root=true
   209  
   210  // +chaos-mesh:base
   211  // +chaos-mesh:webhook:enableUpdate
   212  // +kubebuilder:subresource:status
   213  // PodHttpChaos is the Schema for the podhttpchaos API
   214  type PodHttpChaos struct {
   215  	metav1.TypeMeta   `json:",inline"`
   216  	metav1.ObjectMeta `json:"metadata,omitempty"`
   217  
   218  	Spec   PodHttpChaosSpec   `json:"spec,omitempty"`
   219  	Status PodHttpChaosStatus `json:"status,omitempty"`
   220  }
   221  
   222  // +kubebuilder:object:root=true
   223  
   224  // PodHttpChaosList contains a list of PodHttpChaos
   225  type PodHttpChaosList struct {
   226  	metav1.TypeMeta `json:",inline"`
   227  	metav1.ListMeta `json:"metadata,omitempty"`
   228  	Items           []PodHttpChaos `json:"items"`
   229  }
   230