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 authored and WebberHuang1118 committed Sep 19, 2024
1 parent bdc4343 commit f81a622
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 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
18 changes: 12 additions & 6 deletions pkg/webhook/vlanconfig/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ import (
"k8s.io/apimachinery/pkg/runtime"
kubevirtv1 "kubevirt.io/api/core/v1"

"github.com/harvester/harvester/pkg/util"

networkv1 "github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io/v1beta1"
ctlnetworkv1 "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io/v1beta1"
"github.com/harvester/harvester-network-controller/pkg/network/iface"
"github.com/harvester/harvester-network-controller/pkg/utils"
)

const (
createErr = "could not create vlanConfig %s because %w"
updateErr = "could not update vlanConfig %s because %w"
deleteErr = "could not delete vlanConfig %s because %w"
StorageNetworkNetAttachDefNamespace = "harvester-system"
createErr = "could not create vlanConfig %s because %w"
updateErr = "could not update vlanConfig %s because %w"
deleteErr = "could not delete vlanConfig %s because %w"
StorageNetworkAnnotation = "storage-network.settings.harvesterhci.io"
)

type Validator struct {
Expand Down Expand Up @@ -144,15 +146,19 @@ func (v *Validator) Delete(_ *admission.Request, oldObj runtime.Object) error {
return fmt.Errorf(deleteErr, vc.Name, err)
}

nads, err := v.nadCache.List(StorageNetworkNetAttachDefNamespace, labels.Set(map[string]string{
nads, err := v.nadCache.List(util.HarvesterSystemNamespaceName, labels.Set(map[string]string{
utils.KeyClusterNetworkLabel: vc.Spec.ClusterNetwork,
}).AsSelector())
if err != nil {
return fmt.Errorf(deleteErr, vc.Name, err)
}

if len(nads) > 0 {
return fmt.Errorf(deleteErr, vc.Name, fmt.Errorf("storage network is still attached"))
for _, nad := range nads {
if nad.DeletionTimestamp == nil && 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 f81a622

Please sign in to comment.