Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -99,7 +102,9 @@ func (r *ShardingConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
return reconcile.Result{}, err
}

if err = r.clearOldPods(ctx, shardingConfig.Namespace); err != nil {
return reconcile.Result{}, err
}
allPods, err := r.getPodsForShardingConfig(ctx, shardingConfig)
if err != nil {
return reconcile.Result{}, err
Expand Down Expand Up @@ -280,6 +285,42 @@ func (r *ShardingConfigReconciler) getPodsForShardingConfig(ctx context.Context,
return pods, nil
}

func (r *ShardingConfigReconciler) clearOldPods(ctx context.Context, namespace string) error {
podList := v1.PodList{}
if err := r.List(ctx, &podList, client.InNamespace(namespace), client.HasLabels{ctrlmeshv1alpha1.ShardingConfigInjectedKey}); err != nil {
return err
}
for i := range podList.Items {
po := &podList.Items[i]
if po.DeletionTimestamp != nil {
continue
}
shardName := po.Labels[ctrlmeshv1alpha1.ShardingConfigInjectedKey]
if shardName == "" {
continue
}
shard := &ctrlmeshv1alpha1.ShardingConfig{}
if err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: shardName}, shard); err != nil {
if !errors.IsNotFound(err) {
return err
}
err = r.Delete(ctx, po)
if !errors.IsNotFound(err) {
return err
}
continue
}
selector, _ := metav1.LabelSelectorAsSelector(shard.Spec.Selector)
if !selector.Matches(labels.Set(po.Labels)) {
err := r.Delete(ctx, po)
if !errors.IsNotFound(err) {
return err
}
}
}
return nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *ShardingConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.recorder = mgr.GetEventRecorderFor("sharding-config-controller")
Expand Down