Skip to content

Commit 7924226

Browse files
authored
grpc server (#1058)
Signed-off-by: Wei Liu <[email protected]>
1 parent cbff56a commit 7924226

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+5808
-73
lines changed

cmd/registration/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"open-cluster-management.io/ocm/pkg/cmd/spoke"
1818
"open-cluster-management.io/ocm/pkg/cmd/webhook"
1919
"open-cluster-management.io/ocm/pkg/features"
20+
"open-cluster-management.io/ocm/pkg/server/grpc"
2021
"open-cluster-management.io/ocm/pkg/version"
2122
)
2223

@@ -62,6 +63,7 @@ func newRegistrationCommand() *cobra.Command {
6263
cmd.AddCommand(hub.NewRegistrationController())
6364
cmd.AddCommand(spoke.NewRegistrationAgent())
6465
cmd.AddCommand(webhook.NewRegistrationWebhook())
66+
cmd.AddCommand(grpc.NewGRPCServer())
6567

6668
return cmd
6769
}

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/aws/aws-sdk-go-v2/service/eks v1.63.1
99
github.com/aws/aws-sdk-go-v2/service/iam v1.38.6
1010
github.com/aws/smithy-go v1.22.2
11+
github.com/cloudevents/sdk-go/v2 v2.15.3-0.20240911135016-682f3a9684e4
1112
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
1213
github.com/evanphx/json-patch v5.9.11+incompatible
1314
github.com/ghodss/yaml v1.0.0
@@ -25,6 +26,7 @@ require (
2526
github.com/spf13/pflag v1.0.5
2627
github.com/stretchr/testify v1.10.0
2728
github.com/valyala/fasttemplate v1.2.2
29+
golang.org/x/net v0.38.0
2830
gopkg.in/yaml.v2 v2.4.0
2931
helm.sh/helm/v3 v3.17.3
3032
k8s.io/api v0.32.4
@@ -39,7 +41,7 @@ require (
3941
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738
4042
open-cluster-management.io/addon-framework v1.0.0
4143
open-cluster-management.io/api v1.0.0
42-
open-cluster-management.io/sdk-go v1.0.0
44+
open-cluster-management.io/sdk-go v1.0.1-0.20250708024404-422b23814b5d
4345
sigs.k8s.io/about-api v0.0.0-20250131010323-518069c31c03
4446
sigs.k8s.io/cluster-inventory-api v0.0.0-20240730014211-ef0154379848
4547
sigs.k8s.io/controller-runtime v0.20.2
@@ -48,7 +50,7 @@ require (
4850
)
4951

5052
require (
51-
cel.dev/expr v0.19.1 // indirect
53+
cel.dev/expr v0.23.1 // indirect
5254
cloud.google.com/go/compute/metadata v0.5.0 // indirect
5355
dario.cat/mergo v1.0.1 // indirect
5456
github.com/BurntSushi/toml v1.4.0 // indirect
@@ -76,7 +78,6 @@ require (
7678
github.com/cespare/xxhash/v2 v2.3.0 // indirect
7779
github.com/cloudevents/sdk-go/protocol/kafka_confluent/v2 v2.0.0-20240413090539-7fef29478991 // indirect
7880
github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2 v2.0.0-20241008145627-6bcc075b5b6c // indirect
79-
github.com/cloudevents/sdk-go/v2 v2.15.3-0.20240911135016-682f3a9684e4 // indirect
8081
github.com/confluentinc/confluent-kafka-go/v2 v2.3.0 // indirect
8182
github.com/coreos/go-semver v0.3.1 // indirect
8283
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
@@ -155,7 +156,6 @@ require (
155156
go.uber.org/zap v1.27.0 // indirect
156157
golang.org/x/crypto v0.37.0 // indirect
157158
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 // indirect
158-
golang.org/x/net v0.38.0 // indirect
159159
golang.org/x/oauth2 v0.28.0 // indirect
160160
golang.org/x/sync v0.13.0 // indirect
161161
golang.org/x/sys v0.32.0 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
cel.dev/expr v0.19.1 h1:NciYrtDRIR0lNCnH1LFJegdjspNx9fI59O7TWcua/W4=
2-
cel.dev/expr v0.19.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
1+
cel.dev/expr v0.23.1 h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=
2+
cel.dev/expr v0.23.1/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
33
cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY=
44
cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY=
55
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
@@ -493,8 +493,8 @@ open-cluster-management.io/addon-framework v1.0.0 h1:ejTk4hPAJnwCSxQhY/tVDPg3SeH
493493
open-cluster-management.io/addon-framework v1.0.0/go.mod h1:Gw9zRGvuNJJ3XhTYanIuA7FFFw0EjtoE74l5OBZCZf8=
494494
open-cluster-management.io/api v1.0.0 h1:54QllH9DTudCk6VrGt0q8CDsE3MghqJeTaTN4RHZpE0=
495495
open-cluster-management.io/api v1.0.0/go.mod h1:/OeqXycNBZQoe3WG6ghuWsMgsKGuMZrK8ZpsU6gWL0Y=
496-
open-cluster-management.io/sdk-go v1.0.0 h1:pIiAHM/hzV3rEw3LSMgZuAUiNgkBrn8hLxFvJM5frw0=
497-
open-cluster-management.io/sdk-go v1.0.0/go.mod h1:vkLwIDN9W+WBlrHgHxMl5ZoHRT+H5qOq3cXAk7U5AJc=
496+
open-cluster-management.io/sdk-go v1.0.1-0.20250708024404-422b23814b5d h1:sYgNfYyQ6O7sfiVOUaMuoK/CTeWnTNTfVKY8dWORBgw=
497+
open-cluster-management.io/sdk-go v1.0.1-0.20250708024404-422b23814b5d/go.mod h1:LYX48E3h96XGnm6o+GomV0DSf15w1i9crtggj2HeDvI=
498498
sigs.k8s.io/about-api v0.0.0-20250131010323-518069c31c03 h1:1ShFiMjGQOR/8jTBkmZrk1gORxnvMwm1nOy2/DbHg4U=
499499
sigs.k8s.io/about-api v0.0.0-20250131010323-518069c31c03/go.mod h1:F1pT4mK53U6F16/zuaPSYpBaR7x5Kjym6aKJJC0/DHU=
500500
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.0 h1:CPT0ExVicCzcpeN4baWEV2ko2Z/AsiZgEdwgcfwLgMo=

pkg/common/helpers/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ package helpers
33
const (
44
AwsIrsaAuthType = "awsirsa"
55
CSRAuthType = "csr"
6+
GRPCCAuthType = "grpc"
67
)
8+
9+
const GRPCCAuthSigner = "open-cluster-management.io/grpc"

pkg/registration/hub/manager.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"open-cluster-management.io/ocm/pkg/registration/register"
4747
awsirsa "open-cluster-management.io/ocm/pkg/registration/register/aws_irsa"
4848
"open-cluster-management.io/ocm/pkg/registration/register/csr"
49+
"open-cluster-management.io/ocm/pkg/registration/register/grpc"
4950
)
5051

5152
// HubManagerOptions holds configuration for hub manager controller
@@ -59,6 +60,8 @@ type HubManagerOptions struct {
5960
AutoApprovedARNPatterns []string
6061
AwsResourceTags []string
6162
Labels string
63+
GRPCCAFile string
64+
GRPCCAKeyFile string
6265
}
6366

6467
// NewHubManagerOptions returns a HubManagerOptions
@@ -93,6 +96,8 @@ func (m *HubManagerOptions) AddFlags(fs *pflag.FlagSet) {
9396
fs.StringSliceVar(&m.AwsResourceTags, "aws-resource-tags", m.AwsResourceTags, "A list of tags to apply to AWS resources created through the OCM controllers")
9497
fs.StringVar(&m.Labels, "labels", m.Labels,
9598
"Labels to be added to the resources created by registration controller. The format is key1=value1,key2=value2.")
99+
fs.StringVar(&m.GRPCCAFile, "grpc-ca-file", m.GRPCCAFile, "ca file to sign client cert for grpc")
100+
fs.StringVar(&m.GRPCCAKeyFile, "grpc-key-file", m.GRPCCAKeyFile, "ca key file to sign client cert for grpc")
96101
m.ImportOption.AddFlags(fs)
97102
}
98103

@@ -195,6 +200,13 @@ func (m *HubManagerOptions) RunControllerManagerWithInformers(
195200
return err
196201
}
197202
drivers = append(drivers, awsIRSAHubDriver)
203+
case commonhelpers.GRPCCAuthType:
204+
grpcHubDriver, err := grpc.NewGRPCHubDriver(
205+
kubeClient, kubeInformers, m.GRPCCAKeyFile, m.GRPCCAFile, 720*time.Hour, controllerContext.EventRecorder)
206+
if err != nil {
207+
return err
208+
}
209+
drivers = append(drivers, grpcHubDriver)
198210
}
199211
}
200212
hubDriver := register.NewAggregatedHubDriver(drivers...)

pkg/registration/hub/manifests/rbac/managedcluster-registration-clusterrole.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rules:
1616
# TODO for backward compatible, we do not limit the resource name
1717
# remove this after we no longer support lower versions kubernetes (less than 1.14)
1818
#resourceNames: ["managed-cluster-lease"]
19-
verbs: ["get", "update"]
19+
verbs: ["get", "list", "watch", "update"]
2020
# Allow agent to get/list/watch managed cluster addons
2121
- apiGroups: ["addon.open-cluster-management.io"]
2222
resources: ["managedclusteraddons"]

pkg/registration/register/csr/csr.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ import (
99
"math/rand"
1010
"os"
1111
"path"
12-
"reflect"
1312
"strings"
1413
"time"
1514

1615
"github.com/openshift/library-go/pkg/controller/factory"
1716
"github.com/openshift/library-go/pkg/operator/events"
1817
certificates "k8s.io/api/certificates/v1"
1918
corev1 "k8s.io/api/core/v1"
19+
"k8s.io/apimachinery/pkg/api/equality"
2020
"k8s.io/apimachinery/pkg/api/meta"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2322
"k8s.io/client-go/informers"
2423
"k8s.io/client-go/kubernetes"
2524
"k8s.io/client-go/tools/cache"
@@ -281,7 +280,7 @@ func (c *CSRDriver) IsHubKubeConfigValid(ctx context.Context, secretOption regis
281280
if secretOption.ClusterName != clusterNameInCert || secretOption.AgentName != agentNameInCert {
282281
logger.V(4).Info("Certificate in file is issued for different agent",
283282
"certPath", certPath,
284-
"issuedFor", fmt.Sprintf("%s:%s", secretOption.ClusterName, secretOption.AgentName),
283+
"issuedFor", fmt.Sprintf("%s:%s", clusterNameInCert, agentNameInCert),
285284
"expectedFor", fmt.Sprintf("%s:%s", secretOption.ClusterName, secretOption.AgentName))
286285

287286
return false, nil
@@ -349,23 +348,25 @@ func (c *CSRDriver) BuildClients(ctx context.Context, secretOption register.Secr
349348
return nil, fmt.Errorf("failed to create CSR control: %w", err)
350349
}
351350

352-
err = csrControl.Informer().AddIndexers(cache.Indexers{
353-
indexByCluster: indexByClusterFunc,
354-
})
351+
err = c.SetCSRControl(csrControl, secretOption.ClusterName)
355352
if err != nil {
356353
return nil, err
357354
}
355+
return clients, nil
356+
}
358357

359-
err = csrControl.Informer().AddIndexers(cache.Indexers{
360-
indexByAddon: indexByAddonFunc,
361-
})
362-
if err != nil {
363-
utilruntime.HandleError(err)
358+
func (c *CSRDriver) SetCSRControl(csrControl CSRControl, clusterName string) error {
359+
if err := csrControl.Informer().AddIndexers(cache.Indexers{indexByCluster: indexByClusterFunc}); err != nil {
360+
return err
361+
}
362+
363+
if err := csrControl.Informer().AddIndexers(cache.Indexers{indexByAddon: indexByAddonFunc}); err != nil {
364+
return err
364365
}
365366

366367
c.csrControl = csrControl
367-
c.haltCSRCreation = haltCSRCreationFunc(csrControl.Informer().GetIndexer(), secretOption.ClusterName)
368-
return clients, nil
368+
c.haltCSRCreation = haltCSRCreationFunc(csrControl.Informer().GetIndexer(), clusterName)
369+
return nil
369370
}
370371

371372
var _ register.RegisterDriver = &CSRDriver{}
@@ -481,7 +482,7 @@ func hasAdditionalSecretData(additionalSecretData map[string][]byte, secret *cor
481482
return fmt.Errorf("key %q not found in secret %q", k, secret.Namespace+"/"+secret.Name)
482483
}
483484

484-
if !reflect.DeepEqual(v, value) {
485+
if !equality.Semantic.DeepEqual(v, value) {
485486
return fmt.Errorf("key %q in secret %q does not match the expected value",
486487
k, secret.Namespace+"/"+secret.Name)
487488
}

pkg/registration/register/csr/csr_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,16 @@ func TestIsHubKubeConfigValidFunc(t *testing.T) {
388388
tlsCert: cert1.Cert,
389389
isValid: false,
390390
},
391+
{
392+
name: "invalid issuer",
393+
clusterName: "cluster2",
394+
agentName: "agent1",
395+
kubeconfig: kubeconfig,
396+
bootstapKubeconfig: testinghelpers.NewKubeconfig("c1", "https://127.0.0.1:6001", "", "", nil, nil, nil),
397+
tlsKey: cert1.Key,
398+
tlsCert: cert1.Cert,
399+
isValid: false,
400+
},
391401
{
392402
name: "valid hub client config",
393403
clusterName: "cluster1",

pkg/registration/register/factory/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@ import (
77
"open-cluster-management.io/ocm/pkg/registration/register"
88
awsirsa "open-cluster-management.io/ocm/pkg/registration/register/aws_irsa"
99
"open-cluster-management.io/ocm/pkg/registration/register/csr"
10+
"open-cluster-management.io/ocm/pkg/registration/register/grpc"
1011
)
1112

1213
type Options struct {
1314
RegistrationAuth string
1415
CSROption *csr.Option
1516
AWSISRAOption *awsirsa.AWSOption
17+
GRPCOption *grpc.Option
1618
}
1719

1820
func NewOptions() *Options {
1921
return &Options{
2022
CSROption: csr.NewCSROption(),
2123
AWSISRAOption: awsirsa.NewAWSOption(),
24+
GRPCOption: grpc.NewOptions(),
2225
}
2326
}
2427

@@ -27,12 +30,15 @@ func (s *Options) AddFlags(fs *pflag.FlagSet) {
2730
"The type of authentication to use to authenticate with hub.")
2831
s.CSROption.AddFlags(fs)
2932
s.AWSISRAOption.AddFlags(fs)
33+
s.GRPCOption.AddFlags(fs)
3034
}
3135

3236
func (s *Options) Validate() error {
3337
switch s.RegistrationAuth {
3438
case helpers.AwsIrsaAuthType:
3539
return s.AWSISRAOption.Validate()
40+
case helpers.GRPCCAuthType:
41+
return s.GRPCOption.Validate()
3642
default:
3743
return s.CSROption.Validate()
3844
}
@@ -42,6 +48,8 @@ func (s *Options) Driver(secretOption register.SecretOption) (register.RegisterD
4248
switch s.RegistrationAuth {
4349
case helpers.AwsIrsaAuthType:
4450
return awsirsa.NewAWSIRSADriver(s.AWSISRAOption, secretOption), nil
51+
case helpers.GRPCCAuthType:
52+
return grpc.NewGRPCDriver(s.GRPCOption, s.CSROption, secretOption)
4553
default:
4654
return csr.NewCSRDriver(s.CSROption, secretOption)
4755
}

pkg/registration/register/factory/options_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
awsirsa "open-cluster-management.io/ocm/pkg/registration/register/aws_irsa"
77
"open-cluster-management.io/ocm/pkg/registration/register/csr"
8+
"open-cluster-management.io/ocm/pkg/registration/register/grpc"
89
)
910

1011
func TestValidate(t *testing.T) {
@@ -51,6 +52,34 @@ func TestValidate(t *testing.T) {
5152
},
5253
expectErr: false,
5354
},
55+
{
56+
name: "grpc validate",
57+
opt: &Options{
58+
RegistrationAuth: "grpc",
59+
GRPCOption: &grpc.Option{},
60+
},
61+
expectErr: true,
62+
},
63+
{
64+
name: "grpc validate pass (bootstrap config)",
65+
opt: &Options{
66+
RegistrationAuth: "grpc",
67+
GRPCOption: &grpc.Option{
68+
BootstrapConfigFile: "test-bootstrap-config",
69+
},
70+
},
71+
expectErr: false,
72+
},
73+
{
74+
name: "grpc validate pass",
75+
opt: &Options{
76+
RegistrationAuth: "grpc",
77+
GRPCOption: &grpc.Option{
78+
ConfigFile: "test-config",
79+
},
80+
},
81+
expectErr: false,
82+
},
5483
}
5584

5685
for _, tt := range tests {

0 commit comments

Comments
 (0)