Skip to content

Commit

Permalink
Use structured name for the generated opsReq
Browse files Browse the repository at this point in the history
Signed-off-by: Arnob kumar saha <[email protected]>
  • Loading branch information
ArnobKumarSaha committed Sep 20, 2024
1 parent 955fc0a commit 4cc6e7c
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pkg/controllers/supervisor/recommendation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package supervisor
import (
"context"
"errors"
"fmt"
"sync"
"time"

Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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 fmt.Sprintf("%s-%v-%s-supervisor", dbName, 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)
Expand Down

0 comments on commit 4cc6e7c

Please sign in to comment.