Skip to content

Commit fe7015a

Browse files
committed
add license and fix grpc client
1 parent 72f9a92 commit fe7015a

File tree

13 files changed

+170
-27
lines changed

13 files changed

+170
-27
lines changed

Diff for: .licenserc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ header:
5757
- '.gitignore'
5858
- '.licenserc'
5959
- '**/zz_generated.deepcopy.go'
60+
- '**/*.connect.go'
6061
- '**/*.pb.go'
6162
- '**/*.proto'
63+
- '**/*.gen.go'
6264
comment: never
6365
license-location-threshold: 80

Diff for: pkg/apis/ctrlmesh/proto/throtting_internal.go

+26
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,29 @@ limitations under the License.
1515
*/
1616

1717
package proto
18+
19+
import (
20+
"context"
21+
"crypto/tls"
22+
"net"
23+
"net/http"
24+
"time"
25+
26+
"golang.org/x/net/http2"
27+
)
28+
29+
var (
30+
DefaultHttpClient = &http.Client{
31+
Transport: &http2.Transport{
32+
AllowHTTP: true,
33+
DialTLSContext: func(ctx context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
34+
// TODO:
35+
// If you're also using this client for non-h2c traffic, you may want
36+
// to delegate to tls.Dial if the network isn't TCP or the addr isn't
37+
// in an allowlist.
38+
d := net.Dialer{Timeout: 5 * time.Second}
39+
return d.DialContext(ctx, network, addr)
40+
},
41+
},
42+
}
43+
)

Diff for: pkg/utils/conv/convert.go renamed to pkg/apis/ctrlmesh/utils/conv/conv.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2023 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package conv
218

