Skip to content

Commit

Permalink
bump: webhook to 0.1.3
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Yu <[email protected]>
  • Loading branch information
Yu-Jack committed Jan 17, 2024
1 parent 9bec6b9 commit 29250da
Show file tree
Hide file tree
Showing 31 changed files with 491 additions and 274 deletions.
41 changes: 25 additions & 16 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@ package main

import (
"context"
"fmt"
"os"

ctlcni "github.com/harvester/harvester/pkg/generated/controllers/k8s.cni.cncf.io"
ctlcniv1 "github.com/harvester/harvester/pkg/generated/controllers/k8s.cni.cncf.io/v1"
ctlkubevirt "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io"
ctlkubevirtv1 "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io/v1"
"github.com/harvester/harvester/pkg/indexeres"
"github.com/harvester/webhook/pkg/config"
"github.com/harvester/webhook/pkg/server"
"github.com/harvester/webhook/pkg/types"
ctlcore "github.com/rancher/wrangler/pkg/generated/controllers/core"
ctlcorev1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/kubeconfig"
Expand All @@ -22,11 +15,19 @@ import (
"k8s.io/client-go/rest"
kubevirtv1 "kubevirt.io/api/core/v1"

ctlnetwork "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io"
ctlnetworkv1 "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io/v1beta1"
"github.com/harvester/harvester-network-controller/pkg/webhook/clusternetwork"
"github.com/harvester/harvester-network-controller/pkg/webhook/nad"
"github.com/harvester/harvester-network-controller/pkg/webhook/vlanconfig"
ctlcni "github.com/harvester/harvester/pkg/generated/controllers/k8s.cni.cncf.io"
ctlcniv1 "github.com/harvester/harvester/pkg/generated/controllers/k8s.cni.cncf.io/v1"
ctlkubevirt "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io"
ctlkubevirtv1 "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io/v1"
"github.com/harvester/harvester/pkg/indexeres"
"github.com/harvester/webhook/pkg/config"
"github.com/harvester/webhook/pkg/server"

ctlnetwork "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io"
ctlnetworkv1 "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io/v1beta1"
)

const name = "harvester-network-webhook"
Expand Down Expand Up @@ -107,15 +108,23 @@ func run(ctx context.Context, cfg *rest.Config, options *config.Options) error {
return err
}

webhookServer := server.New(ctx, cfg, name, options)
admitters := []types.Admitter{
types.Validator2Admitter(clusternetwork.NewCnValidator(c.vcCache)),
types.Validator2Admitter(nad.NewNadValidator(c.vmiCache)),
types.Validator2Admitter(vlanconfig.NewVlanConfigValidator(c.nadCache, c.vcCache, c.vsCache, c.vmiCache)),
webhookServer := server.NewWebhookServer(ctx, cfg, name, options)

if err := webhookServer.RegisterMutators(
nad.NewNadMutator(c.cnCache, c.vcCache),
vlanconfig.NewVlanConfigMutator(c.nodeCache),
); err != nil {
return fmt.Errorf("failed to register mutators: %v", err)
}
webhookServer.Register(admitters)

if err := webhookServer.RegisterValidators(
clusternetwork.NewCnValidator(c.vcCache),
nad.NewNadValidator(c.vmiCache),
vlanconfig.NewVlanConfigValidator(c.nadCache, c.vcCache, c.vsCache, c.vmiCache),
); err != nil {
return fmt.Errorf("failed to register validators: %v", err)
}

if err := webhookServer.Start(); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ require (
github.com/deckarep/golang-set/v2 v2.1.0
github.com/go-ping/ping v0.0.0-20211014180314-6e2b003bffdd
github.com/harvester/harvester v1.1.2-rc8
github.com/harvester/webhook v0.1.2
github.com/harvester/webhook v0.1.3
github.com/insomniacslk/dhcp v0.0.0-20201112113307-4de412bc85d8
github.com/k8snetworkplumbingwg/network-attachment-definition-client v0.0.0-20200331171230-d50e42f2b669
github.com/rancher/lasso v0.0.0-20221227210133-6ea88ca2fbcc
github.com/rancher/wrangler v1.1.0
github.com/rancher/wrangler v1.1.1
github.com/sirupsen/logrus v1.9.0
github.com/tidwall/sjson v1.2.5
github.com/urfave/cli v1.22.9
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1119,8 +1119,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFb
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w=
github.com/harvester/harvester v1.1.2-rc8 h1:kw4OuCdHn2O/vVX5m0ZjUsOFfYZtfXhiOCd8/Ex6VNE=
github.com/harvester/harvester v1.1.2-rc8/go.mod h1:0u5p38ODTd4L/ZQtgyjoB851Jg8YcGTK29rmqGtA6To=
github.com/harvester/webhook v0.1.2 h1:fo3PXiDBAEl0Fq2AfOkblMIRihp5Y91wOatP1AERX74=
github.com/harvester/webhook v0.1.2/go.mod h1:vveGwEGuHUN2lEHbNrZKisiMYBTMakrNd0cLSnRkEGI=
github.com/harvester/webhook v0.1.3 h1:rPdpOikIFWTRQGidgWaAUoUc/zgv0E5EzjX8MF3Fi8A=
github.com/harvester/webhook v0.1.3/go.mod h1:vfRPB26WHSPxMF/ONpUVzaEaewTUxpP9qAqu1ZyonR0=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -1496,8 +1496,8 @@ github.com/rancher/wrangler v0.6.2-0.20200820173016-2068de651106/go.mod h1:iKqQc
github.com/rancher/wrangler v0.8.3/go.mod h1:dKEaHNB4izxmPUtpq1Hvr3z3Oh+9k5pCZyFO9sUhlaY=
github.com/rancher/wrangler v0.8.10/go.mod h1:Lte9WjPtGYxYacIWeiS9qawvu2R4NujFU9xuXWJvc/0=
github.com/rancher/wrangler v0.8.11-0.20211214201934-f5aa5d9f2e81/go.mod h1:Lte9WjPtGYxYacIWeiS9qawvu2R4NujFU9xuXWJvc/0=
github.com/rancher/wrangler v1.1.0 h1:1VWistON261oKmCPF5fOPMWb/YwjgEciO9pCw5Z0mzQ=
github.com/rancher/wrangler v1.1.0/go.mod h1:lQorqAAIMkNWteece1GiuwZTmMqkaVTXL5qjiiPVDxQ=
github.com/rancher/wrangler v1.1.1 h1:wmqUwqc2M7ADfXnBCJTFkTB5ZREWpD78rnZMzmxwMvM=
github.com/rancher/wrangler v1.1.1/go.mod h1:ioVbKupzcBOdzsl55MvEDN0R1wdGggj8iNCYGFI5JvM=
github.com/rancher/wrangler-api v0.6.1-0.20200427172631-a7c2f09b783e/go.mod h1:2lcWR98q8HU3U4mVETnXc8quNG0uXxrt8vKd6cAa/30=
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
Expand Down
13 changes: 7 additions & 6 deletions pkg/webhook/clusternetwork/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package clusternetwork
import (
"fmt"

"github.com/harvester/webhook/pkg/types"
admissionregv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"

"github.com/harvester/webhook/pkg/server/admission"

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/utils"
Expand All @@ -18,11 +19,11 @@ const (
)

type CnValidator struct {
types.DefaultValidator
admission.DefaultValidator
vcCache ctlnetworkv1.VlanConfigCache
}

var _ types.Validator = &CnValidator{}
var _ admission.Validator = &CnValidator{}

func NewCnValidator(vcCache ctlnetworkv1.VlanConfigCache) *CnValidator {
validator := &CnValidator{
Expand All @@ -31,7 +32,7 @@ func NewCnValidator(vcCache ctlnetworkv1.VlanConfigCache) *CnValidator {
return validator
}

func (c *CnValidator) Delete(_ *types.Request, oldObj runtime.Object) error {
func (c *CnValidator) Delete(_ *admission.Request, oldObj runtime.Object) error {
cn := oldObj.(*networkv1.ClusterNetwork)

if cn.Name == utils.ManagementClusterNetworkName {
Expand All @@ -57,8 +58,8 @@ func (c *CnValidator) Delete(_ *types.Request, oldObj runtime.Object) error {
return nil
}

func (c *CnValidator) Resource() types.Resource {
return types.Resource{
func (c *CnValidator) Resource() admission.Resource {
return admission.Resource{
Names: []string{"clusternetworks"},
Scope: admissionregv1.ClusterScope,
APIGroup: networkv1.SchemeGroupVersion.Group,
Expand Down
39 changes: 20 additions & 19 deletions pkg/webhook/nad/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ import (
"reflect"
"strconv"

"github.com/harvester/webhook/pkg/types"
cniv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"github.com/tidwall/sjson"
admissionregv1 "k8s.io/api/admissionregistration/v1"
k8slabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"

"github.com/harvester/webhook/pkg/server/admission"

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

var _ types.Mutator = &Mutator{}
var _ admission.Mutator = &Mutator{}

type Mutator struct {
types.DefaultMutator
admission.DefaultMutator
cnCache ctlnetworkv1.ClusterNetworkCache
vcCache ctlnetworkv1.VlanConfigCache
}
Expand All @@ -36,7 +37,7 @@ func NewNadMutator(cnCache ctlnetworkv1.ClusterNetworkCache,
}
}

func (m *Mutator) Create(_ *types.Request, newObj runtime.Object) (types.Patch, error) {
func (m *Mutator) Create(_ *admission.Request, newObj runtime.Object) (admission.Patch, error) {
nad := newObj.(*cniv1.NetworkAttachmentDefinition)

patch, err := m.patchMTU(nad.Spec.Config)
Expand All @@ -47,7 +48,7 @@ func (m *Mutator) Create(_ *types.Request, newObj runtime.Object) (types.Patch,
return patch, nil
}

func (m *Mutator) Update(_ *types.Request, oldObj, newObj runtime.Object) (types.Patch, error) {
func (m *Mutator) Update(_ *admission.Request, oldObj, newObj runtime.Object) (admission.Patch, error) {
oldNad := oldObj.(*cniv1.NetworkAttachmentDefinition)
newNad := newObj.(*cniv1.NetworkAttachmentDefinition)

Expand Down Expand Up @@ -80,8 +81,8 @@ func (m *Mutator) Update(_ *types.Request, oldObj, newObj runtime.Object) (types
return append(patch, annotationPatch...), nil
}

func (m *Mutator) Resource() types.Resource {
return types.Resource{
func (m *Mutator) Resource() admission.Resource {
return admission.Resource{
Names: []string{"network-attachment-definitions"},
Scope: admissionregv1.NamespacedScope,
APIGroup: cniv1.SchemeGroupVersion.Group,
Expand All @@ -94,7 +95,7 @@ func (m *Mutator) Resource() types.Resource {
}
}

func (m *Mutator) ensureLabels(nad *cniv1.NetworkAttachmentDefinition, oldConf, newConf *utils.NetConf) (types.Patch, error) {
func (m *Mutator) ensureLabels(nad *cniv1.NetworkAttachmentDefinition, oldConf, newConf *utils.NetConf) (admission.Patch, error) {
labels := nad.Labels
if labels == nil {
labels = make(map[string]string)
Expand Down Expand Up @@ -130,16 +131,16 @@ func (m *Mutator) ensureLabels(nad *cniv1.NetworkAttachmentDefinition, oldConf,
labels[utils.KeyNetworkReady] = utils.ValueFalse
}

return types.Patch{
types.PatchOp{
Op: types.PatchOpReplace,
return admission.Patch{
admission.PatchOp{
Op: admission.PatchOpReplace,
Path: "/metadata/labels",
Value: labels,
}}, nil
}

// If the vlan or bridge name is changed, we need to tag the route annotation outdated
func tagRouteOutdated(nad *cniv1.NetworkAttachmentDefinition, oldConf, newConf *utils.NetConf) (types.Patch, error) {
func tagRouteOutdated(nad *cniv1.NetworkAttachmentDefinition, oldConf, newConf *utils.NetConf) (admission.Patch, error) {
if oldConf.BrName == newConf.BrName && oldConf.Vlan == newConf.Vlan {
return nil, nil
}
Expand Down Expand Up @@ -173,16 +174,16 @@ func tagRouteOutdated(nad *cniv1.NetworkAttachmentDefinition, oldConf, newConf *
annotations[utils.KeyNetworkRoute] = string(outdatedRoute)
}

return types.Patch{
types.PatchOp{
Op: types.PatchOpReplace,
return admission.Patch{
admission.PatchOp{
Op: admission.PatchOpReplace,
Path: "/metadata/annotations",
Value: annotations,
},
}, nil
}

func (m *Mutator) patchMTU(config string) (types.Patch, error) {
func (m *Mutator) patchMTU(config string) (admission.Patch, error) {
netConf := &utils.NetConf{}
if err := json.Unmarshal([]byte(config), netConf); err != nil {
return nil, err
Expand All @@ -204,9 +205,9 @@ func (m *Mutator) patchMTU(config string) (types.Patch, error) {
return nil, fmt.Errorf("set mtu failed, error: %w", err)
}

return types.Patch{
types.PatchOp{
Op: types.PatchOpReplace,
return admission.Patch{
admission.PatchOp{
Op: admission.PatchOpReplace,
Path: "/spec/config",
Value: newConfig,
},
Expand Down
19 changes: 10 additions & 9 deletions pkg/webhook/nad/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"reflect"
"strings"

ctlkubevirtv1 "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io/v1"
"github.com/harvester/webhook/pkg/types"
cniv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
admissionregv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/runtime"

ctlkubevirtv1 "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io/v1"
"github.com/harvester/webhook/pkg/server/admission"

"github.com/harvester/harvester-network-controller/pkg/network/iface"
"github.com/harvester/harvester-network-controller/pkg/utils"
)
Expand All @@ -23,19 +24,19 @@ const (
)

type Validator struct {
types.DefaultValidator
admission.DefaultValidator
vmiCache ctlkubevirtv1.VirtualMachineInstanceCache
}

var _ types.Validator = &Validator{}
var _ admission.Validator = &Validator{}

func NewNadValidator(vmiCache ctlkubevirtv1.VirtualMachineInstanceCache) *Validator {
return &Validator{
vmiCache: vmiCache,
}
}

func (v *Validator) Create(_ *types.Request, newObj runtime.Object) error {
func (v *Validator) Create(_ *admission.Request, newObj runtime.Object) error {
nad := newObj.(*cniv1.NetworkAttachmentDefinition)

if err := v.checkRoute(nad.Annotations[utils.KeyNetworkRoute]); err != nil {
Expand All @@ -53,7 +54,7 @@ func (v *Validator) Create(_ *types.Request, newObj runtime.Object) error {
return nil
}

func (v *Validator) Update(_ *types.Request, oldObj, newObj runtime.Object) error {
func (v *Validator) Update(_ *admission.Request, oldObj, newObj runtime.Object) error {
newNad := newObj.(*cniv1.NetworkAttachmentDefinition)
oldNad := oldObj.(*cniv1.NetworkAttachmentDefinition)

Expand Down Expand Up @@ -89,7 +90,7 @@ func (v *Validator) Update(_ *types.Request, oldObj, newObj runtime.Object) erro
return nil
}

func (v *Validator) Delete(_ *types.Request, oldObj runtime.Object) error {
func (v *Validator) Delete(_ *admission.Request, oldObj runtime.Object) error {
nad := oldObj.(*cniv1.NetworkAttachmentDefinition)

if err := v.checkVmi(nad); err != nil {
Expand Down Expand Up @@ -143,8 +144,8 @@ func (v *Validator) checkVmi(nad *cniv1.NetworkAttachmentDefinition) error {
return nil
}

func (v *Validator) Resource() types.Resource {
return types.Resource{
func (v *Validator) Resource() admission.Resource {
return admission.Resource{
Names: []string{"network-attachment-definitions"},
Scope: admissionregv1.NamespacedScope,
APIGroup: cniv1.SchemeGroupVersion.Group,
Expand Down
Loading

0 comments on commit 29250da

Please sign in to comment.