Skip to content

Commit a8cd265

Browse files
yaocw2020guangbochen
authored andcommitted
Fix review issues
1 parent 966e14f commit a8cd265

File tree

18 files changed

+242
-136
lines changed

18 files changed

+242
-136
lines changed

cmd/webhook/main.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"github.com/harvester/harvester/pkg/indexeres"
56
"os"
67

78
ctlcni "github.com/harvester/harvester/pkg/generated/controllers/k8s.cni.cncf.io"
@@ -11,15 +12,13 @@ import (
1112
ctlcore "github.com/rancher/wrangler/pkg/generated/controllers/core"
1213
ctlcorev1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
1314
"github.com/rancher/wrangler/pkg/kubeconfig"
14-
"github.com/rancher/wrangler/pkg/leader"
1515
"github.com/rancher/wrangler/pkg/signals"
1616
"github.com/rancher/wrangler/pkg/start"
1717
"github.com/sirupsen/logrus"
1818
"github.com/urfave/cli"
1919
"github.com/yaocw2020/webhook/pkg/config"
2020
"github.com/yaocw2020/webhook/pkg/server"
2121
"github.com/yaocw2020/webhook/pkg/types"
22-
"k8s.io/client-go/kubernetes"
2322
"k8s.io/client-go/rest"
2423

2524
ctlnetwork "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io"
@@ -84,7 +83,6 @@ func main() {
8483
logrus.Fatal(err)
8584
}
8685

87-
client := kubernetes.NewForConfigOrDie(cfg)
8886
ctx := signals.SetupSignalContext()
8987

9088
app := cli.NewApp()
@@ -96,11 +94,9 @@ func main() {
9694
}
9795
}
9896

99-
leader.RunOrDie(ctx, options.Namespace, name, client, func(ctx context.Context) {
100-
if err := app.Run(os.Args); err != nil {
101-
logrus.Fatalf("run webhook server failed: %v", err)
102-
}
103-
})
97+
if err := app.Run(os.Args); err != nil {
98+
logrus.Fatalf("run webhook server failed: %v", err)
99+
}
104100
}
105101

