Skip to content

Commit

Permalink
Add vlanconfig validator test code
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Wang <[email protected]>
  • Loading branch information
w13915984028 committed Jan 31, 2025
1 parent 1ae64ce commit 5b393ff
Show file tree
Hide file tree
Showing 377 changed files with 54,923 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ require (
github.com/rancher/lasso v0.0.0-20221227210133-6ea88ca2fbcc
github.com/rancher/wrangler v1.1.1
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.3
github.com/tidwall/sjson v1.2.5
github.com/urfave/cli v1.22.9
github.com/vishvananda/netlink v1.2.1-beta.2
Expand Down Expand Up @@ -144,6 +145,7 @@ require (
github.com/pborman/uuid v1.2.1 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.62.0 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
Expand Down
74 changes: 74 additions & 0 deletions pkg/utils/fakeclients/clusternetwork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package fakeclients

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"

"github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io/v1beta1"
networktype "github.com/harvester/harvester-network-controller/pkg/generated/clientset/versioned/typed/network.harvesterhci.io/v1beta1"
networkctl "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io/v1beta1"
)

type ClusterNetworkClient func() networktype.ClusterNetworkInterface

func (c ClusterNetworkClient) Create(s *v1beta1.ClusterNetwork) (*v1beta1.ClusterNetwork, error) {
return c().Create(context.TODO(), s, metav1.CreateOptions{})
}

func (c ClusterNetworkClient) Update(s *v1beta1.ClusterNetwork) (*v1beta1.ClusterNetwork, error) {
return c().Update(context.TODO(), s, metav1.UpdateOptions{})
}

func (c ClusterNetworkClient) UpdateStatus(_ *v1beta1.ClusterNetwork) (*v1beta1.ClusterNetwork, error) {
panic("implement me")
}

func (c ClusterNetworkClient) Delete(name string, options *metav1.DeleteOptions) error {
return c().Delete(context.TODO(), name, *options)
}

func (c ClusterNetworkClient) Get(name string, options metav1.GetOptions) (*v1beta1.ClusterNetwork, error) {
return c().Get(context.TODO(), name, options)
}

func (c ClusterNetworkClient) List(opts metav1.ListOptions) (*v1beta1.ClusterNetworkList, error) {
return c().List(context.TODO(), opts)
}

func (c ClusterNetworkClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
return c().Watch(context.TODO(), opts)
}

func (c ClusterNetworkClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1beta1.ClusterNetwork, err error) {
return c().Patch(context.TODO(), name, pt, data, metav1.PatchOptions{}, subresources...)
}

type ClusterNetworkCache func() networktype.ClusterNetworkInterface

func (c ClusterNetworkCache) Get(name string) (*v1beta1.ClusterNetwork, error) {
return c().Get(context.TODO(), name, metav1.GetOptions{})
}

func (c ClusterNetworkCache) List(selector labels.Selector) ([]*v1beta1.ClusterNetwork, error) {
list, err := c().List(context.TODO(), metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return nil, err
}
result := make([]*v1beta1.ClusterNetwork, 0, len(list.Items))
for i := range list.Items {
result = append(result, &list.Items[i])
}
return result, err
}

func (c ClusterNetworkCache) AddIndexer(_ string, _ networkctl.ClusterNetworkIndexer) {
panic("implement me")
}

func (c ClusterNetworkCache) GetByIndex(_, _ string) ([]*v1beta1.ClusterNetwork, error) {
panic("implement me")
}
74 changes: 74 additions & 0 deletions pkg/utils/fakeclients/vlanconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package fakeclients

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"

"github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io/v1beta1"
networktype "github.com/harvester/harvester-network-controller/pkg/generated/clientset/versioned/typed/network.harvesterhci.io/v1beta1"
networkctl "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io/v1beta1"
)

type VlanConfigClient func() networktype.VlanConfigInterface

func (c VlanConfigClient) Create(s *v1beta1.VlanConfig) (*v1beta1.VlanConfig, error) {
return c().Create(context.TODO(), s, metav1.CreateOptions{})
}

func (c VlanConfigClient) Update(s *v1beta1.VlanConfig) (*v1beta1.VlanConfig, error) {
return c().Update(context.TODO(), s, metav1.UpdateOptions{})
}

func (c VlanConfigClient) UpdateStatus(_ *v1beta1.VlanConfig) (*v1beta1.VlanConfig, error) {
panic("implement me")
}

func (c VlanConfigClient) Delete(name string, options *metav1.DeleteOptions) error {
return c().Delete(context.TODO(), name, *options)
}

func (c VlanConfigClient) Get(name string, options metav1.GetOptions) (*v1beta1.VlanConfig, error) {
return c().Get(context.TODO(), name, options)
}

func (c VlanConfigClient) List(opts metav1.ListOptions) (*v1beta1.VlanConfigList, error) {
return c().List(context.TODO(), opts)
}

func (c VlanConfigClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
return c().Watch(context.TODO(), opts)
}

