Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tracking rating status
Browse files Browse the repository at this point in the history
Signed-off-by: zqq454224016 <454224016@qq.com>
zqq454224016 committed Oct 17, 2023
1 parent 0c39546 commit 27d455b
Showing 4 changed files with 113 additions and 39 deletions.
9 changes: 9 additions & 0 deletions api/v1alpha1/condition.go
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ const (
ReasonAvailable ConditionReason = "Available"
ReasonUnavailable ConditionReason = "Unavailable"
ReasonCreating ConditionReason = "Creating"
ReasonCreated ConditionReason = "Created"
ReasonDeleting ConditionReason = "Deleting"
ReasonReconcileSuccess ConditionReason = "ReconcileSuccess"
ReasonReconcileError ConditionReason = "ReconcileError"
@@ -59,6 +60,14 @@ const (
RatingResolvingTaskRef ConditionReason = "ResolvingTaskRef"
RatingRunning ConditionReason = "Running"
RatingSucceeded ConditionReason = "Succeeded"
RatingDidabled ConditionReason = "Disabled"

PipelineRunning ConditionReason = "PipelineRunning"
PipelineRunFailed ConditionReason = "PipelineRunFailed"
PipelineRunSucceeded ConditionReason = "PipelineRunSucceeded"
EvaluationRunning ConditionReason = "EvaluationRunning"
EvaluationFailed ConditionReason = "EvaluationFailed"
EvaluationSucceeded ConditionReason = "EvaluationSucceeded"
)

// A Condition that may apply to a resource.
2 changes: 1 addition & 1 deletion config/samples/rating_test.sh
Original file line number Diff line number Diff line change
@@ -147,7 +147,7 @@ function waitRatingDone() {
namespace=$1
ratingName=$2
START_TIME=$(date +%s)
sleep 2 # wait for operator patch status. avoid 0=0 situationss
sleep 2 # wait for operator patch status. avoid 0=0 situations
while true; do
complete=$(kubectl -n${namespace} get rating ${ratingName} -ojson --ignore-not-found=true | jq '.status.pipelineRuns' | jq '{l:length,o:map(select(.conditions[0].type=="Succeeded" and .conditions[0].status=="True"))|length}' | jq '.l == .o')
if [[ $complete == "true" ]]; then
113 changes: 75 additions & 38 deletions controllers/rating_controller.go
Original file line number Diff line number Diff line change
@@ -82,21 +82,59 @@ func (r *RatingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}
return reconcile.Result{}, err
}
component := corev1alpha1.Component{}
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: instance.Namespace, Name: instance.Spec.ComponentName}, &component); err != nil {
return reconcile.Result{}, err
}

done, err := r.ratingChecker(ctx, &instance)
if !done {
return reconcile.Result{Requeue: true}, err
repository := corev1alpha1.Repository{}
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: instance.Namespace, Name: component.Status.RepositoryRef.Name}, &repository); err != nil {
return reconcile.Result{}, err
}

if err := r.CreatePipelineRun(logger, ctx, &instance); err != nil {
logger.Error(err, "")
// update labels
requeue, err := r.updateLabels(ctx, &instance, &component)
if err != nil {
return reconcile.Result{}, err
}
if requeue {
return reconcile.Result{Requeue: true}, nil
}

// update status to false when rating is disabled
if !repository.Spec.EnableRating {
instanceDeepCopy := instance.DeepCopy()
if instanceDeepCopy.Status.ConditionedStatus.Conditions[0].Reason != corev1alpha1.RatingDidabled {
instanceDeepCopy.Status.ConditionedStatus = corev1alpha1.ConditionedStatus{
Conditions: []corev1alpha1.Condition{
{
Status: v1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: corev1alpha1.RatingDidabled,
Message: "Rating disabled in component's repository",
Type: corev1alpha1.TypeReady,
},
},
}
err := r.Client.Status().Patch(ctx, instanceDeepCopy, client.MergeFrom(&instance))
if err != nil {
return reconcile.Result{}, err
}
}
}

// create pipelineruns when rating enabled
if repository.Spec.EnableRating {
if err := r.CreatePipelineRun(logger, ctx, &instance); err != nil {
logger.Error(err, "")
return reconcile.Result{}, err
}
}

return ctrl.Result{}, nil
}

