-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
Copyright 2023 Tim Ebert. | ||
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 clusterring | ||
|
||
import ( | ||
"context" | ||
|
||
coordinationv1 "k8s.io/api/coordination/v1" | ||
"k8s.io/utils/clock" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1" | ||
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/leases" | ||
) | ||
|
||
// ControllerName is the name of this controller. | ||
const ControllerName = "clusterring" | ||
|
||
// AddToManager adds Reconciler to the given manager. | ||
func (r *Reconciler) AddToManager(mgr manager.Manager) error { | ||
if r.Client == nil { | ||
r.Client = mgr.GetClient() | ||
} | ||
if r.Clock == nil { | ||
r.Clock = clock.RealClock{} | ||
} | ||
|
||
return builder.ControllerManagedBy(mgr). | ||
Named(ControllerName). | ||
For(&shardingv1alpha1.ClusterRing{}, builder.WithPredicates(r.ClusterRingPredicate())). | ||
Watches(&coordinationv1.Lease{}, handler.EnqueueRequestsFromMapFunc(MapLeaseToClusterRing), builder.WithPredicates(r.LeasePredicate())). | ||
WithOptions(controller.Options{ | ||
MaxConcurrentReconciles: 5, | ||
}). | ||
Complete(r) | ||
} | ||
|
||
func (r *Reconciler) ClusterRingPredicate() predicate.Predicate { | ||
return predicate.And( | ||
predicate.GenerationChangedPredicate{}, | ||
// ignore deletion of ClusterRings | ||
predicate.Funcs{ | ||
CreateFunc: func(_ event.CreateEvent) bool { return true }, | ||
UpdateFunc: func(_ event.UpdateEvent) bool { return true }, | ||
DeleteFunc: func(_ event.DeleteEvent) bool { return false }, | ||
}, | ||
) | ||
} | ||
|
||
func MapLeaseToClusterRing(ctx context.Context, obj client.Object) []reconcile.Request { | ||
ring, ok := obj.GetLabels()[shardingv1alpha1.LabelClusterRing] | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return []reconcile.Request{{NamespacedName: client.ObjectKey{Name: ring}}} | ||
} | ||
|
||
func (r *Reconciler) LeasePredicate() predicate.Predicate { | ||
return predicate.And( | ||
predicate.NewPredicateFuncs(isShardLease), | ||
predicate.Funcs{ | ||
CreateFunc: func(_ event.CreateEvent) bool { return true }, | ||
UpdateFunc: func(e event.UpdateEvent) bool { | ||
oldLease, ok := e.ObjectOld.(*coordinationv1.Lease) | ||
if !ok { | ||
return false | ||
} | ||
newLease, ok := e.ObjectNew.(*coordinationv1.Lease) | ||
if !ok { | ||
return false | ||
} | ||
|
||
// only enqueue ring if the shard's state changed | ||
return leases.ToState(oldLease, r.Clock) != leases.ToState(newLease, r.Clock) | ||
}, | ||
DeleteFunc: func(_ event.DeleteEvent) bool { return true }, | ||
}, | ||
) | ||
} | ||
|
||
func isShardLease(obj client.Object) bool { | ||
return obj.GetLabels()[shardingv1alpha1.LabelClusterRing] != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Copyright 2023 Tim Ebert. | ||
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 clusterring | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
coordinationv1 "k8s.io/api/coordination/v1" | ||
apiequality "k8s.io/apimachinery/pkg/api/equality" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/utils/clock" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1" | ||
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/leases" | ||
) | ||
|
||
//+kubebuilder:rbac:groups=sharding.timebertt.dev,resources=clusterrings,verbs=get;list;watch | ||
//+kubebuilder:rbac:groups=sharding.timebertt.dev,resources=clusterrings/status,verbs=update;patch | ||
|
||
// Reconciler reconciles ClusterRings. | ||
type Reconciler struct { | ||
Client client.Client | ||
Clock clock.PassiveClock | ||
} | ||
|
||
// Reconcile reconciles a ClusterRing object. | ||
func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { | ||
log := logf.FromContext(ctx) | ||
|
||
clusterRing := &shardingv1alpha1.ClusterRing{} | ||
if err := r.Client.Get(ctx, req.NamespacedName, clusterRing); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
log.V(1).Info("Object is gone, stop reconciling") | ||
return reconcile.Result{}, nil | ||
} | ||
return reconcile.Result{}, fmt.Errorf("error retrieving object from store: %w", err) | ||
} | ||
|
||
clusterRingCopy := clusterRing.DeepCopy() | ||
|
||
// update status with the latest observed generation | ||
clusterRing.Status.ObservedGeneration = clusterRing.Generation | ||
|
||
leaseList := &coordinationv1.LeaseList{} | ||
if err := r.Client.List(ctx, leaseList, client.MatchingLabels{shardingv1alpha1.LabelClusterRing: clusterRing.Name}); err != nil { | ||
return reconcile.Result{}, fmt.Errorf("error listing Leases for ClusterRing: %w", err) | ||
} | ||
|
||
shards := leases.ToShards(leaseList.Items, r.Clock) | ||
clusterRing.Status.Shards = int32(len(shards)) | ||
clusterRing.Status.AvailableShards = int32(len(shards.AvailableShards())) | ||
|
||
if !apiequality.Semantic.DeepEqual(clusterRing.Status, clusterRingCopy.Status) { | ||
if err := r.Client.Status().Update(ctx, clusterRing); err != nil { | ||
return reconcile.Result{}, fmt.Errorf("error updating ClusterRing status: %w", err) | ||
} | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} |