Skip to content

Commit f515086

Browse files
committed
support join a cluster via grpc
Signed-off-by: Zhiwei Yin <[email protected]>
1 parent c67ac4c commit f515086

File tree

158 files changed

+18499
-14
lines changed

Some content is hidden

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

158 files changed

+18499
-14
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
)
4141

4242
require (
43+
cloud.google.com/go/compute/metadata v0.5.0 // indirect
4344
dario.cat/mergo v1.0.1 // indirect
4445
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
4546
github.com/BurntSushi/toml v1.5.0 // indirect
@@ -53,6 +54,7 @@ require (
5354
github.com/blang/semver/v4 v4.0.0 // indirect
5455
github.com/cespare/xxhash/v2 v2.3.0 // indirect
5556
github.com/chai2010/gettext-go v1.0.2 // indirect
57+
github.com/cloudevents/sdk-go/v2 v2.16.2 // indirect
5658
github.com/containerd/containerd v1.7.27 // indirect
5759
github.com/containerd/errdefs v0.3.0 // indirect
5860
github.com/containerd/log v0.1.0 // indirect
@@ -75,6 +77,7 @@ require (
7577
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
7678
github.com/gobwas/glob v0.2.3 // indirect
7779
github.com/gogo/protobuf v1.3.2 // indirect
80+
github.com/golang/protobuf v1.5.4 // indirect
7881
github.com/google/btree v1.1.3 // indirect
7982
github.com/google/gnostic-models v0.6.9 // indirect
8083
github.com/google/go-cmp v0.7.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY=
2+
cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY=
13
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
24
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
35
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
@@ -38,6 +40,8 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
3840
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
3941
github.com/chai2010/gettext-go v1.0.2 h1:1Lwwip6Q2QGsAdl/ZKPCwTe9fe0CjlUbqj5bFNSjIRk=
4042
github.com/chai2010/gettext-go v1.0.2/go.mod h1:y+wnP2cHYaVj19NZhYKAwEMH2CI1gNHeQQ+5AjwawxA=
43+
github.com/cloudevents/sdk-go/v2 v2.16.2 h1:ZYDFrYke4FD+jM8TZTJJO6JhKHzOQl2oqpFK1D+NnQM=
44+
github.com/cloudevents/sdk-go/v2 v2.16.2/go.mod h1:laOcGImm4nVJEU+PHnUrKL56CKmRL65RlQF0kRmW/kg=
4145
github.com/containerd/containerd v1.7.27 h1:yFyEyojddO3MIGVER2xJLWoCIn+Up4GaHFquP7hsFII=
4246
github.com/containerd/containerd v1.7.27/go.mod h1:xZmPnl75Vc+BLGt4MIfu6bp+fy03gdHAn9bz+FreFR0=
4347
github.com/containerd/errdefs v0.3.0 h1:FSZgGOeK4yuT/+DnF07/Olde/q4KBoMsaamhXxIMDp4=
@@ -308,6 +312,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
308312
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
309313
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
310314
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
315+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
316+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
311317
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
312318
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
313319
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=

pkg/cmd/accept/exec.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ const (
2828
groupNameBootstrap = "system:bootstrappers:managedcluster"
2929
userNameSignatureBootstrapPrefix = "system:bootstrap:"
3030
userNameSignatureSA = "system:serviceaccount:open-cluster-management:agent-registration-bootstrap"
31+
userNameGRPCSignatureSA = "system:serviceaccount:open-cluster-management-hub:grpc-server-sa"
3132
groupNameSA = "system:serviceaccounts:open-cluster-management"
33+
groupNameGRPC = "system:serviceaccounts:open-cluster-management-hub"
3234
clusterLabel = "open-cluster-management.io/cluster-name"
3335
)
3436

@@ -143,13 +145,15 @@ func (o *Options) approveCSR(kubeClient *kubernetes.Clientset, clusterName strin
143145
for _, item := range csrs.Items {
144146
// Does not have the correct name prefix
145147
if !strings.HasPrefix(item.Spec.Username, userNameSignatureBootstrapPrefix) &&
146-
!strings.HasPrefix(item.Spec.Username, userNameSignatureSA) {
148+
!strings.HasPrefix(item.Spec.Username, userNameSignatureSA) &&
149+
!strings.HasPrefix(item.Spec.Username, userNameGRPCSignatureSA) {
147150
continue
148151
}
149152
// Check groups
150153
groups := sets.NewString(item.Spec.Groups...)
151154
if !groups.Has(groupNameBootstrap) &&
152-
!groups.Has(groupNameSA) {
155+
!groups.Has(groupNameSA) &&
156+
!groups.Has(groupNameGRPC) {
153157
continue
154158
}
155159
passedCSRs = append(passedCSRs, item)

pkg/cmd/join/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,7 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
8888
cmd.Flags().StringVar(&o.managedClusterArn, "managed-cluster-arn", "", "The arn of the managed cluster(i.e. EKS cluster) which will be joining the hub")
8989
cmd.Flags().StringArrayVar(&o.klusterletAnnotations, "klusterlet-annotation", []string{}, fmt.Sprintf("Annotations to set on the ManagedCluster, in key=value format. Note: each key will be automatically prefixed with '%s/', if not set.", operatorv1.ClusterAnnotationsKeyPrefix))
9090
cmd.Flags().StringVar(&o.klusterletValuesFile, "klusterlet-values-file", "", "The path to a YAML file containing klusterlet Helm chart values. The values from the file override both the default klusterlet chart values and the values from other flags.")
91+
cmd.Flags().StringVar(&o.gRPCServer, "grpc-server", "", "The grpc server url of the hub")
92+
cmd.Flags().StringVar(&o.grpcCAFile, "grpc-ca-file", "", "the file path to grpc server ca, required if --hub-grpc-server is set")
9193
return cmd
9294
}

pkg/cmd/join/exec.go

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import (
3636
clusterv1 "open-cluster-management.io/api/cluster/v1"
3737
ocmfeature "open-cluster-management.io/api/feature"
3838
operatorv1 "open-cluster-management.io/api/operator/v1"
39-
4039
"open-cluster-management.io/clusteradm/pkg/cmd/join/preflight"
40+
"open-cluster-management.io/clusteradm/pkg/config"
4141
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
4242
"open-cluster-management.io/clusteradm/pkg/helpers"
4343
"open-cluster-management.io/clusteradm/pkg/helpers/klusterlet"
@@ -48,13 +48,14 @@ import (
4848
"open-cluster-management.io/clusteradm/pkg/helpers/wait"
4949
"open-cluster-management.io/clusteradm/pkg/version"
5050
"open-cluster-management.io/ocm/pkg/operator/helpers/chart"
51+
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options/cert"
52+
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options/grpc"
5153
sdkhelpers "open-cluster-management.io/sdk-go/pkg/helpers"
5254
)
5355

5456
const (
55-
AgentNamespacePrefix = "open-cluster-management-"
56-
57-
OperatorNamesapce = "open-cluster-management"
57+
AgentNamespacePrefix = "open-cluster-management-"
58+
OperatorNamespace = "open-cluster-management"
5859
DefaultOperatorName = "klusterlet"
5960
AwsIrsaAuthentication = "awsirsa"
6061
)
@@ -191,7 +192,7 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
191192
if err != nil {
192193
return err
193194
}
194-
o.HubCADate = cabytes
195+
o.HubCAData = cabytes
195196
}
196197

197198
// code logic of building hub client in join process:
@@ -212,6 +213,12 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
212213
return err
213214
}
214215

216+
if o.gRPCServer != "" {
217+
if err = o.getGRPCCAData(externalClientUnSecure); err != nil {
218+
return err
219+
}
220+
}
221+
215222
// get managed cluster externalServerURL
216223
var kubeClient *kubernetes.Clientset
217224
switch o.mode {
@@ -284,6 +291,11 @@ func (o *Options) validate() error {
284291
return err
285292
}
286293

294+
err = o.setGRPCConfig()
295+
if err != nil {
296+
return err
297+
}
298+
287299
// get ManagedKubeconfig from given file
288300
if o.mode == string(operatorv1.InstallModeHosted) {
289301
managedConfig, err := os.ReadFile(o.managedKubeconfigFile)
@@ -395,7 +407,7 @@ func (o *Options) applyKlusterlet(r *reader.ResourceReader, operatorClient opera
395407
o.klusterletChartConfig.NoOperator = true
396408
}
397409

398-
crds, raw, err := chart.RenderKlusterletChart(o.klusterletChartConfig, OperatorNamesapce)
410+
crds, raw, err := chart.RenderKlusterletChart(o.klusterletChartConfig, OperatorNamespace)
399411
if err != nil {
400412
return err
401413
}
@@ -457,7 +469,7 @@ func checkIfRegistrationOperatorAvailable(f util.Factory) (bool, error) {
457469
return false, err
458470
}
459471

460-
deploy, err := client.AppsV1().Deployments(OperatorNamesapce).
472+
deploy, err := client.AppsV1().Deployments(OperatorNamespace).
461473
Get(context.TODO(), DefaultOperatorName, metav1.GetOptions{})
462474
if err != nil {
463475
if errors.IsNotFound(err) {
@@ -550,7 +562,7 @@ func waitUntilRegistrationOperatorConditionIsTrue(w io.Writer, f util.Factory, t
550562

551563
return helpers.WatchUntil(
552564
func() (watch.Interface, error) {
553-
return client.CoreV1().Pods(OperatorNamesapce).
565+
return client.CoreV1().Pods(OperatorNamespace).
554566
Watch(context.TODO(), metav1.ListOptions{
555567
TimeoutSeconds: &timeout,
556568
LabelSelector: "app=klusterlet",
@@ -664,9 +676,9 @@ func (o *Options) createClientcmdapiv1Config(externalClientUnSecure *kubernetes.
664676
bootstrapConfig := bootstrapExternalConfigUnSecure.DeepCopy()
665677
bootstrapConfig.Clusters[0].Cluster.InsecureSkipTLSVerify = false
666678
bootstrapConfig.Clusters[0].Cluster.Server = o.hubAPIServer
667-
if o.HubCADate != nil {
679+
if o.HubCAData != nil {
668680
// directly set ca-data if --ca-file is set
669-
bootstrapConfig.Clusters[0].Cluster.CertificateAuthorityData = o.HubCADate
681+
bootstrapConfig.Clusters[0].Cluster.CertificateAuthorityData = o.HubCAData
670682
} else {
671683
// get ca data from externalClientUnsecure, ca may empty(cluster-info exists with no ca data)
672684
ca, err := sdkhelpers.GetCACert(externalClientUnSecure)
@@ -713,6 +725,51 @@ func (o *Options) setKubeconfig() error {
713725
return nil
714726
}
715727

728+
func (o *Options) setGRPCConfig() error {
729+
if o.gRPCServer == "" {
730+
return nil
731+
}
732+
733+
gRPCConfig := grpc.GRPCConfig{
734+
CertConfig: cert.CertConfig{
735+
CAData: o.GRPCCAData,
736+
},
737+
URL: o.gRPCServer,
738+
Token: o.token,
739+
}
740+
741+
configStr, err := yaml.Marshal(gRPCConfig)
742+
if err != nil {
743+
return fmt.Errorf("failed to marshal GRPC server configuration. %v", err)
744+
}
745+
746+
o.klusterletChartConfig.GRPCConfig = string(configStr)
747+
o.klusterletChartConfig.Klusterlet.RegistrationConfiguration.RegistrationDriver = operatorv1.RegistrationDriver{
748+
AuthType: operatorv1.GRPCAuthType,
749+
}
750+
return nil
751+
}
752+
753+
func (o *Options) getGRPCCAData(kubeClient kubernetes.Interface) error {
754+
if o.grpcCAFile != "" {
755+
caData, err := os.ReadFile(o.grpcCAFile)
756+
if err != nil {
757+
return fmt.Errorf("--grpc-ca-file is invalid")
758+
}
759+
o.GRPCCAData = caData
760+
return nil
761+
}
762+
763+
cm, err := kubeClient.CoreV1().ConfigMaps(config.HubClusterNamespace).Get(context.TODO(),
764+
config.CABundleConfigmap, metav1.GetOptions{})
765+
if err != nil {
766+
return err
767+
}
768+
769+
o.GRPCCAData = []byte(cm.Data["ca-bundle.crt"])
770+
return nil
771+
}
772+
716773
func mergeCertificateData(caBundles ...[]byte) ([]byte, error) {
717774
var all []*x509.Certificate
718775
for _, caBundle := range caBundles {

pkg/cmd/join/options.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ type Options struct {
1919
token string
2020
// The external hub apiserver url (https://<host>:<port>)
2121
hubAPIServer string
22+
// The grpc server of the hub cluster
23+
gRPCServer string
24+
2225
// The hub ca-file(optional)
2326
caFile string
27+
28+
// The grpc ca file which can be found in the configmap ca-bundle-configmap in open-cluster-management-hub ns
29+
grpcCAFile string
30+
2431
// the name under the cluster must be imported
2532
clusterName string
2633

@@ -59,11 +66,14 @@ type Options struct {
5966
hubInClusterEndpoint string
6067

6168
// Values below are tempoary data
62-
// HubCADate: data in hub ca file
63-
HubCADate []byte
69+
// HubCAData: data in hub ca file
70+
HubCAData []byte
6471
// hub config
6572
HubConfig *clientcmdapiv1.Config
6673

74+
// GRPCCAData: ca data used by the GRPC server
75+
GRPCCAData []byte
76+
6777
// The URL of a forward proxy server which will be used by agnets on the managed cluster
6878
// to connect to the hub cluster (optional)
6979
proxyURL string

pkg/config/env.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ const (
1616
ManagedClusterNamespace = "open-cluster-management-agent"
1717
ManagedProxyConfigurationName = "cluster-proxy"
1818
ImagePullSecret = "open-cluster-management-image-pull-credentials"
19+
CABundleConfigmap = "ca-bundle-configmap"
1920
)

0 commit comments

Comments
 (0)