Skip to content

Cooperate/GitHub chunqi faultinjection #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ bin/
# Dependency directories (remove the comment below to include it)
# vendor/

.vscode
.idea/**
demo/**
testdemo/**
.DS_Store
.DS_Store
.vscode/launch.json
15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
CLUSTER_NAME ?= kindcluster

CRD_OPTIONS ?= "crd:crdVersions=v1"

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

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

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

##@ Deployment

deploy-local-kind:
sed -i'' -e 's@name: .*@name: '"${CLUSTER_NAME}"'@' ./e2e/scripts/kind-conf.yaml
kind create cluster --image kindest/node:v1.21.1 --config ./e2e/scripts/kind-conf.yaml

kind-kube-config:
kind get kubeconfig --name ${CLUSTER_NAME} > /tmp/kind-kubeconfig.yaml
export KUBECONFIG=/tmp/kind-kubeconfig.yaml

kind: deploy-local-kind kind-kube-config # deploy kind

clear-kind:
kind delete cluster --name ${CLUSTER_NAME}

install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd

Expand Down
235 changes: 235 additions & 0 deletions config/crd/bases/ctrlmesh.kusionstack.io_faultinjections.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.10.0
creationTimestamp: null
name: faultinjections.ctrlmesh.kusionstack.io
spec:
group: ctrlmesh.kusionstack.io
names:
kind: FaultInjection
listKind: FaultInjectionList
plural: faultinjections
shortNames:
- fj
singular: faultinjection
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
properties:
disabled:
type: boolean
httpFault:
items:
description: HTTPFaultInjection can be used to specify one or more
faults to inject while forwarding HTTP requests to the destination
specified in a route.
properties:
abort:
description: Abort Http request attempts and return error codes
back to downstream service, giving the impression that the
upstream service is faulty.
properties:
httpStatus:
description: HttpStatus is used to indicate the HTTP status
code to return to the caller.
type: integer
percent:
description: Percent of requests to be aborted with the
error code provided. If not specified, no request will
be aborted.
type: string
type: object
delay:
description: Delay requests before forwarding, emulating various
failures such as network issues, overloaded upstream service,
etc.
properties:
fixedDelay:
description: FixedDelay is used to indicate the amount of
delay in seconds.
type: string
percent:
description: Percent of requests on which the delay will
be injected. If left unspecified, no request will be delayed
type: string
type: object
effectiveTime:
description: Effective time of fault injection
properties:
daysOfMonth:
description: DaysOfMonth specifies on which days of the
month the fault injection configuration is effective.
For example, 1 represents the first day of the month,
and so on.
items:
type: integer
type: array
daysOfWeek:
description: DaysOfWeek specifies on which days of the week
the fault injection configuration is effective. 0 represents
Sunday, 1 represents Monday, and so on.
items:
type: integer
type: array
endTime:
description: EndTime is the ending time of fault injection.
type: string
months:
description: Months specifies in which months of the year
the fault injection configuration is effective. 1 represents
January, 2 represents February, and so on.
items:
type: integer
type: array
startTime:
description: StartTime is the starting time of fault injection.
type: string
type: object
match:
description: Match specifies a set of criterion to be met in
order for the rule to be applied to the HTTP request.
properties:
httpMatch:
items:
description: HttpMatch specifies the criteria for matching
HTTP requests to RESTful resources as part of HTTP FaultInjection.
Each rule can target one or more URLs and HTTP methods.
properties:
method:
description: 'Method specifies the http method of
the request, like: PUT, POST, GET, DELETE.'
items:
type: string
type: array
url:
description: URL gives the location of the rest request,
in standard URL form (`scheme://host:port/path`)
items:
type: string
type: array
required:
- method
- url
type: object
type: array
resources:
items:
properties:
apiGroups:
items:
type: string
type: array
namespaces:
items:
type: string
type: array
resources:
items:
type: string
type: array
verbs:
items:
type: string
type: array
type: object
type: array
type: object
name:
description: Name is the name of the policy
type: string
type: object
type: array
selector:
description: Selector is a label query over pods of this configuration.
properties:
matchExpressions:
description: matchExpressions is a list of label selector requirements.
The requirements are ANDed.
items:
description: A label selector requirement is a selector that
contains values, a key, and an operator that relates the key
and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to
a set of values. Valid operators are In, NotIn, Exists
and DoesNotExist.
type: string
values:
description: values is an array of string values. If the
operator is In or NotIn, the values array must be non-empty.
If the operator is Exists or DoesNotExist, the values
array must be empty. This array is replaced during a strategic
merge patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
matchLabels:
additionalProperties:
type: string
description: matchLabels is a map of {key,value} pairs. A single
{key,value} in the matchLabels map is equivalent to an element
of matchExpressions, whose key field is "key", the operator
is "In", and the values array contains only "value". The requirements
are ANDed.
type: object
type: object
x-kubernetes-map-type: atomic
type: object
status:
properties:
currentSpecHash:
type: string
lastUpdatedTime:
format: date-time
type: string
observedGeneration:
format: int64
type: integer
targetStatus:
items:
properties:
configHash:
type: string
message:
type: string
podIP:
type: string
podName:
type: string
type: object
type: array
type: object
type: object
served: true
storage: true
subresources:
status: {}
8 changes: 4 additions & 4 deletions config/demo/circuitbreaker-demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ apiVersion: ctrlmesh.kusionstack.io/v1alpha1
kind: CircuitBreaker
metadata:
name: breaker-demo
namespace: default
namespace: kusionstack-system
spec:
rateLimitings:
- bucket:
burst: 500
interval: 1s
limit: 20
burst: 1
interval: 10s
limit: 1
name: deletePod
recoverPolicy:
sleepingWindowSize: 10m
Expand Down
29 changes: 29 additions & 0 deletions config/demo/faultinjection-demo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: ctrlmesh.kusionstack.io/v1alpha1
kind: FaultInjection
metadata:
name: fault-injection-demo
namespace: default
spec:
selector:
matchLabels:
app: foo
httpFault:
- delay:
fixedDelay: 5s
percent: 10
match:
- method: POST
url: https://*.com/*/trafficOff
- abort:
httpStatus: 500
percent: 100
match:
relatedResources:
- apiGroups:
- ""
namespaces:
- '*'
resources:
- pods
verbs:
- delete
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
golang.org/x/sync v0.1.0
golang.org/x/time v0.3.0
gomodules.xyz/jsonpatch/v2 v2.2.0
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21
google.golang.org/grpc v1.49.0
google.golang.org/protobuf v1.32.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
Expand Down Expand Up @@ -102,7 +103,6 @@ require (
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/ctrlmesh/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ const (
EnvEnableSim = "ENABLE_SIM"

EnvDisableCircuitBreaker = "DISABLE_CIRCUIT_BREAKER"
EnvDisableFaultInjection = "DISABLE_FAULT_INJECTION"
EnvEnableApiServerCircuitBreaker = "ENABLE_API_SERVER_BREAKER"
EnvEnableRestCircuitBreaker = "ENABLE_REST_BREAKER"
EnvEnableRestFaultInjection = "ENABLE_REST_FAULT_INJECTION"

EnvProxyGRPCServerPort = "PROXY_GRPC_SERVER_PORT"
)
Expand All @@ -77,6 +79,7 @@ func AllProxySyncEnvKey() []string {
EnvDisableCircuitBreaker,
EnvEnableApiServerCircuitBreaker,
EnvEnableRestCircuitBreaker,
EnvEnableRestFaultInjection,
}
return keys
}
Loading