Skip to content

Commit

Permalink
fix: trying to prevent operator crash when accessing SO.Status.ScaleT…
Browse files Browse the repository at this point in the history
…argetGVKR (kedacore#4722)

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>
  • Loading branch information
zroubalik authored and SpiritZhou committed Jun 30, 2023
1 parent 478b39d commit 6065ef8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ New deprecation(s):
- **General**: Refactor several functions for Status & Conditions handling into pkg util functions ([#2906](https://github.com/kedacore/keda/pull/2906))
- **General**: Bump `kubernetes-sigs/controller-runtime` to v0.15.0 and code alignment ([#4582](https://github.com/kedacore/keda/pull/4582))
- **General**: Stop logging errors for paused ScaledObject (with "autoscaling.keda.sh/paused-replicas" annotation) by skipping reconciliation loop for the object (stop the scale loop and delete the HPA) ([#4253](https://github.com/kedacore/keda/pull/4253))
- **General**: Trying to prevent operator crash when accessing `ScaledObject.Status.ScaleTargetGVKR` ([#4389](https://github.com/kedacore/keda/issues/4389))

## v2.10.1

Expand Down
18 changes: 18 additions & 0 deletions pkg/scaling/resolver/scale_resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ func ResolveScaleTargetPodSpec(ctx context.Context, kubeClient client.Client, sc
case *kedav1alpha1.ScaledObject:
// Try to get a real object instance for better cache usage, but fall back to an Unstructured if needed.
podTemplateSpec := corev1.PodTemplateSpec{}

// trying to prevent operator crashes, due to some race condition, sometimes obj.Status.ScaleTargetGVKR is nil
// see https://github.com/kedacore/keda/issues/4389
if obj.Status.ScaleTargetGVKR == nil {
scaledObject := &kedav1alpha1.ScaledObject{}
err := kubeClient.Get(ctx, types.NamespacedName{Name: obj.Name, Namespace: obj.Namespace}, scaledObject)
if err != nil {
log.Error(err, "failed to get ScaledObject", "name", obj.Name, "namespace", obj.Namespace)
return nil, "", err
}
obj = scaledObject
}
if obj.Status.ScaleTargetGVKR == nil {
err := fmt.Errorf("failed to get ScaledObject.Status.ScaleTargetGVKR, probably invalid ScaledObject cache")
log.Error(err, "failed to get ScaledObject.Status.ScaleTargetGVKR, probably invalid ScaledObject cache", "scaledObject.Name", obj.Name, "scaledObject.Namespace", obj.Namespace)
return nil, "", err
}

gvk := obj.Status.ScaleTargetGVKR.GroupVersionKind()
objKey := client.ObjectKey{Namespace: obj.Namespace, Name: obj.Spec.ScaleTargetRef.Name}

Expand Down

0 comments on commit 6065ef8

Please sign in to comment.