@@ -21,6 +21,7 @@ import (
21
21
"encoding/json"
22
22
"errors"
23
23
"fmt"
24
+
24
25
"math/rand"
25
26
"net/http"
26
27
"net/url"
@@ -30,9 +31,7 @@ import (
30
31
31
32
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
32
33
apirequest "k8s.io/apiserver/pkg/endpoints/request"
33
- logf "sigs.k8s.io/controller-runtime/pkg/log"
34
-
35
- // pkgfi "github.com/KusionStack/controller-mesh/circuitbreaker"
34
+ "k8s.io/klog/v2"
36
35
37
36
ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
38
37
"github.com/KusionStack/controller-mesh/pkg/utils"
@@ -41,7 +40,6 @@ import (
41
40
const timeLayout = "15:04:05"
42
41
43
42
var (
44
- logger = logf .Log .WithName ("fault-injection-manager" )
45
43
randNum = rand .New (rand .NewSource (time .Now ().UnixNano ()))
46
44
)
47
45
@@ -89,6 +87,9 @@ func (m *manager) Sync(config *ctrlmeshproto.FaultInjection) (*ctrlmeshproto.Fau
89
87
Message : fmt .Sprintf ("faultInjection spec hash not updated, hash %s" , fi .ConfigHash ),
90
88
}, nil
91
89
} else {
90
+ if ok {
91
+ m .unregisterRules (fi .Name )
92
+ }
92
93
m .faultInjectionMap [config .Name ] = config
93
94
m .registerRules (config )
94
95
var msg string
@@ -117,16 +118,16 @@ func (m *manager) Sync(config *ctrlmeshproto.FaultInjection) (*ctrlmeshproto.Fau
117
118
}, nil
118
119
}
119
120
case ctrlmeshproto .FaultInjection_CHECK :
120
- cb , ok := m .faultInjectionMap [config .Name ]
121
+ fi , ok := m .faultInjectionMap [config .Name ]
121
122
if ! ok {
122
123
return & ctrlmeshproto.FaultInjectConfigResp {
123
124
Success : false ,
124
- Message : fmt .Sprintf ("fault injection config %s not found" , cb .Name ),
125
+ Message : fmt .Sprintf ("fault injection config %s not found" , fi .Name ),
125
126
}, nil
126
- } else if config .ConfigHash != cb .ConfigHash {
127
+ } else if config .ConfigHash != fi .ConfigHash {
127
128
return & ctrlmeshproto.FaultInjectConfigResp {
128
129
Success : false ,
129
- Message : fmt .Sprintf ("unequal fault injection %s hash, old %s, new %s" , cb .Name , cb .ConfigHash , config .ConfigHash ),
130
+ Message : fmt .Sprintf ("unequal fault injection %s hash, old %s, new %s" , fi .Name , fi .ConfigHash , config .ConfigHash ),
130
131
}, nil
131
132
}
132
133
return & ctrlmeshproto.FaultInjectConfigResp {
@@ -150,17 +151,14 @@ func (m *manager) HandlerWrapper() func(http.Handler) http.Handler {
150
151
151
152
func (m * manager ) FaultInjectionRest (URL string , method string ) (result * FaultInjectionResult ) {
152
153
now := time .Now ()
153
- defer func () {
154
- logger .Info ("validate rest" , "URL" , URL , "method" , method , "result" , result , "cost time" , time .Since (now ).String ())
155
- }()
156
-
157
154
urls := generateWildcardUrls (URL , method )
158
155
for _ , url := range urls {
159
156
faultInjections , states := m .faultInjectionStore .byIndex (IndexRest , url )
160
157
if len (faultInjections ) == 0 {
161
158
continue
162
159
}
163
160
result = m .doFaultInjection (faultInjections , states )
161
+ klog .Infof ("validate rest, URL: %s, method:%s, result: %v, cost time: %v " , URL , method , result , time .Since (now ).String ())
164
162
return result
165
163
}
166
164
result = & FaultInjectionResult {Abort : false , Reason : "No rule match" }
@@ -173,7 +171,7 @@ func generateWildcardUrls(URL string, method string) []string {
173
171
URL = strings .TrimSuffix (URL , "/" )
174
172
u , err := url .Parse (URL )
175
173
if err != nil {
176
- logger . Error ( err , "failed to url" , " URL" , URL , " method" , method )
174
+ klog . Errorf ( "failed to url, URL: %s, method: %s,err: %v " , URL , method , err )
177
175
return result
178
176
}
179
177
if len (u .Path ) > 0 {
@@ -189,16 +187,14 @@ func generateWildcardUrls(URL string, method string) []string {
189
187
190
188
func (m * manager ) FaultInjectionResource (namespace , apiGroup , resource , verb string ) (result * FaultInjectionResult ) {
191
189
now := time .Now ()
192
- defer func () {
193
- logger .Info ("validate resource" , "namespace" , namespace , "apiGroup" , apiGroup , "resource" , resource , "verb" , verb , "result" , result , "cost time" , time .Since (now ).String ())
194
- }()
195
190
seeds := generateWildcardSeeds (namespace , apiGroup , resource , verb )
196
191
for _ , seed := range seeds {
197
192
faultInjections , states := m .faultInjectionStore .byIndex (IndexResource , seed )
198
193
if len (faultInjections ) == 0 {
199
194
continue
200
195
}
201
196
result = m .doFaultInjection (faultInjections , states )
197
+ klog .Infof ("validate resource, namespace: %s, apiGroup: %s, resource: %s, verb: %s, result: %v, cost time: %v, " , namespace , apiGroup , resource , verb , result , time .Since (now ).String ())
202
198
return result
203
199
}
204
200
result = & FaultInjectionResult {Abort : false , Reason : "No rule match" }
@@ -253,8 +249,12 @@ func withFaultInjection(injector FaultInjector, handler http.Handler) http.Handl
253
249
if apiErr .Code != http .StatusOK {
254
250
w .Header ().Set ("Content-Type" , "application/json" )
255
251
w .WriteHeader (int (apiErr .Code ))
256
- json .NewEncoder (w ).Encode (apiErr )
257
- logger .Info ("faultinjection rule" , fmt .Sprintf ("fault injection, %s, %s,%d" , result .Reason , result .Message , result .ErrCode ))
252
+ if err := json .NewEncoder (w ).Encode (apiErr ); err != nil {
253
+ // Error encoding the JSON response, at this point the headers are already written.
254
+ klog .Errorf ("failed to write api error response: %v" , err )
255
+ return
256
+ }
257
+ klog .Infof ("faultinjection rule: %s" , fmt .Sprintf ("fault injection, %s, %s,%d" , apiErr .Reason , apiErr .Message , apiErr .Code ))
258
258
return
259
259
}
260
260
}
@@ -278,7 +278,7 @@ func (m *manager) doFaultInjection(faultInjections []*ctrlmeshproto.HTTPFaultInj
278
278
if isInpercentRange (faultInjections [idx ].Delay .Percent ) {
279
279
delay := faultInjections [idx ].Delay .GetFixedDelay ()
280
280
delayDuration := delay .AsDuration ()
281
- logger . Info ("Delaying time " , "for " , delayDuration )
281
+ klog . Infof ("Delaying time: %v " , delayDuration )
282
282
time .Sleep (delayDuration )
283
283
}
284
284
}
@@ -398,11 +398,7 @@ func isEffectiveTimeRange(timeRange *ctrlmeshproto.EffectiveTimeRange) bool {
398
398
399
399
// RegisterRules register a fault injection to the local store
400
400
func (m * manager ) registerRules (fi * ctrlmeshproto.FaultInjection ) {
401
- logger .Info ("register rule" , "faultInjection" , fi .Name )
402
- if _ , ok := m .faultInjectionMap [fi .Name ]; ok {
403
- m .unregisterRules (fi .Name )
404
- }
405
-
401
+ klog .Infof ("register rule, faultInjection: %s" , fi .Name )
406
402
for _ , faultInjection := range fi .HttpFaultInjections {
407
403
key := fmt .Sprintf ("%s:%s" , fi .Name , faultInjection .Name )
408
404
m .faultInjectionStore .createOrUpdateRule (
@@ -413,7 +409,7 @@ func (m *manager) registerRules(fi *ctrlmeshproto.FaultInjection) {
413
409
414
410
// UnregisterRules unregister a fault injection to the local store
415
411
func (m * manager ) unregisterRules (fiName string ) {
416
- logger . Info ("unregister rule" , " faultInjection" , fiName )
412
+ klog . Infof ("unregister rule, faultInjection: %s " , fiName )
417
413
fi , ok := m .faultInjectionMap [fiName ]
418
414
if ! ok {
419
415
return
0 commit comments