Skip to content

Commit 5b393ff

Browse files
committed
Add vlanconfig validator test code
Signed-off-by: Jian Wang <[email protected]>
1 parent 1ae64ce commit 5b393ff

File tree

377 files changed

+54923
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

377 files changed

+54923
-1
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ require (
7373
github.com/rancher/lasso v0.0.0-20221227210133-6ea88ca2fbcc
7474
github.com/rancher/wrangler v1.1.1
7575
github.com/sirupsen/logrus v1.9.0
76+
github.com/stretchr/testify v1.8.3
7677
github.com/tidwall/sjson v1.2.5
7778
github.com/urfave/cli v1.22.9
7879
github.com/vishvananda/netlink v1.2.1-beta.2
@@ -144,6 +145,7 @@ require (
144145
github.com/pborman/uuid v1.2.1 // indirect
145146
github.com/pierrec/lz4/v4 v4.1.15 // indirect
146147
github.com/pkg/errors v0.9.1 // indirect
148+
github.com/pmezard/go-difflib v1.0.0 // indirect
147149
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.62.0 // indirect
148150
github.com/prometheus/client_golang v1.12.2 // indirect
149151
github.com/prometheus/client_model v0.3.0 // indirect
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package fakeclients
2+
3+
import (
4+
"context"
5+
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/apimachinery/pkg/labels"
8+
"k8s.io/apimachinery/pkg/types"
9+
"k8s.io/apimachinery/pkg/watch"
10+
11+
"github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io/v1beta1"
12+
networktype "github.com/harvester/harvester-network-controller/pkg/generated/clientset/versioned/typed/network.harvesterhci.io/v1beta1"
13+
networkctl "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io/v1beta1"
14+
)
15+
16+
type ClusterNetworkClient func() networktype.ClusterNetworkInterface
17+
18+
func (c ClusterNetworkClient) Create(s *v1beta1.ClusterNetwork) (*v1beta1.ClusterNetwork, error) {
19+
return c().Create(context.TODO(), s, metav1.CreateOptions{})
20+
}
21+
22+
func (c ClusterNetworkClient) Update(s *v1beta1.ClusterNetwork) (*v1beta1.ClusterNetwork, error) {
23+
return c().Update(context.TODO(), s, metav1.UpdateOptions{})
24+
}
25+
26+
func (c ClusterNetworkClient) UpdateStatus(_ *v1beta1.ClusterNetwork) (*v1beta1.ClusterNetwork, error) {
27+
panic("implement me")
28+
}
29+
30+
func (c ClusterNetworkClient) Delete(name string, options *metav1.DeleteOptions) error {
31+
return c().Delete(context.TODO(), name, *options)
32+
}
33+
34+
func (c ClusterNetworkClient) Get(name string, options metav1.GetOptions) (*v1beta1.ClusterNetwork, error) {
35+
return c().Get(context.TODO(), name, options)
36+
}
37+
38+
func (c ClusterNetworkClient) List(opts metav1.ListOptions) (*v1beta1.ClusterNetworkList, error) {
39+
return c().List(context.TODO(), opts)
40+
}
41+
42+
func (c ClusterNetworkClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
43+
return c().Watch(context.TODO(), opts)
44+
}
45+
46+
func (c ClusterNetworkClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1beta1.ClusterNetwork, err error) {
47+
return c().Patch(context.TODO(), name, pt, data, metav1.PatchOptions{}, subresources...)
48+
}
49+
50+
type ClusterNetworkCache func() networktype.ClusterNetworkInterface
51+
52+
func (c ClusterNetworkCache) Get(name string) (*v1beta1.ClusterNetwork, error) {
53+
return c().Get(context.TODO(), name, metav1.GetOptions{})
54+
}
55+
56+
func (c ClusterNetworkCache) List(selector labels.Selector) ([]*v1beta1.ClusterNetwork, error) {
57+
list, err := c().List(context.TODO(), metav1.ListOptions{LabelSelector: selector.String()})
58+
if err != nil {
59+
return nil, err
60+
}
61+
result := make([]*v1beta1.ClusterNetwork, 0, len(list.Items))
62+
for i := range list.Items {
63+
result = append(result, &list.Items[i])
64+
}
65+
return result, err
66+
}
67+
68+
func (c ClusterNetworkCache) AddIndexer(_ string, _ networkctl.ClusterNetworkIndexer) {
69+
panic("implement me")
70+
}
71+
72+
func (c ClusterNetworkCache) GetByIndex(_, _ string) ([]*v1beta1.ClusterNetwork, error) {
73+
panic("implement me")
74+
}

pkg/utils/fakeclients/vlanconfig.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package fakeclients
2+
3+
import (
4+
"context"
5+
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/apimachinery/pkg/labels"
8+
"k8s.io/apimachinery/pkg/types"
9+
"k8s.io/apimachinery/pkg/watch"
10+
11+
"github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io/v1beta1"
12+
networktype "github.com/harvester/harvester-network-controller/pkg/generated/clientset/versioned/typed/network.harvesterhci.io/v1beta1"
13+
networkctl "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io/v1beta1"
14+
)
15+
16+
type VlanConfigClient func() networktype.VlanConfigInterface
17+
18+
func (c VlanConfigClient) Create(s *v1beta1.VlanConfig) (*v1beta1.VlanConfig, error) {
19+
return c().Create(context.TODO(), s, metav1.CreateOptions{})
20+
}
21+
22+
func (c VlanConfigClient) Update(s *v1beta1.VlanConfig) (*v1beta1.VlanConfig, error) {
23+
return c().Update(context.TODO(), s, metav1.UpdateOptions{})
24+
}
25+
26+
func (c VlanConfigClient) UpdateStatus(_ *v1beta1.VlanConfig) (*v1beta1.VlanConfig, error) {
27+
panic("implement me")
28+
}
29+
30+
func (c VlanConfigClient) Delete(name string, options *metav1.DeleteOptions) error {
31+
return c().Delete(context.TODO(), name, *options)
32+
}
33+
34+
func (c VlanConfigClient) Get(name string, options metav1.GetOptions) (*v1beta1.VlanConfig, error) {
35+
return c().Get(context.TODO(), name, options)
36+
}
37+
38+
func (c VlanConfigClient) List(opts metav1.ListOptions) (*v1beta1.VlanConfigList, error) {
39+
return c().List(context.TODO(), opts)
40+
}
41+
42+
func (c VlanConfigClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
43+
return c().Watch(context.TODO(), opts)
44+
}
45+
46+
func (c VlanConfigClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1beta1.VlanConfig, err error) {
47+
return c().Patch(context.TODO(), name, pt, data, metav1.PatchOptions{}, subresources...)
48+
}
49+
50+
type VlanConfigCache func() networktype.VlanConfigInterface
51+
52+
func (c VlanConfigCache) Get(name string) (*v1beta1.VlanConfig, error) {
53+
return c().Get(context.TODO(), name, metav1.GetOptions{})
54+
}
55+
56+
func (c VlanConfigCache) List(selector labels.Selector) ([]*v1beta1.VlanConfig, error) {
57+
list, err := c().List(context.TODO(), metav1.ListOptions{LabelSelector: selector.String()})
58+
if err != nil {
59+
return nil, err
60+
}
61+
result := make([]*v1beta1.VlanConfig, 0, len(list.Items))
62+
for i := range list.Items {
63+
result = append(result, &list.Items[i])
64+
}
65+
return result, err
66+
}
67+
68+
func (c VlanConfigCache) AddIndexer(_ string, _ networkctl.VlanConfigIndexer) {
69+
panic("implement me")
70+
}
71+
72+
func (c VlanConfigCache) GetByIndex(_, _ string) ([]*v1beta1.VlanConfig, error) {
73+
panic("implement me")
74+
}

pkg/utils/fakeclients/vlanstatus.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package fakeclients
2+
3+
import (
4+
"context"
5+
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/apimachinery/pkg/labels"
8+
"k8s.io/apimachinery/pkg/types"
9+
"k8s.io/apimachinery/pkg/watch"
10+
11+
"github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io/v1beta1"
12+
networktype "github.com/harvester/harvester-network-controller/pkg/generated/clientset/versioned/typed/network.harvesterhci.io/v1beta1"
13+
networkctl "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io/v1beta1"
14+
)
15+
16+
type VlanStatusClient func() networktype.VlanStatusInterface
17+
18+
func (c VlanStatusClient) Create(s *v1beta1.VlanStatus) (*v1beta1.VlanStatus, error) {
19+
return c().Create(context.TODO(), s, metav1.CreateOptions{})
20+
}
21+
22+
func (c VlanStatusClient) Update(s *v1beta1.VlanStatus) (*v1beta1.VlanStatus, error) {
23+
return c().Update(context.TODO(), s, metav1.UpdateOptions{})
24+
}
25+
26+
func (c VlanStatusClient) UpdateStatus(_ *v1beta1.VlanStatus) (*v1beta1.VlanStatus, error) {
27+
panic("implement me")
28+
}
29+
30+
func (c VlanStatusClient) Delete(name string, options *metav1.DeleteOptions) error {
31+
return c().Delete(context.TODO(), name, *options)
32+
}
33+
34+
func (c VlanStatusClient) Get(name string, options metav1.GetOptions) (*v1beta1.VlanStatus, error) {
35+
return c().Get(context.TODO(), name, options)
36+
}
37+
38+
func (c VlanStatusClient) List(opts metav1.ListOptions) (*v1beta1.VlanStatusList, error) {
39+
return c().List(context.TODO(), opts)
40+
}
41+
42+
func (c VlanStatusClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
43+
return c().Watch(context.TODO(), opts)
44+
}
45+
46+
func (c VlanStatusClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1beta1.VlanStatus, err error) {
47+
return c().Patch(context.TODO(), name, pt, data, metav1.PatchOptions{}, subresources...)
48+
}
49+
50+
type VlanStatusCache func() networktype.VlanStatusInterface
51+
52+
func (c VlanStatusCache) Get(name string) (*v1beta1.VlanStatus, error) {
53+
return c().Get(context.TODO(), name, metav1.GetOptions{})
54+
}
55+
56+
func (c VlanStatusCache) List(selector labels.Selector) ([]*v1beta1.VlanStatus, error) {
57+
list, err := c().List(context.TODO(), metav1.ListOptions{LabelSelector: selector.String()})
58+
if err != nil {
59+
return nil, err
60+
}
61+
result := make([]*v1beta1.VlanStatus, 0, len(list.Items))
62+
for i := range list.Items {
63+
result = append(result, &list.Items[i])
64+
}
65+
return result, err
66+
}
67+
68+
func (c VlanStatusCache) AddIndexer(_ string, _ networkctl.VlanStatusIndexer) {
69+
panic("implement me")
70+
}
71+
72+
func (c VlanStatusCache) GetByIndex(_, _ string) ([]*v1beta1.VlanStatus, error) {
73+
panic("implement me")
74+
}

pkg/webhook/vlanconfig/validator.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,21 @@ type Validator struct {
3838
vcCache ctlnetworkv1.VlanConfigCache
3939
vsCache ctlnetworkv1.VlanStatusCache
4040
vmiCache ctlkubevirtv1.VirtualMachineInstanceCache
41+
cnCache ctlnetworkv1.ClusterNetworkCache
4142
}
4243

4344
func NewVlanConfigValidator(
4445
nadCache ctlcniv1.NetworkAttachmentDefinitionCache,
4546
vcCache ctlnetworkv1.VlanConfigCache,
4647
vsCache ctlnetworkv1.VlanStatusCache,
47-
vmiCache ctlkubevirtv1.VirtualMachineInstanceCache) *Validator {
48+
vmiCache ctlkubevirtv1.VirtualMachineInstanceCache,
49+
cnCache ctlnetworkv1.ClusterNetworkCache) *Validator {
4850
return &Validator{
4951
nadCache: nadCache,
5052
vcCache: vcCache,
5153
vsCache: vsCache,
5254
vmiCache: vmiCache,
55+
cnCache: cnCache,
5356
}
5457
}
5558

@@ -180,6 +183,9 @@ func (v *Validator) Resource() admission.Resource {
180183
}
181184

182185
func (v *Validator) checkOverlaps(vc *networkv1.VlanConfig, nodes mapset.Set[string]) error {
186+
if nodes == nil {
187+
return nil
188+
}
183189
overlapNods := mapset.NewSet[string]()
184190
for node := range nodes.Iter() {
185191
vsName := utils.Name("", vc.Spec.ClusterNetwork, node)

0 commit comments

Comments
 (0)