diff --git a/k8s/apis/ai/v1/model_webhook.go b/k8s/apis/ai/v1/model_webhook.go index 9adb85543..f792ffa82 100644 --- a/k8s/apis/ai/v1/model_webhook.go +++ b/k8s/apis/ai/v1/model_webhook.go @@ -155,7 +155,8 @@ func (r *Model) validateCreateModel() error { pack := extractModelValidationPack(models, p) ret, err := configutils.CheckValidationPack(r.ObjectMeta.Name, readModelValiationTarget(r, p), p.ValidationType, pack) if err != nil { - return err + allErrs = append(allErrs, field.Forbidden(&field.Path{}, strings.ReplaceAll(p.Message, "%s", "Validation encountered an unexpected error."))) + return apierrors.NewInvalid(schema.GroupKind{Group: "ai.symphony", Kind: "Model"}, r.Name, allErrs) } if ret != "" { allErrs = append(allErrs, field.Forbidden(&field.Path{}, strings.ReplaceAll(p.Message, "%s", ret))) @@ -188,7 +189,8 @@ func (r *Model) validateUpdateModel() error { pack := extractModelValidationPack(models, p) ret, err := configutils.CheckValidationPack(r.ObjectMeta.Name, readModelValiationTarget(r, p), p.ValidationType, pack) if err != nil { - return err + allErrs = append(allErrs, field.Forbidden(&field.Path{}, strings.ReplaceAll(p.Message, "%s", "Validation encountered an unexpected error."))) + return apierrors.NewInvalid(schema.GroupKind{Group: "ai.symphony", Kind: "Model"}, r.Name, allErrs) } if ret != "" { allErrs = append(allErrs, field.Forbidden(&field.Path{}, strings.ReplaceAll(p.Message, "%s", ret))) diff --git a/k8s/apis/ai/v1/skill_webhook.go b/k8s/apis/ai/v1/skill_webhook.go index dcd791ef3..1ed40a258 100644 --- a/k8s/apis/ai/v1/skill_webhook.go +++ b/k8s/apis/ai/v1/skill_webhook.go @@ -12,6 +12,7 @@ import ( "gopls-workspace/apis/metrics/v1" "time" + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -126,7 +127,7 @@ func (r *Skill) validateCreateSkill() error { var skills SkillList mySkillClient.List(context.Background(), &skills, client.InNamespace(r.Namespace), client.MatchingFields{".spec.displayName": r.Spec.DisplayName}) if len(skills.Items) != 0 { - return fmt.Errorf("skill display name '%s' is already taken", r.Spec.DisplayName) + return apierrors.NewBadRequest(fmt.Sprintf("skill display name '%s' is already taken", r.Spec.DisplayName)) } return nil } @@ -135,10 +136,10 @@ func (r *Skill) validateUpdateSkill() error { var skills SkillList err := mySkillClient.List(context.Background(), &skills, client.InNamespace(r.Namespace), client.MatchingFields{".spec.displayName": r.Spec.DisplayName}) if err != nil { - return err + return apierrors.NewInternalError(err) } if !(len(skills.Items) == 0 || len(skills.Items) == 1 && skills.Items[0].ObjectMeta.Name == r.ObjectMeta.Name) { - return fmt.Errorf("skill display name '%s' is already taken", r.Spec.DisplayName) + return apierrors.NewBadRequest(fmt.Sprintf("skill display name '%s' is already taken", r.Spec.DisplayName)) } return nil } diff --git a/k8s/apis/fabric/v1/device_webhook.go b/k8s/apis/fabric/v1/device_webhook.go index b884e2bd4..b85fbdb58 100644 --- a/k8s/apis/fabric/v1/device_webhook.go +++ b/k8s/apis/fabric/v1/device_webhook.go @@ -12,6 +12,7 @@ import ( "gopls-workspace/apis/metrics/v1" "time" + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -126,7 +127,7 @@ func (r *Device) validateCreateDevice() error { var devices DeviceList myDeviceClient.List(context.Background(), &devices, client.InNamespace(r.Namespace), client.MatchingFields{".spec.displayName": r.Spec.DisplayName}) if len(devices.Items) != 0 { - return fmt.Errorf("device display name '%s' is already taken", r.Spec.DisplayName) + return apierrors.NewBadRequest(fmt.Sprintf("device display name '%s' is already taken", r.Spec.DisplayName)) } return nil } @@ -135,10 +136,10 @@ func (r *Device) validateUpdateDevice() error { var devices DeviceList err := myDeviceClient.List(context.Background(), &devices, client.InNamespace(r.Namespace), client.MatchingFields{".spec.displayName": r.Spec.DisplayName}) if err != nil { - return err + return apierrors.NewInternalError(err) } if !(len(devices.Items) == 0 || len(devices.Items) == 1 && devices.Items[0].ObjectMeta.Name == r.ObjectMeta.Name) { - return fmt.Errorf("device display name '%s' is already taken", r.Spec.DisplayName) + return apierrors.NewBadRequest(fmt.Sprintf("device display name '%s' is already taken", r.Spec.DisplayName)) } return nil } diff --git a/k8s/apis/federation/v1/catalog_webhook.go b/k8s/apis/federation/v1/catalog_webhook.go index 80e379d2e..f6b7a996a 100644 --- a/k8s/apis/federation/v1/catalog_webhook.go +++ b/k8s/apis/federation/v1/catalog_webhook.go @@ -9,11 +9,12 @@ package v1 import ( "context" "encoding/json" + "fmt" "gopls-workspace/apis/metrics/v1" "time" "github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/utils" - "github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2" + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -130,7 +131,7 @@ func (r *Catalog) checkSchema() error { err := myCatalogClient.List(context.Background(), &catalogs, client.InNamespace(r.ObjectMeta.Namespace), client.MatchingFields{".metadata.name": schemaName}) if err != nil || len(catalogs.Items) == 0 { cataloglog.Error(err, "Could not find the required schema.", "name", schemaName) - return v1alpha2.NewCOAError(err, "schema not found", v1alpha2.NotFound) + return apierrors.NewBadRequest(fmt.Sprintf("Could not find the required schema, %s.", schemaName)) } jData, _ := json.Marshal(catalogs.Items[0].Spec.Properties) @@ -138,7 +139,7 @@ func (r *Catalog) checkSchema() error { err = json.Unmarshal(jData, &properties) if err != nil { cataloglog.Error(err, "Invalid schema.", "name", schemaName) - return v1alpha2.NewCOAError(err, "invalid schema", v1alpha2.ValidateFailed) + return apierrors.NewBadRequest(fmt.Sprintf("Invalid schema, %s.", schemaName)) } if spec, ok := properties["spec"]; ok { var schemaObj utils.Schema @@ -146,23 +147,23 @@ func (r *Catalog) checkSchema() error { err := json.Unmarshal(jData, &schemaObj) if err != nil { cataloglog.Error(err, "Invalid schema.", "name", schemaName) - return v1alpha2.NewCOAError(err, "invalid schema", v1alpha2.ValidateFailed) + return apierrors.NewBadRequest(fmt.Sprintf("Invalid schema, %s.", schemaName)) } jData, _ = json.Marshal(r.Spec.Properties) var properties map[string]interface{} err = json.Unmarshal(jData, &properties) if err != nil { cataloglog.Error(err, "Validating failed.") - return v1alpha2.NewCOAError(err, "invalid properties", v1alpha2.ValidateFailed) + return apierrors.NewBadRequest("Invalid properties of the catalog.") } result, err := schemaObj.CheckProperties(properties, nil) if err != nil { cataloglog.Error(err, "Validating failed.") - return v1alpha2.NewCOAError(err, "invalid properties", v1alpha2.ValidateFailed) + return apierrors.NewBadRequest("Validate failed for the catalog.") } if !result.Valid { cataloglog.Error(err, "Validating failed.") - return v1alpha2.NewCOAError(err, "invalid properties", v1alpha2.ValidateFailed) + return apierrors.NewBadRequest("This is not a valid catalog according to the schema.") } } cataloglog.Info("Validation finished.", "name", r.Name) diff --git a/k8s/apis/solution/v1/solution_webhook.go b/k8s/apis/solution/v1/solution_webhook.go index 8700a0a4c..4c81fcf44 100644 --- a/k8s/apis/solution/v1/solution_webhook.go +++ b/k8s/apis/solution/v1/solution_webhook.go @@ -10,6 +10,7 @@ import ( "context" "fmt" + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -78,7 +79,7 @@ func (r *Solution) validateCreateSolution() error { var solutions SolutionList mySolutionClient.List(context.Background(), &solutions, client.InNamespace(r.Namespace), client.MatchingFields{".spec.displayName": r.Spec.DisplayName}) if len(solutions.Items) != 0 { - return fmt.Errorf("solution display name '%s' is already taken", r.Spec.DisplayName) + return apierrors.NewBadRequest(fmt.Sprintf("solution display name '%s' is already taken", r.Spec.DisplayName)) } return nil } @@ -87,10 +88,10 @@ func (r *Solution) validateUpdateSolution() error { var solutions SolutionList err := mySolutionClient.List(context.Background(), &solutions, client.InNamespace(r.Namespace), client.MatchingFields{".spec.displayName": r.Spec.DisplayName}) if err != nil { - return err + return apierrors.NewInternalError(err) } if !(len(solutions.Items) == 0 || len(solutions.Items) == 1 && solutions.Items[0].ObjectMeta.Name == r.ObjectMeta.Name) { - return fmt.Errorf("solution display name '%s' is already taken", r.Spec.DisplayName) + return apierrors.NewBadRequest(fmt.Sprintf("solution display name '%s' is already taken", r.Spec.DisplayName)) } return nil }