Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use structured name for the generated opsReq #30

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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)
Expand Down
Loading