diff --git a/pkg/controllers/supervisor/recommendation_controller.go b/pkg/controllers/supervisor/recommendation_controller.go index a9fdb3a9..dfe30172 100644 --- a/pkg/controllers/supervisor/recommendation_controller.go +++ b/pkg/controllers/supervisor/recommendation_controller.go @@ -19,6 +19,7 @@ package supervisor import ( "context" "errors" + "fmt" "sync" "time" @@ -32,7 +33,6 @@ import ( "github.com/jonboulle/clockwork" "gomodules.xyz/pointer" - "gomodules.xyz/x/crypto/rand" core "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -263,12 +263,15 @@ func (r *RecommendationReconciler) runMaintenanceWork(ctx context.Context, rcmd return ctrl.Result{RequeueAfter: r.RequeueAfterDuration}, nil } - // Creating OpsRequest from given raw object - opsReqName := rand.WithUniqSuffix("supervisor") unObj, err := shared.GetUnstructuredObj(rcmd.Spec.Operation) if err != nil { return r.handleErr(ctx, rcmd, err, api.Failed) } + + opsReqName, err := generateOpsRequestName(unObj) + if err != nil { + return ctrl.Result{}, err + } unObj.SetName(opsReqName) err = r.Client.Create(ctx, unObj) @@ -293,6 +296,19 @@ func (r *RecommendationReconciler) runMaintenanceWork(ctx context.Context, rcmd return ctrl.Result{}, err } +func generateOpsRequestName(unObj *unstructured.Unstructured) (string, error) { + dbName, found, err := unstructured.NestedString(unObj.Object, "spec", "databaseRef", "name") + if err != nil { + return "", err + } + if !found { + return "", fmt.Errorf("invalid recommendation %s; missing `spec.databaseRef.name` in spec.opration", unObj.GetName()) + } + unixTime := time.Now().Unix() + operationType := unObj.GetName() + return meta_util.ValidNameWithPrefix(dbName, fmt.Sprintf("%v-%s-auto", unixTime, operationType)), nil +} + func (r *RecommendationReconciler) handleErr(ctx context.Context, rcmd *api.Recommendation, err error, phase api.RecommendationPhase) (ctrl.Result, error) { _, pErr := kmc.PatchStatus(ctx, r.Client, rcmd, func(obj client.Object) client.Object { in := obj.(*api.Recommendation)