Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the update status error for ehpa #667

Merged
merged 3 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
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
26 changes: 22 additions & 4 deletions pkg/controller/ehpa/effective_hpa_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/client-go/discovery"
"k8s.io/client-go/scale"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -153,15 +155,31 @@ func (c *EffectiveHPAController) Reconcile(ctx context.Context, req ctrl.Request

func (c *EffectiveHPAController) UpdateStatus(ctx context.Context, ehpa *autoscalingapi.EffectiveHorizontalPodAutoscaler, newStatus *autoscalingapi.EffectiveHorizontalPodAutoscalerStatus) {
if !equality.Semantic.DeepEqual(&ehpa.Status, newStatus) {
ehpa.Status = *newStatus
err := c.Status().Update(ctx, ehpa)
ehpaCopy := ehpa.DeepCopy()
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
ehpaCopy.Status = *newStatus
err := c.Status().Update(ctx, ehpaCopy)
if err == nil {
return nil
}

updated := &autoscalingapi.EffectiveHorizontalPodAutoscaler{}
errGet := c.Get(context.TODO(), types.NamespacedName{Namespace: ehpaCopy.Namespace, Name: ehpaCopy.Name}, updated)
if errGet == nil {
ehpaCopy = updated
}

return err

})

if err != nil {
c.Recorder.Event(ehpa, v1.EventTypeWarning, "FailedUpdateStatus", err.Error())
klog.Errorf("Failed to update status, ehpa %s error %v", klog.KObj(ehpa), err)
klog.Errorf("Failed to update status, EffectiveHorizontalPodAutoscaler %s error %v", klog.KObj(ehpa), err)
return
}

klog.Infof("Update EffectiveHorizontalPodAutoscaler status successful, ehpa %s", klog.KObj(ehpa))
klog.V(2).Infof("Update EffectiveHorizontalPodAutoscaler %s status successful ", klog.KObj(ehpa))
}
}

Expand Down
20 changes: 19 additions & 1 deletion pkg/controller/ehpa/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -161,12 +163,28 @@ func (c *EffectiveHPAController) UpdateHPAIfNeed(ctx context.Context, ehpa *auto
}

if needUpdate {
err := c.Update(ctx, hpaExist)
hpaCopy := hpaExist.DeepCopy()
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
err := c.Update(ctx, hpaCopy)
if err == nil {
return nil
}

updated := &autoscalingv2.HorizontalPodAutoscaler{}
errGet := c.Get(context.TODO(), types.NamespacedName{Namespace: hpaCopy.Namespace, Name: hpaCopy.Name}, updated)
if errGet == nil {
hpaCopy = updated
}

return err
})

if err != nil {
c.Recorder.Event(ehpa, v1.EventTypeWarning, "FailedUpdateHPA", err.Error())
klog.Errorf("Failed to update HorizontalPodAutoscaler %s error %v", klog.KObj(hpaExist), err)
return nil, err
}

klog.Infof("Update HorizontalPodAutoscaler successful, HorizontalPodAutoscaler %s", klog.KObj(hpaExist))
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/ehpa/substitute.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (c *EffectiveHPAController) UpdateSubstituteIfNeed(ctx context.Context, ehp
return nil, err
}

klog.Infof("Update Substitute successful, Substitute %s", klog.KObj(substituteExist))
klog.V(2).Infof("Update Substitute successful, Substitute %s", klog.KObj(substituteExist))
}

return substituteExist, nil
Expand Down
21 changes: 19 additions & 2 deletions pkg/controller/ehpa/substitute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/scale"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -72,8 +74,23 @@ func (c *SubstituteController) Reconcile(ctx context.Context, req ctrl.Request)
}

if !equality.Semantic.DeepEqual(&substitute.Status, &newStatus) {
substitute.Status = newStatus
err := c.Status().Update(ctx, substitute)
substituteCopy := substitute.DeepCopy()
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
substituteCopy.Status = newStatus
err := c.Status().Update(ctx, substituteCopy)
if err == nil {
return nil
}

updated := &autoscalingapi.Substitute{}
errGet := c.Get(context.TODO(), types.NamespacedName{Namespace: substituteCopy.Namespace, Name: substituteCopy.Name}, updated)
if errGet == nil {
substituteCopy = updated
}

return err
})

