Skip to content

Commit

Permalink
Add virtualmachineimages.harvesterhci.io validator
Browse files Browse the repository at this point in the history
Signed-off-by: Kiefer Chang <kiefer.chang@suse.com>
  • Loading branch information
bk201 authored and guangbochen committed Jun 11, 2021
1 parent 3b4e2b0 commit f7c2dd2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 64 deletions.
21 changes: 0 additions & 21 deletions pkg/api/image/schema.go

This file was deleted.

2 changes: 0 additions & 2 deletions pkg/api/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/rancher/steve/pkg/server"

"github.com/harvester/harvester/pkg/api/image"
"github.com/harvester/harvester/pkg/api/keypair"
"github.com/harvester/harvester/pkg/api/network"
"github.com/harvester/harvester/pkg/api/node"
Expand All @@ -32,7 +31,6 @@ func registerSchemas(scaled *config.Scaled, server *server.Server, options confi
func Setup(ctx context.Context, server *server.Server, controllers *server.Controllers, options config.Options) error {
scaled := config.ScaledWithContext(ctx)
return registerSchemas(scaled, server, options,
image.RegisterSchema,
keypair.RegisterSchema,
vmtemplate.RegisterSchema,
vm.RegisterSchema,
Expand Down
41 changes: 0 additions & 41 deletions pkg/api/store/displayname.go

This file was deleted.

69 changes: 69 additions & 0 deletions pkg/webhook/resources/virtualmachineimage/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package virtualmachineimage

import (
admissionregv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"

"github.com/harvester/harvester/pkg/apis/harvesterhci.io/v1beta1"
ctlharvesterv1 "github.com/harvester/harvester/pkg/generated/controllers/harvesterhci.io/v1beta1"
werror "github.com/harvester/harvester/pkg/webhook/error"
"github.com/harvester/harvester/pkg/webhook/types"
)

const (
fieldDisplayName = "spec.displayName"
)

func NewValidator(vmimages ctlharvesterv1.VirtualMachineImageCache) types.Validator {
return &virtualMachineImageValidator{
vmimages: vmimages,
}
}

type virtualMachineImageValidator struct {
types.DefaultValidator

vmimages ctlharvesterv1.VirtualMachineImageCache
}

func (v *virtualMachineImageValidator) Resource() types.Resource {
return types.Resource{
Name: v1beta1.VirtualMachineImageResourceName,
Scope: admissionregv1.NamespacedScope,
APIGroup: v1beta1.SchemeGroupVersion.Group,
APIVersion: v1beta1.SchemeGroupVersion.Version,
ObjectType: &v1beta1.VirtualMachineImage{},
OperationTypes: []admissionregv1.OperationType{
admissionregv1.Create,
admissionregv1.Update,
},
}
}

func (v *virtualMachineImageValidator) Create(request *types.Request, newObj runtime.Object) error {
newImage := newObj.(*v1beta1.VirtualMachineImage)

if newImage.Spec.DisplayName == "" {
return werror.NewInvalidError("displayName is required", fieldDisplayName)
}

allImages, err := v.vmimages.List(newImage.Namespace, labels.Everything())
if err != nil {
return err
}
for _, image := range allImages {
if newImage.Name == image.Name {
continue
}
if newImage.Spec.DisplayName == image.Spec.DisplayName {
return werror.NewConflict("A resource with the same name exists")
}
}

return nil
}

func (v *virtualMachineImageValidator) Update(request *types.Request, oldObj runtime.Object, newObj runtime.Object) error {
return v.Create(request, newObj)
}

0 comments on commit f7c2dd2

Please sign in to comment.