Skip to content

Commit

Permalink
Add UpdateMariaDBOpsRequestStatus function (#727)
Browse files Browse the repository at this point in the history
Signed-off-by: Md. Alif Biswas <alif@appscode.com>
  • Loading branch information
Md. Alif Biswas committed Jun 5, 2021
1 parent 98cd75f commit 161b3fe
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apis/kubedb/v1alpha2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ const (
MariaDBInitDBMountPath = "/docker-entrypoint-initdb.d"
MariaDBCustomConfigMountPath = "/etc/percona-server.conf.d/"
MariaDBClusterCustomConfigMountPath = "/etc/percona-xtradb-cluster.conf.d/"
MariaDBTLSConfigCustom = "custom"
MariaDBInitContainerName = "mariadb-init"

// =========================== PostgreSQL Constants ============================
PostgresDatabasePortName = "db"
Expand Down
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
}

0 comments on commit 161b3fe

Please sign in to comment.