Skip to content

Commit

Permalink
consolidate constants for workspace and ragengine controller
Browse files Browse the repository at this point in the history
Signed-off-by: Bangqi Zhu <[email protected]>
  • Loading branch information
Bangqi Zhu committed Oct 15, 2024
1 parent 1d99028 commit 85f57e0
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 84 deletions.
3 changes: 2 additions & 1 deletion api/v1alpha1/ragengine_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"

"github.com/azure/kaito/pkg/utils/consts"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/klog/v2"
"knative.dev/pkg/apis"
Expand All @@ -22,7 +23,7 @@ func (w *RAGEngine) SupportedVerbs() []admissionregistrationv1.OperationType {
func (w *RAGEngine) Validate(ctx context.Context) (errs *apis.FieldError) {
base := apis.GetBaseline(ctx)
if base == nil {
klog.InfoS("Validate creation", "ragengine", fmt.Sprintf("%s/%s", w.Namespace, w.Name))
klog.InfoS("Validate creation", consts.RAGEngineString, fmt.Sprintf("%s/%s", w.Namespace, w.Name))

Check warning on line 26 in api/v1alpha1/ragengine_validation.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/ragengine_validation.go#L26

Added line #L26 was not covered by tests
errs = errs.Also(w.validateCreate().ViaField("spec"))
}
return errs
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/workspace_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (w *Workspace) SupportedVerbs() []admissionregistrationv1.OperationType {
func (w *Workspace) Validate(ctx context.Context) (errs *apis.FieldError) {
base := apis.GetBaseline(ctx)
if base == nil {
klog.InfoS("Validate creation", "workspace", fmt.Sprintf("%s/%s", w.Namespace, w.Name))
klog.InfoS("Validate creation", consts.WorkspaceString, fmt.Sprintf("%s/%s", w.Namespace, w.Name))

Check warning on line 46 in api/v1alpha1/workspace_validation.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/workspace_validation.go#L46

Added line #L46 was not covered by tests
errs = errs.Also(w.validateCreate().ViaField("spec"))
if w.Inference != nil {
// TODO: Add Adapter Spec Validation - Including DataSource Validation for Adapter
Expand All @@ -56,7 +56,7 @@ func (w *Workspace) Validate(ctx context.Context) (errs *apis.FieldError) {
w.Tuning.validateCreate(ctx, w.Namespace).ViaField("tuning"))
}
} else {
klog.InfoS("Validate update", "workspace", fmt.Sprintf("%s/%s", w.Namespace, w.Name))
klog.InfoS("Validate update", consts.WorkspaceString, fmt.Sprintf("%s/%s", w.Namespace, w.Name))

Check warning on line 59 in api/v1alpha1/workspace_validation.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/workspace_validation.go#L59

Added line #L59 was not covered by tests
old := base.(*Workspace)
errs = errs.Also(
w.validateUpdate(old).ViaField("spec"),
Expand Down
5 changes: 3 additions & 2 deletions pkg/controllers/ragengine_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"

kaitov1alpha1 "github.com/azure/kaito/api/v1alpha1"
"github.com/azure/kaito/pkg/utils/consts"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
Expand All @@ -21,13 +22,13 @@ func (c *RAGEngineReconciler) updateStatusConditionIfNotMatch(ctx context.Contex
return nil
}
}
klog.InfoS("updateStatusCondition", "ragengine", klog.KObj(ragObj), "conditionType", cType, "status", cStatus, "reason", cReason, "message", cMessage)
klog.InfoS("updateStatusCondition", consts.RAGEngineString, klog.KObj(ragObj), "conditionType", cType, "status", cStatus, "reason", cReason, "message", cMessage)
cObj := metav1.Condition{
Type: string(cType),
Status: cStatus,
Reason: cReason,
ObservedGeneration: ragObj.GetGeneration(),
Message: cMessage,
}
return updateObjStatus(ctx, c.Client, &client.ObjectKey{Name: ragObj.Name, Namespace: ragObj.Namespace}, "ragengine", &cObj, nil)
return updateObjStatus(ctx, c.Client, &client.ObjectKey{Name: ragObj.Name, Namespace: ragObj.Namespace}, consts.RAGEngineString, &cObj, nil)
}
9 changes: 5 additions & 4 deletions pkg/controllers/ragengine_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

kaitov1alpha1 "github.com/azure/kaito/api/v1alpha1"
"github.com/azure/kaito/pkg/utils/consts"
"github.com/azure/kaito/pkg/utils/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -38,7 +39,7 @@ func TestUpdateRAGEngineStatus(t *testing.T) {
mockClient.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&kaitov1alpha1.RAGEngine{}), mock.Anything).Return(nil)
mockClient.StatusMock.On("Update", mock.IsType(context.Background()), mock.IsType(&kaitov1alpha1.RAGEngine{}), mock.Anything).Return(nil)

err := updateObjStatus(ctx, reconciler.Client, &client.ObjectKey{Name: ragengine.Name, Namespace: ragengine.Namespace}, "ragengine", &condition, workerNodes)
err := updateObjStatus(ctx, reconciler.Client, &client.ObjectKey{Name: ragengine.Name, Namespace: ragengine.Namespace}, consts.RAGEngineString, &condition, workerNodes)
assert.Nil(t, err)
})

Expand All @@ -60,7 +61,7 @@ func TestUpdateRAGEngineStatus(t *testing.T) {

mockClient.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&kaitov1alpha1.RAGEngine{}), mock.Anything).Return(errors.New("Get operation failed"))

err := updateObjStatus(ctx, reconciler.Client, &client.ObjectKey{Name: ragengine.Name, Namespace: ragengine.Namespace}, "ragengine", &condition, workerNodes)
err := updateObjStatus(ctx, reconciler.Client, &client.ObjectKey{Name: ragengine.Name, Namespace: ragengine.Namespace}, consts.RAGEngineString, &condition, workerNodes)
assert.NotNil(t, err)
})

Expand All @@ -80,9 +81,9 @@ func TestUpdateRAGEngineStatus(t *testing.T) {
}
workerNodes := []string{"node1", "node2"}

mockClient.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&kaitov1alpha1.RAGEngine{}), mock.Anything).Return(apierrors.NewNotFound(schema.GroupResource{}, "ragengine"))
mockClient.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&kaitov1alpha1.RAGEngine{}), mock.Anything).Return(apierrors.NewNotFound(schema.GroupResource{}, consts.RAGEngineString))

err := updateObjStatus(ctx, reconciler.Client, &client.ObjectKey{Name: ragengine.Name, Namespace: ragengine.Namespace}, "ragengine", &condition, workerNodes)
err := updateObjStatus(ctx, reconciler.Client, &client.ObjectKey{Name: ragengine.Name, Namespace: ragengine.Namespace}, consts.RAGEngineString, &condition, workerNodes)
assert.Nil(t, err)
})
}
Expand Down
Loading

0 comments on commit 85f57e0

Please sign in to comment.