Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hub election leader controller #2281

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions charts/yurt-manager/crds/apps.openyurt.io_nodepools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ spec:
LeaderNodeLabelSelector is used only when LeaderElectionStrategy is mark. leader Yurhub will be
elected from nodes that filtered by this label selector.
type: object
leaderReplicas:
description: |-
LeaderReplicas is used for specifying the number of leader replicas in the nodepool.
If the field is not specified, the default value is 1.
format: int32
type: integer
poolScopeMetadata:
description: |-
PoolScopeMetadata is used for specifying resources which will be shared in the nodepool.
Expand Down
34 changes: 34 additions & 0 deletions charts/yurt-manager/templates/yurt-manager-auto-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ metadata:
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: yurt-manager-hubleader-controller
namespace: {{ .Release.Namespace }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: yurt-manager-load-balancer-set-controller
namespace: {{ .Release.Namespace }}
Expand Down Expand Up @@ -471,6 +477,21 @@ rules:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: yurt-manager-hubleader-controller
rules:
- apiGroups:
- apps.openyurt.io
resources:
- nodepool
- nodepool/status
verbs:
- get
- patch
- update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: yurt-manager-load-balancer-set-controller
rules:
Expand Down Expand Up @@ -1035,6 +1056,19 @@ subjects:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: yurt-manager-hubleader-controller-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: yurt-manager-hubleader-controller
subjects:
- kind: ServiceAccount
name: yurt-manager-hubleader-controller
namespace: {{ .Release.Namespace }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: yurt-manager-load-balancer-set-controller-binding
roleRef:
Expand Down
69 changes: 69 additions & 0 deletions cmd/yurt-manager/app/options/hubleadercontroller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2025 The OpenYurt Authors.

Licensed under the Apache License, Version 2.0 (the License);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an AS IS BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package options

import (
"github.com/spf13/pflag"

"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/hubleader/config"
)

type HubLeaderControllerOptions struct {
*config.HubLeaderControllerConfiguration
}

func NewHubLeaderControllerOptions() *HubLeaderControllerOptions {
return &HubLeaderControllerOptions{
&config.HubLeaderControllerConfiguration{
ConcurrentHubLeaderWorkers: 3,
},

Check warning on line 33 in cmd/yurt-manager/app/options/hubleadercontroller.go

View check run for this annotation

Codecov / codecov/patch

cmd/yurt-manager/app/options/hubleadercontroller.go#L29-L33

Added lines #L29 - L33 were not covered by tests
}
}

// AddFlags adds flags related to hubleader for yurt-manager to the specified FlagSet.
func (h *HubLeaderControllerOptions) AddFlags(fs *pflag.FlagSet) {
if h == nil {
return

Check warning on line 40 in cmd/yurt-manager/app/options/hubleadercontroller.go

View check run for this annotation

Codecov / codecov/patch

cmd/yurt-manager/app/options/hubleadercontroller.go#L38-L40

Added lines #L38 - L40 were not covered by tests
}

fs.Int32Var(
&h.ConcurrentHubLeaderWorkers,
"concurrent-hubleader-workers",
h.ConcurrentHubLeaderWorkers,
"The number of nodepool objects that are allowed to reconcile concurrently.",
)

Check warning on line 48 in cmd/yurt-manager/app/options/hubleadercontroller.go

View check run for this annotation

Codecov / codecov/patch

cmd/yurt-manager/app/options/hubleadercontroller.go#L43-L48

Added lines #L43 - L48 were not covered by tests
}

// ApplyTo fills up hubleader config with options.
func (h *HubLeaderControllerOptions) ApplyTo(cfg *config.HubLeaderControllerConfiguration) error {
if h == nil {
return nil

Check warning on line 54 in cmd/yurt-manager/app/options/hubleadercontroller.go

View check run for this annotation

Codecov / codecov/patch

cmd/yurt-manager/app/options/hubleadercontroller.go#L52-L54

Added lines #L52 - L54 were not covered by tests
}

cfg.ConcurrentHubLeaderWorkers = h.ConcurrentHubLeaderWorkers

Check warning on line 57 in cmd/yurt-manager/app/options/hubleadercontroller.go

View check run for this annotation

Codecov / codecov/patch

cmd/yurt-manager/app/options/hubleadercontroller.go#L57

Added line #L57 was not covered by tests

return nil

Check warning on line 59 in cmd/yurt-manager/app/options/hubleadercontroller.go

View check run for this annotation

Codecov / codecov/patch

cmd/yurt-manager/app/options/hubleadercontroller.go#L59

Added line #L59 was not covered by tests
}

// Validate checks validation of HubLeaderControllerOptions.
func (h *HubLeaderControllerOptions) Validate() []error {
if h == nil {
return nil

Check warning on line 65 in cmd/yurt-manager/app/options/hubleadercontroller.go

View check run for this annotation

Codecov / codecov/patch

cmd/yurt-manager/app/options/hubleadercontroller.go#L63-L65

Added lines #L63 - L65 were not covered by tests
}
errs := []error{}
return errs

Check warning on line 68 in cmd/yurt-manager/app/options/hubleadercontroller.go

View check run for this annotation

Codecov / codecov/patch

cmd/yurt-manager/app/options/hubleadercontroller.go#L67-L68

Added lines #L67 - L68 were not covered by tests
}
12 changes: 11 additions & 1 deletion cmd/yurt-manager/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
GatewayDNSController *GatewayDNSControllerOptions
GatewayInternalSvcController *GatewayInternalSvcControllerOptions
GatewayPublicSvcController *GatewayPublicSvcControllerOptions
HubLeaderController *HubLeaderControllerOptions
}

// NewYurtManagerOptions creates a new YurtManagerOptions with a default config.
Expand Down Expand Up @@ -73,6 +74,7 @@
GatewayDNSController: NewGatewayDNSControllerOptions(),
GatewayInternalSvcController: NewGatewayInternalSvcControllerOptions(),
GatewayPublicSvcController: NewGatewayPublicSvcControllerOptions(),
HubLeaderController: NewHubLeaderControllerOptions(),

Check warning on line 77 in cmd/yurt-manager/app/options/options.go

View check run for this annotation

Codecov / codecov/patch

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

Added line #L77 was not covered by tests
}

