Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync MTU and check MTU valid values #116

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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
59 changes: 58 additions & 1 deletion pkg/controller/manager/nad/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
ctlcniv1 "github.com/harvester/harvester/pkg/generated/controllers/k8s.cni.cncf.io/v1"
cniv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
ctlbatchv1 "github.com/rancher/wrangler/pkg/generated/controllers/batch/v1"
"github.com/tidwall/sjson"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"

"github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io"
Expand Down Expand Up @@ -89,10 +91,65 @@ func Register(ctx context.Context, management *config.Management) error {

nads.OnChange(ctx, ControllerName, handler.OnChange)
nads.OnRemove(ctx, ControllerName, handler.OnRemove)

cns.OnChange(ctx, ControllerName, handler.OnCNChange)
return nil
}

func (h Handler) OnCNChange(_ string, cn *networkv1.ClusterNetwork) (*networkv1.ClusterNetwork, error) {
if cn == nil || cn.DeletionTimestamp != nil {
return nil, nil
}

// MTU label is not set
curLbMTU := cn.Labels[utils.KeyUplinkMTU]
if curLbMTU == "" {
return nil, nil
}
MTU, err := strconv.Atoi(curLbMTU)
if err != nil {
return nil, fmt.Errorf("cluster network %v has an invalid label %v/%v, error %w", cn.Name, utils.KeyUplinkMTU, curLbMTU, err)
}
// skip
if MTU <= 0 {
klog.Infof("cluster network %v has an unexpected label %v/%v, skip to sync with nad", cn.Name, utils.KeyUplinkMTU, curLbMTU)
return nil, nil
}

nads, err := h.nadCache.List("", labels.Set(map[string]string{
utils.KeyClusterNetworkLabel: cn.Name,
}).AsSelector())
if err != nil {
return nil, fmt.Errorf("failed to list cluster network %v related nads, error %w", cn.Name, err)
}

// sync with the possible new MTU
for _, nad := range nads {
netConf := &utils.NetConf{}
if err := json.Unmarshal([]byte(nad.Spec.Config), netConf); err != nil {
return nil, fmt.Errorf("failed to Unmarshal nad %v config %v error %w", nad.Name, nad.Spec.Config, err)
}

// default value or equal
if (MTU == utils.MTUDefault && netConf.MTU == 0) || MTU == netConf.MTU {
continue
}

// Don't modify the unmarshalled structure and marshal it again because some fields may be lost during unmarshalling.
newConfig, err := sjson.Set(nad.Spec.Config, "mtu", MTU)
if err != nil {
return nil, fmt.Errorf("failed to set nad %v with new MTU %v error %w", nad.Name, MTU, err)
}
nadCopy := nad.DeepCopy()
nadCopy.Spec.Config = newConfig
if _, err := h.nadClient.Update(nadCopy); err != nil {
return nil, err
}
klog.Infof("sync cluster network %v label mtu %v/%v to nad %v", cn.Name, utils.KeyUplinkMTU, curLbMTU, nad.Name)
}

return nil, nil
}

func (h Handler) OnChange(_ string, nad *cniv1.NetworkAttachmentDefinition) (*cniv1.NetworkAttachmentDefinition, error) {
if nad == nil || nad.DeletionTimestamp != nil {
return nil, nil
Expand Down
41 changes: 36 additions & 5 deletions pkg/controller/manager/vlanconfig/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (h Handler) EnsureClusterNetwork(_ string, vc *networkv1.VlanConfig) (*netw

klog.Infof("vlan config %s has been changed, spec: %+v", vc.Name, vc.Spec)

if err := h.ensureClusterNetwork(vc.Spec.ClusterNetwork); err != nil {
if err := h.ensureClusterNetwork(vc); err != nil {
return nil, err
}
return vc, nil
Expand Down Expand Up @@ -83,16 +83,47 @@ func (h Handler) SetClusterNetworkUnready(_ string, vs *networkv1.VlanStatus) (*
return vs, nil
}

func (h Handler) ensureClusterNetwork(name string) error {
if _, err := h.cnCache.Get(name); err != nil && !apierrors.IsNotFound(err) {
func (h Handler) ensureClusterNetwork(vc *networkv1.VlanConfig) error {
name := vc.Spec.ClusterNetwork
curCn, err := h.cnCache.Get(name)
if err != nil && !apierrors.IsNotFound(err) {
return err
} else if err == nil {
}

MTU := utils.MTUDefault
if vc.Spec.Uplink.LinkAttrs.MTU != 0 && MTU != vc.Spec.Uplink.LinkAttrs.MTU {
MTU = vc.Spec.Uplink.LinkAttrs.MTU
}
targetLbMTU := fmt.Sprintf("%v", MTU)

// check if the configured VC MTU value is updated to ClusterNetwork label
if curCn != nil {
curLbMTU := curCn.Labels[utils.KeyUplinkMTU]
if curLbMTU == targetLbMTU {
return nil
}
// update the new MTU
cnCopy := curCn.DeepCopy()
if cnCopy.Labels == nil {
cnCopy.Labels = make(map[string]string, 2)
}
cnCopy.Labels[utils.KeyUplinkMTU] = targetLbMTU
cnCopy.Labels[utils.KeyMTUSourceVlanConfig] = vc.Name
if _, err := h.cnClient.Update(cnCopy); err != nil {
return fmt.Errorf("failed to update cluster network %s label %s with MTU %s error %w", name, utils.KeyUplinkMTU, targetLbMTU, err)
}
return nil
}

// if cn is not existing
cn := &networkv1.ClusterNetwork{
ObjectMeta: metav1.ObjectMeta{Name: name},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
utils.KeyUplinkMTU: targetLbMTU,
utils.KeyMTUSourceVlanConfig: vc.Name,
},
},
}
if _, err := h.cnClient.Create(cn); err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package utils

const (
MTUDefault = 1500
MTUMax = 9000
MTUMin = 1280 // IPv4 does not define; IPv6 requires >= 1280
)
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")
}
Loading
Loading