Skip to content

Commit 4a7aeef

Browse files
committed
feat:add fault injection effective time
1 parent 9b1061f commit 4a7aeef

File tree

13 files changed

+641
-184
lines changed

13 files changed

+641
-184
lines changed

config/crd/bases/ctrlmesh.kusionstack.io_faultinjections.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,38 @@ spec:
7373
be injected. If left unspecified, no request will be delayed
7474
type: string
7575
type: object
76+
effectiveTime:
77+
description: Effective time of fault injection
78+
properties:
79+
daysOfMonth:
80+
description: DaysOfMonth specifies on which days of the
81+
month the fault injection configuration is effective.
82+
For example, 1 represents the first day of the month,
83+
and so on.
84+
items:
85+
type: integer
86+
type: array
87+
daysOfWeek:
88+
description: DaysOfWeek specifies on which days of the week
89+
the fault injection configuration is effective. 0 represents
90+
Sunday, 1 represents Monday, and so on.
91+
items:
92+
type: integer
93+
type: array
94+
endTime:
95+
description: EndTime is the ending time of fault injection.
96+
type: string
97+
months:
98+
description: Months specifies in which months of the year
99+
the fault injection configuration is effective. 1 represents
100+
January, 2 represents February, and so on.
101+
items:
102+
type: integer
103+
type: array
104+
startTime:
105+
description: StartTime is the starting time of fault injection.
106+
type: string
107+
type: object
76108
match:
77109
description: Match specifies a set of criterion to be met in
78110
order for the rule to be applied to the HTTP request.
@@ -124,6 +156,7 @@ spec:
124156
type: array
125157
type: object
126158
name:
159+
description: Name is the name of the policy
127160
type: string
128161
type: object
129162
type: array

pkg/apis/ctrlmesh/constants/constants.go

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const (
6666
EnvDisableFaultInjection = "DISABLE_FAULT_INJECTION"
6767
EnvEnableApiServerCircuitBreaker = "ENABLE_API_SERVER_BREAKER"
6868
EnvEnableRestCircuitBreaker = "ENABLE_REST_BREAKER"
69+
EnvEnableRestFaultInjection = "ENABLE_REST_Fault_Injection"
6970

7071
EnvProxyGRPCServerPort = "PROXY_GRPC_SERVER_PORT"
7172
)

pkg/apis/ctrlmesh/proto/faultinjection.pb.go

+289-158
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/ctrlmesh/proto/faultinjection.proto

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package proto;
55
import "k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto";
66
import "google/protobuf/duration.proto";
77
import "google/api/field_behavior.proto";
8+
import "google/protobuf/timestamp.proto";
9+
import "google/protobuf/wrappers.proto";
810