return &s, nil
Expand Down Expand Up @@ -101,6 +103,7 @@
y.GatewayDNSController.AddFlags(fss.FlagSet("gatewaydns controller"))
y.GatewayInternalSvcController.AddFlags(fss.FlagSet("gatewayinternalsvc controller"))
y.GatewayPublicSvcController.AddFlags(fss.FlagSet("gatewaypublicsvc controller"))
y.HubLeaderController.AddFlags(fss.FlagSet("hubleader controller"))

Check warning on line 106 in cmd/yurt-manager/app/options/options.go

View check run for this annotation

Codecov / codecov/patch

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

Added line #L106 was not covered by tests
return fss
}

Expand Down Expand Up @@ -128,6 +131,7 @@
errs = append(errs, y.GatewayDNSController.Validate()...)
errs = append(errs, y.GatewayInternalSvcController.Validate()...)
errs = append(errs, y.GatewayPublicSvcController.Validate()...)
errs = append(errs, y.HubLeaderController.Validate()...)

Check warning on line 134 in cmd/yurt-manager/app/options/options.go

View check run for this annotation

Codecov / codecov/patch

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

Added line #L134 was not covered by tests
return utilerrors.NewAggregate(errs)
}

Expand Down Expand Up @@ -196,11 +200,17 @@
if err := y.GatewayPublicSvcController.ApplyTo(&c.ComponentConfig.GatewayPublicSvcController); err != nil {
return err
}
if err := y.HubLeaderController.ApplyTo(&c.ComponentConfig.HubLeaderController); err != nil {
return err

Check warning on line 204 in cmd/yurt-manager/app/options/options.go

View check run for this annotation

Codecov / codecov/patch

cmd/yurt-manager/app/options/options.go#L203-L204

Added lines #L203 - L204 were not covered by tests
}
return nil
}