func (c VlanConfigClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1beta1.VlanConfig, err error) {
return c().Patch(context.TODO(), name, pt, data, metav1.PatchOptions{}, subresources...)
}

type VlanConfigCache func() networktype.VlanConfigInterface

func (c VlanConfigCache) Get(name string) (*v1beta1.VlanConfig, error) {
return c().Get(context.TODO(), name, metav1.GetOptions{})
}

func (c VlanConfigCache) List(selector labels.Selector) ([]*v1beta1.VlanConfig, error) {
list, err := c().List(context.TODO(), metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return nil, err
}
result := make([]*v1beta1.VlanConfig, 0, len(list.Items))
for i := range list.Items {
result = append(result, &list.Items[i])
}
return result, err
}

func (c VlanConfigCache) AddIndexer(_ string, _ networkctl.VlanConfigIndexer) {
panic("implement me")
}

func (c VlanConfigCache) GetByIndex(_, _ string) ([]*v1beta1.VlanConfig, error) {
panic("implement me")
}
74 changes: 74 additions & 0 deletions pkg/utils/fakeclients/vlanstatus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package fakeclients

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"

"github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io/v1beta1"
networktype "github.com/harvester/harvester-network-controller/pkg/generated/clientset/versioned/typed/network.harvesterhci.io/v1beta1"
networkctl "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io/v1beta1"
)

type VlanStatusClient func() networktype.VlanStatusInterface

func (c VlanStatusClient) Create(s *v1beta1.VlanStatus) (*v1beta1.VlanStatus, error) {
return c().Create(context.TODO(), s, metav1.CreateOptions{})
}

func (c VlanStatusClient) Update(s *v1beta1.VlanStatus) (*v1beta1.VlanStatus, error) {
return c().Update(context.TODO(), s, metav1.UpdateOptions{})
}

func (c VlanStatusClient) UpdateStatus(_ *v1beta1.VlanStatus) (*v1beta1.VlanStatus, error) {
panic("implement me")
}

func (c VlanStatusClient) Delete(name string, options *metav1.DeleteOptions) error {
return c().Delete(context.TODO(), name, *options)
}

func (c VlanStatusClient) Get(name string, options metav1.GetOptions) (*v1beta1.VlanStatus, error) {
return c().Get(context.TODO(), name, options)
}

func (c VlanStatusClient) List(opts metav1.ListOptions) (*v1beta1.VlanStatusList, error) {
return c().List(context.TODO(), opts)
}

func (c VlanStatusClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
return c().Watch(context.TODO(), opts)
}

func (c VlanStatusClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1beta1.VlanStatus, err error) {
return c().Patch(context.TODO(), name, pt, data, metav1.PatchOptions{}, subresources...)
}

type VlanStatusCache func() networktype.VlanStatusInterface

func (c VlanStatusCache) Get(name string) (*v1beta1.VlanStatus, error) {
return c().Get(context.TODO(), name, metav1.GetOptions{})
}

func (c VlanStatusCache) List(selector labels.Selector) ([]*v1beta1.VlanStatus, error) {
list, err := c().List(context.TODO(), metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return nil, err
}
result := make([]*v1beta1.VlanStatus, 0, len(list.Items))
for i := range list.Items {
result = append(result, &list.Items[i])
}
return result, err
}

func (c VlanStatusCache) AddIndexer(_ string, _ networkctl.VlanStatusIndexer) {
panic("implement me")
}

func (c VlanStatusCache) GetByIndex(_, _ string) ([]*v1beta1.VlanStatus, error) {
panic("implement me")
}
8 changes: 7 additions & 1 deletion pkg/webhook/vlanconfig/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ type Validator struct {
vcCache ctlnetworkv1.VlanConfigCache
vsCache ctlnetworkv1.VlanStatusCache
vmiCache ctlkubevirtv1.VirtualMachineInstanceCache
cnCache ctlnetworkv1.ClusterNetworkCache
}

func NewVlanConfigValidator(
nadCache ctlcniv1.NetworkAttachmentDefinitionCache,
vcCache ctlnetworkv1.VlanConfigCache,
vsCache ctlnetworkv1.VlanStatusCache,
vmiCache ctlkubevirtv1.VirtualMachineInstanceCache) *Validator {
vmiCache ctlkubevirtv1.VirtualMachineInstanceCache,
cnCache ctlnetworkv1.ClusterNetworkCache) *Validator {
return &Validator{
nadCache: nadCache,
vcCache: vcCache,
vsCache: vsCache,
vmiCache: vmiCache,
cnCache: cnCache,
}
}

Expand Down Expand Up @@ -180,6 +183,9 @@ func (v *Validator) Resource() admission.Resource {
}

func (v *Validator) checkOverlaps(vc *networkv1.VlanConfig, nodes mapset.Set[string]) error {
if nodes == nil {
return nil
}
overlapNods := mapset.NewSet[string]()
for node := range nodes.Iter() {
vsName := utils.Name("", vc.Spec.ClusterNetwork, node)
Expand Down
Loading

0 comments on commit 5b393ff

Please sign in to comment.