Skip to content

Commit 24cb737

Browse files
authored
Cooperate/GitHub chunqi faultinjection (#20)
## What type of PR is this? /kind feature <!-- Add one of the following kinds: /kind bug /kind cleanup /kind documentation /kind chore --> ## What this PR does / why we need it: ## Which issue(s) this PR fixes: - [x] #12 ## Special notes for your reviewer: ### Does this PR introduce a user-facing change? <!-- If no, just write "NONE" in the release-note block below. If yes, a release note is required: Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required". --> ```release-note ``` ### Additional documentation e.g., design docs, usage docs, etc.: <!-- Please use the following format for linking documentation: - [Design]: <link> - [Usage]: <link> - [Other doc]: <link> --> ```docs ```
2 parents 3b5f3ce + 128a21f commit 24cb737

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+5929
-260
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ bin/
1616
# Dependency directories (remove the comment below to include it)
1717
# vendor/
1818

19+
.vscode
1920
.idea/**
2021
demo/**
2122
testdemo/**
22-
.DS_Store
23+
.DS_Store
24+
.vscode/launch.json

Makefile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# Image URL to use all building/pushing image targets
33
IMG ?= controller:latest
4+
CLUSTER_NAME ?= kindcluster
45

56
CRD_OPTIONS ?= "crd:crdVersions=v1"
67

@@ -45,7 +46,6 @@ generate-client: controller-gen ## Generate code containing DeepCopy, DeepCopyIn
4546
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
4647

4748
generate-protobuf: controller-gen ## Generate protobuf code.
48-
#@artifacts/scripts/generate_protobuf.sh
4949
buf generate --path pkg
5050
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
5151

@@ -78,6 +78,19 @@ docker-push: ## Push docker image with the manager.
7878

7979
##@ Deployment
8080

81+
deploy-local-kind:
82+
sed -i'' -e 's@name: .*@name: '"${CLUSTER_NAME}"'@' ./e2e/scripts/kind-conf.yaml
83+
kind create cluster --image kindest/node:v1.21.1 --config ./e2e/scripts/kind-conf.yaml
84+
85+
kind-kube-config:
86+
kind get kubeconfig --name ${CLUSTER_NAME} > /tmp/kind-kubeconfig.yaml
87+
export KUBECONFIG=/tmp/kind-kubeconfig.yaml
88+
89+
kind: deploy-local-kind kind-kube-config # deploy kind
90+
91+
clear-kind:
92+
kind delete cluster --name ${CLUSTER_NAME}
93+
8194
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
8295
$(KUSTOMIZE) build config/crd
8396

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: v0.10.0
7+
creationTimestamp: null
8+
name: faultinjections.ctrlmesh.kusionstack.io
9+
spec:
10+
group: ctrlmesh.kusionstack.io
11+
names:
12+
kind: FaultInjection
13+
listKind: FaultInjectionList
14+
plural: faultinjections
15+
shortNames:
16+
- fj
17+
singular: faultinjection
18+
scope: Namespaced
19+
versions:
20+
- name: v1alpha1
21+
schema:
22+
openAPIV3Schema:
23+
properties:
24+
apiVersion:
25+
description: 'APIVersion defines the versioned schema of this representation
26+
of an object. Servers should convert recognized schemas to the latest
27+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
28+
type: string
29+
kind:
30+
description: 'Kind is a string value representing the REST resource this
31+
object represents. Servers may infer this from the endpoint the client
32+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
33+
type: string
34+
metadata:
35+
type: object
36+
spec:
37+
properties:
38+
disabled:
39+
type: boolean
40+
httpFault:
41+
items:
42+
description: HTTPFaultInjection can be used to specify one or more
43+
faults to inject while forwarding HTTP requests to the destination
44+
specified in a route.
45+
properties:
46+
abort:
47+
description: Abort Http request attempts and return error codes
48+
back to downstream service, giving the impression that the
49+
upstream service is faulty.
50+
properties:
51+
httpStatus:
52+
description: HttpStatus is used to indicate the HTTP status
53+
code to return to the caller.
54+
type: integer
55+
percent:
56+
description: Percent of requests to be aborted with the
57+
error code provided. If not specified, no request will
58+
be aborted.
59+
type: string
60+
type: object
61+
delay:
62+
description: Delay requests before forwarding, emulating various
63+
failures such as network issues, overloaded upstream service,
64+
etc.
65+
properties:
66+
fixedDelay:
67+
description: FixedDelay is used to indicate the amount of
68+
delay in seconds.
69+
type: string
70+
percent:
71+
description: Percent of requests on which the delay will
72+
be injected. If left unspecified, no request will be delayed
73+
type: string
74+
type: object
75+
effectiveTime:
76+
description: Effective time of fault injection
77+
properties:
78+
daysOfMonth:
79+
description: DaysOfMonth specifies on which days of the
80+
month the fault injection configuration is effective.
81+
For example, 1 represents the first day of the month,
82+
and so on.
83+
items:
84+
type: integer
85+
type: array
86+
daysOfWeek:
87+
description: DaysOfWeek specifies on which days of the week
88+
the fault injection configuration is effective. 0 represents
89+
Sunday, 1 represents Monday, and so on.
90+
items:
91+
type: integer
92+
type: array
93+
endTime:
94+
description: EndTime is the ending time of fault injection.
95+
type: string
96+
months:
97+
description: Months specifies in which months of the year
98+
the fault injection configuration is effective. 1 represents
99+
January, 2 represents February, and so on.
100+
items:
101+
type: integer
102+
type: array
103+
startTime:
104+
description: StartTime is the starting time of fault injection.
105+
type: string
106+
type: object
107+
match:
108+
description: Match specifies a set of criterion to be met in
109+
order for the rule to be applied to the HTTP request.
110+
properties:
111+
httpMatch:
112+
items:
113+
description: HttpMatch specifies the criteria for matching
114+
HTTP requests to RESTful resources as part of HTTP FaultInjection.
115+
Each rule can target one or more URLs and HTTP methods.
116+
properties:
117+
method:
118+
description: 'Method specifies the http method of
119+
the request, like: PUT, POST, GET, DELETE.'
120+
items:
121+
type: string
122+
type: array
123+
url:
124+
description: URL gives the location of the rest request,
125+
in standard URL form (`scheme://host:port/path`)
126+
items:
127+
type: string
128+
type: array
129+
required:
130+
- method
131+
- url
132+
type: object
133+
type: array
134+
resources:
135+
items:
136+
properties:
137+
apiGroups:
138+
items:
139+
type: string
140+
type: array
141+
namespaces:
142+
items:
143+
type: string
144+
type: array
145+
resources:
146+
items:
147+
type: string
148+
type: array
149+
verbs:
150+
items:
151+
type: string
152+
type: array
153+
type: object
154+
type: array
155+
type: object
156+
name:
157+
description: Name is the name of the policy
158+
type: string
159+
type: object
160+
type: array
161+
selector:
162+
description: Selector is a label query over pods of this configuration.
163+
properties:
164+
matchExpressions:
165+
description: matchExpressions is a list of label selector requirements.
166+
The requirements are ANDed.
167+
items:
168+
description: A label selector requirement is a selector that
169+
contains values, a key, and an operator that relates the key
170+
and values.
171+
properties:
172+
key:
173+
description: key is the label key that the selector applies
174+
to.
175+
type: string
176+
operator:
177+
description: operator represents a key's relationship to
178+
a set of values. Valid operators are In, NotIn, Exists
179+
and DoesNotExist.
180+
type: string
181+
values:
182+
description: values is an array of string values. If the
183+
operator is In or NotIn, the values array must be non-empty.
184+
If the operator is Exists or DoesNotExist, the values
185+
array must be empty. This array is replaced during a strategic
186+
merge patch.
187+
items:
188+
type: string
189+
type: array
190+
required:
191+
- key
192+
- operator
193+
type: object
194+
type: array
195+
matchLabels:
196+
additionalProperties:
197+
type: string
198+
description: matchLabels is a map of {key,value} pairs. A single
199+
{key,value} in the matchLabels map is equivalent to an element
200+
of matchExpressions, whose key field is "key", the operator
201+
is "In", and the values array contains only "value". The requirements
202+
are ANDed.
203+
type: object
204+
type: object
205+
x-kubernetes-map-type: atomic
206+
type: object
207+
status:
208+
properties:
209+
currentSpecHash:
210+
type: string
211+
lastUpdatedTime:
212+
format: date-time
213+
type: string
214+
observedGeneration:
215+
format: int64
216+
type: integer
217+
targetStatus:
218+
items:
219+
properties:
220+
configHash:
221+
type: string
222+
message:
223+
type: string
224+
podIP:
225+
type: string
226+
podName:
227+
type: string
228+
type: object
229+
type: array
230+
type: object
231+
type: object
232+
served: true
233+
storage: true
234+
subresources:
235+
status: {}

config/demo/circuitbreaker-demo.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ apiVersion: ctrlmesh.kusionstack.io/v1alpha1
22
kind: CircuitBreaker
33
metadata:
44
name: breaker-demo
5-
namespace: default
5+
namespace: kusionstack-system
66
spec:
77
rateLimitings:
88
- bucket:
9-
burst: 500
10-
interval: 1s
11-
limit: 20
9+
burst: 1
10+
interval: 10s
11+
limit: 1
1212
name: deletePod
1313
recoverPolicy:
1414
sleepingWindowSize: 10m

config/demo/faultinjection-demo.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apiVersion: ctrlmesh.kusionstack.io/v1alpha1
2+
kind: FaultInjection
3+
metadata:
4+
name: fault-injection-demo
5+
namespace: default
6+
spec:
7+
selector:
8+
matchLabels:
9+
app: foo
10+
httpFault:
11+
- delay:
12+
fixedDelay: 5s
13+
percent: 10
14+
match:
15+
- method: POST
16+
url: https://*.com/*/trafficOff
17+
- abort:
18+
httpStatus: 500
19+
percent: 100
20+
match:
21+
relatedResources:
22+
- apiGroups:
23+
- ""
24+
namespaces:
25+
- '*'
26+
resources:
27+
- pods
28+
verbs:
29+
- delete

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
golang.org/x/sync v0.1.0
1515
golang.org/x/time v0.3.0
1616
gomodules.xyz/jsonpatch/v2 v2.2.0
17+
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21
1718
google.golang.org/grpc v1.49.0
1819
google.golang.org/protobuf v1.32.0
1920
gopkg.in/natefinch/lumberjack.v2 v2.0.0
@@ -102,7 +103,6 @@ require (
102103
golang.org/x/text v0.13.0 // indirect
103104
golang.org/x/tools v0.6.0 // indirect
104105
google.golang.org/appengine v1.6.7 // indirect
105-
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
106106
gopkg.in/inf.v0 v0.9.1 // indirect
107107
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
108108
gopkg.in/yaml.v2 v2.4.0 // indirect

pkg/apis/ctrlmesh/constants/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ const (
6363
EnvEnableSim = "ENABLE_SIM"
6464

6565
EnvDisableCircuitBreaker = "DISABLE_CIRCUIT_BREAKER"
66+
EnvDisableFaultInjection = "DISABLE_FAULT_INJECTION"
6667
EnvEnableApiServerCircuitBreaker = "ENABLE_API_SERVER_BREAKER"
6768
EnvEnableRestCircuitBreaker = "ENABLE_REST_BREAKER"
69+
EnvEnableRestFaultInjection = "ENABLE_REST_FAULT_INJECTION"
6870

6971
EnvProxyGRPCServerPort = "PROXY_GRPC_SERVER_PORT"
7072
)
@@ -77,6 +79,7 @@ func AllProxySyncEnvKey() []string {
7779
EnvDisableCircuitBreaker,
7880
EnvEnableApiServerCircuitBreaker,
7981
EnvEnableRestCircuitBreaker,
82+
EnvEnableRestFaultInjection,
8083
}
8184
return keys
8285
}

0 commit comments

Comments
 (0)