-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UpdateMariaDBOpsRequestStatus function (#727)
Signed-off-by: Md. Alif Biswas <alif@appscode.com>
- Loading branch information
Md. Alif Biswas
committed
Jun 5, 2021
1 parent
98cd75f
commit 161b3fe
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
client/clientset/versioned/typed/ops/v1alpha1/util/mariadbopsrequest.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
api "kubedb.dev/apimachinery/apis/ops/v1alpha1" | ||
cs "kubedb.dev/apimachinery/client/clientset/versioned/typed/ops/v1alpha1" | ||
|
||
kerr "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
kutil "kmodules.xyz/client-go" | ||
) | ||
|
||
func UpdateMariaDBOpsRequestStatus( | ||
ctx context.Context, | ||
c cs.OpsV1alpha1Interface, | ||
meta metav1.ObjectMeta, | ||
transform func(*api.MariaDBOpsRequestStatus) (types.UID, *api.MariaDBOpsRequestStatus), | ||
opts metav1.UpdateOptions, | ||
) (result *api.MariaDBOpsRequest, err error) { | ||
apply := func(x *api.MariaDBOpsRequest) *api.MariaDBOpsRequest { | ||
uid, updatedStatus := transform(x.Status.DeepCopy()) | ||
// Ignore status update when uid does not match | ||
if uid != "" && uid != x.UID { | ||
return x | ||
} | ||
return &api.MariaDBOpsRequest{ | ||
TypeMeta: x.TypeMeta, | ||
ObjectMeta: x.ObjectMeta, | ||
Spec: x.Spec, | ||
Status: *updatedStatus, | ||
} | ||
} | ||
|
||
attempt := 0 | ||
cur, err := c.MariaDBOpsRequests(meta.Namespace).Get(ctx, 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.MariaDBOpsRequests(meta.Namespace).UpdateStatus(ctx, apply(cur), opts) | ||
if kerr.IsConflict(e2) { | ||
latest, e3 := c.MariaDBOpsRequests(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) | ||
switch { | ||
case e3 == nil: | ||
cur = latest | ||
return false, nil | ||
case kutil.IsRequestRetryable(e3): | ||
return false, nil | ||
default: | ||
return false, e3 | ||
} | ||
} else if err != nil && !kutil.IsRequestRetryable(e2) { | ||
return false, e2 | ||
} | ||
return e2 == nil, nil | ||
}) | ||
|
||
if err != nil { | ||
err = fmt.Errorf("failed to update status of MariaDBOpsRequest %s/%s after %d attempts due to %v", meta.Namespace, meta.Name, attempt, err) | ||
} | ||
return | ||
} |