Skip to content

Commit

Permalink
Fix UpdateStatus() function
Browse files Browse the repository at this point in the history
Signed-off-by: hossainemruz <emruz@appscode.com>
  • Loading branch information
hossainemruz committed May 18, 2020
1 parent a7bd75a commit dbb0287
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 39 deletions.
17 changes: 10 additions & 7 deletions client/clientset/versioned/typed/stash/v1alpha1/util/recovery.go
Expand Up @@ -99,7 +99,7 @@ func TryUpdateRecovery(c cs.StashV1alpha1Interface, meta metav1.ObjectMeta, tran
}

func SetRecoveryStats(c cs.StashV1alpha1Interface, recovery *api.Recovery, path string, d time.Duration, phase api.RecoveryPhase) (*api.Recovery, error) {
out, err := UpdateRecoveryStatus(c, recovery, func(in *api.RecoveryStatus) *api.RecoveryStatus {
out, err := UpdateRecoveryStatus(c, recovery.ObjectMeta, func(in *api.RecoveryStatus) *api.RecoveryStatus {
found := false
for _, stats := range in.Stats {
if stats.Path == path {
Expand All @@ -122,27 +122,30 @@ func SetRecoveryStats(c cs.StashV1alpha1Interface, recovery *api.Recovery, path

func UpdateRecoveryStatus(
c cs.StashV1alpha1Interface,
in *api.Recovery,
meta metav1.ObjectMeta,
transform func(*api.RecoveryStatus) *api.RecoveryStatus,
) (result *api.Recovery, err error) {
apply := func(x *api.Recovery) *api.Recovery {
out := &api.Recovery{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(in.Status.DeepCopy()),
Status: *transform(x.Status.DeepCopy()),
}
return out
}

attempt := 0
cur := in.DeepCopy()
cur, err := c.Recoveries(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) {
attempt++
var e2 error
result, e2 = c.Recoveries(in.Namespace).UpdateStatus(apply(cur))
result, e2 = c.Recoveries(meta.Namespace).UpdateStatus(apply(cur))
if kerr.IsConflict(e2) {
latest, e3 := c.Recoveries(in.Namespace).Get(in.Name, metav1.GetOptions{})
latest, e3 := c.Recoveries(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
switch {
case e3 == nil:
cur = latest
Expand All @@ -159,7 +162,7 @@ func UpdateRecoveryStatus(
})

if err != nil {
err = fmt.Errorf("failed to update status of Recovery %s/%s after %d attempts due to %v", in.Namespace, in.Name, attempt, err)
err = fmt.Errorf("failed to update status of Recovery %s/%s after %d attempts due to %v", meta.Namespace, meta.Name, attempt, err)
}
return
}
Expand Up @@ -99,27 +99,30 @@ func TryUpdateRepository(c cs.StashV1alpha1Interface, meta metav1.ObjectMeta, tr

func UpdateRepositoryStatus(
c cs.StashV1alpha1Interface,
in *api.Repository,
meta metav1.ObjectMeta,
transform func(*api.RepositoryStatus) *api.RepositoryStatus,
) (result *api.Repository, err error) {
apply := func(x *api.Repository) *api.Repository {
out := &api.Repository{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(in.Status.DeepCopy()),
Status: *transform(x.Status.DeepCopy()),
}
return out
}

attempt := 0
cur := in.DeepCopy()
cur, err := c.Repositories(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) {
attempt++
var e2 error
result, e2 = c.Repositories(in.Namespace).UpdateStatus(apply(cur))
result, e2 = c.Repositories(meta.Namespace).UpdateStatus(apply(cur))
if kerr.IsConflict(e2) {
latest, e3 := c.Repositories(in.Namespace).Get(in.Name, metav1.GetOptions{})
latest, e3 := c.Repositories(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
switch {
case e3 == nil:
cur = latest
Expand All @@ -136,7 +139,7 @@ func UpdateRepositoryStatus(
})

if err != nil {
err = fmt.Errorf("failed to update status of Repository %s/%s after %d attempts due to %v", in.Namespace, in.Name, attempt, err)
err = fmt.Errorf("failed to update status of Repository %s/%s after %d attempts due to %v", meta.Namespace, meta.Name, attempt, err)
}
return
}
Expand Up @@ -99,27 +99,30 @@ func TryUpdateBackupBatch(c cs.StashV1beta1Interface, meta metav1.ObjectMeta, tr

func UpdateBackupBatchStatus(
c cs.StashV1beta1Interface,
in *api.BackupBatch,
meta metav1.ObjectMeta,
transform func(*api.BackupBatchStatus) *api.BackupBatchStatus,
) (result *api.BackupBatch, err error) {
apply := func(x *api.BackupBatch) *api.BackupBatch {
out := &api.BackupBatch{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(in.Status.DeepCopy()),
Status: *transform(x.Status.DeepCopy()),
}
return out
}

attempt := 0
cur := in.DeepCopy()
cur, err := c.BackupBatches(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) {
attempt++
var e2 error
result, e2 = c.BackupBatches(in.Namespace).UpdateStatus(apply(cur))
result, e2 = c.BackupBatches(meta.Namespace).UpdateStatus(apply(cur))
if kerr.IsConflict(e2) {
latest, e3 := c.BackupBatches(in.Namespace).Get(in.Name, metav1.GetOptions{})
latest, e3 := c.BackupBatches(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
switch {
case e3 == nil:
cur = latest
Expand All @@ -136,7 +139,7 @@ func UpdateBackupBatchStatus(
})

if err != nil {
err = fmt.Errorf("failed to update status of BackupBatch %s/%s after %d attempts due to %v", in.Namespace, in.Name, attempt, err)
err = fmt.Errorf("failed to update status of BackupBatch %s/%s after %d attempts due to %v", meta.Namespace, meta.Name, attempt, err)
}
return
}
Expand Up @@ -99,27 +99,30 @@ func TryUpdateBackupConfiguration(c cs.StashV1beta1Interface, meta metav1.Object

func UpdateBackupConfigurationStatus(
c cs.StashV1beta1Interface,
in *api.BackupConfiguration,
meta metav1.ObjectMeta,
transform func(*api.BackupConfigurationStatus) *api.BackupConfigurationStatus,
) (result *api.BackupConfiguration, err error) {
apply := func(x *api.BackupConfiguration) *api.BackupConfiguration {
out := &api.BackupConfiguration{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(in.Status.DeepCopy()),
Status: *transform(x.Status.DeepCopy()),
}
return out
}

attempt := 0
cur := in.DeepCopy()
cur, err := c.BackupConfigurations(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) {
attempt++
var e2 error
result, e2 = c.BackupConfigurations(in.Namespace).UpdateStatus(apply(cur))
result, e2 = c.BackupConfigurations(meta.Namespace).UpdateStatus(apply(cur))
if kerr.IsConflict(e2) {
latest, e3 := c.BackupConfigurations(in.Namespace).Get(in.Name, metav1.GetOptions{})
latest, e3 := c.BackupConfigurations(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
switch {
case e3 == nil:
cur = latest
Expand All @@ -136,7 +139,7 @@ func UpdateBackupConfigurationStatus(
})

if err != nil {
err = fmt.Errorf("failed to update status of BackupConfiguration %s/%s after %d attempts due to %v", in.Namespace, in.Name, attempt, err)
err = fmt.Errorf("failed to update status of BackupConfiguration %s/%s after %d attempts due to %v", meta.Namespace, meta.Name, attempt, err)
}
return
}
Expand Up @@ -98,7 +98,7 @@ func TryUpdateBackupSession(c cs.StashV1beta1Interface, meta metav1.ObjectMeta,
}

func UpdateBackupSessionStatusForHost(c cs.StashV1beta1Interface, targetRef api_v1beta1.TargetRef, backupSession *api_v1beta1.BackupSession, hostStats api_v1beta1.HostBackupStats) (*api_v1beta1.BackupSession, error) {
out, err := UpdateBackupSessionStatus(c, backupSession, func(in *api_v1beta1.BackupSessionStatus) *api_v1beta1.BackupSessionStatus {
out, err := UpdateBackupSessionStatus(c, backupSession.ObjectMeta, func(in *api_v1beta1.BackupSessionStatus) *api_v1beta1.BackupSessionStatus {
targetIdx, hostIdx, targets := UpsertHostForTarget(backupSession.Status.Targets, targetRef, hostStats)
in.Targets = targets
if int32(len(targets[targetIdx].Stats)) != *targets[targetIdx].TotalHosts {
Expand Down Expand Up @@ -149,27 +149,30 @@ func UpsertHost(stats []api_v1beta1.HostBackupStats, stat ...api_v1beta1.HostBac

func UpdateBackupSessionStatus(
c cs.StashV1beta1Interface,
in *api_v1beta1.BackupSession,
meta metav1.ObjectMeta,
transform func(*api_v1beta1.BackupSessionStatus) *api_v1beta1.BackupSessionStatus,
) (result *api_v1beta1.BackupSession, err error) {
apply := func(x *api_v1beta1.BackupSession) *api_v1beta1.BackupSession {
out := &api_v1beta1.BackupSession{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(in.Status.DeepCopy()),
Status: *transform(x.Status.DeepCopy()),
}
return out
}

attempt := 0
cur := in.DeepCopy()
cur, err := c.BackupSessions(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) {
attempt++
var e2 error
result, e2 = c.BackupSessions(in.Namespace).UpdateStatus(apply(cur))
result, e2 = c.BackupSessions(meta.Namespace).UpdateStatus(apply(cur))
if kerr.IsConflict(e2) {
latest, e3 := c.BackupSessions(in.Namespace).Get(in.Name, metav1.GetOptions{})
latest, e3 := c.BackupSessions(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
switch {
case e3 == nil:
cur = latest
Expand All @@ -186,7 +189,7 @@ func UpdateBackupSessionStatus(
})

if err != nil {
err = fmt.Errorf("failed to update status of BackupSession %s/%s after %d attempts due to %v", in.Namespace, in.Name, attempt, err)
err = fmt.Errorf("failed to update status of BackupSession %s/%s after %d attempts due to %v", meta.Namespace, meta.Name, attempt, err)
}
return
}
Expand Up @@ -98,7 +98,7 @@ func TryUpdateRestoreSession(c cs.StashV1beta1Interface, meta metav1.ObjectMeta,
}

func UpdateRestoreSessionStatusForHost(c cs.StashV1beta1Interface, restoreSession *api_v1beta1.RestoreSession, hostStats api_v1beta1.HostRestoreStats) (*api_v1beta1.RestoreSession, error) {
out, err := UpdateRestoreSessionStatus(c, restoreSession, func(in *api_v1beta1.RestoreSessionStatus) *api_v1beta1.RestoreSessionStatus {
out, err := UpdateRestoreSessionStatus(c, restoreSession.ObjectMeta, func(in *api_v1beta1.RestoreSessionStatus) *api_v1beta1.RestoreSessionStatus {
// if an entry already exist for this host then update it
for i, v := range restoreSession.Status.Stats {
if v.Hostname == hostStats.Hostname {
Expand All @@ -115,27 +115,30 @@ func UpdateRestoreSessionStatusForHost(c cs.StashV1beta1Interface, restoreSessio

func UpdateRestoreSessionStatus(
c cs.StashV1beta1Interface,
in *api_v1beta1.RestoreSession,
meta metav1.ObjectMeta,
transform func(*api_v1beta1.RestoreSessionStatus) *api_v1beta1.RestoreSessionStatus,
) (result *api_v1beta1.RestoreSession, err error) {
apply := func(x *api_v1beta1.RestoreSession) *api_v1beta1.RestoreSession {
out := &api_v1beta1.RestoreSession{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(in.Status.DeepCopy()),
Status: *transform(x.Status.DeepCopy()),
}
return out
}

attempt := 0
cur := in.DeepCopy()
cur, err := c.RestoreSessions(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) {
attempt++
var e2 error
result, e2 = c.RestoreSessions(in.Namespace).UpdateStatus(apply(cur))
result, e2 = c.RestoreSessions(meta.Namespace).UpdateStatus(apply(cur))
if kerr.IsConflict(e2) {
latest, e3 := c.RestoreSessions(in.Namespace).Get(in.Name, metav1.GetOptions{})
latest, e3 := c.RestoreSessions(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
switch {
case e3 == nil:
cur = latest
Expand All @@ -152,7 +155,7 @@ func UpdateRestoreSessionStatus(
})

if err != nil {
err = fmt.Errorf("failed to update status of RestoreSession %s/%s after %d attempts due to %v", in.Namespace, in.Name, attempt, err)
err = fmt.Errorf("failed to update status of RestoreSession %s/%s after %d attempts due to %v", meta.Namespace, meta.Name, attempt, err)
}
return
}

0 comments on commit dbb0287

Please sign in to comment.