Skip to content

Commit

Permalink
feat:add a new filed EnableRating in repository
Browse files Browse the repository at this point in the history
Signed-off-by: zqq454224016 <[email protected]>
  • Loading branch information
zqq454224016 committed Sep 20, 2023
1 parent 57d54a7 commit afe4c2a
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 1 deletion.
2 changes: 2 additions & 0 deletions api/v1alpha1/rating_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha1/repository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions config/crd/bases/core.kubebb.k8s.com.cn_ratings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/core.kubebb.k8s.com.cn_repositories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions config/samples/core_v1alpha1_rating_3.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions config/samples/core_v1alpha1_repository_kubebb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ metadata:
namespace: kubebb-system
spec:
url: https://kubebb.github.io/components
enableRating: true
pullStategy:
intervalSeconds: 120
retry: 5
11 changes: 11 additions & 0 deletions config/samples/core_v1alpha1_repository_kubebb_2.yaml
Original file line number Diff line number Diff line change
@@ -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
35 changes: 34 additions & 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 situations
sleep 2 # wait for operator patch status. avoid 0=0 situationss
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 All @@ -164,6 +164,30 @@ function waitRatingDone() {
done
}

function waitRatingFail() {
namespace=$1
ratingName=$2
START_TIME=$(date +%s)
sleep 2 # wait for operator patch status. avoid 0=0 situations
while true; do
status=$(kubectl -n${namespace} get rating ${ratingName} -ojson --ignore-not-found=true | jq -r '.status.conditions[0].status')
type=$(kubectl -n${namespace} get rating ${ratingName} -ojson --ignore-not-found=true | jq -r '.status.conditions[0].type')
message=$(kubectl -n${namespace} get rating ${ratingName} -ojson --ignore-not-found=true | jq -r '.status.conditions[0].message')
if [[ $status == "False" && $type == "Ready" && $message == "Rating disabled in component's repository" ]]; then
echo "rating ${ratingName} task incompleted"
break
fi
CURRENT_TIME=$(date +%s)
ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
if [ $ELAPSED_TIME -gt $TimeoutSeconds ]; then
error "Timeout reached"
exit 1
fi
sleep 5
done

}

function waitEvaluationsDone() {
namespace=$1
ratingName=$2
Expand Down Expand Up @@ -316,4 +340,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
waitRatingFail "kubebb-system" "disable-rating"

info "all finished! ✅"
24 changes: 24 additions & 0 deletions controllers/rating_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -110,6 +111,29 @@ 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 {
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, nil
}

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

0 comments on commit afe4c2a

Please sign in to comment.