1) Install Sharding Components
kubectl apply --server-side –k \
"https://github.com/timebertt/kubernetes-controller-sharding//config/default"
2) ControllerRing
| apiVersion: sharding.timebertt.dev/v1alpha1 |
| kind: ControllerRing |
| metadata: |
| name: webhosting-operator |
| spec: |
| resources: |
| - group: webhosting.timebertt.dev |
| resource: websites |
| controlledResources: |
| - group: apps |
| resource: deployments |
| - group: networking.k8s.io |
| resource: ingresses |
| |
| apiVersion: sharding.timebertt.dev/v1alpha1 |
| kind: ControllerRing |
| metadata: |
| name: webhosting-operator |
| spec: |
| resources: |
| - group: webhosting.timebertt.dev |
| resource: websites |
| controlledResources: |
| - group: apps |
| resource: deployments |
| - group: networking.k8s.io |
| resource: ingresses |
| |
| apiVersion: sharding.timebertt.dev/v1alpha1 |
| kind: ControllerRing |
| metadata: |
| name: webhosting-operator |
| spec: |
| resources: |
| - group: webhosting.timebertt.dev |
| resource: websites |
| controlledResources: |
| - group: apps |
| resource: deployments |
| - group: networking.k8s.io |
| resource: ingresses |
| |
3) Shard Lease
| apiVersion: coordination.k8s.io/v1 |
| kind: Lease |
| metadata: |
| labels: |
| alpha.sharding.timebertt.dev/controllerring: webhosting-operator |
| name: webhosting-operator-65ffcdb674-8sst9 |
| namespace: webhosting-system |
| spec: |
| holderIdentity: webhosting-operator-65ffcdb674-8sst9 |
| acquireTime: "2025-04-03T10:45:51.992779Z" |
| renewTime: "2025-04-03T11:02:26.817751Z" |
| leaseDurationSeconds: 15 |
| apiVersion: coordination.k8s.io/v1 |
| kind: Lease |
| metadata: |
| labels: |
| alpha.sharding.timebertt.dev/controllerring: webhosting-operator |
| name: webhosting-operator-65ffcdb674-8sst9 |
| namespace: webhosting-system |
| spec: |
| holderIdentity: webhosting-operator-65ffcdb674-8sst9 |
| acquireTime: "2025-04-03T10:45:51.992779Z" |
| renewTime: "2025-04-03T11:02:26.817751Z" |
| leaseDurationSeconds: 15 |
| apiVersion: coordination.k8s.io/v1 |
| kind: Lease |
| metadata: |
| labels: |
| alpha.sharding.timebertt.dev/controllerring: webhosting-operator |
| name: webhosting-operator-65ffcdb674-8sst9 |
| namespace: webhosting-system |
| spec: |
| holderIdentity: webhosting-operator-65ffcdb674-8sst9 |
| acquireTime: "2025-04-03T10:45:51.992779Z" |
| renewTime: "2025-04-03T11:02:26.817751Z" |
| leaseDurationSeconds: 15 |
3) Shard Lease (Go)
| shardLease, err := shardlease.NewResourceLock(restConfig, shardlease.Options{ |
| ControllerRingName: "webhosting-operator", |
| }) |
| if err != nil { |
| return err |
| } |
| |
| mgr, err := manager.New(restConfig, manager.Options{ |
| LeaderElection: true, |
| LeaderElectionResourceLockInterface: shardLease, |
| LeaderElectionReleaseOnCancel: true, |
| |
| }) |
| if err != nil { |
| return err |
| } |
| shardLease, err := shardlease.NewResourceLock(restConfig, shardlease.Options{ |
| ControllerRingName: "webhosting-operator", |
| }) |
| if err != nil { |
| return err |
| } |
| |
| mgr, err := manager.New(restConfig, manager.Options{ |
| LeaderElection: true, |
| LeaderElectionResourceLockInterface: shardLease, |
| LeaderElectionReleaseOnCancel: true, |
| |
| }) |
| if err != nil { |
| return err |
| } |
| shardLease, err := shardlease.NewResourceLock(restConfig, shardlease.Options{ |
| ControllerRingName: "webhosting-operator", |
| }) |
| if err != nil { |
| return err |
| } |
| |
| mgr, err := manager.New(restConfig, manager.Options{ |
| LeaderElection: true, |
| LeaderElectionResourceLockInterface: shardLease, |
| LeaderElectionReleaseOnCancel: true, |
| |
| }) |
| if err != nil { |
| return err |
| } |
4) Filter Watch Cache
Only watch and reconcile objects with this label:
shard.alpha.sharding.timebertt.dev/webhosting-operator: "<shard-name>"
4) Filter Watch Cache (Go)
| labelSelector := labels.SelectorFromSet(labels.Set{ |
| |
| shardingv1alpha1.LabelShard("webhosting-operator"): shardLease.Identity(), |
| }) |
| |
| mgr, err := manager.New(restConfig, manager.Options{ |
| Cache: cache.Options{ |
| DefaultLabelSelector: labelSelector, |
| }, |
| }) |
| if err != nil { |
| return err |
| } |
| labelSelector := labels.SelectorFromSet(labels.Set{ |
| |
| shardingv1alpha1.LabelShard("webhosting-operator"): shardLease.Identity(), |
| }) |
| |
| mgr, err := manager.New(restConfig, manager.Options{ |
| Cache: cache.Options{ |
| DefaultLabelSelector: labelSelector, |
| }, |
| }) |
| if err != nil { |
| return err |
| } |
5) Drain Operations
Stop reconciling objects with this label and remove the labels:
drain.alpha.sharding.timebertt.dev/webhosting-operator: "true"
5) Drain Operations (Go)
| builder.ControllerManagedBy(mgr). |
| For(&webhostingv1alpha1.Website{}, builder.WithPredicates( |
| WebsitePredicate(), |
| )). |
| Owns(&appsv1.Deployment{}, builder.WithPredicates(DeploymentPredicate())). |
| Complete( |
| reconciler, |
| ) |
| builder.ControllerManagedBy(mgr). |
| For(&webhostingv1alpha1.Website{}, builder.WithPredicates( |
| WebsitePredicate(), |
| )). |
| Owns(&appsv1.Deployment{}, builder.WithPredicates(DeploymentPredicate())). |
| Complete( |
| reconciler, |
| ) |
5) Drain Operations (Go)
| builder.ControllerManagedBy(mgr). |
| For(&webhostingv1alpha1.Website{}, builder.WithPredicates( |
| shardcontroller.Predicate(controllerRing, shardName, WebsitePredicate()), |
| )). |
| Owns(&appsv1.Deployment{}, builder.WithPredicates(DeploymentPredicate())). |
| Complete( |
| reconciler, |
| ) |
| builder.ControllerManagedBy(mgr). |
| For(&webhostingv1alpha1.Website{}, builder.WithPredicates( |
| shardcontroller.Predicate(controllerRing, shardName, WebsitePredicate()), |
| )). |
| Owns(&appsv1.Deployment{}, builder.WithPredicates(DeploymentPredicate())). |
| Complete( |
| reconciler, |
| ) |
5) Drain Operations (Go)
| builder.ControllerManagedBy(mgr). |
| For(&webhostingv1alpha1.Website{}, builder.WithPredicates( |
| shardcontroller.Predicate(controllerRing, shardName, WebsitePredicate()), |
| )). |
| Owns(&appsv1.Deployment{}, builder.WithPredicates(DeploymentPredicate())). |
| Complete( |
| shardcontroller.NewShardedReconciler(mgr). |
| For(&webhostingv1alpha1.Website{}). |
| InControllerRing(controllerRing). |
| WithShardName(shardName). |
| MustBuild(reconciler), |
| ) |
Summary