Skip to content

Commit 683e5f8

Browse files
committed
add logs, add ut, fix breaker
1 parent c3dab0c commit 683e5f8

File tree

7 files changed

+37
-10
lines changed

7 files changed

+37
-10
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ spec:
123123
type: array
124124
required:
125125
- apiGroups
126+
- namespaces
126127
- resources
127128
- verbs
128129
type: object

config/demo/circuitbreaker-demo.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ spec:
1010
interval: 1s
1111
limit: 20
1212
name: deletePod
13-
properties:
14-
sleepingWindowSize: 15m
15-
recoverPolicy: SleepingWindow
13+
recoverPolicy:
14+
sleepingWindowSize: 10m
15+
type: SleepingWindow
1616
resourceRules:
1717
- apiGroups:
1818
- ""
@@ -22,20 +22,24 @@ spec:
2222
- pods
2323
verbs:
2424
- delete
25-
triggerPolicy: Normal
25+
triggerPolicy: LimiterOnly
2626
- bucket:
2727
burst: 200
2828
interval: 1s
2929
limit: 20
3030
name: trafficOffLimit
3131
restRules:
3232
- method: POST
33-
url: https://*.com/*/trafficOff
33+
url: https://xxx.com/xxx/trafficOff
3434
triggerPolicy: LimiterOnly
35+
selector:
36+
matchLabels:
37+
control-plane: kusionstack-operating
3538
trafficInterceptRules:
36-
- contents:
39+
- contentType: Regexp
40+
contents:
3741
- .*(127.0.0.1).*
38-
interceptType: White
42+
interceptType: Whitelist
3943
methods:
4044
- POST
4145
- GET

pkg/apis/ctrlmesh/v1alpha1/throttlingconfig_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type ResourceRule struct {
3131
// Verb is a list of kubernetes resource API verbs, like: get, list, watch, create, update, delete, proxy. "*" means all.
3232
Verbs []string `json:"verbs"`
3333
// Namespaces is a list of namespaces the rule applies to. "*" means all.
34-
Namespaces []string `json:"namespaces,omitempty"`
34+
Namespaces []string `json:"namespaces"`
3535
}
3636

3737
// RestRule defines the target rest resource of the limiting policy

pkg/manager/controllers/circuitbreaker/circuitbreaker_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ func (r *CircuitBreakerReconciler) Reconcile(ctx context.Context, req ctrl.Reque
110110
klog.Errorf(msg)
111111
reconcileErr = errors.Join(reconcileErr, stateErr)
112112
} else {
113+
klog.Infof("sync circuitbreaker %s to pod %s success, configHash=%s", cb.Name, utils.KeyFunc(&po), protoCb.ConfigHash)
113114
currentHash = protoCb.ConfigHash
114-
defaultPodConfigCache.Add(po.Namespace, state.PodName, cb.Name)
115+
defaultPodConfigCache.Add(po.Namespace, po.Name, cb.Name)
115116
}
116117
status := &ctrlmeshv1alpha1.TargetStatus{
117118
PodName: po.Name,
@@ -289,6 +290,7 @@ func (r *CircuitBreakerReconciler) disableConfig(ctx context.Context, podIp stri
289290
if resp != nil && resp.Msg != nil && !resp.Msg.Success {
290291
return fmt.Errorf("fail to disable pod [%s, %s] circuit breaker config, %s", podIp, name, resp.Msg.Message)
291292
}
293+
klog.Infof("pod[ip=%s] circuitbreaker %s was disabled", podIp, name)
292294
return nil
293295
}
294296

pkg/manager/controllers/circuitbreaker/circuitbreaker_controller_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/types"
3030
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3131

32+
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh"
3233
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/constants"
3334
ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
3435
ctrlmeshv1alpha1 "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/v1alpha1"
@@ -88,6 +89,9 @@ var circuitBreaker = &ctrlmeshv1alpha1.CircuitBreaker{
8889
Verbs: []string{
8990
"delete",
9091
},
92+
Namespaces: []string{
93+
"*",
94+
},
9195
},
9296
},
9397
Bucket: ctrlmeshv1alpha1.Bucket{
@@ -142,6 +146,15 @@ func TestCircuitBreaker(t *testing.T) {
142146
waitProcess()
143147
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
144148
g.Expect(breakerManager.ValidateTrafficIntercept("aaa.aaa.aaa", "GET").Allowed).Should(gomega.BeFalse())
149+
if cb.Labels == nil {
150+
cb.Labels = map[string]string{}
151+
}
152+
cb.Labels[ctrlmesh.CtrlmeshCircuitBreakerDisableKey] = "true"
153+
g.Expect(c.Update(ctx, cb)).Should(gomega.BeNil())
154+
waitProcess()
155+
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
156+
g.Expect(len(cb.Status.TargetStatus) == 0).Should(gomega.BeTrue())
157+
g.Expect(len(cb.Finalizers) == 0).Should(gomega.BeTrue())
145158
g.Expect(c.Delete(ctx, testBreaker)).Should(gomega.BeNil())
146159
waitProcess()
147160
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.HaveOccurred())

pkg/manager/controllers/circuitbreaker/filter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package circuitbreaker
1919
import (
2020
"sigs.k8s.io/controller-runtime/pkg/event"
2121

22+
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh"
2223
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/utils/conv"
2324
ctrlmeshv1alpha1 "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/v1alpha1"
2425
)
@@ -43,6 +44,12 @@ func (b *BreakerPredicate) Update(e event.UpdateEvent) bool {
4344
if newCB.DeletionTimestamp != nil || len(oldCB.Finalizers) != len(newCB.Finalizers) {
4445
return true
4546
}
47+
if newCB.Labels != nil {
48+
_, ok := newCB.Labels[ctrlmesh.CtrlmeshCircuitBreakerDisableKey]
49+
if ok {
50+
return true
51+
}
52+
}
4653
oldProtoCB := conv.ConvertCircuitBreaker(oldCB)
4754
newProtoCB := conv.ConvertCircuitBreaker(newCB)
4855
return oldProtoCB.ConfigHash != newProtoCB.ConfigHash

pkg/proxy/circuitbreaker/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func (m *manager) doValidation(limitings []*ctrlmeshproto.RateLimiting, limiters
406406
result.Reason = "OverLimit"
407407
result.Message = fmt.Sprintf("the request is over limit by limiting rule: %s", limitings[idx].Name)
408408
if limitings[idx].RecoverPolicy.Type == ctrlmeshproto.RateLimiting_RECOVER_POLICY_SLEEPING_WINDOW {
409-
d, _ := time.ParseDuration(limitings[idx].Properties["sleepingWindowSize"])
409+
d, _ := time.ParseDuration(limitings[idx].RecoverPolicy.SleepingWindowSize)
410410
states[idx].triggerBreakerWithTimeWindow(d)
411411
m.limiterStore.registerState(states[idx])
412412
} else {

0 commit comments

Comments
 (0)