Skip to content

Commit

Permalink
Merge branch 'xigsun/fix' of https://github.com/FireDefend/symphony i…
Browse files Browse the repository at this point in the history
…nto xigsun/fix
  • Loading branch information
FireDefend committed May 24, 2024
2 parents 140d690 + 0dcf4f7 commit d22a879
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
6 changes: 4 additions & 2 deletions k8s/apis/ai/v1/model_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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)))
Expand Down
7 changes: 4 additions & 3 deletions k8s/apis/ai/v1/skill_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
7 changes: 4 additions & 3 deletions k8s/apis/fabric/v1/device_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
15 changes: 8 additions & 7 deletions k8s/apis/federation/v1/catalog_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -130,39 +131,39 @@ 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)
var properties map[string]interface{}
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
jData, _ := json.Marshal(spec)
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)
Expand Down
7 changes: 4 additions & 3 deletions k8s/apis/solution/v1/solution_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

0 comments on commit d22a879

Please sign in to comment.