func (r RatingReconciler) ratingChecker(ctx context.Context, instance *corev1alpha1.Rating) (bool, error) {
func (r RatingReconciler) updateLabels(ctx context.Context, instance *corev1alpha1.Rating, component *corev1alpha1.Component) (bool, error) {
if instance.Labels == nil {
instance.Labels = make(map[string]string)
}
@@ -107,44 +145,32 @@ func (r RatingReconciler) ratingChecker(ctx context.Context, instance *corev1alp
updateLabel = true
}

component := corev1alpha1.Component{}
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: instance.Namespace, Name: instance.Spec.ComponentName}, &component); err != nil {
return false, err
}

repository := corev1alpha1.Repository{}
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: instance.Namespace, Name: component.Status.RepositoryRef.Name}, &repository); err != nil {
return false, err
}
if !repository.Spec.EnableRating {
instanceDeepCopy := instance.DeepCopy()
cond := corev1alpha1.Condition{
Status: v1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: corev1alpha1.ReasonReconcileError,
Message: "Rating disabled in component's repository",
Type: corev1alpha1.TypeReady,
}
instanceDeepCopy.Status.ConditionedStatus = corev1alpha1.ConditionedStatus{
Conditions: []corev1alpha1.Condition{cond},
}
if err := r.Client.Status().Patch(ctx, instanceDeepCopy, client.MergeFrom(instance)); err != nil {
return false, err
}

return false, errors.NewResourceExpired("Rating disabled in component's repository")
}

if v, ok := instance.Labels[corev1alpha1.RatingRepositoryLabel]; !ok || v != component.Labels[corev1alpha1.ComponentRepositoryLabel] {
instance.Labels[corev1alpha1.RatingRepositoryLabel] = component.Labels[corev1alpha1.ComponentRepositoryLabel]
updateLabel = true
}
if updateLabel {
return false, r.Client.Update(ctx, instance)

if !updateLabel {
return false, nil
}

// add other checker
return true, nil
if err := r.Client.Update(ctx, instance); err != nil {
return false, err
}
// when update labels, we should patch a initial status
instanceDeepCopy := instance.DeepCopy()
instanceDeepCopy.Status.ConditionedStatus = corev1alpha1.ConditionedStatus{
Conditions: []corev1alpha1.Condition{
{
Status: v1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: corev1alpha1.ReasonCreated,
Message: "Rating is created.",
Type: corev1alpha1.TypeReady,
},
},
}
return true, r.Client.Status().Patch(ctx, instanceDeepCopy, client.MergeFrom(instance))
}

func (r *RatingReconciler) CreatePipelineRun(logger logr.Logger, ctx context.Context, instance *corev1alpha1.Rating) error {
@@ -216,6 +242,17 @@ func (r *RatingReconciler) CreatePipelineRun(logger logr.Logger, ctx context.Con

instanceDeepCopy := instance.DeepCopy()
instanceDeepCopy.Status.PipelineRuns = pipelineRunStatus
instanceDeepCopy.Status.ConditionedStatus = corev1alpha1.ConditionedStatus{
Conditions: []corev1alpha1.Condition{
{
Status: v1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: corev1alpha1.PipelineRunning,
Message: "Rating enabled in component's repository",
Type: corev1alpha1.TypeReady,
},
},
}
if err := r.Client.Status().Patch(ctx, instanceDeepCopy, client.MergeFrom(instance)); err != nil {
return err
}
28 changes: 28 additions & 0 deletions pkg/evaluator/evaluator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2023 The Kubebb Authors.
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.
*/

/*
Copyright 2023 KubeAGI.
@@ -27,6 +43,7 @@ import (
arcadiav1 "github.com/kubeagi/arcadia/api/v1alpha1"
"github.com/kubeagi/arcadia/pkg/llms"
"github.com/kubeagi/arcadia/pkg/llms/zhipuai"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
@@ -242,6 +259,17 @@ func OnPromptUpdate(logger logr.Logger, c client.Client) func(event.UpdateEvent,
// }
// }

deepCopyRating.Status.ConditionedStatus = corev1alpha1.ConditionedStatus{
Conditions: []corev1alpha1.Condition{
{
Status: v1.ConditionTrue,
LastTransitionTime: metav1.Now(),
Reason: corev1alpha1.EvaluationSucceeded,
Message: "Evaluation succeeded",
Type: corev1alpha1.TypeReady,
},
},
}
deepCopyRating.Status.Evaluations[dimension] = evaluationStatus
err = c.Status().Patch(context.TODO(), deepCopyRating, client.MergeFrom(rating))
if err != nil {

0 comments on commit 27d455b

Please sign in to comment.