Skip to content

Commit

Permalink
enhance: clear pods which shardingconfig is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikykun committed Jan 11, 2024
1 parent 24cb737 commit e200574
Showing 1 changed file with 42 additions and 1 deletion.
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
}

Check warning on line 107 in pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go#L105-L107

Added lines #L105 - L107 were not covered by tests
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

Check warning on line 296 in pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go#L288-L296

Added lines #L288 - L296 were not covered by tests
}
shardName := po.Labels[ctrlmeshv1alpha1.ShardingConfigInjectedKey]
if shardName == "" {
continue

Check warning on line 300 in pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go#L298-L300

Added lines #L298 - L300 were not covered by tests
}
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

Check warning on line 311 in pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go#L302-L311

Added lines #L302 - L311 were not covered by tests
}
selector, _ := metav1.LabelSelectorAsSelector(shard.Spec.Selector)
if !selector.Matches(labels.Set(po.Labels)) {
err := r.Delete(ctx, po)
if !errors.IsNotFound(err) {
return err
}

Check warning on line 318 in pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go#L313-L318

Added lines #L313 - L318 were not covered by tests
}
}
return nil

Check warning on line 321 in pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/manager/controllers/shardingconfigserver/shardingconfig_controller.go#L321

Added line #L321 was not covered by tests
}

// 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

0 comments on commit e200574

Please sign in to comment.