Skip to content

Commit 6660dca

Browse files
author
Simon Tien
committed
feat: add hub election leader controller
fix: update configuration param
1 parent d690808 commit 6660dca

24 files changed

+1460
-90
lines changed

charts/yurt-manager/crds/apps.openyurt.io_nodepools.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ spec:
373373
LeaderNodeLabelSelector is used only when LeaderElectionStrategy is mark. leader Yurhub will be
374374
elected from nodes that filtered by this label selector.
375375
type: object
376+
leaderReplicas:
377+
description: |-
378+
LeaderReplicas is used for specifying the number of leader replicas in the nodepool.
379+
If the field is not specified, the default value is 1.
380+
format: int32
381+
type: integer
376382
poolScopeMetadata:
377383
description: |-
378384
PoolScopeMetadata is used for specifying resources which will be shared in the nodepool.

charts/yurt-manager/templates/yurt-manager-auto-generated.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ metadata:
5050
---
5151
apiVersion: v1
5252
kind: ServiceAccount
53+
metadata:
54+
name: yurt-manager-hubleader-controller
55+
namespace: {{ .Release.Namespace }}
56+
---
57+
apiVersion: v1
58+
kind: ServiceAccount
5359
metadata:
5460
name: yurt-manager-load-balancer-set-controller
5561
namespace: {{ .Release.Namespace }}
@@ -471,6 +477,21 @@ rules:
471477
---
472478
apiVersion: rbac.authorization.k8s.io/v1
473479
kind: ClusterRole
480+
metadata:
481+
name: yurt-manager-hubleader-controller
482+
rules:
483+
- apiGroups:
484+
- apps.openyurt.io
485+
resources:
486+
- nodepool
487+
- nodepool/status
488+
verbs:
489+
- get
490+
- patch
491+
- update
492+
---
493+
apiVersion: rbac.authorization.k8s.io/v1
494+
kind: ClusterRole
474495
metadata:
475496
name: yurt-manager-load-balancer-set-controller
476497
rules:
@@ -1035,6 +1056,19 @@ subjects:
10351056
---
10361057
apiVersion: rbac.authorization.k8s.io/v1
10371058
kind: ClusterRoleBinding
1059+
metadata:
1060+
name: yurt-manager-hubleader-controller-binding
1061+
roleRef:
1062+
apiGroup: rbac.authorization.k8s.io
1063+
kind: ClusterRole
1064+
name: yurt-manager-hubleader-controller
1065+
subjects:
1066+
- kind: ServiceAccount
1067+
name: yurt-manager-hubleader-controller
1068+
namespace: {{ .Release.Namespace }}
1069+
---
1070+
apiVersion: rbac.authorization.k8s.io/v1
1071+
kind: ClusterRoleBinding
10381072
metadata:
10391073
name: yurt-manager-load-balancer-set-controller-binding
10401074
roleRef:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2025 The OpenYurt 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+
17+
package options
18+
19+
import (
20+
"github.com/spf13/pflag"
21+
22+
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/hubleader/config"
23+
)
24+
25+
type HubLeaderControllerOptions struct {
26+
*config.HubLeaderControllerConfiguration
27+
}
28+
29+
func NewHubLeaderControllerOptions() *HubLeaderControllerOptions {
30+
return &HubLeaderControllerOptions{
31+
&config.HubLeaderControllerConfiguration{
32+
ConcurrentHubLeaderWorkers: 3,
33+
},
34+
}
35+
}
36+
37+
// AddFlags adds flags related to hubleader for yurt-manager to the specified FlagSet.
38+
func (h *HubLeaderControllerOptions) AddFlags(fs *pflag.FlagSet) {
39+
if h == nil {
40+
return
41+
}
42+
43+
fs.Int32Var(
44+
&h.ConcurrentHubLeaderWorkers,
45+
"concurrent-hubleader-workers",
46+
h.ConcurrentHubLeaderWorkers,
47+
"The number of nodepool objects that are allowed to reconcile concurrently.",
48+
)
49+
}
50+
51+
// ApplyTo fills up hubleader config with options.
52+
func (h *HubLeaderControllerOptions) ApplyTo(cfg *config.HubLeaderControllerConfiguration) error {
53+
if h == nil {
54+
return nil
55+
}
56+
57+
cfg.ConcurrentHubLeaderWorkers = h.ConcurrentHubLeaderWorkers
58+
59+
return nil
60+
}
61+
62+
// Validate checks validation of HubLeaderControllerOptions.
63+
func (h *HubLeaderControllerOptions) Validate() []error {
64+
if h == nil {
65+
return nil
66+
}
67+
errs := []error{}
68+
return errs
69+
}

cmd/yurt-manager/app/options/options.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type YurtManagerOptions struct {
4646
GatewayDNSController *GatewayDNSControllerOptions
4747
GatewayInternalSvcController *GatewayInternalSvcControllerOptions
4848
GatewayPublicSvcController *GatewayPublicSvcControllerOptions
49+
HubLeaderController *HubLeaderControllerOptions
4950
}
5051

5152
// NewYurtManagerOptions creates a new YurtManagerOptions with a default config.
@@ -73,6 +74,7 @@ func NewYurtManagerOptions() (*YurtManagerOptions, error) {
7374
GatewayDNSController: NewGatewayDNSControllerOptions(),
7475
GatewayInternalSvcController: NewGatewayInternalSvcControllerOptions(),
7576
GatewayPublicSvcController: NewGatewayPublicSvcControllerOptions(),
77+
HubLeaderController: NewHubLeaderControllerOptions(),
7678
}
7779

