Skip to content

Commit

Permalink
2995 remove cluster network validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rrajendran17 committed Sep 16, 2024
1 parent 2ce5bb5 commit 255b7d8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func run(ctx context.Context, cfg *rest.Config, options *config.Options) error {
}

if err := webhookServer.RegisterValidators(
clusternetwork.NewCnValidator(c.vcCache, c.nadCache),
clusternetwork.NewCnValidator(c.vcCache),
nad.NewNadValidator(c.vmiCache),
vlanconfig.NewVlanConfigValidator(c.nadCache, c.vcCache, c.vsCache, c.vmiCache),
); err != nil {
Expand Down
25 changes: 5 additions & 20 deletions pkg/webhook/clusternetwork/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package clusternetwork
import (
"fmt"

ctlcniv1 "github.com/harvester/harvester/pkg/generated/controllers/k8s.cni.cncf.io/v1"
"github.com/harvester/webhook/pkg/server/admission"
admissionregv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/labels"
Expand All @@ -16,23 +15,20 @@ import (
)

const (
createErr = "could not create cluster network %s because %w"
deleteErr = "could not delete cluster network %s because %w"
StorageNetworkNetAttachDefNamespace = "harvester-system"
createErr = "could not create cluster network %s because %w"
deleteErr = "could not delete cluster network %s because %w"
)

type CnValidator struct {
admission.DefaultValidator
vcCache ctlnetworkv1.VlanConfigCache
nadCache ctlcniv1.NetworkAttachmentDefinitionCache
vcCache ctlnetworkv1.VlanConfigCache
}

var _ admission.Validator = &CnValidator{}

func NewCnValidator(vcCache ctlnetworkv1.VlanConfigCache, nadCache ctlcniv1.NetworkAttachmentDefinitionCache) *CnValidator {
func NewCnValidator(vcCache ctlnetworkv1.VlanConfigCache) *CnValidator {
validator := &CnValidator{
vcCache: vcCache,
nadCache: nadCache,
vcCache: vcCache,
}
return validator
}
Expand All @@ -57,17 +53,6 @@ func (c *CnValidator) Delete(_ *admission.Request, oldObj runtime.Object) error
return fmt.Errorf(deleteErr, cn.Name, fmt.Errorf("it's not allowed"))
}

nads, err := c.nadCache.List(StorageNetworkNetAttachDefNamespace, labels.Set(map[string]string{
utils.KeyClusterNetworkLabel: cn.Name,
}).AsSelector())
if err != nil {
return fmt.Errorf(deleteErr, cn.Name, err)
}

if len(nads) > 0 {
return fmt.Errorf(deleteErr, cn.Name, fmt.Errorf("storage network is still attached"))
}

vcs, err := c.vcCache.List(labels.Set{
utils.KeyClusterNetworkLabel: cn.Name,
}.AsSelector())
Expand Down
7 changes: 6 additions & 1 deletion pkg/webhook/vlanconfig/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
updateErr = "could not update vlanConfig %s because %w"
deleteErr = "could not delete vlanConfig %s because %w"
StorageNetworkNetAttachDefNamespace = "harvester-system"
StorageNetworkAnnotation = "storage-network.settings.harvesterhci.io"
)

type Validator struct {
Expand Down Expand Up @@ -152,7 +153,11 @@ func (v *Validator) Delete(_ *admission.Request, oldObj runtime.Object) error {
}

if len(nads) > 0 {
return fmt.Errorf(deleteErr, vc.Name, fmt.Errorf("storage network is still attached"))
for _, nad := range nads {
if nad.Annotations[StorageNetworkAnnotation] == "true" {
return fmt.Errorf(deleteErr, vc.Name, fmt.Errorf(`storage network nad %s is still attached`, nad.Name))
}
}
}

return nil
Expand Down

0 comments on commit 255b7d8

Please sign in to comment.