319
import (

Diff for: pkg/manager/controllers/circuitbreaker/cache.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2023 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package circuitbreaker
218

319
import (

Diff for: pkg/manager/controllers/circuitbreaker/circuitbreaker_controller.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ import (
2121
"errors"
2222
"flag"
2323
"fmt"
24-
"net/http"
2524
"os"
2625
"time"
2726

2827
"connectrpc.com/connect"
29-
"golang.org/x/net/http2"
3028
v1 "k8s.io/api/core/v1"
3129
"k8s.io/apimachinery/pkg/api/equality"
3230
k8sErr "k8s.io/apimachinery/pkg/api/errors"
@@ -45,9 +43,9 @@ import (
4543
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/constants"
4644
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
4745
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto/protoconnect"
46+
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/utils/conv"
4847
ctrlmeshv1alpha1 "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/v1alpha1"
4948
"github.com/KusionStack/controller-mesh/pkg/utils"
50-
"github.com/KusionStack/controller-mesh/pkg/utils/conv"
5149
)
5250

5351
var (
@@ -56,12 +54,6 @@ var (
5654

5755
defaultRequeueTime = 60 * time.Second
5856
concurrentReconciles = flag.Int("ctrlmesh-server-workers", 3, "Max concurrent workers for CtrlMesh Server controller.")
59-
60-
defaultHttpClient = &http.Client{
61-
Transport: &http2.Transport{
62-
AllowHTTP: true,
63-
},
64-
}
6557
)
6658

6759
// CircuitBreakerReconciler reconciles a CircuitBreaker object
@@ -288,11 +280,11 @@ func (r *CircuitBreakerReconciler) disableConfig(ctx context.Context, podIp stri
288280
}
289281

290282
func protoClient(podIp string) protoconnect.ThrottlingClient {
291-
return protoconnect.NewThrottlingClient(defaultHttpClient, podAddr(podIp))
283+
return protoconnect.NewThrottlingClient(proto.DefaultHttpClient, podAddr(podIp))
292284
}
293285

294286
func podAddr(podIp string) string {
295-
return fmt.Sprintf("%s:%d", podIp, constants.ProxyGRPCServerPort)
287+
return fmt.Sprintf("https://%s:%d", podIp, constants.ProxyGRPCServerPort)
296288
}
297289

298290
func isProxyAvailable(po *v1.Pod) bool {

Diff for: pkg/manager/controllers/circuitbreaker/event_handler.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2023 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package circuitbreaker
218

319
import (

Diff for: pkg/manager/controllers/circuitbreaker/filter.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
/*
2+
Copyright 2023 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package circuitbreaker
218

319
import (
420
"sigs.k8s.io/controller-runtime/pkg/event"
521

22+
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/utils/conv"
623
ctrlmeshv1alpha1 "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/v1alpha1"
7-
"github.com/KusionStack/controller-mesh/pkg/utils/conv"
824
)
925

1026
type BreakerPredicate struct {
@@ -17,9 +33,6 @@ func (b *BreakerPredicate) Create(event.CreateEvent) bool {
1733

1834
// Delete returns true if the Delete event should be processed
1935
func (b *BreakerPredicate) Delete(e event.DeleteEvent) bool {
20-
cb := e.Object.(*ctrlmeshv1alpha1.CircuitBreaker)
21-
protoCB := conv.ConvertCircuitBreaker(cb)
22-
defaultHashCache.DeleteHash(protoCB.ConfigHash)
2336
return true
2437
}
2538

@@ -30,7 +43,6 @@ func (b *BreakerPredicate) Update(e event.UpdateEvent) bool {
3043
oldProtoCB := conv.ConvertCircuitBreaker(oldCB)
3144
newProtoCB := conv.ConvertCircuitBreaker(newCB)
3245
if oldProtoCB.ConfigHash != newProtoCB.ConfigHash {
33-
defaultHashCache.DeleteHash(oldProtoCB.ConfigHash)
3446
return true
3547
}
3648
if newCB.DeletionTimestamp != nil {

Diff for: pkg/proxy/cache/informer.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2023 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package cache
218

319
import (

Diff for: pkg/proxy/circuitbreaker/manager_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2929

3030
ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
31+
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/utils/conv"
3132
ctrlmeshv1alpha1 "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/v1alpha1"
32-
"github.com/KusionStack/controller-mesh/pkg/utils/conv"
3333
)
3434

3535
func init() {

Diff for: pkg/proxy/circuitbreaker/store_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2828

2929
ctrlmeshproto "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
30+
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/utils/conv"
3031
ctrlmeshv1alpha1 "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/v1alpha1"
31-
"github.com/KusionStack/controller-mesh/pkg/utils/conv"
3232
)
3333

3434
func init() {

Diff for: pkg/proxy/grpcserver/server.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2023 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package grpcserver
218

319
import (
@@ -8,6 +24,7 @@ import (
824
"strconv"
925

1026
"connectrpc.com/connect"
27+
"github.com/golang/protobuf/jsonpb"
1128
"golang.org/x/net/http2"
1229
"golang.org/x/net/http2/h2c"
1330
"k8s.io/klog/v2"
@@ -56,7 +73,9 @@ type grpcThrottlingServer struct {
5673
}
5774

5875
func (g *grpcThrottlingServer) SendConfig(ctx context.Context, req *connect.Request[ctrlmeshproto.CircuitBreaker]) (*connect.Response[ctrlmeshproto.ConfigResp], error) {
59-
klog.Infof("handle CircuitBreaker gRPC request %+v", req.Msg)
76+
mar := &jsonpb.Marshaler{EmitDefaults: true}
77+
msg, _ := mar.MarshalToString(req.Msg)
78+
klog.Infof("handle CircuitBreaker gRPC request %s", msg)
6079
if req.Msg == nil {
6180
return connect.NewResponse(&ctrlmeshproto.ConfigResp{Success: false}), fmt.Errorf("nil CircuitBreaker recieived from client")
6281
}

Diff for: pkg/proxy/grpcserver/server_test.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1+
/*
2+
Copyright 2023 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package grpcserver
218

319
import (
420
"context"
5-
"net/http"
21+
"fmt"
622
"testing"
723

824
"connectrpc.com/connect"
925
"github.com/onsi/gomega"
10-
"golang.org/x/net/http2"
1126
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1227

1328
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto"
1429
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/proto/protoconnect"
30+
"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/utils/conv"
1531
ctrlmeshv1alpha1 "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/v1alpha1"
1632
"github.com/KusionStack/controller-mesh/pkg/proxy/circuitbreaker"
17-
"github.com/KusionStack/controller-mesh/pkg/utils/conv"
1833
)
1934

2035
func TestServer(t *testing.T) {
@@ -25,11 +40,8 @@ func TestServer(t *testing.T) {
2540
proxyServer := &GrpcServer{BreakerMgr: breakerMgr}
2641
go proxyServer.Start(ctx)
2742

28-
grpcClient := protoconnect.NewThrottlingClient(&http.Client{
29-
Transport: &http2.Transport{
30-
AllowHTTP: true,
31-
},
32-
}, "127.0.0.1:8889")
43+
fmt.Println(proto.TrafficInterceptRule_NORMAL.String())
44+
grpcClient := protoconnect.NewThrottlingClient(proto.DefaultHttpClient, "http://127.0.0.1:8889")
3345

3446
cb := &ctrlmeshv1alpha1.CircuitBreaker{
3547
ObjectMeta: metav1.ObjectMeta{

Diff for: pkg/utils/controller.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2023 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package utils
218

319
import (

0 commit comments

Comments
 (0)