106102
func run(ctx context.Context, cfg *rest.Config, options *config.Options) error {
@@ -144,13 +140,15 @@ func newCaches(ctx context.Context, cfg *rest.Config, threadiness int) (*caches,
144140
starters = append(starters, harvesterNetworkFactory)
145141
coreFactory := ctlcore.NewFactoryFromConfigOrDie(cfg)
146142
starters = append(starters, coreFactory)
147-
// must declare cache before starting the factories
143+
// must declare cache before starting informers
148144
c := &caches{
149145
vmCache: kubevirtFactory.Kubevirt().V1().VirtualMachine().Cache(),
150146
nadCache: cniFactory.K8s().V1().NetworkAttachmentDefinition().Cache(),
151147
vsCache: harvesterNetworkFactory.Network().V1beta1().VlanStatus().Cache(),
152148
nodeCache: coreFactory.Core().V1().Node().Cache(),
153149
}
150+
// Indexer must be added before starting the informer, otherwise panic `cannot add indexers to running index` happens
151+
c.vmCache.AddIndexer(indexeres.VMByNetworkIndex, indexeres.VMByNetwork)
154152

155153
if err := start.All(ctx, threadiness, starters...); err != nil {
156154
return nil, err

manifests/crds/network.harvesterhci.io_vlanstatuses.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,16 @@ spec:
9191
type: string
9292
masterIndex:
9393
type: integer
94+
name:
95+
type: string
9496
promiscuous:
9597
type: boolean
9698
state:
9799
type: string
98-
string:
99-
type: string
100100
type:
101101
type: string
102102
required:
103-
- string
103+
- name
104104
type: object
105105
type: array
106106
localAreas:

pkg/apis/network.harvesterhci.io/v1beta1/vlanstatus.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@ type VlStatus struct {
3030

3131
Node string `json:"node"`
3232
// +optional
33-
VLANIDs []uint16 `json:"vlanIds,omitempty"`
33+
LocalAreas []LocalArea `json:"localAreas,omitempty"`
3434
// +optional
3535
LinkStatus []LinkStatus `json:"linkStatus,omitempty"`
3636
// +optional
3737
Conditions []Condition `json:"conditions,omitempty"`
3838
}
3939

40+
type LocalArea struct {
41+
VID uint16 `json:"vlanID"`
42+
CIDR string `json:"cidr,omitempty"`
43+
}
44+
4045
type LinkStatus struct {
46+
Name string `json:"name"`
4147
// +optional
4248
Index int `json:"index,omitempty"`
4349
// +optional

pkg/controller/agent/nad/controller.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func Register(ctx context.Context, management *config.Management) error {
3636
return nil
3737
}
3838

39-
func (h Handler) OnChange(_ string, nad *nadv1.NetworkAttachmentDefinition) (*nadv1.NetworkAttachmentDefinition, error) {
39+
func (h Handler) OnChange(key string, nad *nadv1.NetworkAttachmentDefinition) (*nadv1.NetworkAttachmentDefinition, error) {
4040
if nad == nil || nad.DeletionTimestamp != nil {
4141
return nil, nil
4242
}
@@ -54,7 +54,7 @@ func (h Handler) OnChange(_ string, nad *nadv1.NetworkAttachmentDefinition) (*na
5454
return nad, nil
5555
}
5656

57-
func (h Handler) OnRemove(_ string, nad *nadv1.NetworkAttachmentDefinition) (*nadv1.NetworkAttachmentDefinition, error) {
57+
func (h Handler) OnRemove(key string, nad *nadv1.NetworkAttachmentDefinition) (*nadv1.NetworkAttachmentDefinition, error) {
5858
if nad == nil {
5959
return nil, nil
6060
}
@@ -78,26 +78,19 @@ func (h Handler) OnRemove(_ string, nad *nadv1.NetworkAttachmentDefinition) (*na
7878
}
7979

8080
func (h Handler) addLocalArea(nad *nadv1.NetworkAttachmentDefinition) error {
81-
vlanID, err := strconv.Atoi(nad.Labels[utils.KeyVlanLabel])
82-
if err != nil {
83-
return fmt.Errorf("invalid vlanconfig %s", nad.Labels[utils.KeyVlanLabel])
84-
}
85-
8681
v, err := vlan.GetVlan(nad.Labels[utils.KeyClusterNetworkLabel])
8782
if err != nil && !errors.As(err, &netlink.LinkNotFoundError{}) {
8883
return err
8984
} else if errors.As(err, &netlink.LinkNotFoundError{}) {
9085
return nil
9186
}
9287

93-
layer3NetworkConf := &utils.Layer3NetworkConf{}
94-
if nad.Annotations != nil && nad.Annotations[utils.KeyNetworkConf] != "" {
95-
if layer3NetworkConf, err = utils.NewLayer3NetworkConf(nad.Annotations[utils.KeyNetworkConf]); err != nil {
96-
return err
97-
}
88+
localArea, err := GetLocalArea(nad)
89+
if err != nil {
90+
return fmt.Errorf("failed to get local area from nad %s/%s, error: %w", nad.Namespace, nad.Name, err)
9891
}
9992

100-
return v.AddLocalArea(uint16(vlanID), layer3NetworkConf.CIDR)
93+
return v.AddLocalArea(localArea)
10194
}
10295

10396
func (h Handler) existDuplicateNad(vlanIdStr, cn string) (bool, error) {
@@ -113,25 +106,36 @@ func (h Handler) existDuplicateNad(vlanIdStr, cn string) (bool, error) {
113106
}
114107

115108
func (h Handler) removeLocalArea(nad *nadv1.NetworkAttachmentDefinition) error {
116-
vlanID, err := strconv.Atoi(nad.Labels[utils.KeyVlanLabel])
117-
if err != nil {
118-
return fmt.Errorf("invalid vlanconfig %s", nad.Labels[utils.KeyVlanLabel])
119-
}
120-
121109
v, err := vlan.GetVlan(nad.Labels[utils.KeyClusterNetworkLabel])
122110
if err != nil && !errors.As(err, &netlink.LinkNotFoundError{}) {
123111
return err
124112
} else if errors.As(err, &netlink.LinkNotFoundError{}) {
125113
return nil
126114
}
127115

116+
localArea, err := GetLocalArea(nad)
117+
if err != nil {
118+
return fmt.Errorf("failed to get local area from nad %s/%s, error: %w", nad.Namespace, nad.Name, err)
119+
}
120+
121+
return v.RemoveLocalArea(localArea)
122+
}
123+
124+
func GetLocalArea(nad *nadv1.NetworkAttachmentDefinition) (*vlan.LocalArea, error) {
125+
vlanID, err := strconv.Atoi(nad.Labels[utils.KeyVlanLabel])
126+
if err != nil {
127+
return nil, fmt.Errorf("invalid vlanconfig %s", nad.Labels[utils.KeyVlanLabel])
128+
}
129+
128130
layer3NetworkConf := &utils.Layer3NetworkConf{}
129131
if nad.Annotations != nil && nad.Annotations[utils.KeyNetworkConf] != "" {
130-
layer3NetworkConf, err = utils.NewLayer3NetworkConf(nad.Annotations[utils.KeyNetworkConf])
131-
if err != nil {
132-
return err
132+
if layer3NetworkConf, err = utils.NewLayer3NetworkConf(nad.Annotations[utils.KeyNetworkConf]); err != nil {
133+
return nil, err
133134
}
134135
}
135136

136-
return v.RemoveLocalArea(uint16(vlanID), layer3NetworkConf.CIDR)
137+
return &vlan.LocalArea{
138+
Vid: uint16(vlanID),
139+
Cidr: layer3NetworkConf.CIDR,
140+
}, nil
137141
}

0 commit comments

Comments
 (0)