if err != nil {
c.Recorder.Event(substitute, v1.EventTypeWarning, "FailedUpdateStatus", err.Error())
klog.Errorf("Failed to update status, Substitute %s error %v", klog.KObj(substitute), err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/recommendation/recommendation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *RecommendationController) UpdateStatus(ctx context.Context, recommendat
return err
}

klog.Infof("Update Recommendation status successful, Recommendation %s", klog.KObj(recommendation))
klog.V(2).Infof("Update Recommendation status successful, Recommendation %s", klog.KObj(recommendation))
}

return nil
Expand Down
24 changes: 11 additions & 13 deletions pkg/controller/recommendation/recommendation_rule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,10 @@ func (c *RecommendationRuleController) executeMission(ctx context.Context, wg *s
}

func (c *RecommendationRuleController) UpdateStatus(ctx context.Context, recommendationRule *analysisv1alph1.RecommendationRule, newStatus *analysisv1alph1.RecommendationRuleStatus) {
klog.Infof("Updating RecommendationRule %s status", klog.KObj(recommendationRule))
recommendationRuleCopy := recommendationRule.DeepCopy()
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
if !equality.Semantic.DeepEqual(&recommendationRuleCopy.Status, newStatus) {
if !equality.Semantic.DeepEqual(&recommendationRule.Status, newStatus) {
klog.V(2).Infof("Updating RecommendationRule %s status", klog.KObj(recommendationRule))
recommendationRuleCopy := recommendationRule.DeepCopy()
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
recommendationRuleCopy.Status = *newStatus
err := c.Update(ctx, recommendationRuleCopy)
if err == nil {
Expand All @@ -431,18 +431,16 @@ func (c *RecommendationRuleController) UpdateStatus(ctx context.Context, recomme
}

return err
}
})

return nil
})
if err != nil {
c.Recorder.Event(recommendationRule, corev1.EventTypeWarning, "FailedUpdateStatus", err.Error())
klog.Errorf("Failed to update status, RecommendationRule %s error %v", klog.KObj(recommendationRule), err)
return
}

if err != nil {
c.Recorder.Event(recommendationRule, corev1.EventTypeWarning, "FailedUpdateStatus", err.Error())
klog.Errorf("Failed to update status, RecommendationRule %s error %v", klog.KObj(recommendationRule), err)
return
klog.V(2).Infof("Update RecommendationRule status successful, RecommendationRule %s", klog.KObj(recommendationRule))
}

klog.Infof("Update RecommendationRule status successful, RecommendationRule %s", klog.KObj(recommendationRule))
}

type ObjectIdentity struct {
Expand Down
21 changes: 19 additions & 2 deletions pkg/controller/timeseriesprediction/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -173,8 +175,23 @@ func (tc *Controller) doPredict(tsPrediction *predictionapi.TimeSeriesPrediction

func (tc *Controller) UpdateStatus(ctx context.Context, tsPrediction *predictionapi.TimeSeriesPrediction, newStatus *predictionapi.TimeSeriesPredictionStatus) error {
if !equality.Semantic.DeepEqual(&tsPrediction.Status, newStatus) {
tsPrediction.Status = *newStatus
err := tc.Client.Status().Update(ctx, tsPrediction)
tsPredictionCopy := tsPrediction.DeepCopy()
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
tsPredictionCopy.Status = *newStatus
err := tc.Client.Status().Update(ctx, tsPredictionCopy)
if err == nil {
return nil
}

updated := &predictionapi.TimeSeriesPrediction{}
errGet := tc.Get(context.TODO(), types.NamespacedName{Namespace: tsPredictionCopy.Namespace, Name: tsPredictionCopy.Name}, updated)
if errGet == nil {
tsPredictionCopy = updated
}

return err
})

if err != nil {
tc.Recorder.Event(tsPrediction, v1.EventTypeWarning, "FailedUpdateStatus", err.Error())
klog.Errorf("Failed to update status for %v", klog.KObj(tsPrediction))
Expand Down