diff --git a/api/v1alpha1/rating_types.go b/api/v1alpha1/rating_types.go index 3c61be2f..8b92c9c9 100644 --- a/api/v1alpha1/rating_types.go +++ b/api/v1alpha1/rating_types.go @@ -95,6 +95,8 @@ type RatingStatus struct { PipelineRuns map[string]PipelineRunStatus `json:"pipelineRuns,omitempty"` // Evaluations contains the evaluator status with the `Dimension` as the key Evaluations map[string]EvaluatorStatus `json:"evaluations,omitempty"` + + ConditionedStatus `json:",inline"` } type EvaluatorStatus struct { diff --git a/api/v1alpha1/repository_types.go b/api/v1alpha1/repository_types.go index 327bc14f..0d2c8f1d 100644 --- a/api/v1alpha1/repository_types.go +++ b/api/v1alpha1/repository_types.go @@ -90,6 +90,9 @@ type RepositorySpec struct { // KeywordLenLimit the keyword array length limit KeywordLenLimit int `json:"keywordLenLimit,omitempty"` + + // EnableRating enable component rating + EnableRating bool `json:"enableRating,omitempty"` } type PathOverride struct { diff --git a/config/crd/bases/core.kubebb.k8s.com.cn_ratings.yaml b/config/crd/bases/core.kubebb.k8s.com.cn_ratings.yaml index c4018401..2e37a368 100644 --- a/config/crd/bases/core.kubebb.k8s.com.cn_ratings.yaml +++ b/config/crd/bases/core.kubebb.k8s.com.cn_ratings.yaml @@ -111,6 +111,44 @@ spec: type: object status: properties: + conditions: + description: Conditions of the resource. + items: + description: A Condition that may apply to a resource. + properties: + lastSuccessfulTime: + description: LastSuccessfulTime is repository Last Successful + Update Time + format: date-time + type: string + lastTransitionTime: + description: LastTransitionTime is the last time this condition + transitioned from one status to another. + format: date-time + type: string + message: + description: A Message containing details about this condition's + last transition from one status to another, if any. + type: string + reason: + description: A Reason for this condition's last transition from + one status to another. + type: string + status: + description: Status of this condition; is it currently True, + False, or Unknown + type: string + type: + description: Type of this condition. At most one of each condition + type may apply to a resource at any point in time. + type: string + required: + - lastTransitionTime + - reason + - status + - type + type: object + type: array evaluations: additionalProperties: properties: diff --git a/config/crd/bases/core.kubebb.k8s.com.cn_repositories.yaml b/config/crd/bases/core.kubebb.k8s.com.cn_repositories.yaml index 8ecce48e..e163da80 100644 --- a/config/crd/bases/core.kubebb.k8s.com.cn_repositories.yaml +++ b/config/crd/bases/core.kubebb.k8s.com.cn_repositories.yaml @@ -43,6 +43,9 @@ spec: set the username and password to secret, with the fields user and password respectively. type: string + enableRating: + description: EnableRating enable component rating + type: boolean filter: items: properties: diff --git a/config/samples/core_v1alpha1_rating_3.yaml b/config/samples/core_v1alpha1_rating_3.yaml new file mode 100644 index 00000000..c67833a7 --- /dev/null +++ b/config/samples/core_v1alpha1_rating_3.yaml @@ -0,0 +1,31 @@ +apiVersion: core.kubebb.k8s.com.cn/v1alpha1 +kind: Rating +metadata: + name: disable-rating + namespace: kubebb-system +spec: + evaluator: + llm: zhipuai + componentName: repository-kubebb-disablerating.kubebb-core + pipelineParams: + - pipelineName: component-reliability + dimension: "reliability" + params: + - name: URL + value: + type: string + stringVal: https://github.com/kubebb/components/releases/download/kubebb-core-v0.1.10/kubebb-core-v0.1.10.tgz + arrayVal: [] + objectVal: {} + - name: COMPONENT_NAME + value: + stringVal: kubebb-core + type: string + - name: VERSION + value: + stringVal: v0.1.10 + type: string + - name: REPOSITORY_NAME + value: + stringVal: repository-kubebb-disablerating + type: string \ No newline at end of file diff --git a/config/samples/core_v1alpha1_repository_kubebb.yaml b/config/samples/core_v1alpha1_repository_kubebb.yaml index dfc2936c..021bb226 100644 --- a/config/samples/core_v1alpha1_repository_kubebb.yaml +++ b/config/samples/core_v1alpha1_repository_kubebb.yaml @@ -5,6 +5,7 @@ metadata: namespace: kubebb-system spec: url: https://kubebb.github.io/components + enableRating: true pullStategy: intervalSeconds: 120 retry: 5 diff --git a/config/samples/core_v1alpha1_repository_kubebb_2.yaml b/config/samples/core_v1alpha1_repository_kubebb_2.yaml new file mode 100644 index 00000000..be4f5994 --- /dev/null +++ b/config/samples/core_v1alpha1_repository_kubebb_2.yaml @@ -0,0 +1,11 @@ +apiVersion: core.kubebb.k8s.com.cn/v1alpha1 +kind: Repository +metadata: + name: repository-kubebb-disablerating + namespace: kubebb-system +spec: + url: https://kubebb.github.io/components + enableRating: false + pullStategy: + intervalSeconds: 120 + retry: 5 diff --git a/config/samples/rating_test.sh b/config/samples/rating_test.sh index b7f7615d..82271760 100755 --- a/config/samples/rating_test.sh +++ b/config/samples/rating_test.sh @@ -148,6 +148,12 @@ function waitRatingDone() { ratingName=$2 START_TIME=$(date +%s) sleep 2 # wait for operator patch status. avoid 0=0 situations + result=$(kubectl -n${namespace} get rating ${ratingName} -ojson --ignore-not-found=true | jq -r '.status.conditions[0].status') + if [[ $result == "False" ]]; then + echo "rating ${ratingName} task incompleted" + return + fi + 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 @@ -316,4 +322,13 @@ if [ -n "$LLM_API_KEY" ]; then waitEvaluationsDone "kubebb-system" "two-dimension-rating" "security" fi +info "8 verify repository with disabled rating" +info "8.1 add kubebb repository with enableRating false" +kubectl apply -f config/samples/core_v1alpha1_repository_kubebb_2.yaml +waitComponentStatus "kubebb-system" "repository-kubebb-disablerating.kubebb-core" + +info "8.2 verify rating" +kubectl apply -f config/samples/core_v1alpha1_rating_3.yaml +waitRatingDone "kubebb-system" "disable-rating" + info "all finished! ✅" diff --git a/controllers/rating_controller.go b/controllers/rating_controller.go index 5d398941..eaf66159 100644 --- a/controllers/rating_controller.go +++ b/controllers/rating_controller.go @@ -23,6 +23,7 @@ import ( "github.com/go-logr/logr" arcadiav1 "github.com/kubeagi/arcadia/api/v1alpha1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -110,6 +111,27 @@ func (r RatingReconciler) ratingChecker(ctx context.Context, instance *corev1alp 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 || !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, err + } + if v, ok := instance.Labels[corev1alpha1.RatingRepositoryLabel]; !ok || v != component.Labels[corev1alpha1.ComponentRepositoryLabel] { instance.Labels[corev1alpha1.RatingRepositoryLabel] = component.Labels[corev1alpha1.ComponentRepositoryLabel] updateLabel = true