7880
return &s, nil
@@ -101,6 +103,7 @@ func (y *YurtManagerOptions) Flags(allControllers, disabledByDefaultControllers
101103
y.GatewayDNSController.AddFlags(fss.FlagSet("gatewaydns controller"))
102104
y.GatewayInternalSvcController.AddFlags(fss.FlagSet("gatewayinternalsvc controller"))
103105
y.GatewayPublicSvcController.AddFlags(fss.FlagSet("gatewaypublicsvc controller"))
106+
y.HubLeaderController.AddFlags(fss.FlagSet("hubleader controller"))
104107
return fss
105108
}
106109

@@ -128,6 +131,7 @@ func (y *YurtManagerOptions) Validate(allControllers []string, controllerAliases
128131
errs = append(errs, y.GatewayDNSController.Validate()...)
129132
errs = append(errs, y.GatewayInternalSvcController.Validate()...)
130133
errs = append(errs, y.GatewayPublicSvcController.Validate()...)
134+
errs = append(errs, y.HubLeaderController.Validate()...)
131135
return utilerrors.NewAggregate(errs)
132136
}
133137

@@ -196,11 +200,17 @@ func (y *YurtManagerOptions) ApplyTo(c *config.Config, controllerAliases map[str
196200
if err := y.GatewayPublicSvcController.ApplyTo(&c.ComponentConfig.GatewayPublicSvcController); err != nil {
197201
return err
198202
}
203+
if err := y.HubLeaderController.ApplyTo(&c.ComponentConfig.HubLeaderController); err != nil {
204+
return err
205+
}
199206
return nil
200207
}
201208

202209
// Config return a yurt-manager config objective
203-
func (y *YurtManagerOptions) Config(allControllers []string, controllerAliases map[string]string) (*config.Config, error) {
210+
func (y *YurtManagerOptions) Config(
211+
allControllers []string,
212+
controllerAliases map[string]string,
213+
) (*config.Config, error) {
204214
if err := y.Validate(allControllers, controllerAliases); err != nil {
205215
return nil, err
206216
}

cmd/yurt-manager/names/controller_names.go

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const (
3737
NodeLifeCycleController = "node-life-cycle-controller"
3838
NodeBucketController = "node-bucket-controller"
3939
LoadBalancerSetController = "load-balancer-set-controller"
40+
HubLeaderController = "hubleader-controller"
4041
)
4142

4243
func YurtManagerControllerAliases() map[string]string {

pkg/apis/apps/v1alpha1/nodepool_conversion.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (src *NodePool) ConvertTo(dstRaw conversion.Hub) error {
4646
// Set interconnectivity to false which will not use leader election strategy or reuse list/watch events
4747
dst.Spec.InterConnectivity = false
4848
dst.Spec.LeaderElectionStrategy = string(v1beta2.ElectionStrategyRandom)
49+
dst.Spec.LeaderReplicas = 1
4950

5051
klog.V(4).Infof("convert from v1alpha1 to v1beta1 for nodepool %s", dst.Name)
5152

pkg/apis/apps/v1beta1/nodepool_conversion.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (src *NodePool) ConvertTo(dstRaw conversion.Hub) error {
4646
// Set interconnectivity to false which will not use leader election strategy or reuse list/watch events
4747
dst.Spec.InterConnectivity = false
4848
dst.Spec.LeaderElectionStrategy = string(v1beta2.ElectionStrategyRandom)
49+
dst.Spec.LeaderReplicas = 1
4950

5051
klog.V(4).Infof("convert from v1beta to v1beta2 for nodepool %s", dst.Name)
5152

pkg/apis/apps/v1beta2/default.go

+3
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ func SetDefaultsNodePool(obj *NodePool) {
2323
obj.Annotations = make(map[string]string)
2424
}
2525

26+
if obj.Spec.LeaderReplicas <= 0 {
27+
obj.Spec.LeaderReplicas = 1
28+
}
2629
}

pkg/apis/apps/v1beta2/nodepool_types.go

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ type NodePoolSpec struct {
8484
// PoolScopeMetadata is used for specifying resources which will be shared in the nodepool.
8585
// And it is supported to modify dynamically. and the default value is v1.Service and discovery.Endpointslice.
8686
PoolScopeMetadata []metav1.GroupVersionKind `json:"poolScopeMetadata,omitempty"`
87+
88+
// LeaderReplicas is used for specifying the number of leader replicas in the nodepool.
89+
// If the field is not specified, the default value is 1.
90+
// + optional
91+
LeaderReplicas int32 `json:"leaderReplicas,omitempty"`
8792
}
8893

8994
// NodePoolStatus defines the observed state of NodePool

pkg/yurtmanager/controller/apis/config/types.go

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
csrapproverconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/csrapprover/config"
2525
daemonpodupdaterconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/daemonpodupdater/config"
26+
hubleaderconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/hubleader/config"
2627
loadbalancersetconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/loadbalancerset/loadbalancerset/config"
2728
nodebucketconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodebucket/config"
2829
nodepoolconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodepool/config"
@@ -105,6 +106,9 @@ type YurtManagerConfiguration struct {
105106

106107
// GatewayPublicSvcController holds configuration for GatewayPublicSvcController related features.
107108
GatewayPublicSvcController gatewaypublicsvcconfig.GatewayPublicSvcControllerConfiguration
109+
110+
// HubLeaderController holds configuration for HubLeaderController related features.
111+
HubLeaderController hubleaderconfig.HubLeaderControllerConfiguration
108112
}
109113

110114
type GenericConfiguration struct {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright 2023 The OpenYurt 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+
17+
package config
18+
19+
// HubLeaderControllerConfiguration contains elements describing HubLeaderController.
20+
type HubLeaderControllerConfiguration struct {
21+
ConcurrentHubLeaderWorkers int32
22+
}

0 commit comments

Comments
 (0)