Skip to content

Commit

Permalink
Ignore status update when uid does not match
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Nov 5, 2020
1 parent 43808b1 commit 0bdc1bb
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 60 deletions.
13 changes: 9 additions & 4 deletions client/clientset/versioned/typed/stash/v1alpha1/util/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,20 @@ func UpdateRecoveryStatus(
ctx context.Context,
c cs.StashV1alpha1Interface,
meta metav1.ObjectMeta,
transform func(types.UID, *api.RecoveryStatus) *api.RecoveryStatus,
transform func(*api.RecoveryStatus) (types.UID, *api.RecoveryStatus),
opts metav1.UpdateOptions,
) (result *api.Recovery, err error) {
apply := func(x *api.Recovery) *api.Recovery {
uid, updatedStatus := transform(x.Status.DeepCopy())
// Ignore status update when uid does not match
if uid != "" && uid != x.UID {
return x
}
out := &api.Recovery{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(x.UID, x.Status.DeepCopy()),
Status: *updatedStatus,
}
return out
}
Expand Down Expand Up @@ -152,7 +157,7 @@ func UpdateRecoveryStatus(
}

func SetRecoveryStats(ctx context.Context, c cs.StashV1alpha1Interface, recovery metav1.ObjectMeta, path string, d time.Duration, phase api.RecoveryPhase, opts metav1.UpdateOptions) (*api.Recovery, error) {
return UpdateRecoveryStatus(ctx, c, recovery, func(uid types.UID, in *api.RecoveryStatus) *api.RecoveryStatus {
return UpdateRecoveryStatus(ctx, c, recovery, func(in *api.RecoveryStatus) (types.UID, *api.RecoveryStatus) {
found := false
for _, stats := range in.Stats {
if stats.Path == path {
Expand All @@ -168,6 +173,6 @@ func SetRecoveryStats(ctx context.Context, c cs.StashV1alpha1Interface, recovery
Phase: phase,
})
}
return in
return "", in
}, opts)
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ func UpdateRepositoryStatus(
ctx context.Context,
c cs.StashV1alpha1Interface,
meta metav1.ObjectMeta,
transform func(types.UID, *api.RepositoryStatus) *api.RepositoryStatus,
transform func(*api.RepositoryStatus) (types.UID, *api.RepositoryStatus),
opts metav1.UpdateOptions,
) (result *api.Repository, err error) {
apply := func(x *api.Repository) *api.Repository {
uid, updatedStatus := transform(x.Status.DeepCopy())
// Ignore status update when uid does not match
if uid != "" && uid != x.UID {
return x
}
out := &api.Repository{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(x.UID, x.Status.DeepCopy()),
Status: *updatedStatus,
}
return out
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ func UpdateBackupBatchStatus(
ctx context.Context,
c cs.StashV1beta1Interface,
meta metav1.ObjectMeta,
transform func(types.UID, *api.BackupBatchStatus) *api.BackupBatchStatus,
transform func(*api.BackupBatchStatus) (types.UID, *api.BackupBatchStatus),
opts metav1.UpdateOptions,
) (result *api.BackupBatch, err error) {
apply := func(x *api.BackupBatch) *api.BackupBatch {
uid, updatedStatus := transform(x.Status.DeepCopy())
// Ignore status update when uid does not match
if uid != "" && uid != x.UID {
return x
}
out := &api.BackupBatch{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(x.UID, x.Status.DeepCopy()),
Status: *updatedStatus,
}
return out
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ func UpdateBackupConfigurationStatus(
ctx context.Context,
c cs.StashV1beta1Interface,
meta metav1.ObjectMeta,
transform func(types.UID, *api.BackupConfigurationStatus) *api.BackupConfigurationStatus,
transform func(*api.BackupConfigurationStatus) (types.UID, *api.BackupConfigurationStatus),
opts metav1.UpdateOptions,
) (result *api.BackupConfiguration, err error) {
apply := func(x *api.BackupConfiguration) *api.BackupConfiguration {
uid, updatedStatus := transform(x.Status.DeepCopy())
// Ignore status update when uid does not match
if uid != "" && uid != x.UID {
return x
}
out := &api.BackupConfiguration{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(x.UID, x.Status.DeepCopy()),
Status: *updatedStatus,
}
return out
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ func UpdateBackupSessionStatus(
ctx context.Context,
c cs.StashV1beta1Interface,
meta metav1.ObjectMeta,
transform func(types.UID, *api_v1beta1.BackupSessionStatus) *api_v1beta1.BackupSessionStatus,
transform func(*api_v1beta1.BackupSessionStatus) (types.UID, *api_v1beta1.BackupSessionStatus),
opts metav1.UpdateOptions,
) (result *api_v1beta1.BackupSession, err error) {
apply := func(x *api_v1beta1.BackupSession) *api_v1beta1.BackupSession {
uid, updatedStatus := transform(x.Status.DeepCopy())
// Ignore status update when uid does not match
if uid != "" && uid != x.UID {
return x
}
out := &api_v1beta1.BackupSession{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(x.UID, x.Status.DeepCopy()),
Status: *updatedStatus,
}
return out
}
Expand Down Expand Up @@ -158,9 +163,9 @@ func UpdateBackupSessionStatusForHost(
hostStats api_v1beta1.HostBackupStats,
opts metav1.UpdateOptions,
) (*api_v1beta1.BackupSession, error) {
return UpdateBackupSessionStatus(ctx, c, backupSession, func(uid types.UID, in *api_v1beta1.BackupSessionStatus) *api_v1beta1.BackupSessionStatus {
return UpdateBackupSessionStatus(ctx, c, backupSession, func(in *api_v1beta1.BackupSessionStatus) (types.UID, *api_v1beta1.BackupSessionStatus) {
in.Targets = UpsertHostForTarget(in.Targets, targetRef, hostStats)
return in
return "", in
}, opts)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ func UpdateRestoreBatchStatus(
ctx context.Context,
c cs.StashV1beta1Interface,
meta metav1.ObjectMeta,
transform func(types.UID, *api.RestoreBatchStatus) *api.RestoreBatchStatus,
transform func(*api.RestoreBatchStatus) (types.UID, *api.RestoreBatchStatus),
opts metav1.UpdateOptions,
) (result *api.RestoreBatch, err error) {
apply := func(x *api.RestoreBatch) *api.RestoreBatch {
uid, updatedStatus := transform(x.Status.DeepCopy())
// Ignore status update when uid does not match
if uid != "" && uid != x.UID {
return x
}
out := &api.RestoreBatch{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(x.UID, x.Status.DeepCopy()),
Status: *updatedStatus,
}
return out
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ func UpdateRestoreSessionStatus(
ctx context.Context,
c cs.StashV1beta1Interface,
meta metav1.ObjectMeta,
transform func(types.UID, *api_v1beta1.RestoreSessionStatus) *api_v1beta1.RestoreSessionStatus,
transform func(*api_v1beta1.RestoreSessionStatus) (types.UID, *api_v1beta1.RestoreSessionStatus),
opts metav1.UpdateOptions,
) (result *api_v1beta1.RestoreSession, err error) {
apply := func(x *api_v1beta1.RestoreSession) *api_v1beta1.RestoreSession {
uid, updatedStatus := transform(x.Status.DeepCopy())
// Ignore status update when uid does not match
if uid != "" && uid != x.UID {
return x
}
out := &api_v1beta1.RestoreSession{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
Status: *transform(x.UID, x.Status.DeepCopy()),
Status: *updatedStatus,
}
return out
}
Expand Down Expand Up @@ -151,16 +156,16 @@ func UpdateRestoreSessionStatus(
}

func UpdateRestoreSessionStatusForHost(ctx context.Context, c cs.StashV1beta1Interface, restoreSession metav1.ObjectMeta, hostStats api_v1beta1.HostRestoreStats, opts metav1.UpdateOptions) (*api_v1beta1.RestoreSession, error) {
return UpdateRestoreSessionStatus(ctx, c, restoreSession, func(uid types.UID, in *api_v1beta1.RestoreSessionStatus) *api_v1beta1.RestoreSessionStatus {
return UpdateRestoreSessionStatus(ctx, c, restoreSession, func(in *api_v1beta1.RestoreSessionStatus) (types.UID, *api_v1beta1.RestoreSessionStatus) {
// if an entry already exist for this host then update it
for i, v := range in.Stats {
if v.Hostname == hostStats.Hostname {
in.Stats[i] = hostStats
return in
return "", in
}
}
// no entry for this host. so add a new entry.
in.Stats = append(in.Stats, hostStats)
return in
return "", in
}, opts)
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ require (
github.com/kr/pretty v0.2.0 // indirect
github.com/mailru/easyjson v0.7.1 // indirect
github.com/mitchellh/mapstructure v1.2.2 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.6.0
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.5.1
Expand Down
16 changes: 8 additions & 8 deletions pkg/conditions/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func SetGlobalPreBackupHookSucceededConditionToFalse(stashClient cs.Interface, b
context.TODO(),
stashClient.StashV1beta1(),
backupSession.ObjectMeta,
func(_ types.UID, in *api_v1beta1.BackupSessionStatus) *api_v1beta1.BackupSessionStatus {
func(in *api_v1beta1.BackupSessionStatus) (types.UID, *api_v1beta1.BackupSessionStatus) {
in.Conditions = kmapi.SetCondition(in.Conditions, kmapi.Condition{
Type: apis.GlobalPreBackupHookSucceeded,
Status: core.ConditionFalse,
Reason: apis.GlobalPreBackupHookExecutionFailed,
Message: fmt.Sprintf("Failed to execute global PreBackup Hook. Reason: %v.", hookErr),
},
)
return in
return backupSession.UID, in
},
metav1.UpdateOptions{},
)
Expand All @@ -56,15 +56,15 @@ func SetGlobalPreBackupHookSucceededConditionToTrue(stashClient cs.Interface, ba
context.TODO(),
stashClient.StashV1beta1(),
backupSession.ObjectMeta,
func(_ types.UID, in *api_v1beta1.BackupSessionStatus) *api_v1beta1.BackupSessionStatus {
func(in *api_v1beta1.BackupSessionStatus) (types.UID, *api_v1beta1.BackupSessionStatus) {
in.Conditions = kmapi.SetCondition(in.Conditions, kmapi.Condition{
Type: apis.GlobalPreBackupHookSucceeded,
Status: core.ConditionTrue,
Reason: apis.GlobalPreBackupHookExecutedSuccessfully,
Message: "Global PreBackup hook has been executed successfully",
},
)
return in
return backupSession.UID, in
},
metav1.UpdateOptions{},
)
Expand All @@ -75,15 +75,15 @@ func SetGlobalPostBackupHookSucceededConditionToFalse(stashClient cs.Interface,
context.TODO(),
stashClient.StashV1beta1(),
backupSession.ObjectMeta,
func(_ types.UID, in *api_v1beta1.BackupSessionStatus) *api_v1beta1.BackupSessionStatus {
func(in *api_v1beta1.BackupSessionStatus) (types.UID, *api_v1beta1.BackupSessionStatus) {
in.Conditions = kmapi.SetCondition(in.Conditions, kmapi.Condition{
Type: apis.GlobalPostBackupHookSucceeded,
Status: core.ConditionFalse,
Reason: apis.GlobalPostBackupHookExecutionFailed,
Message: fmt.Sprintf("Failed to execute global PostBackup Hook. Reason: %v.", hookErr),
},
)
return in
return backupSession.UID, in
},
metav1.UpdateOptions{},
)
Expand All @@ -94,15 +94,15 @@ func SetGlobalPostBackupHookSucceededConditionToTrue(stashClient cs.Interface, b
context.TODO(),
stashClient.StashV1beta1(),
backupSession.ObjectMeta,
func(_ types.UID, in *api_v1beta1.BackupSessionStatus) *api_v1beta1.BackupSessionStatus {
func(in *api_v1beta1.BackupSessionStatus) (types.UID, *api_v1beta1.BackupSessionStatus) {
in.Conditions = kmapi.SetCondition(in.Conditions, kmapi.Condition{
Type: apis.GlobalPostBackupHookSucceeded,
Status: core.ConditionTrue,
Reason: apis.GlobalPostBackupHookExecutedSuccessfully,
Message: "Global PostBackup hook has been executed successfully",
},
)
return in
return backupSession.UID, in
},
metav1.UpdateOptions{},
)
Expand Down
Loading

0 comments on commit 0bdc1bb

Please sign in to comment.