// Config return a yurt-manager config objective
func (y *YurtManagerOptions) Config(allControllers []string, controllerAliases map[string]string) (*config.Config, error) {
func (y *YurtManagerOptions) Config(
allControllers []string,
controllerAliases map[string]string,
) (*config.Config, error) {

Check warning on line 213 in cmd/yurt-manager/app/options/options.go

View check run for this annotation

Codecov / codecov/patch

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

Added line #L213 was not covered by tests
if err := y.Validate(allControllers, controllerAliases); err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/yurt-manager/names/controller_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
NodeLifeCycleController = "node-life-cycle-controller"
NodeBucketController = "node-bucket-controller"
LoadBalancerSetController = "load-balancer-set-controller"
HubLeaderController = "hubleader-controller"
)

func YurtManagerControllerAliases() map[string]string {
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/apps/v1alpha1/nodepool_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
// Set interconnectivity to false which will not use leader election strategy or reuse list/watch events
dst.Spec.InterConnectivity = false
dst.Spec.LeaderElectionStrategy = string(v1beta2.ElectionStrategyRandom)
dst.Spec.LeaderReplicas = 1

Check warning on line 49 in pkg/apis/apps/v1alpha1/nodepool_conversion.go

View check run for this annotation

Codecov / codecov/patch

pkg/apis/apps/v1alpha1/nodepool_conversion.go#L49

Added line #L49 was not covered by tests

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

Expand Down
1 change: 1 addition & 0 deletions pkg/apis/apps/v1beta1/nodepool_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
// Set interconnectivity to false which will not use leader election strategy or reuse list/watch events
dst.Spec.InterConnectivity = false
dst.Spec.LeaderElectionStrategy = string(v1beta2.ElectionStrategyRandom)
dst.Spec.LeaderReplicas = 1

Check warning on line 49 in pkg/apis/apps/v1beta1/nodepool_conversion.go

View check run for this annotation

Codecov / codecov/patch

pkg/apis/apps/v1beta1/nodepool_conversion.go#L49

Added line #L49 was not covered by tests

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

Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/apps/v1beta2/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@
obj.Annotations = make(map[string]string)
}

if obj.Spec.LeaderReplicas <= 0 {
obj.Spec.LeaderReplicas = 1

Check warning on line 27 in pkg/apis/apps/v1beta2/default.go

View check run for this annotation

Codecov / codecov/patch

pkg/apis/apps/v1beta2/default.go#L26-L27

Added lines #L26 - L27 were not covered by tests
}
}
5 changes: 5 additions & 0 deletions pkg/apis/apps/v1beta2/nodepool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ type NodePoolSpec struct {
// PoolScopeMetadata is used for specifying resources which will be shared in the nodepool.
// And it is supported to modify dynamically. and the default value is v1.Service and discovery.Endpointslice.
PoolScopeMetadata []metav1.GroupVersionKind `json:"poolScopeMetadata,omitempty"`

// LeaderReplicas is used for specifying the number of leader replicas in the nodepool.
// If the field is not specified, the default value is 1.
// + optional
LeaderReplicas int32 `json:"leaderReplicas,omitempty"`
}

// NodePoolStatus defines the observed state of NodePool
Expand Down
4 changes: 4 additions & 0 deletions pkg/yurtmanager/controller/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

csrapproverconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/csrapprover/config"
daemonpodupdaterconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/daemonpodupdater/config"
hubleaderconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/hubleader/config"
loadbalancersetconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/loadbalancerset/loadbalancerset/config"
nodebucketconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodebucket/config"
nodepoolconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodepool/config"
Expand Down Expand Up @@ -105,6 +106,9 @@ type YurtManagerConfiguration struct {

// GatewayPublicSvcController holds configuration for GatewayPublicSvcController related features.
GatewayPublicSvcController gatewaypublicsvcconfig.GatewayPublicSvcControllerConfiguration

// HubLeaderController holds configuration for HubLeaderController related features.
HubLeaderController hubleaderconfig.HubLeaderControllerConfiguration
}

type GenericConfiguration struct {
Expand Down
22 changes: 22 additions & 0 deletions pkg/yurtmanager/controller/hubleader/config/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright 2023 The OpenYurt Authors.

Licensed under the Apache License, Version 2.0 (the License);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an AS IS BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

// HubLeaderControllerConfiguration contains elements describing HubLeaderController.
type HubLeaderControllerConfiguration struct {
ConcurrentHubLeaderWorkers int32
}
Loading
Loading