Skip to content

Commit

Permalink
feat: tracking rating status
Browse files Browse the repository at this point in the history
Signed-off-by: zqq454224016 <[email protected]>
  • Loading branch information
zqq454224016 committed Oct 17, 2023
1 parent a1cbc9d commit 83e574b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
7 changes: 7 additions & 0 deletions api/v1alpha1/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ const (
RatingResolvingTaskRef ConditionReason = "ResolvingTaskRef"
RatingRunning ConditionReason = "Running"
RatingSucceeded ConditionReason = "Succeeded"

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.
Expand Down
2 changes: 1 addition & 1 deletion config/samples/rating_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 38 additions & 25 deletions controllers/rating_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,36 @@ func (r *RatingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return reconcile.Result{Requeue: true}, err
}

if err := r.CreatePipelineRun(logger, ctx, &instance); err != nil {
logger.Error(err, "")
instanceDeepCopy := instance.DeepCopy()
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
}

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 repository.Spec.EnableRating {
if err := r.CreatePipelineRun(logger, ctx, &instance); err != nil {
logger.Error(err, "")
return reconcile.Result{}, err
}
} else if instanceDeepCopy.Status.ConditionedStatus.Conditions[0].Status != "False" {
instanceDeepCopy.Status.ConditionedStatus = corev1alpha1.ConditionedStatus{
Conditions: []corev1alpha1.Condition{
{
Status: v1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: corev1alpha1.ReasonReconcileError,
Message: "Rating disabled in component's repository",
Type: corev1alpha1.TypeReady,
},
},
}
err := r.Client.Status().Patch(ctx, instanceDeepCopy, client.MergeFrom(&instance))
return reconcile.Result{}, err
}
return ctrl.Result{}, nil
}

Expand All @@ -112,29 +137,6 @@ func (r RatingReconciler) ratingChecker(ctx context.Context, instance *corev1alp
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
Expand Down Expand Up @@ -216,6 +218,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
}
Expand Down
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.
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 83e574b

Please sign in to comment.