911
service FaultInject {
1012
rpc SendConfig(FaultInjection) returns (FaultInjectConfigResp);
@@ -57,6 +59,8 @@ message HTTPFaultInjection {
5759

5860
string Name = 4;
5961

62+
EffectiveTimeRange effective_time = 5;
63+
6064
message Delay {
6165
// Percentage of requests on which the delay will be injected (0-100).
6266
// Use of integer `percent` value is deprecated. Use the double `percentage`
@@ -93,6 +97,29 @@ message HTTPFaultInjection {
9397
}
9498
}
9599

100+
// EffectiveTimeRange specifies the effective time range for fault injection configuration.
101+
message EffectiveTimeRange {
102+
// StartTime is the time when the fault injection configuration starts to take effect.
103+
string start_time = 1;
104+
105+
// EndTime is the time when the fault injection configuration ceases to be effective.
106+
string end_time = 2;
107+
108+
// DaysOfWeek specifies which days of the week the fault injection configuration is effective.
109+
// 0 represents Sunday, 1 represents Monday, and so on.
110+
repeated int32 days_of_week = 3;
111+
112+
// DaysOfMonth specifies on which days of the month the fault injection configuration is effective.
113+
// For example, 1 represents the first day of the month, and so on.
114+
repeated int32 days_of_month = 4;
115+
116+
// Months specifies which months of the year the fault injection configuration is effective.
117+
// 1 represents January, 2 represents February, and so on.
118+
repeated int32 months = 5;
119+
}
120+
121+
122+
96123
message MultiRestRule {
97124
repeated string url = 1;
98125
repeated string method = 2;

pkg/apis/ctrlmesh/proto/faultinjection_deepcopy.gen.go

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/ctrlmesh/utils/conv/faultconv.go

+31
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func ConvertHTTPFaultInjection(faultInjection *ctrlmeshv1alpha1.HTTPFaultInjecti
7878
protoFaultInjection.Match = ConvertHTTPMatch(faultInjection.Match)
7979

8080
}
81+
if faultInjection.EffectiveTime != nil {
82+
protoFaultInjection.EffectiveTime = ConvertEffectiveTime(faultInjection.EffectiveTime)
83+
}
8184
return protoFaultInjection
8285
}
8386

@@ -110,6 +113,34 @@ func ConvertHTTPMatch(match *ctrlmeshv1alpha1.HTTPMatchRequest) *ctrlmeshproto.H
110113
return httpMatchRequest
111114
}
112115

116+
func ConvertEffectiveTime(timeRange *ctrlmeshv1alpha1.EffectiveTimeRange) *ctrlmeshproto.EffectiveTimeRange {
117+
if timeRange == nil {
118+
return nil
119+
}
120+
121+
timeRangeRes := &ctrlmeshproto.EffectiveTimeRange{
122+
StartTime: timeRange.StartTime,
123+
EndTime: timeRange.EndTime,
124+
}
125+
126+
// Convert DaysOfWeek slice to protobuf repeated field
127+
for _, day := range timeRange.DaysOfWeek {
128+
timeRangeRes.DaysOfWeek = append(timeRangeRes.DaysOfWeek, int32(day))
129+
}
130+
131+
// Convert DaysOfMonth slice to protobuf repeated field
132+
for _, day := range timeRange.DaysOfMonth {
133+
timeRangeRes.DaysOfMonth = append(timeRangeRes.DaysOfMonth, int32(day))
134+
}
135+
136+
// Convert Months slice to protobuf repeated field
137+
for _, month := range timeRange.Months {
138+
timeRangeRes.Months = append(timeRangeRes.Months, int32(month))
139+
}
140+
141+
return timeRangeRes
142+
}
143+
113144
func ConvertRelatedResources(resourceRule *ctrlmeshv1alpha1.ResourceMatch) *ctrlmeshproto.ResourceMatch {
114145
protoResourceRule := &ctrlmeshproto.ResourceMatch{}
115146
if resourceRule.Resources != nil {

pkg/apis/ctrlmesh/v1alpha1/faultinjection_types.go

+24
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type HTTPMatchRequest struct {
8888
// HTTPFaultInjection can be used to specify one or more faults to inject
8989
// while forwarding HTTP requests to the destination specified in a route.
9090
type HTTPFaultInjection struct {
91+
// Name is the name of the policy
9192
Name string `json:"name,omitempty"`
9293
// Delay requests before forwarding, emulating various failures such as
9394
// network issues, overloaded upstream service, etc.
@@ -99,6 +100,8 @@ type HTTPFaultInjection struct {
99100
// Match specifies a set of criterion to be met in order for the
100101
// rule to be applied to the HTTP request.
101102
Match *HTTPMatchRequest `json:"match,omitempty"`
103+
// Effective time of fault injection
104+
EffectiveTime *EffectiveTimeRange `json:"effectiveTime,omitempty"`
102105
}
103106

104107
type FaultInjectionSpec struct {
@@ -110,6 +113,27 @@ type FaultInjectionSpec struct {
110113
HTTPFaultInjections []*HTTPFaultInjection `json:"httpFault,omitempty"`
111114
}
112115

116+
type EffectiveTimeRange struct {
117+
// StartTime is the starting time of fault injection.
118+
StartTime string `json:"startTime,omitempty"`
119+
120+
// EndTime is the ending time of fault injection.
121+
EndTime string `json:"endTime,omitempty"`
122+
123+
// DaysOfWeek specifies on which days of the week the fault injection configuration is effective.
124+
// 0 represents Sunday, 1 represents Monday, and so on.
125+
DaysOfWeek []int `json:"daysOfWeek,omitempty"`
126+
127+
// DaysOfMonth specifies on which days of the month the fault injection configuration is effective.
128+
// For example, 1 represents the first day of the month, and so on.
129+
DaysOfMonth []int `json:"daysOfMonth,omitempty"`
130+
131+
// Months specifies in which months of the year the fault injection configuration is effective.
132+
// 1 represents January, 2 represents February, and so on.
133+
Months []int `json:"months,omitempty"`
134+
}
135+
136+
113137
// FaultInjectionState is the status of the fault injection, which may be 'Opened' or 'Closed'.
114138
type FaultInjectionState string
115139

pkg/apis/ctrlmesh/v1alpha1/zz_generated.deepcopy.go

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/manager/controllers/faultinjection/faultinject_controller.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var (
5656
concurrentReconciles = flag.Int("ctrlmesh-inject-workers", 3, "Max concurrent workers for CtrlMesh Server controller.")
5757
)
5858

59-
// FaultInjectionReconciler reconciles a CircuitBreaker object
59+
// FaultInjectionReconciler reconciles a faultinjection object
6060
type FaultInjectionReconciler struct {
6161
client.Client
6262
recorder record.EventRecorder
@@ -111,7 +111,7 @@ func (r *FaultInjectionReconciler) Reconcile(ctx context.Context, req ctrl.Reque
111111
klog.Errorf(msg)
112112
reconcileErr = errors.Join(reconcileErr, stateErr)
113113
} else {
114-
klog.Infof("sync circuitbreaker %s to pod %s success, configHash=%s", fi.Name, utils.KeyFunc(&po), protoFi.ConfigHash)
114+
klog.Infof("sync faultinjection %s to pod %s success, configHash=%s", fi.Name, utils.KeyFunc(&po), protoFi.ConfigHash)
115115
currentHash = protoFi.ConfigHash
116116
defaultPodConfigCache.Add(po.Namespace, po.Name, fi.Name)
117117
}
@@ -276,7 +276,7 @@ func disableConfig(ctx context.Context, podIp string, name string) error {
276276
if resp != nil && resp.Msg != nil && !resp.Msg.Success {
277277
return fmt.Errorf("fail to disable pod [%s, %s] circuit breaker config, %s", podIp, name, resp.Msg.Message)
278278
}
279-
klog.Infof("pod[ip=%s] circuitbreaker %s was disabled", podIp, name)
279+
klog.Infof("pod[ip=%s] faultinjection %s was disabled", podIp, name)
280280
return nil
281281
}
282282

@@ -321,7 +321,6 @@ func (r *FaultInjectionReconciler) InjectClient(c client.Client) error {
321321
// SetupWithManager sets up the controller with the Manager.
322322
func (r *FaultInjectionReconciler) SetupWithManager(mgr ctrl.Manager) error {
323323
r.recorder = mgr.GetEventRecorderFor("sharding-config-controller")
324-
klog.Infof("===>", "starting fault manager")
325324
return ctrl.NewControllerManagedBy(mgr).
326325
WithOptions(controller.Options{MaxConcurrentReconciles: *concurrentReconciles}).
327326
For(&ctrlmeshv1alpha1.FaultInjection{}, builder.WithPredicates(&FaultInjectionPredicate{})).

pkg/manager/controllers/faultinjection/filter.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,21 @@ func (b *FaultInjectionPredicate) Update(e event.UpdateEvent) bool {
4444
if newFault.DeletionTimestamp != nil || len(oldFault.Finalizers) != len(newFault.Finalizers) {
4545
return true
4646
}
47+
48+
oldValue, oldExists := "", false
49+
newValue, newExists := "", false
50+
51+
if oldFault.Labels != nil {
52+
oldValue, oldExists = oldFault.Labels[ctrlmesh.CtrlmeshFaultInjectionDisableKey]
53+
}
4754
if newFault.Labels != nil {
48-
_, ok := newFault.Labels[ctrlmesh.CtrlmeshFaultInjectionDisableKey]
49-
if ok {
50-
return true
51-
}
55+
newValue, newExists = newFault.Labels[ctrlmesh.CtrlmeshFaultInjectionDisableKey]
5256
}
57+
58+
if (oldExists != newExists) || (oldExists && newExists && oldValue != newValue) {
59+
return true
60+
}
61+
5362
oldProtoFault := conv.ConvertFaultInjection(oldFault)
5463
newProtoFault := conv.ConvertFaultInjection(newFault)
5564
return oldProtoFault.ConfigHash != newProtoFault.ConfigHash

pkg/proxy/apiserver/handler.go

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ var (
5757
disableFaultInjection = os.Getenv(constants.EnvDisableCircuitBreaker) == "true"
5858
)
5959

60+
61+
6062
type Proxy struct {
6163
opts *Options
6264
inSecureServer *http.Server

0 commit comments

Comments
 (0)