Skip to content

Commit

Permalink
feat: add hub election leader controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Tien committed Feb 2, 2025
1 parent d690808 commit c936915
Show file tree
Hide file tree
Showing 24 changed files with 1,395 additions and 90 deletions.
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
50 changes: 50 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,37 @@ rules:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: yurt-manager-hubleader-controller
rules:
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
- list
- apiGroups:
- apps.openyurt.io
resources:
- nodepool
verbs:
- get
- list
- patch
- update
- watch
- apiGroups:
- apps.openyurt.io
resources:
- 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 +1072,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
60 changes: 60 additions & 0 deletions cmd/yurt-manager/app/options/hubleadercontroller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
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{},

Check warning on line 31 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-L31

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

//fs.BoolVar(&n.CreateDefaultPool, "create-default-pool", n.CreateDefaultPool, "Create default cloud/edge pools if indicated.")
}

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

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

View check run for this annotation

Codecov / codecov/patch

cmd/yurt-manager/app/options/hubleadercontroller.go#L45-L47

Added lines #L45 - L47 were not covered by tests
}

return nil

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L50 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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#L58-L59

Added lines #L58 - L59 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 @@ type YurtManagerOptions struct {
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 @@ func NewYurtManagerOptions() (*YurtManagerOptions, error) {
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 @@ func (y *YurtManagerOptions) Flags(allControllers, disabledByDefaultControllers
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 @@ func (y *YurtManagerOptions) Validate(allControllers []string, controllerAliases
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 @@ func (y *YurtManagerOptions) ApplyTo(c *config.Config, controllerAliases map[str
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 @@ func (src *NodePool) ConvertTo(dstRaw conversion.Hub) error {
// 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 @@ func (src *NodePool) ConvertTo(dstRaw conversion.Hub) error {
// 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 @@ func SetDefaultsNodePool(obj *NodePool) {
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
21 changes: 21 additions & 0 deletions pkg/yurtmanager/controller/hubleader/config/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
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 {
}
Loading

0 comments on commit c936915

Please sign in to comment.