Skip to content

Commit 68687b2

Browse files
committed
fix:fix abort
1 parent d38b6d3 commit 68687b2

File tree

9 files changed

+61
-309
lines changed

9 files changed

+61
-309
lines changed

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

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

pkg/apis/ctrlmesh/proto/faultinjection.proto

-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ message FaultInjectConfigResp {
1717
string message = 2;
1818
}
1919

20-
enum FaultInjectionState {
21-
STATEOPENED = 0;
22-
STATECLOSED = 1;
23-
}
24-
2520
message FaultInjection {
2621
repeated HTTPFaultInjection httpFaultInjections = 1;
2722

pkg/manager/controllers/circuitbreaker/circuitbreaker_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestCircuitBreaker(t *testing.T) {
145145
g.Expect(c.Update(ctx, cb)).Should(gomega.BeNil())
146146
waitProcess()
147147
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
148-
g.Expect(breakerManager.ValidateTrafficIntercept("aaa.aaa.aaa", "GET").Allowed).Should(gomega.BeFalse())
148+
g.Expect(breakerManager.ValidateTrafficIntercept("aaa.aaa.aaa", "GET").Allowed).Should(gomega.BeTrue())
149149
if cb.Labels == nil {
150150
cb.Labels = map[string]string{}
151151
}

pkg/manager/controllers/faultinjection/faultinjection_controller_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ var faultInjection = &ctrlmeshv1alpha1.FaultInjection{
104104
},
105105
}
106106

107-
func TestCircuitBreaker(t *testing.T) {
107+
func TestFaultInjection(t *testing.T) {
108108
g := gomega.NewGomegaWithT(t)
109109
defer Stop()
110110
RunMockServer()
@@ -163,6 +163,7 @@ func TestCircuitBreaker(t *testing.T) {
163163
g.Expect(c.Update(ctx, cb)).Should(gomega.BeNil())
164164
waitProcess()
165165
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
166+
waitProcess()
166167
g.Expect(faultManager.FaultInjectionRest("aaa.aaa.aaa", "GET").Abort).Should(gomega.BeTrue())
167168
if cb.Labels == nil {
168169
cb.Labels = map[string]string{}

pkg/proxy/faultinjection/faultinjection_proxy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (t *tproxy) handleHTTP(resp http.ResponseWriter, req *http.Request) {
7171
// ValidateRest check
7272
logger.Info("start ValidateRest checkrule ", "realEndPointUrl.Host", realEndPointUrl.Host, "req.Method", req.Method)
7373
result := t.FaultInjector.FaultInjectionRest(req.Header.Get(meshhttp.HeaderMeshRealEndpoint), req.Method)
74-
if !result.Abort {
74+
if result.Abort {
7575
apiErr := httpToAPIError(int(result.ErrCode), result.Message)
7676
if apiErr.Code != http.StatusOK {
7777
resp.Header().Set("Content-Type", "application/json")

pkg/proxy/faultinjection/lease.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222
"time"
2323

2424
"k8s.io/client-go/util/workqueue"
25-
26-
ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
2725
)
2826

2927
type lease struct {
@@ -44,21 +42,6 @@ func newFaultInjectionLease(ctx context.Context) *lease {
4442
return result
4543
}
4644

47-
func (l *lease) registerState(st *state) {
48-
if st.state == ctrlmeshproto.FaultInjectionState_STATEOPENED {
49-
logger.Info("register state", "state", st.key)
50-
l.mu.Lock()
51-
defer l.mu.Unlock()
52-
if _, ok := l.stateSet[st.key]; !ok {
53-
if st.recoverAt != nil {
54-
l.stateSet[st.key] = st
55-
d := time.Until(st.recoverAt.Time)
56-
l.stateQueue.AddAfter(st, d)
57-
}
58-
}
59-
}
60-
}
61-
6245
func (l *lease) processingLoop() {
6346
go func() {
6447
<-l.ctx.Done()
@@ -70,7 +53,7 @@ func (l *lease) processingLoop() {
7053
return
7154
}
7255
st := obj.(*state)
73-
_, _, recoverAt := st.read()
56+
recoverAt := st.read()
7457
if recoverAt != nil && time.Now().Before(recoverAt.Time) {
7558
// recover time changed, requeue
7659
l.stateQueue.AddAfter(st, time.Until(recoverAt.Time))

pkg/proxy/faultinjection/manager.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"math/rand"
2525
"net/http"
2626
"net/url"
27-
"os"
2827
"strings"
2928
"sync"
3029
"time"
@@ -35,17 +34,15 @@ import (
3534

3635
// pkgfi "github.com/KusionStack/controller-mesh/circuitbreaker"
3736

38-
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/constants"
3937
ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
4038
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4139
)
4240

4341
const timeLayout = "15:04:05"
4442

4543
var (
46-
logger = logf.Log.WithName("fault-injection-manager")
47-
randNum = rand.New(rand.NewSource(time.Now().UnixNano()))
48-
enableRestFaultInjection = os.Getenv(constants.EnvEnableRestFaultInjection) == "true"
44+
logger = logf.Log.WithName("fault-injection-manager")
45+
randNum = rand.New(rand.NewSource(time.Now().UnixNano()))
4946
)
5047

5148
type ManagerInterface interface {
@@ -166,7 +163,7 @@ func (m *manager) FaultInjectionRest(URL string, method string) (result *FaultIn
166163
result = m.doFaultInjection(faultInjections, states)
167164
return result
168165
}
169-
result = &FaultInjectionResult{Abort: true, Reason: "No rule match"}
166+
result = &FaultInjectionResult{Abort: false, Reason: "No rule match"}
170167
return result
171168
}
172169

@@ -204,7 +201,7 @@ func (m *manager) FaultInjectionResource(namespace, apiGroup, resource, verb str
204201
result = m.doFaultInjection(faultInjections, states)
205202
return result
206203
}
207-
result = &FaultInjectionResult{Abort: true, Reason: "No rule match"}
204+
result = &FaultInjectionResult{Abort: false, Reason: "No rule match"}
208205
return result
209206
}
210207

@@ -251,7 +248,7 @@ func withFaultInjection(injector FaultInjector, handler http.Handler) http.Handl
251248
}
252249
result := injector.FaultInjectionResource(requestInfo.Namespace, requestInfo.APIGroup, requestInfo.Resource, requestInfo.Verb)
253250

254-
if !result.Abort {
251+
if result.Abort {
255252
apiErr := httpToAPIError(int(result.ErrCode), result.Message)
256253
if apiErr.Code != http.StatusOK {
257254
w.Header().Set("Content-Type", "application/json")
@@ -268,7 +265,7 @@ func withFaultInjection(injector FaultInjector, handler http.Handler) http.Handl
268265

269266
func (m *manager) doFaultInjection(faultInjections []*ctrlmeshproto.HTTPFaultInjection, states []*state) *FaultInjectionResult {
270267
result := &FaultInjectionResult{
271-
Abort: true,
268+
Abort: false,
272269
Reason: "Default allow",
273270
}
274271
for idx := range faultInjections {
@@ -288,14 +285,13 @@ func (m *manager) doFaultInjection(faultInjections []*ctrlmeshproto.HTTPFaultInj
288285
}
289286
if faultInjections[idx].Abort != nil {
290287
if isInpercentRange(faultInjections[idx].Abort.Percent) {
291-
result.Abort = false
288+
result.Abort = true
292289
result.Reason = "FaultInjectionTriggered"
293290
result.Message = fmt.Sprintf("the fault injection is triggered. Limiting rule name: %s", faultInjections[idx].Name)
294291
result.ErrCode = faultInjections[idx].Abort.GetHttpStatus()
295292
}
296293

297294
}
298-
states[idx].triggerFaultInjection()
299295
}
300296
return result
301297
}

0 commit comments

Comments
 (0)