Skip to content

Commit

Permalink
fix shard exists logic
Browse files Browse the repository at this point in the history
  • Loading branch information
soggycactus committed Jul 19, 2023
1 parent 731f0fc commit b7c4690
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/controller/pod_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,10 @@ func (p *PodMutatingWebhook) Handle(ctx context.Context, req admission.Request)
return admission.Errored(http.StatusBadRequest, ErrMissingTenantLabel)
}

// Find the existing shard for the tenant, if it exists
shuffleShard := v1.ShuffleShard{}
err = p.Client.Get(ctx, types.NamespacedName{Name: tenant}, &shuffleShard)

// If the error is any error other than not found, return an error
if client.IgnoreNotFound(err) != nil {
if err = p.Client.Get(ctx, types.NamespacedName{Name: tenant}, &shuffleShard); client.IgnoreNotFound(err) != nil {
logger.Error(err, "failed to get ShuffleShard")
return admission.Errored(http.StatusInternalServerError, err)
}
Expand All @@ -273,6 +272,7 @@ func (p *PodMutatingWebhook) Handle(ctx context.Context, req admission.Request)
nodeGroups = groups
}

// Create the nodeSelectorTerm to patch
nodeSelectorTerm := corev1.NodeSelectorTerm{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Expand All @@ -283,6 +283,8 @@ func (p *PodMutatingWebhook) Handle(ctx context.Context, req admission.Request)
},
}

// Unfortunately, we need to check for nil pointers on the pod's Affinity
// Appending our node groups to a nil NodeAffinity will cause a panic
switch {
case pod.Spec.Affinity == nil:
pod.Spec.Affinity = &corev1.Affinity{
Expand Down Expand Up @@ -372,13 +374,18 @@ func (p *PodMutatingWebhook) ShardExists(ctx context.Context, shardHash string)
FieldSelector: fields.SelectorFromSet(fields.Set{
"status.shardHash": shardHash,
}),
}); client.IgnoreNotFound(err) != nil {
}); err != nil {
// return true in case the caller doesn't check the err
// to force another iteration of backtracking
return true, err
}

return false, nil
// if no shards exist, return false
if len(shuffleShardList.Items) == 0 {
return false, nil
}

return true, nil
}

// SetupWithManager registers the handler with manager's webhook server
Expand Down

0 comments on commit b7c4690

Please sign in to comment.