Skip to content

Commit

Permalink
test validator
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Wang <jian.wang@suse.com>
  • Loading branch information
w13915984028 committed Sep 17, 2024
1 parent 45e1d33 commit d5fd96c
Show file tree
Hide file tree
Showing 381 changed files with 55,324 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 @@ -119,7 +119,7 @@ func run(ctx context.Context, cfg *rest.Config, options *config.Options) error {
if err := webhookServer.RegisterValidators(
clusternetwork.NewCnValidator(c.vcCache),
nad.NewNadValidator(c.vmiCache),
vlanconfig.NewVlanConfigValidator(c.nadCache, c.vcCache, c.vsCache, c.vmiCache),
vlanconfig.NewVlanConfigValidator(c.nadCache, c.vcCache, c.vsCache, c.cnCache, c.vmiCache),
); err != nil {
return fmt.Errorf("failed to register validators: %v", err)
}
Expand Down
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
51 changes: 33 additions & 18 deletions pkg/utils/fakeclients/clusternetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,85 @@ import (
"context"

// "github.com/rancher/wrangler/v3/pkg/generic"
"github.com/rancher/wrangler/pkg/generic"
// "github.com/rancher/wrangler/pkg/generic"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/rest"
// "k8s.io/client-go/rest"

"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
type ClusterNetworkClient func() networktype.ClusterNetworkInterface

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

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

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

func (c VlanConfigClient) Delete(name string, options *metav1.DeleteOptions) error {
func (c ClusterNetworkClient) 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) {
func (c ClusterNetworkClient) Get(name string, options metav1.GetOptions) (*v1beta1.ClusterNetwork, error) {
return c().Get(context.TODO(), name, options)
}

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

func (c VlanConfigClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
func (c ClusterNetworkClient) 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) {
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...)
}

func (c VlanConfigClient) WithImpersonation(_ rest.ImpersonationConfig) (generic.NonNamespacedClientInterface[*v1beta1.VlanConfig, *v1beta1.VlanConfigList], error) {
/*
func (c ClusterNetworkClient) WithImpersonation(_ rest.ImpersonationConfig) (generic.NonNamespacedClientInterface[*v1beta1.ClusterNetwork, *v1beta1.ClusterNetworkList], error) {
panic("implement me")
}
*/

type VlanConfigCache func() networktype.VlanConfigInterface
type ClusterNetworkCache func() networktype.ClusterNetworkInterface

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

func (c VlanConfigCache) List(_ labels.Selector) ([]*v1beta1.VlanConfig, error) {
panic("implement me")
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 VlanConfigCache) AddIndexer(_ string, _ generic.Indexer[*v1beta1.VlanConfig]) {

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

func (c VlanConfigCache) GetByIndex(_, _ string) ([]*v1beta1.VlanConfig, error) {
// type ClusterNetworkIndexer func(obj *v1beta1.ClusterNetwork) ([]string, error)


func (c ClusterNetworkCache) GetByIndex(_, _ string) ([]*v1beta1.ClusterNetwork, error) {
panic("implement me")
}

20 changes: 14 additions & 6 deletions pkg/utils/fakeclients/vlanconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package fakeclients
import (
"context"

// "github.com/rancher/wrangler/v3/pkg/generic"
"github.com/rancher/wrangler/pkg/generic"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/rest"

"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
Expand Down Expand Up @@ -49,21 +47,31 @@ func (c VlanConfigClient) Patch(name string, pt types.PatchType, data []byte, su
return c().Patch(context.TODO(), name, pt, data, metav1.PatchOptions{}, subresources...)
}

/*
func (c VlanConfigClient) WithImpersonation(_ rest.ImpersonationConfig) (generic.NonNamespacedClientInterface[*v1beta1.VlanConfig, *v1beta1.VlanConfigList], error) {
panic("implement me")
}
*/

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(_ labels.Selector) ([]*v1beta1.VlanConfig, error) {
panic("implement me")
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, _ generic.Indexer[*v1beta1.VlanConfig]) {
func (c VlanConfigCache) AddIndexer(_ string, _ networkctl.VlanConfigIndexer) {
panic("implement me")
}

Expand Down
82 changes: 82 additions & 0 deletions pkg/utils/fakeclients/vlanstatus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package fakeclients

import (
"context"

// "github.com/rancher/wrangler/v3/pkg/generic"
// "github.com/rancher/wrangler/pkg/generic"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
// "k8s.io/client-go/rest"

"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...)
}

/*
func (c VlanStatusClient) WithImpersonation(_ rest.ImpersonationConfig) (generic.NonNamespacedClientInterface[*v1beta1.VlanStatus, *v1beta1.VlanStatusList], error) {
panic("implement me")
}
*/
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")
}
Loading

0 comments on commit d5fd96c

Please sign in to comment.